Search in sources :

Example 1 with ExtensionTreatmentCodec

use of org.onosproject.net.flow.ExtensionTreatmentCodec in project onos by opennetworkinglab.

the class EncodeInstructionCodecHelper method encodeExtension.

/**
 * 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());
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) DefaultServiceDirectory(org.onlab.osgi.DefaultServiceDirectory) ServiceDirectory(org.onlab.osgi.ServiceDirectory) Device(org.onosproject.net.Device) ExtensionTreatmentCodec(org.onosproject.net.flow.ExtensionTreatmentCodec) DeviceService(org.onosproject.net.device.DeviceService) Instructions(org.onosproject.net.flow.instructions.Instructions) DefaultServiceDirectory(org.onlab.osgi.DefaultServiceDirectory)

Example 2 with ExtensionTreatmentCodec

use of org.onosproject.net.flow.ExtensionTreatmentCodec in project onos by opennetworkinglab.

the class DecodeInstructionCodecHelper method decodeExtension.

/**
 * Decodes a extension instruction.
 *
 * @return extension treatment
 */
private Instruction decodeExtension() {
    ObjectNode node = (ObjectNode) json.get(InstructionCodec.EXTENSION);
    if (node != null) {
        DeviceId deviceId = getDeviceId();
        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)) {
            ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
            ExtensionTreatment treatment = treatmentCodec.decode(node, context);
            return Instructions.extension(treatment, deviceId);
        } else {
            throw new IllegalArgumentException("There is no codec to decode extension for device " + deviceId.toString());
        }
    }
    return null;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) ServiceDirectory(org.onlab.osgi.ServiceDirectory) DefaultServiceDirectory(org.onlab.osgi.DefaultServiceDirectory) Device(org.onosproject.net.Device) ExtensionTreatmentCodec(org.onosproject.net.flow.ExtensionTreatmentCodec) DeviceService(org.onosproject.net.device.DeviceService) DefaultServiceDirectory(org.onlab.osgi.DefaultServiceDirectory) ExtensionTreatment(org.onosproject.net.flow.instructions.ExtensionTreatment)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DefaultServiceDirectory (org.onlab.osgi.DefaultServiceDirectory)2 ServiceDirectory (org.onlab.osgi.ServiceDirectory)2 Device (org.onosproject.net.Device)2 DeviceId (org.onosproject.net.DeviceId)2 DeviceService (org.onosproject.net.device.DeviceService)2 ExtensionTreatmentCodec (org.onosproject.net.flow.ExtensionTreatmentCodec)2 ExtensionTreatment (org.onosproject.net.flow.instructions.ExtensionTreatment)1 Instructions (org.onosproject.net.flow.instructions.Instructions)1