use of org.onosproject.core.CoreService in project onos by opennetworkinglab.
the class VirtualFlowsListCommand method doExecute.
@Override
protected void doExecute() {
CoreService coreService = get(CoreService.class);
VirtualNetworkService vnetservice = get(VirtualNetworkService.class);
DeviceService deviceService = vnetservice.get(NetworkId.networkId(networkId), DeviceService.class);
FlowRuleService service = vnetservice.get(NetworkId.networkId(networkId), FlowRuleService.class);
contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
compilePredicate();
SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
if (outputJson()) {
print("%s", json(flows.keySet(), flows));
} else {
flows.forEach((device, flow) -> printFlows(device, flow, coreService));
}
}
use of org.onosproject.core.CoreService in project onos by opennetworkinglab.
the class FlowsListCommand method doExecute.
@Override
protected void doExecute() {
CoreService coreService = get(CoreService.class);
DeviceService deviceService = get(DeviceService.class);
FlowRuleService service = get(FlowRuleService.class);
contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
compilePredicate();
if (countOnly && !suppressCoreOutput && filter.isEmpty() && remove == null) {
if (state == null && uri == null) {
deviceService.getDevices().forEach(device -> printCount(device, service));
} else if (uri == null) {
deviceService.getDevices().forEach(device -> printCount(device, FlowEntryState.valueOf(state.toUpperCase()), service));
} else {
Device device = deviceService.getDevice(DeviceId.deviceId(uri));
if (device != null) {
printCount(device, FlowEntryState.valueOf(state.toUpperCase()), service);
}
}
return;
}
SortedMap<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service, coreService);
// Remove flows
if (remove != null) {
flows.values().forEach(flowList -> {
if (!remove.isEmpty()) {
filter.add(remove);
contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
}
if (!filter.isEmpty() || (remove != null && !remove.isEmpty())) {
flowList = filterFlows(flowList);
this.removeFlowsInteractive(flowList, service, coreService);
}
});
return;
}
// Show flows
if (outputJson()) {
print("%s", json(flows.keySet(), flows));
} else {
flows.forEach((device, flow) -> printFlows(device, flow, coreService));
}
}
use of org.onosproject.core.CoreService 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.core.CoreService 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.core.CoreService 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