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;
}
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());
}
}
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();
}
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);
}
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();
}
Aggregations