use of org.onosproject.mastership.MastershipService in project onos by opennetworkinglab.
the class ServerControllerConfig method getControllers.
@Override
public List<ControllerInfo> getControllers() {
List<ControllerInfo> controllers = Lists.newArrayList();
DeviceId deviceId = getDeviceId();
checkNotNull(deviceId, MSG_DEVICE_ID_NULL);
MastershipService mastershipService = getHandler().get(MastershipService.class);
checkNotNull(mastershipService, MSG_MASTERSHIP_NULL);
if (!mastershipService.isLocalMaster(deviceId)) {
log.warn("I am not master for {}. " + "Please use master {} to get controllers for this device", deviceId, mastershipService.getMasterFor(deviceId));
return controllers;
}
// Hit the path that provides the server's controllers
InputStream response = null;
try {
response = getController().get(deviceId, URL_CONTROLLERS_GET, JSON);
} catch (ProcessingException pEx) {
log.error("Failed to get controllers of device: {}", deviceId);
return controllers;
}
// Load the JSON into objects
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> jsonMap = null;
JsonNode jsonNode = null;
ObjectNode objNode = null;
try {
jsonMap = mapper.readValue(response, Map.class);
jsonNode = mapper.convertValue(jsonMap, JsonNode.class);
objNode = (ObjectNode) jsonNode;
} catch (IOException ioEx) {
log.error("Failed to get controllers of device: {}", deviceId);
return controllers;
}
if (jsonMap == null) {
log.error("Failed to get controllers of device: {}", deviceId);
return controllers;
}
// Fetch controllers' array
JsonNode ctrlNode = objNode.path(PARAM_CTRL);
for (JsonNode cn : ctrlNode) {
ObjectNode ctrlObjNode = (ObjectNode) cn;
// Get the attributes of a controller
String ctrlIpStr = get(cn, PARAM_CTRL_IP);
int ctrlPort = ctrlObjNode.path(PARAM_CTRL_PORT).asInt();
String ctrlType = get(cn, PARAM_CTRL_TYPE);
// Implies no controller
if (ctrlIpStr.isEmpty()) {
continue;
}
// Check data format and range
IpAddress ctrlIp = null;
try {
ctrlIp = IpAddress.valueOf(ctrlIpStr);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(e);
}
if ((ctrlPort < 0) || (ctrlPort > TpPort.MAX_PORT)) {
final String msg = "Invalid controller port: " + ctrlPort;
throw new IllegalArgumentException(msg);
}
controllers.add(new ControllerInfo(ctrlIp, ctrlPort, ctrlType));
}
return controllers;
}
Aggregations