Search in sources :

Example 41 with MastershipService

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MastershipService(org.onosproject.mastership.MastershipService) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) IpAddress(org.onlab.packet.IpAddress) Map(java.util.Map) ControllerInfo(org.onosproject.net.behaviour.ControllerInfo) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

MastershipService (org.onosproject.mastership.MastershipService)41 DeviceId (org.onosproject.net.DeviceId)29 DriverHandler (org.onosproject.net.driver.DriverHandler)20 NetconfController (org.onosproject.netconf.NetconfController)20 NetconfException (org.onosproject.netconf.NetconfException)20 NodeId (org.onosproject.cluster.NodeId)9 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 ControllerInfo (org.onosproject.net.behaviour.ControllerInfo)5 ArrayList (java.util.ArrayList)4 DeviceService (org.onosproject.net.device.DeviceService)4 ClusterService (org.onosproject.cluster.ClusterService)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Sets (com.google.common.collect.Sets)2 Set (java.util.Set)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors.newSingleThreadScheduledExecutor (java.util.concurrent.Executors.newSingleThreadScheduledExecutor)2