Java Code Examples for org.onosproject.net.Device#as()

The following examples show how to use org.onosproject.net.Device#as() . 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: OpenstackVtapManager.java    From onos with Apache License 2.0 6 votes vote down vote up
private ExtensionTreatment buildResubmitExtension(DeviceId deviceId, int tableId) {
    Device device = deviceService.getDevice(deviceId);
    if (device == null || !device.is(ExtensionTreatmentResolver.class)) {
        log.warn("Nicira extension treatment is not supported");
        return null;
    }

    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    ExtensionTreatment treatment =
            resolver.getExtensionInstruction(NICIRA_RESUBMIT_TABLE.type());

    try {
        treatment.setPropertyValue(TABLE_EXTENSION, ((short) tableId));
        return treatment;
    } catch (ExtensionPropertyException e) {
        log.error("Failed to set nicira resubmit extension treatment for {}", deviceId);
        return null;
    }
}
 
Example 2
Source File: DefaultK8sNodeHandler.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a bridge with a given name on a given kubernetes node.
 *
 * @param k8sNode       kubernetes node
 * @param bridgeName    bridge name
 * @param devId         device identifier
 */
private void createBridge(K8sNode k8sNode, String bridgeName, DeviceId devId) {
    Device device = deviceService.getDevice(k8sNode.ovsdb());

    List<ControllerInfo> controllers = clusterService.getNodes().stream()
            .map(n -> new ControllerInfo(n.ip(), DEFAULT_OFPORT, DEFAULT_OF_PROTO))
            .collect(Collectors.toList());

    String dpid = devId.toString().substring(DPID_BEGIN);

    BridgeDescription.Builder builder = DefaultBridgeDescription.builder()
            .name(bridgeName)
            .failMode(BridgeDescription.FailMode.SECURE)
            .datapathId(dpid)
            .disableInBand()
            .controllers(controllers);

    BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
    bridgeConfig.addBridge(builder.build());
}
 
Example 3
Source File: PortAvailableWaveLengthCommand.java    From onos with Apache License 2.0 6 votes vote down vote up
@Override
protected void doExecute() throws Exception {
    DeviceService deviceService = get(DeviceService.class);
    ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPointString);

    Device d = deviceService.getDevice(cp.deviceId());
    if (d.is(LambdaQuery.class)) {
        LambdaQuery lambdaQuery = d.as(LambdaQuery.class);
        lambdaQuery.queryLambdas(cp.port()).forEach(lambda -> {
            print(FMT, lambda.toString(), lambda.centralFrequency().asGHz());
        });

    } else {
        print("Device is not capable of querying lambdas");
    }

}
 
Example 4
Source File: RulePopulatorUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the nicira load extension treatment.
 *
 * @param device        device instance
 * @param field         field code
 * @param value         value to load
 * @return load extension treatment
 */
public static ExtensionTreatment buildLoadExtension(Device device,
                                                    long field,
                                                    long value) {
    if (!checkTreatmentResolver(device)) {
        return null;
    }

    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    ExtensionTreatment treatment =
            resolver.getExtensionInstruction(NICIRA_LOAD.type());

    int ofsNbits = OFF_SET_BIT << 6 | (REMAINDER_BIT - 1);

    try {
        treatment.setPropertyValue(OFF_SET_N_BITS, ofsNbits);
        treatment.setPropertyValue(DESTINATION, field);
        treatment.setPropertyValue(VALUE, value);
        return treatment;
    } catch (ExtensionPropertyException e) {
        log.error("Failed to set nicira load extension treatment for {}",
                device.id());
        return null;
    }
}
 
Example 5
Source File: RulePopulatorUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns tunnel destination extension treatment object.
 *
 * @param deviceService driver service
 * @param deviceId device id to apply this treatment
 * @param remoteIp tunnel destination ip address
 * @return extension treatment
 */
public static ExtensionTreatment buildExtension(DeviceService deviceService,
                                                DeviceId deviceId,
                                                Ip4Address remoteIp) {
    Device device = deviceService.getDevice(deviceId);

    if (!checkTreatmentResolver(device)) {
        return null;
    }

    if (device == null) {
        return null;
    }

    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    ExtensionTreatment treatment =
            resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
    try {
        treatment.setPropertyValue(TUNNEL_DST, remoteIp);
        return treatment;
    } catch (ExtensionPropertyException e) {
        log.warn("Failed to get tunnelDst extension treatment for {} " +
                "because of {}", deviceId, e);
        return null;
    }
}
 
Example 6
Source File: EncodeInstructionCodecHelper.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes a extension instruction.
 *
 * @param result json node that the instruction attributes are added to
 */
private void encodeExtension(ObjectNode result) {
    final Instructions.ExtensionInstructionWrapper extensionInstruction =
            (Instructions.ExtensionInstructionWrapper) instruction;

    DeviceId deviceId = extensionInstruction.deviceId();

    ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
    DeviceService deviceService = serviceDirectory.get(DeviceService.class);
    Device device = deviceService.getDevice(deviceId);

    if (device == null) {
        throw new IllegalArgumentException("Device not found");
    }

    if (device.is(ExtensionTreatmentCodec.class)) {
        // for extension instructions, encoding device id is needed for the corresponding decoder
        result.put("deviceId", deviceId.toString());
        ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
        ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context);
        result.set(InstructionCodec.EXTENSION, node);
    } else {
        throw new IllegalArgumentException(
                "There is no codec to encode extension for device " + deviceId.toString());
    }
}
 
Example 7
Source File: OpenstackVtapManager.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns tunnel destination extension treatment object.
 *
 * @param deviceId device id to apply this treatment
 * @param remoteIp tunnel destination ip address
 * @return extension treatment
 */
private ExtensionTreatment buildTunnelExtension(DeviceId deviceId, IpAddress remoteIp) {
    Device device = deviceService.getDevice(deviceId);
    if (device == null || !device.is(ExtensionTreatmentResolver.class)) {
        log.warn("Nicira extension treatment is not supported");
        return null;
    }

    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    ExtensionTreatment treatment =
            resolver.getExtensionInstruction(NICIRA_SET_TUNNEL_DST.type());
    try {
        treatment.setPropertyValue(TUNNEL_DST_EXTENSION, remoteIp.getIp4Address());
        return treatment;
    } catch (ExtensionPropertyException e) {
        log.error("Failed to set nicira tunnelDst extension treatment for {}", deviceId);
        return null;
    }
}
 
Example 8
Source File: MeterDriverProvider.java    From onos with Apache License 2.0 5 votes vote down vote up
private MeterProgrammable getMeterProgrammable(DeviceId deviceId) {
    Device device = deviceService.getDevice(deviceId);
    if (device.is(MeterProgrammable.class)) {
        return device.as(MeterProgrammable.class);
    } else {
        log.debug("Device {} is not meter programmable", deviceId);
        return null;
    }
}
 
Example 9
Source File: RoadmManager.java    From onos with Apache License 2.0 5 votes vote down vote up
private LambdaQuery getLambdaQuery(DeviceId deviceId) {
    Device device = deviceService.getDevice(deviceId);
    if (device != null && device.is(LambdaQuery.class)) {
        return device.as(LambdaQuery.class);
    }
    // Do not need warning here for port polling.
    log.debug("Unable to load LambdaQuery for {}", deviceId);
    return null;
}
 
Example 10
Source File: DefaultOpenstackNodeHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private void removePhysicalPatchPorts(OpenstackNode osNode, String network) {
    Device device = deviceService.getDevice(osNode.ovsdb());

    if (device == null || !device.is(InterfaceConfig.class)) {
        log.error("Failed to remove patch interface on {}", osNode.ovsdb());
        return;
    }

    String intToPhyPatchPort = structurePortName(
            INTEGRATION_TO_PHYSICAL_PREFIX + network);

    InterfaceConfig ifaceConfig = device.as(InterfaceConfig.class);
    ifaceConfig.removePatchMode(intToPhyPatchPort);
}
 
Example 11
Source File: DefaultOpenstackNodeHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private void createDpdkTunnelBridge(OpenstackNode osNode) {
    Device device = deviceService.getDevice(osNode.ovsdb());

    BridgeDescription.Builder builder = DefaultBridgeDescription.builder()
            .name(TUNNEL_BRIDGE)
            .datapathType(NETDEV.name().toLowerCase());

    BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
    bridgeConfig.addBridge(builder.build());
}
 
Example 12
Source File: DefaultOpenstackNodeHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a bridge with a given name on a given openstack node.
 *
 * @param osNode openstack node
 * @param bridgeName bridge name
 * @param deviceId device identifier
 */
private void createBridge(OpenstackNode osNode, String bridgeName, DeviceId deviceId) {
    Device device = deviceService.getDevice(osNode.ovsdb());

    List<ControllerInfo> controllers;

    if (osNode.controllers() != null && osNode.controllers().size() > 0) {
        controllers = (List<ControllerInfo>) osNode.controllers();
    } else {
        Set<IpAddress> controllerIps = clusterService.getNodes().stream()
                .map(ControllerNode::ip)
                .collect(Collectors.toSet());

        controllers = controllerIps.stream()
                .map(ip -> new ControllerInfo(ip, DEFAULT_OFPORT, DEFAULT_OF_PROTO))
                .collect(Collectors.toList());
    }

    String dpid = deviceId.toString().substring(DPID_BEGIN);

    BridgeDescription.Builder builder = DefaultBridgeDescription.builder()
            .name(bridgeName)
            .failMode(BridgeDescription.FailMode.SECURE)
            .datapathId(dpid)
            .disableInBand()
            .controllers(controllers);

    if (osNode.datapathType().equals(NETDEV)) {
        builder.datapathType(NETDEV.name().toLowerCase());
    }

    BridgeConfig bridgeConfig = device.as(BridgeConfig.class);
    bridgeConfig.addBridge(builder.build());
}
 
Example 13
Source File: RoadmPortViewMessageHandler.java    From onos with Apache License 2.0 5 votes vote down vote up
private String getPreFecBer(DeviceId deviceId, PortNumber portNumber) {
    Device device = deviceService.getDevice(deviceId);
    if (device == null || !device.is(BitErrorRateState.class)) {
        return RoadmUtil.UNKNOWN;
    }
    BitErrorRateState bitErrorRateState = device.as(BitErrorRateState.class);
    Optional<Double> preFecBer = bitErrorRateState.getPreFecBer(deviceId, portNumber);
    Double preFecBerVal = null;
    if (preFecBer.isPresent()) {
        preFecBerVal = preFecBer.orElse(Double.MIN_VALUE);
    }
    return RoadmUtil.objectToString(preFecBerVal, RoadmUtil.UNKNOWN);
}
 
Example 14
Source File: RulePopulatorUtil.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the nicira move source MAC to destination MAC extension treatment.
 *
 * @param device        device instance
 * @return move extension treatment
 */
public static ExtensionTreatment buildMoveEthSrcToDstExtension(Device device) {
    if (!checkTreatmentResolver(device)) {
        return null;
    }

    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    return resolver.getExtensionInstruction(NICIRA_MOV_ETH_SRC_TO_DST.type());
}
 
Example 15
Source File: RulePopulatorUtil.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the nicira pop extension treatment.
 *
 * @param device        device instance
 * @return pop extension treatment
 */
public static ExtensionTreatment buildPopExtension(Device device) {
    if (!checkTreatmentResolver(device)) {
        return null;
    }

    ExtensionTreatmentResolver resolver = device.as(ExtensionTreatmentResolver.class);
    return resolver.getExtensionInstruction(NICIRA_POP_NSH.type());
}
 
Example 16
Source File: PiReplicationGroupTranslatorImpl.java    From onos with Apache License 2.0 5 votes vote down vote up
private static PortNumber logicalToPipelineSpecific(
        PortNumber logicalPort, Device device)
        throws PiTranslationException {
    if (!device.is(PiPipelineInterpreter.class)) {
        throw new PiTranslationException(
                "missing interpreter, cannot map logical port " + logicalPort.toString());
    }
    final PiPipelineInterpreter interpreter = device.as(PiPipelineInterpreter.class);
    Optional<Integer> mappedPort = interpreter.mapLogicalPortNumber(logicalPort);
    if (!mappedPort.isPresent()) {
        throw new PiTranslationException(
                "interpreter cannot map logical port " + logicalPort.toString());
    }
    return PortNumber.portNumber(mappedPort.get());
}
 
Example 17
Source File: P4RuntimePacketProvider.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void emit(OutboundPacket packet) {
    if (packet != null) {
        DeviceId deviceId = packet.sendThrough();
        Device device = deviceService.getDevice(deviceId);
        if (device.is(PacketProgrammable.class) && mastershipService.isLocalMaster(deviceId)) {
            PacketProgrammable packetProgrammable = device.as(PacketProgrammable.class);
            packetProgrammable.emit(packet);
        } else {
            log.warn("No PacketProgrammable behavior for device {}", deviceId);
        }
    }
}
 
Example 18
Source File: MappingAddressBuilder.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Converts LCAF address to extension mapping address.
 *
 * @param deviceService device service
 * @param deviceId      device identifier
 * @param lcaf          LCAF address
 * @return extension mapping address
 */
private static MappingAddress lcaf2extension(DeviceService deviceService,
                                             DeviceId deviceId,
                                             LispLcafAddress lcaf) {

    Device device = deviceService.getDevice(deviceId);

    ExtensionMappingAddressInterpreter addressInterpreter;
    ExtensionMappingAddress mappingAddress = null;
    if (device.is(ExtensionMappingAddressInterpreter.class)) {
        addressInterpreter = device.as(ExtensionMappingAddressInterpreter.class);
    } else {
        addressInterpreter = null;
    }

    switch (lcaf.getType()) {
        case LIST:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(LIST_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case SEGMENT:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(SEGMENT_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case AS:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(AS_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case APPLICATION_DATA:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(APPLICATION_DATA_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case GEO_COORDINATE:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(GEO_COORDINATE_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case NAT:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(NAT_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case NONCE:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(NONCE_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case MULTICAST:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(MULTICAST_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case TRAFFIC_ENGINEERING:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(TRAFFIC_ENGINEERING_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        case SOURCE_DEST:
            if (addressInterpreter != null &&
                    addressInterpreter.supported(SOURCE_DEST_ADDRESS.type())) {
                mappingAddress = addressInterpreter.mapLcafAddress(lcaf);
            }
            break;
        default:
            log.warn("Unsupported extension mapping address type {}", lcaf.getType());
            break;
    }

    return mappingAddress != null ?
            MappingAddresses.extensionMappingAddressWrapper(mappingAddress, deviceId) : null;
}
 
Example 19
Source File: SnmpDeviceProvider.java    From onos with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize SNMP Device object, and notify core saying device connected.
 */
private void advertiseDevices() {
    try {
        if (device == null) {
            log.warn("The Request SNMP Device is null, cannot proceed further");
            return;
        }
        DeviceId did = device.deviceId();
        ChassisId cid = new ChassisId();

        SparseAnnotations annotations = DefaultAnnotations.builder()
                .set(AnnotationKeys.PROTOCOL, SCHEME.toUpperCase())
                .build();

        DeviceDescription desc = new DefaultDeviceDescription(
                did.uri(), Device.Type.OTHER, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, cid, annotations);

        log.debug("Persisting Device " + did.uri().toString());

        controller.addDevice(device);
        providerService.deviceConnected(did, desc);
        log.info("Added device to ONOS core. Device Info: "
                         + device.deviceInfo() + " " + did.uri().toString());
        //FIXME this description will be populated only if driver is pushed from outside
        // because otherwise default driver is used
        Device d = deviceService.getDevice(did);
        if (d.is(DeviceDescriptionDiscovery.class)) {
            DeviceDescriptionDiscovery descriptionDiscovery = d.as(DeviceDescriptionDiscovery.class);
            DeviceDescription description = descriptionDiscovery.discoverDeviceDetails();
            if (description != null) {
                deviceStore.createOrUpdateDevice(
                        new ProviderId("snmp", "org.onosproject.provider.device"),
                        did, description);
            } else {
                log.info("No other description given for device {}", d.id());
            }
            providerService.updatePorts(did, descriptionDiscovery.discoverPortDetails());
        } else {
            log.warn("No populate description and ports behaviour for device {}", did);
        }
    } catch (Exception e) {
        log.error("Error while initializing session for the device: "
                          + (device != null ? device.deviceInfo() : null), e);
    }
}
 
Example 20
Source File: NetconfDeviceProvider.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
public void changePortState(DeviceId deviceId, PortNumber portNumber, boolean enable) {
    Device device = deviceService.getDevice(deviceId);
    if (device == null) {
        log.error("Device {} is not present in the store", deviceId);
        return;
    }
    if (!mastershipService.isLocalMaster(deviceId)) {
        log.info("Not master but {}, not changing port state", mastershipService.getLocalRole(deviceId));
        return;
    }
    if (!device.is(PortAdmin.class)) {
        log.warn("Device {} does not support Port Admin", deviceId);
        return;
    }
    PortAdmin portAdmin = device.as(PortAdmin.class);
    CompletableFuture<Boolean> modified;
    if (enable) {
        modified = portAdmin.enable(portNumber);
    } else {
        modified = portAdmin.disable(portNumber);
    }
    modified.thenAccept(result -> {
        if (result) {
            Port port = deviceService.getPort(deviceId, portNumber);
            //rebuilding port description with admin state changed.
            providerService.portStatusChanged(deviceId,
                    DefaultPortDescription.builder()
                            .withPortNumber(portNumber)
                            .isEnabled(enable)
                            .isRemoved(false)
                            .type(port.type())
                            .portSpeed(port.portSpeed())
                            .annotations((SparseAnnotations) port.annotations())
                            .build());
        } else {
            log.warn("Your device {} port {} status can't be changed to {}",
                    deviceId, portNumber, enable);
        }
    });
}