Java Code Examples for org.onosproject.mastership.MastershipService#isLocalMaster()

The following examples show how to use org.onosproject.mastership.MastershipService#isLocalMaster() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NetconfControllerConfig.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public List<ControllerInfo> getControllers() {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId deviceId = handler.data().deviceId();
    Preconditions.checkNotNull(controller, "Netconf controller is null");
    List<ControllerInfo> controllers = new ArrayList<>();
    if (mastershipService.isLocalMaster(deviceId)) {
        try {
            String reply = controller.getNetconfDevice(deviceId).getSession().
                    getConfig(DatastoreId.RUNNING);
            log.debug("Reply XML {}", reply);
            controllers.addAll(XmlConfigParser.parseStreamControllers(XmlConfigParser.
                    loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
        } catch (NetconfException e) {
            log.error("Cannot communicate with device {} ", deviceId, e);
        }
    } else {
        log.warn("I'm not master for {} please use master, {} to execute command",
                 deviceId,
                 mastershipService.getMasterFor(deviceId));
    }
    return controllers;
}
 
Example 2
Source File: FujitsuVoltControllerConfig.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public void setControllers(List<ControllerInfo> controllers) {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncdeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");
    if (mastershipService.isLocalMaster(ncdeviceId)) {
        try {
            NetconfDevice device = controller.getNetconfDevice(ncdeviceId);
            String config = createVoltControllersConfig(
                    XmlConfigParser.loadXml(getClass().
                            getResourceAsStream(RESOURCE_XML)),
                    RUNNING, MERGE, controllers);
            device.getSession().editConfig(config.substring(
                    config.indexOf(END_LICENSE_HEADER) + END_LICENSE_HEADER.length()));
        } catch (NetconfException e) {
            log.error("Cannot communicate to device {} , exception {}", ncdeviceId, e);
        }
    } else {
        log.warn("I'm not master for {} please use master, {} to execute command",
                 ncdeviceId,
                 mastershipService.getMasterFor(ncdeviceId));
    }
}
 
Example 3
Source File: PolatisAlarmConsumer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public List<Alarm> consumeAlarms() {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    checkNotNull(controller, "Netconf controller is null");

    MastershipService mastershipService = handler.get(MastershipService.class);
    deviceId = handler.data().deviceId();

    List<Alarm> alarms = new ArrayList<>();
    if (!mastershipService.isLocalMaster(deviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 deviceId,
                 mastershipService.getMasterFor(deviceId));
        return ImmutableList.copyOf(alarms);
    }

    try {
        String request = xmlEmpty(KEY_SYSTEMALARMS_XMLNS);
        String reply = controller.getDevicesMap()
                           .get(deviceId)
                           .getSession()
                           .get(request, null);
        if (reply != null) {
            alarms = parseAlarms(reply);
        }
    } catch (NetconfException e) {
        log.error("Error reading alarms for device {} exception {}", deviceId, e);
    }

    return ImmutableList.copyOf(alarms);
}
 
Example 4
Source File: FujitsuVoltAlertConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public String getAlertFilter() {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");
    String reply = null;

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return null;
    }

    try {
        StringBuilder request = new StringBuilder();
        request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
        request.append(ANGLE_RIGHT + NEW_LINE);
        request.append(buildStartTag(VOLT_ALERTS))
            .append(buildEmptyTag(ALERT_FILTER))
            .append(buildEndTag(VOLT_ALERTS))
            .append(VOLT_NE_CLOSE);

        reply = controller
                .getDevicesMap()
                .get(ncDeviceId)
                .getSession()
                .get(request.toString(), REPORT_ALL);
    } catch (NetconfException e) {
        log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
    }
    return reply;
}
 
Example 5
Source File: FujitsuVoltAlertConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean setAlertFilter(String severity) {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return false;
    }

    if (!SEVERITYLEVELS.contains(severity)) {
        log.error("Invalid severity level: {}", severity);
        return false;
    }

    try {
        StringBuilder request = new StringBuilder();
        request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
        request.append(ANGLE_RIGHT + NEW_LINE);
        request.append(buildStartTag(VOLT_ALERTS))
            .append(buildStartTag(ALERT_FILTER, false))
            .append(severity)
            .append(buildEndTag(ALERT_FILTER))
            .append(buildEndTag(VOLT_ALERTS))
            .append(VOLT_NE_CLOSE);

        controller.getDevicesMap().get(ncDeviceId).getSession().
                editConfig(RUNNING, null, request.toString());
    } catch (NetconfException e) {
        log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
        return false;
    }
    return true;
}
 
Example 6
Source File: FujitsuVoltAlertConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public boolean subscribe(String mode) {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return false;
    }

    if (mode != null) {
        if (!DISABLE.equals(mode)) {
            log.error("Invalid mode: {}", mode);
            return false;
        }
    }

    try {
        if (mode != null) {
            controller.getDevicesMap().get(ncDeviceId).getSession().
                    endSubscription();
        } else {
            StringBuilder request = new StringBuilder();
            request.append(ANGLE_LEFT + NOTIFY_ALERT + SPACE);
            request.append(VOLT_NE_NAMESPACE + SLASH + ANGLE_RIGHT);

            controller.getDevicesMap().get(ncDeviceId).getSession().
                    startSubscription(request.toString());
        }
    } catch (NetconfException e) {
        log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
        return false;
    }
    return true;
}
 
Example 7
Source File: FujitsuVoltNeConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public String getAll() {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");
    String reply = null;

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return null;
    }

    try {
        StringBuilder request = new StringBuilder();
        request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
        request.append(ANGLE_RIGHT + NEW_LINE);
        request.append(VOLT_NE_CLOSE);

        reply = controller
                .getDevicesMap()
                .get(ncDeviceId)
                .getSession()
                .get(request.toString(), REPORT_ALL);
    } catch (NetconfException e) {
        log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
    }
    return reply;
}
 
Example 8
Source File: FujitsuVoltControllerConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public List<ControllerInfo> getControllers() {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");
    List<ControllerInfo> controllers = new ArrayList<>();
    if (mastershipService.isLocalMaster(ncDeviceId)) {
        try {
            StringBuilder request = new StringBuilder();
            request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE + ">\n");
            request.append(buildEmptyTag(VOLT_OFCONFIG));
            request.append(VOLT_NE_CLOSE);

            String reply;
            reply = controller
                        .getDevicesMap()
                        .get(ncDeviceId)
                        .getSession()
                        .get(request.toString(), REPORT_ALL);
            log.debug("Reply XML {}", reply);
            controllers.addAll(parseStreamVoltControllers(XmlConfigParser.
                    loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))));
        } catch (NetconfException e) {
            log.error("Cannot communicate to device {} ", ncDeviceId);
        }
    } else {
        log.warn("I'm not master for {} please use master, {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
    }
    return ImmutableList.copyOf(controllers);
}
 
Example 9
Source File: FujitsuVoltAlarmConsumer.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public List<Alarm> consumeAlarms() {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return null;
    }

    dateFormat.setTimeZone(ZONE);
    List<Alarm> alarms = new ArrayList<>();
    try {
        StringBuilder request = new StringBuilder();
        request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE)
            .append(ANGLE_RIGHT + NEW_LINE)
            .append(buildStartTag(VOLT_ALERTS))
            .append(buildEmptyTag(OLT_ACTIVE_ALERTS))
            .append(buildEndTag(VOLT_ALERTS))
            .append(VOLT_NE_CLOSE);

        String reply = controller.getDevicesMap()
                           .get(ncDeviceId)
                           .getSession()
                           .get(request.toString(), null);
        if (reply != null) {
            alarms = parseVoltActiveAlerts(XmlConfigParser.
                loadXml(new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8))));
        }
    } catch (NetconfException e) {
        log.error("Error reading alarms for device {} exception {}", ncDeviceId, e);
    }

    return ImmutableList.copyOf(alarms);
}
 
Example 10
Source File: ServerControllerConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void removeControllers(List<ControllerInfo> controllers) {
    DeviceId deviceId = getDeviceId();
    checkNotNull(deviceId, MSG_DEVICE_ID_NULL);

    MastershipService mastershipService = getHandler().get(MastershipService.class);
    checkNotNull(deviceId, MSG_MASTERSHIP_NULL);

    if (!mastershipService.isLocalMaster(deviceId)) {
        log.warn(
            "I am not master for {}. " +
            "Please use master {} to remove controllers from this device",
            deviceId, mastershipService.getMasterFor(deviceId));
        return;
    }

    for (ControllerInfo ctrl : controllers) {
        log.info("Remove controller with {}:{}:{}",
            ctrl.type(), ctrl.ip().toString(), ctrl.port());

        String remCtrlUrl = URL_CONTROLLERS_DEL + SLASH + ctrl.ip().toString();

        // Remove this controller
        int response = getController().delete(deviceId, remCtrlUrl, null, JSON);

        if (!checkStatusCode(response)) {
            log.error("Failed to remove controller {}:{}:{} from device {}",
                ctrl.type(), ctrl.ip().toString(), ctrl.port(), deviceId);
        }
    }

    return;
}
 
Example 11
Source File: DistributedVirtualFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public FlowRuleEvent pendingFlowRule(NetworkId networkId, FlowEntry rule) {
    MastershipService mastershipService =
            vnaService.get(networkId, MastershipService.class);
    if (mastershipService.isLocalMaster(rule.deviceId())) {
        StoredFlowEntry stored = flowTable.getFlowEntry(networkId, rule);
        if (stored != null &&
                stored.state() != FlowEntry.FlowEntryState.PENDING_ADD) {
            stored.setState(FlowEntry.FlowEntryState.PENDING_ADD);
            return new FlowRuleEvent(FlowRuleEvent.Type.RULE_UPDATED, rule);
        }
    }
    return null;
}
 
Example 12
Source File: FujitsuVoltOnuOperConfig.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public String rebootOnu(String target) {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");
    String reply = null;
    String[] onuId = null;

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return null;
    }

    onuId = checkIdString(target, TWO);
    if (onuId == null) {
        log.error("Invalid ONU identifier {}", target);
        return null;
    }

    try {
        StringBuilder request = new StringBuilder();
        request.append(ANGLE_LEFT + ONU_REBOOT + SPACE);
        request.append(VOLT_NE_NAMESPACE + ANGLE_RIGHT + NEW_LINE);

        request.append(buildStartTag(PONLINK_ID, false))
            .append(onuId[FIRST_PART])
            .append(buildEndTag(PONLINK_ID))
            .append(buildStartTag(ONU_ID, false))
            .append(onuId[SECOND_PART])
            .append(buildEndTag(ONU_ID))
            .append(buildEndTag(ONU_REBOOT));

        reply = controller
                .getDevicesMap()
                .get(ncDeviceId)
                .getSession()
                .doWrappedRpc(request.toString());
    } catch (NetconfException e) {
        log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
    }
    return reply;
}
 
Example 13
Source File: FujitsuVoltOnuConfig.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public String getOnus(String target) {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");
    String reply = null;
    String[] onuId = null;

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return null;
    }

    if (target != null) {
        onuId = checkIdString(target);
        if (onuId == null) {
            log.error("Invalid ONU identifier {}", target);
            return null;
        }
    }

    try {
        StringBuilder request = new StringBuilder();
        request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
        request.append(ANGLE_RIGHT + NEW_LINE);
        if (onuId != null) {
            request.append(buildStartTag(VOLT_ONUS))
                .append(buildStartTag(ONUS_PERLINK))
                .append(buildStartTag(PONLINK_ID, false))
                .append(onuId[FIRST_PART])
                .append(buildEndTag(PONLINK_ID));
            if (onuId.length > ONE) {
                request.append(buildStartTag(ONUS_LIST))
                    .append(buildStartTag(ONU_INFO))
                    .append(buildStartTag(ONU_ID, false))
                    .append(onuId[SECOND_PART])
                    .append(buildEndTag(ONU_ID))
                    .append(buildEndTag(ONU_INFO))
                    .append(buildEndTag(ONUS_LIST));
            }
            request.append(buildEndTag(ONUS_PERLINK))
                .append(buildEndTag(VOLT_ONUS));
        } else {
            request.append(buildEmptyTag(VOLT_ONUS));
        }
        request.append(VOLT_NE_CLOSE);

        reply = controller
                    .getDevicesMap()
                    .get(ncDeviceId)
                    .getSession()
                    .get(request.toString(), REPORT_ALL);
    } catch (NetconfException e) {
        log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
    }
    return reply;
}
 
Example 14
Source File: FujitsuVoltOnuConfig.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public String getOnuStatistics(String target) {
    DriverHandler handler = handler();
    NetconfController controller = handler.get(NetconfController.class);
    MastershipService mastershipService = handler.get(MastershipService.class);
    DeviceId ncDeviceId = handler.data().deviceId();
    checkNotNull(controller, "Netconf controller is null");
    String reply = null;
    String[] onuId = null;

    if (!mastershipService.isLocalMaster(ncDeviceId)) {
        log.warn("Not master for {} Use {} to execute command",
                 ncDeviceId,
                 mastershipService.getMasterFor(ncDeviceId));
        return null;
    }

    if (target != null) {
        onuId = checkIdString(target);
        if (onuId == null) {
            log.error("Failed to check ID: {}", target);
            return null;
        }
    }

    try {
        StringBuilder request = new StringBuilder();
        request.append(VOLT_NE_OPEN + VOLT_NE_NAMESPACE);
        request.append(ANGLE_RIGHT + NEW_LINE);
        request.append(buildStartTag(VOLT_STATISTICS));
        if (onuId != null) {
            request.append(buildStartTag(ONU_STATISTICS))
                .append(buildStartTag(ONU_GEM_STATS))
                .append(buildStartTag(GEM_STATS))
                .append(buildStartTag(PONLINK_ID, false))
                .append(onuId[FIRST_PART])
                .append(buildEndTag(PONLINK_ID));
            if (onuId.length > ONE) {
                request.append(buildStartTag(ONU_ID, false))
                    .append(onuId[SECOND_PART])
                    .append(buildEndTag(ONU_ID));
            }
            request.append(buildEndTag(GEM_STATS))
                .append(buildEndTag(ONU_GEM_STATS));

            request.append(buildStartTag(ONU_ETH_STATS))
                .append(buildStartTag(ETH_STATS))
                .append(buildStartTag(PONLINK_ID, false))
                .append(onuId[FIRST_PART])
                .append(buildEndTag(PONLINK_ID));
            if (onuId.length > ONE) {
                request.append(buildStartTag(ONU_ID, false))
                    .append(onuId[SECOND_PART])
                    .append(buildEndTag(ONU_ID));
            }
            request.append(buildEndTag(ETH_STATS))
                .append(buildEndTag(ONU_ETH_STATS))
                .append(buildEndTag(ONU_STATISTICS));
        } else  {
            request.append(buildEmptyTag(ONU_STATISTICS));
        }
        request.append(buildEndTag(VOLT_STATISTICS))
            .append(VOLT_NE_CLOSE);

        reply = controller
                    .getDevicesMap()
                    .get(ncDeviceId)
                    .getSession()
                    .get(request.toString(), REPORT_ALL);
    } catch (NetconfException e) {
        log.error("Cannot communicate to device {} exception {}", ncDeviceId, e);
    }
    return reply;
}
 
Example 15
Source File: ServerControllerConfig.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void setControllers(List<ControllerInfo> controllers) {
    DeviceId deviceId = getDeviceId();
    checkNotNull(deviceId, MSG_DEVICE_ID_NULL);

    MastershipService mastershipService = getHandler().get(MastershipService.class);
    checkNotNull(deviceId, MSG_MASTERSHIP_NULL);

    if (!mastershipService.isLocalMaster(deviceId)) {
        log.warn(
            "I am not master for {}. " +
            "Please use master {} to set controllers for this device",
            deviceId, mastershipService.getMasterFor(deviceId));
        return;
    }

    ObjectMapper mapper = new ObjectMapper();

    // Create the object node to host the data
    ObjectNode sendObjNode = mapper.createObjectNode();

    // Insert header
    ArrayNode ctrlsArrayNode = sendObjNode.putArray(PARAM_CTRL);

    // Add each controller's information object
    for (ControllerInfo ctrl : controllers) {
        ObjectNode ctrlObjNode = mapper.createObjectNode();
        ctrlObjNode.put(PARAM_CTRL_IP,   ctrl.ip().toString());
        ctrlObjNode.put(PARAM_CTRL_PORT, ctrl.port());
        ctrlObjNode.put(PARAM_CTRL_TYPE, ctrl.type());
        ctrlsArrayNode.add(ctrlObjNode);
    }

    // Post the controllers to the device
    int response = getController().post(
        deviceId, URL_CONTROLLERS_SET,
        new ByteArrayInputStream(sendObjNode.toString().getBytes()), JSON);

    if (!checkStatusCode(response)) {
        log.error("Failed to set controllers on device {}", deviceId);
    }

    return;
}