use of org.onosproject.net.flow.TrafficSelector 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.flow.TrafficSelector in project onos by opennetworkinglab.
the class ForwardingObjectiveCodec method encode.
@Override
public ObjectNode encode(ForwardingObjective forwardingObjective, CodecContext context) {
checkNotNull(forwardingObjective, NOT_NULL_MESSAGE);
final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
// encode common properties
ObjectiveCodecHelper och = new ObjectiveCodecHelper();
ObjectNode result = och.encode(forwardingObjective, context);
// encode id
result.put(ID, forwardingObjective.id());
// encode flag
result.put(FLAG, forwardingObjective.flag().toString());
// encode op
result.put(OPERATION, forwardingObjective.op().toString());
// encode selector
ObjectNode trafficSelectorNode = trafficSelectorCodec.encode(forwardingObjective.selector(), context);
result.set(SELECTOR, trafficSelectorNode);
// encode nextId
if (forwardingObjective.nextId() != null) {
result.put(NEXT_ID, forwardingObjective.nextId());
}
// encode treatment
if (forwardingObjective.treatment() != null) {
ObjectNode trafficTreatmentNode = trafficTreatmentCodec.encode(forwardingObjective.treatment(), context);
result.set(TREATMENT, trafficTreatmentNode);
}
return result;
}
use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class NextObjectiveCodec method decode.
@Override
public NextObjective decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
CoreService coreService = context.getService(CoreService.class);
final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
ObjectiveCodecHelper och = new ObjectiveCodecHelper();
DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder();
final DefaultNextObjective.Builder builder = (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context);
// decode id
JsonNode idJson = json.get(ID);
checkNotNull(idJson);
builder.withId(idJson.asInt());
// decode application id
JsonNode appIdJson = json.get(APP_ID);
String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
builder.fromApp(coreService.registerApplication(appId));
// decode type
String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
switch(typeStr) {
case "HASHED":
builder.withType(NextObjective.Type.HASHED);
break;
case "BROADCAST":
builder.withType(NextObjective.Type.BROADCAST);
break;
case "FAILOVER":
builder.withType(NextObjective.Type.FAILOVER);
break;
case "SIMPLE":
builder.withType(NextObjective.Type.SIMPLE);
break;
default:
throw new IllegalArgumentException("The requested type " + typeStr + " is not defined for NextObjective.");
}
// decode treatments
JsonNode treatmentsJson = json.get(TREATMENTS);
checkNotNull(treatmentsJson);
if (treatmentsJson != null) {
IntStream.range(0, treatmentsJson.size()).forEach(i -> {
ObjectNode treatmentJson = get(treatmentsJson, i);
JsonNode weightJson = treatmentJson.get(WEIGHT);
int weight = (weightJson != null) ? weightJson.asInt() : NextTreatment.DEFAULT_WEIGHT;
builder.addTreatment(DefaultNextTreatment.of(trafficTreatmentCodec.decode(treatmentJson, context), weight));
});
}
// decode meta
JsonNode metaJson = json.get(META);
if (metaJson != null) {
TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context);
builder.withMeta(trafficSelector);
}
// decode operation
String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
NextObjective nextObjective;
switch(opStr) {
case "ADD":
nextObjective = builder.add();
break;
case "REMOVE":
nextObjective = builder.remove();
break;
default:
throw new IllegalArgumentException("The requested operation " + opStr + " is not defined for NextObjective.");
}
return nextObjective;
}
use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class PacketRequestCodec method encode.
@Override
public ObjectNode encode(PacketRequest packetRequest, CodecContext context) {
checkNotNull(packetRequest, NULL_OBJECT_MSG);
final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
final ObjectNode result = context.mapper().createObjectNode().put(NODE_ID, packetRequest.nodeId().toString()).put(PRIORITY, packetRequest.priority().name()).put(APP_ID, packetRequest.appId().toString());
if (packetRequest.deviceId().isPresent()) {
result.put(DEVICE_ID, packetRequest.deviceId().get().toString());
}
result.set(TRAFFIC_SELECTOR, trafficSelectorCodec.encode(packetRequest.selector(), context));
return result;
}
use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class PacketRequestCodec method decode.
@Override
public PacketRequest decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
TrafficSelector trafficSelector = trafficSelectorCodec.decode(get(json, TRAFFIC_SELECTOR), context);
NodeId nodeId = NodeId.nodeId(extractMember(NODE_ID, json));
PacketPriority priority = PacketPriority.valueOf(extractMember(PRIORITY, json));
CoreService coreService = context.getService(CoreService.class);
// TODO check appId (currently hardcoded - should it be read from json node?)
ApplicationId appId = coreService.registerApplication(REST_APP_ID);
DeviceId deviceId = null;
JsonNode node = json.get(DEVICE_ID);
if (node != null) {
deviceId = DeviceId.deviceId(node.asText());
}
return new DefaultPacketRequest(trafficSelector, priority, appId, nodeId, Optional.ofNullable(deviceId));
}
Aggregations