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

The following examples show how to use org.onosproject.mastership.MastershipService#getMasterFor() . 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: DistributedVirtualFlowRuleStore.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
public Iterable<TableStatisticsEntry> getTableStatistics(NetworkId networkId, DeviceId deviceId) {
    MastershipService mastershipService =
            vnaService.get(networkId, MastershipService.class);
    NodeId master = mastershipService.getMasterFor(deviceId);

    if (master == null) {
        log.debug("Failed to getTableStats: No master for {}", deviceId);
        return Collections.emptyList();
    }

    if (deviceTableStats.get(networkId) == null) {
        deviceTableStats.put(networkId, Maps.newConcurrentMap());
    }

    List<TableStatisticsEntry> tableStats = deviceTableStats.get(networkId).get(deviceId);
    if (tableStats == null) {
        return Collections.emptyList();
    }
    return ImmutableList.copyOf(tableStats);
}
 
Example 2
Source File: RolesCommand.java    From onos with Apache License 2.0 5 votes vote down vote up
private JsonNode json(MastershipService service, ObjectMapper mapper,
                      Device device) {
    NodeId master = service.getMasterFor(device.id());
    ObjectNode result = mapper.createObjectNode()
            .put("id", device.id().toString())
            .put("master", master != null ? master.toString() : "none");
    RoleInfo nodes = service.getNodesFor(device.id());
    ArrayNode standbys = mapper.createArrayNode();
    for (NodeId nid : nodes.backups()) {
        standbys.add(nid.toString());
    }
    result.set("standbys", standbys);
    return result;
}
 
Example 3
Source File: DistributedVirtualFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public FlowEntry getFlowEntry(NetworkId networkId, FlowRule rule) {
    MastershipService mastershipService =
            vnaService.get(networkId, MastershipService.class);
    NodeId master = mastershipService.getMasterFor(rule.deviceId());

    if (master == null) {
        log.debug("Failed to getFlowEntry: No master for {}, vnet {}",
                  rule.deviceId(), networkId);
        return null;
    }

    if (Objects.equals(local, master)) {
        return flowTable.getFlowEntry(networkId, rule);
    }

    log.trace("Forwarding getFlowEntry to {}, which is the primary (master) " +
                      "for device {}, vnet {}",
              master, rule.deviceId(), networkId);

    VirtualFlowRule vRule = new VirtualFlowRule(networkId, rule);

    return Tools.futureGetOrElse(clusterCommunicator.sendAndReceive(vRule,
                                                                    GET_FLOW_ENTRY,
                                                                    serializer::encode,
                                                                    serializer::decode,
                                                                    master),
                                 FLOW_RULE_STORE_TIMEOUT_MILLIS,
                                 TimeUnit.MILLISECONDS,
                                 null);
}
 
Example 4
Source File: DistributedVirtualFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<FlowEntry> getFlowEntries(NetworkId networkId, DeviceId deviceId) {
    MastershipService mastershipService =
            vnaService.get(networkId, MastershipService.class);
    NodeId master = mastershipService.getMasterFor(deviceId);

    if (master == null) {
        log.debug("Failed to getFlowEntries: No master for {}, vnet {}", deviceId, networkId);
        return Collections.emptyList();
    }

    if (Objects.equals(local, master)) {
        return flowTable.getFlowEntries(networkId, deviceId);
    }

    log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}",
              master, deviceId);

    return Tools.futureGetOrElse(
            clusterCommunicator.sendAndReceive(deviceId,
                                               GET_DEVICE_FLOW_ENTRIES,
                                               serializer::encode,
                                               serializer::decode,
                                               master),
            FLOW_RULE_STORE_TIMEOUT_MILLIS,
            TimeUnit.MILLISECONDS,
            Collections.emptyList());
}
 
Example 5
Source File: DistributedVirtualFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public FlowRuleEvent addOrUpdateFlowRule(NetworkId networkId, FlowEntry rule) {
    MastershipService mastershipService =
            vnaService.get(networkId, MastershipService.class);
    NodeId master = mastershipService.getMasterFor(rule.deviceId());
    if (Objects.equals(local, master)) {
        return addOrUpdateFlowRuleInternal(networkId, rule);
    }

    log.warn("Tried to update FlowRule {} state,"
                     + " while the Node was not the master.", rule);
    return null;
}
 
Example 6
Source File: DistributedVirtualFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public FlowRuleEvent removeFlowRule(NetworkId networkId, FlowEntry rule) {
    final DeviceId deviceId = rule.deviceId();

    MastershipService mastershipService =
            vnaService.get(networkId, MastershipService.class);
    NodeId master = mastershipService.getMasterFor(deviceId);

    if (Objects.equals(local, master)) {
        // bypass and handle it locally
        return removeFlowRuleInternal(new VirtualFlowEntry(networkId, rule));
    }

    if (master == null) {
        log.warn("Failed to removeFlowRule: No master for {}", deviceId);
        // TODO: revisit if this should be null (="no-op") or Exception
        return null;
    }

    log.trace("Forwarding removeFlowRule to {}, which is the master for device {}",
              master, deviceId);

    return Futures.getUnchecked(clusterCommunicator.sendAndReceive(
            new VirtualFlowEntry(networkId, rule),
            REMOVE_FLOW_ENTRY,
            serializer::encode,
            serializer::decode,
            master));
}
 
Example 7
Source File: DistributedVirtualFlowRuleStore.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(final ClusterMessage message) {
    VirtualFlowRuleBatchOperation vOperation = serializer.decode(message.payload());
    log.debug("received batch request {}", vOperation);

    FlowRuleBatchOperation operation = vOperation.operation();

    final DeviceId deviceId = operation.deviceId();
    MastershipService mastershipService =
            vnaService.get(vOperation.networkId(), MastershipService.class);
    NodeId master = mastershipService.getMasterFor(deviceId);
    if (!Objects.equals(local, master)) {
        Set<FlowRule> failures = new HashSet<>(operation.size());
        for (FlowRuleBatchEntry op : operation.getOperations()) {
            failures.add(op.target());
        }
        CompletedBatchOperation allFailed = new CompletedBatchOperation(false, failures, deviceId);
        // This node is no longer the master, respond as all failed.
        // TODO: we might want to wrap response in envelope
        // to distinguish sw programming failure and hand over
        // it make sense in the latter case to retry immediately.
        message.respond(serializer.encode(allFailed));
        return;
    }

    pendingResponses.put(operation.id(), message.sender());
    storeBatchInternal(vOperation.networkId(), operation);
}
 
Example 8
Source File: DeviceViewMessageHandler.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void process(ObjectNode payload) {
    String id = string(payload, ID, ZERO_URI);

    DeviceId deviceId = deviceId(id);
    DeviceService service = get(DeviceService.class);
    MastershipService ms = get(MastershipService.class);
    Device device = service.getDevice(deviceId);
    ObjectNode data = objectNode();
    NodeId masterFor = ms.getMasterFor(deviceId);

    data.put(ID, deviceId.toString());
    data.put(NAME, deviceName(device));
    data.put(TYPE, capitalizeFully(device.type().toString()));
    data.put(TYPE_IID, getTypeIconId(device));
    data.put(MFR, device.manufacturer());
    data.put(HW, device.hwVersion());
    data.put(SW, device.swVersion());
    data.put(SERIAL, device.serialNumber());
    data.put(CHASSIS_ID, device.chassisId().toString());
    data.put(MASTER_ID, masterFor != null ? masterFor.toString() : NONE);
    data.put(PROTOCOL, deviceProtocol(device));
    data.put(PIPECONF, devicePipeconf(device));

    ArrayNode ports = arrayNode();

    List<Port> portList = new ArrayList<>(service.getPorts(deviceId));
    portList.sort((p1, p2) -> {
        long delta = p1.number().toLong() - p2.number().toLong();
        return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
    });

    for (Port p : portList) {
        ports.add(portData(p, deviceId));
    }
    data.set(PORTS, ports);

    ObjectNode rootNode = objectNode();
    rootNode.set(DETAILS, data);

    // NOTE: ... an alternate way of getting all the details of an item:
    // Use the codec context to get a JSON of the device. See ONOS-5976.
    rootNode.set(DEVICE, getJsonCodecContext().encode(device, Device.class));

    sendMessage(DEV_DETAILS_RESP, rootNode);
}