Search in sources :

Example 81 with DeviceId

use of org.onosproject.net.DeviceId in project onos by opennetworkinglab.

the class EncodeConstraintCodecHelper method encodeObstacleConstraint.

/**
 * Encodes an obstacle constraint.
 *
 * @return JSON ObjectNode representing the constraint
 */
private ObjectNode encodeObstacleConstraint() {
    checkNotNull(constraint, "Obstacle constraint cannot be null");
    final ObstacleConstraint obstacleConstraint = (ObstacleConstraint) constraint;
    final ObjectNode result = context.mapper().createObjectNode();
    final ArrayNode jsonObstacles = result.putArray("obstacles");
    for (DeviceId did : obstacleConstraint.obstacles()) {
        jsonObstacles.add(did.toString());
    }
    return result;
}
Also used : ObstacleConstraint(org.onosproject.net.intent.constraint.ObstacleConstraint) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 82 with DeviceId

use of org.onosproject.net.DeviceId 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 83 with DeviceId

use of org.onosproject.net.DeviceId in project onos by opennetworkinglab.

the class FlowRuleCodec method decode.

@Override
public FlowRule decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
    CoreService coreService = context.getService(CoreService.class);
    JsonNode appIdJson = json.get(APP_ID);
    String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
    resultBuilder.fromApp(coreService.registerApplication(appId));
    int priority = nullIsIllegal(json.get(PRIORITY), PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
    resultBuilder.withPriority(priority);
    boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT), IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean();
    if (isPermanent) {
        resultBuilder.makePermanent();
    } else {
        resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT), TIMEOUT + MISSING_MEMBER_MESSAGE + " if the flow is temporary").asInt());
    }
    JsonNode tableIdJson = json.get(TABLE_ID);
    if (tableIdJson != null) {
        String tableId = tableIdJson.asText();
        try {
            int tid = Integer.parseInt(tableId);
            resultBuilder.forTable(IndexTableId.of(tid));
        } catch (NumberFormatException e) {
            resultBuilder.forTable(PiTableId.of(tableId));
        }
    }
    DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
    resultBuilder.forDevice(deviceId);
    ObjectNode treatmentJson = get(json, TREATMENT);
    if (treatmentJson != null) {
        JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class);
        resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
    }
    ObjectNode selectorJson = get(json, SELECTOR);
    if (selectorJson != null) {
        JsonCodec<TrafficSelector> selectorCodec = context.codec(TrafficSelector.class);
        resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
    }
    return resultBuilder.build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) CoreService(org.onosproject.core.CoreService) JsonNode(com.fasterxml.jackson.databind.JsonNode) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) FlowRule(org.onosproject.net.flow.FlowRule)

Example 84 with DeviceId

use of org.onosproject.net.DeviceId in project onos by opennetworkinglab.

the class GroupCodec method decode.

@Override
public Group decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    final JsonCodec<GroupBucket> groupBucketCodec = context.codec(GroupBucket.class);
    CoreService coreService = context.getService(CoreService.class);
    // parse uInt Group id from the ID field
    JsonNode idNode = json.get(ID);
    Long id = (null == idNode) ? // use GROUP_ID for the corresponding Group id if ID is not supplied
    nullIsIllegal(json.get(GROUP_ID), ID + MISSING_MEMBER_MESSAGE).asLong() : idNode.asLong();
    GroupId groupId = GroupId.valueOf(id.intValue());
    // parse uInt Group id given by caller. see the GroupDescription javadoc
    JsonNode givenGroupIdNode = json.get(GIVEN_GROUP_ID);
    // if GIVEN_GROUP_ID is not supplied, set null so the group subsystem
    // to choose the Group id (which is the value of ID indeed).
    // else, must be same with ID to show both originate from the same source
    Integer givenGroupIdInt = (null == givenGroupIdNode) ? null : new Long(json.get(GIVEN_GROUP_ID).asLong()).intValue();
    if (givenGroupIdInt != null && !givenGroupIdInt.equals(groupId.id())) {
        throw new IllegalArgumentException(GIVEN_GROUP_ID + " must be same with " + ID);
    }
    // parse group key (appCookie)
    String groupKeyStr = nullIsIllegal(json.get(APP_COOKIE), APP_COOKIE + MISSING_MEMBER_MESSAGE).asText();
    if (!groupKeyStr.startsWith("0x")) {
        throw new IllegalArgumentException("APP_COOKIE must be a hex string starts with 0x");
    }
    GroupKey groupKey = new DefaultGroupKey(HexString.fromHexString(groupKeyStr.split("0x")[1], ""));
    // parse device id
    DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
    // application id
    ApplicationId appId = coreService.registerApplication(REST_APP_ID);
    // parse group type
    String type = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
    GroupDescription.Type groupType = null;
    switch(type) {
        case "SELECT":
            groupType = Group.Type.SELECT;
            break;
        case "INDIRECT":
            groupType = Group.Type.INDIRECT;
            break;
        case "ALL":
            groupType = Group.Type.ALL;
            break;
        case "CLONE":
            groupType = Group.Type.CLONE;
            break;
        case "FAILOVER":
            groupType = Group.Type.FAILOVER;
            break;
        default:
            nullIsIllegal(groupType, "The requested group type " + type + " is not valid");
    }
    // parse group buckets
    GroupBuckets buckets = null;
    List<GroupBucket> groupBucketList = new ArrayList<>();
    JsonNode bucketsJson = json.get(BUCKETS);
    checkNotNull(bucketsJson);
    if (bucketsJson != null) {
        IntStream.range(0, bucketsJson.size()).forEach(i -> {
            ObjectNode bucketJson = get(bucketsJson, i);
            bucketJson.put("type", type);
            groupBucketList.add(groupBucketCodec.decode(bucketJson, context));
        });
        buckets = new GroupBuckets(groupBucketList);
    }
    GroupDescription groupDescription = new DefaultGroupDescription(deviceId, groupType, buckets, groupKey, givenGroupIdInt, appId);
    return new DefaultGroup(groupId, groupDescription);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceId(org.onosproject.net.DeviceId) GroupKey(org.onosproject.net.group.GroupKey) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) ArrayList(java.util.ArrayList) DefaultGroup(org.onosproject.net.group.DefaultGroup) CoreService(org.onosproject.core.CoreService) JsonNode(com.fasterxml.jackson.databind.JsonNode) HexString(org.onlab.util.HexString) GroupBuckets(org.onosproject.net.group.GroupBuckets) GroupId(org.onosproject.core.GroupId) DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription) GroupDescription(org.onosproject.net.group.GroupDescription) DefaultGroupKey(org.onosproject.net.group.DefaultGroupKey) GroupBucket(org.onosproject.net.group.GroupBucket) ApplicationId(org.onosproject.core.ApplicationId) DefaultGroupDescription(org.onosproject.net.group.DefaultGroupDescription)

Example 85 with DeviceId

use of org.onosproject.net.DeviceId in project onos by opennetworkinglab.

the class MeterRequestCodec method decode.

@Override
public MeterRequest decode(ObjectNode json, CodecContext context) {
    if (json == null || !json.isObject()) {
        return null;
    }
    final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
    // parse device id
    DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
    // application id
    if (applicationId == null) {
        CoreService coreService = context.getService(CoreService.class);
        applicationId = coreService.registerApplication(REST_APP_ID);
    }
    // parse burst
    boolean burst = false;
    JsonNode burstJson = json.get("burst");
    if (burstJson != null) {
        burst = burstJson.asBoolean();
    }
    // parse unit type
    String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
    Meter.Unit meterUnit = null;
    switch(unit) {
        case "KB_PER_SEC":
            meterUnit = Meter.Unit.KB_PER_SEC;
            break;
        case "PKTS_PER_SEC":
            meterUnit = Meter.Unit.PKTS_PER_SEC;
            break;
        case "BYTES_PER_SEC":
            meterUnit = Meter.Unit.BYTES_PER_SEC;
            break;
        default:
            nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter.");
    }
    // parse meter bands
    List<Band> bandList = new ArrayList<>();
    JsonNode bandsJson = json.get(BANDS);
    checkNotNull(bandsJson);
    if (bandsJson != null) {
        IntStream.range(0, bandsJson.size()).forEach(i -> {
            ObjectNode bandJson = get(bandsJson, i);
            bandList.add(meterBandCodec.decode(bandJson, context));
        });
    }
    // parse scope and index
    JsonNode scopeJson = json.get(SCOPE);
    MeterScope scope = null;
    if (scopeJson != null && !isNullOrEmpty(scopeJson.asText())) {
        scope = MeterScope.of(scopeJson.asText());
    }
    JsonNode indexJson = json.get(INDEX);
    Long index = null;
    if (indexJson != null && !isNullOrEmpty(indexJson.asText()) && scope != null) {
        index = indexJson.asLong();
    }
    // build the final request
    MeterRequest.Builder meterRequest = DefaultMeterRequest.builder();
    if (scope != null) {
        meterRequest.withScope(scope);
    }
    if (index != null) {
        meterRequest.withIndex(index);
    }
    meterRequest.fromApp(applicationId).forDevice(deviceId).withUnit(meterUnit).withBands(bandList);
    if (burst) {
        meterRequest.burst();
    }
    return meterRequest.add();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) ArrayList(java.util.ArrayList) CoreService(org.onosproject.core.CoreService) Band(org.onosproject.net.meter.Band) JsonNode(com.fasterxml.jackson.databind.JsonNode) MeterScope(org.onosproject.net.meter.MeterScope) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest)

Aggregations

DeviceId (org.onosproject.net.DeviceId)782 List (java.util.List)178 PortNumber (org.onosproject.net.PortNumber)170 Collectors (java.util.stream.Collectors)152 ConnectPoint (org.onosproject.net.ConnectPoint)152 Set (java.util.Set)138 DeviceService (org.onosproject.net.device.DeviceService)122 DefaultTrafficTreatment (org.onosproject.net.flow.DefaultTrafficTreatment)118 ArrayList (java.util.ArrayList)115 VlanId (org.onlab.packet.VlanId)115 Logger (org.slf4j.Logger)114 Collections (java.util.Collections)109 DefaultTrafficSelector (org.onosproject.net.flow.DefaultTrafficSelector)109 Device (org.onosproject.net.Device)107 Collection (java.util.Collection)106 Test (org.junit.Test)104 CoreService (org.onosproject.core.CoreService)103 FlowRule (org.onosproject.net.flow.FlowRule)101 ImmutableSet (com.google.common.collect.ImmutableSet)99 TrafficTreatment (org.onosproject.net.flow.TrafficTreatment)96