@Override public Intent decode(ObjectNode json, CodecContext context) { checkNotNull(json, "JSON cannot be null"); String type = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText(); if (type.equals(PointToPointIntent.class.getSimpleName())) { return context.codec(PointToPointIntent.class).decode(json, context); } else if (type.equals(HostToHostIntent.class.getSimpleName())) { return context.codec(HostToHostIntent.class).decode(json, context); } else if (type.equals(SinglePointToMultiPointIntent.class.getSimpleName())) { return context.codec(SinglePointToMultiPointIntent.class).decode(json, context); } else if (type.equals(MultiPointToSinglePointIntent.class.getSimpleName())) { return context.codec(MultiPointToSinglePointIntent.class).decode(json, context); } throw new IllegalArgumentException("Intent type " + type + " is not supported"); }
@Override public ObjectNode encode(DpiStatistics ds, CodecContext context) { checkNotNull(ds, "DpiStatistics cannot be null"); final ObjectNode result = context.mapper().createObjectNode(); result.put(RECEIVED_TIME, ds.receivedTime()); final JsonCodec<DpiStatInfo> dpiStatInfoCodec = context.codec(DpiStatInfo.class); final ObjectNode jsonDpiStatInfo = dpiStatInfoCodec.encode(ds.dpiStatInfo(), context); result.set(DPI_STATISTICS, jsonDpiStatInfo); return result; }
@Override public DpiStatistics decode(ObjectNode json, CodecContext context) { if (json == null || !json.isObject()) { return null; } log.debug("receivedTime={}, full json={} ", json.get("receivedTime"), json); JsonNode receivedTimeJson = json.get(RECEIVED_TIME); String receivedTime = receivedTimeJson == null ? "" : receivedTimeJson.asText(); final JsonCodec<DpiStatInfo> dpiStatInfoCodec = context.codec(DpiStatInfo.class); DpiStatInfo dpiStatInfo = dpiStatInfoCodec.decode(get(json, DPI_STATISTICS), context); return new DpiStatistics(receivedTime, dpiStatInfo); } }
@Override public ObjectNode encode(HostToHostIntent intent, CodecContext context) { checkNotNull(intent, "Host to host intent cannot be null"); final JsonCodec<ConnectivityIntent> connectivityIntentCodec = context.codec(ConnectivityIntent.class); final ObjectNode result = connectivityIntentCodec.encode(intent, context); final String one = intent.one().toString(); final String two = intent.two().toString(); result.put(ONE, one); result.put(TWO, two); return result; }
@Override public ObjectNode encode(TrafficSelector selector, CodecContext context) { checkNotNull(selector, "Traffic selector cannot be null"); final ObjectNode result = context.mapper().createObjectNode(); final ArrayNode jsonCriteria = result.putArray(CRITERIA); if (selector.criteria() != null) { final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class); for (final Criterion criterion : selector.criteria()) { jsonCriteria.add(criterionCodec.encode(criterion, context)); } } return result; }
@Override public TrafficSelector decode(ObjectNode json, CodecContext context) { final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class); JsonNode criteriaJson = json.get(CRITERIA); TrafficSelector.Builder builder = DefaultTrafficSelector.builder(); if (criteriaJson != null) { IntStream.range(0, criteriaJson.size()) .forEach(i -> builder.add( criterionCodec.decode(get(criteriaJson, i), context))); } return builder.build(); } }
@Override public ObjectNode encode(PointToPointIntent intent, CodecContext context) { checkNotNull(intent, "Point to point intent cannot be null"); final JsonCodec<ConnectivityIntent> connectivityIntentCodec = context.codec(ConnectivityIntent.class); final ObjectNode result = connectivityIntentCodec.encode(intent, context); final JsonCodec<ConnectPoint> connectPointCodec = context.codec(ConnectPoint.class); final ObjectNode ingress = connectPointCodec.encode(intent.filteredIngressPoint().connectPoint(), context); final ObjectNode egress = connectPointCodec.encode(intent.filteredEgressPoint().connectPoint(), context); result.set(INGRESS_POINT, ingress); result.set(EGRESS_POINT, egress); return result; }
@Override public ObjectNode encode(Path path, CodecContext context) { checkNotNull(path, "Path cannot be null"); JsonCodec<Link> codec = context.codec(Link.class); ObjectNode result = context.mapper() .createObjectNode() .put("cost", path.cost()); ArrayNode jsonLinks = result.putArray("links"); for (Link link : path.links()) { jsonLinks.add(codec.encode(link, context)); } return annotate(result, path, context); } }
/** * Adds JSON encoding of the given item annotations to the specified node. * * @param node node to add annotations to * @param entity annotated entity * @param context encode context * @return the given node */ protected ObjectNode annotate(ObjectNode node, T entity, CodecContext context) { if (!entity.annotations().keys().isEmpty()) { JsonCodec<Annotations> codec = context.codec(Annotations.class); node.set("annotations", codec.encode(entity.annotations(), context)); } return node; }
@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; }
@Override public ObjectNode encode(SinglePointToMultiPointIntent intent, CodecContext context) { checkNotNull(intent, "Single Point to Multi Point intent cannot be null"); final JsonCodec<ConnectivityIntent> connectivityIntentCodec = context.codec(ConnectivityIntent.class); final ObjectNode result = connectivityIntentCodec.encode(intent, context); final JsonCodec<ConnectPoint> connectPointCodec = context.codec(ConnectPoint.class); final ObjectNode ingress = connectPointCodec.encode(intent.ingressPoint(), context); final ObjectNode result2 = context.mapper().createObjectNode(); final ArrayNode jsonconnectPoints = result2.putArray(CP_POINTS); if (intent.egressPoints() != null) { for (final ConnectPoint cp : intent.egressPoints()) { jsonconnectPoints.add(connectPointCodec.encode(cp, context)); } result.set(EGRESS_POINT, jsonconnectPoints); } result.set(INGRESS_POINT, ingress); return result; }
@Override public ObjectNode encode(MultiPointToSinglePointIntent intent, CodecContext context) { checkNotNull(intent, "Multi Point to Single Point intent cannot be null"); final JsonCodec<ConnectivityIntent> connectivityIntentCodec = context.codec(ConnectivityIntent.class); final ObjectNode result = connectivityIntentCodec.encode(intent, context); final JsonCodec<ConnectPoint> connectPointCodec = context.codec(ConnectPoint.class); final ObjectNode egress = connectPointCodec.encode(intent.egressPoint(), context); // Check ingress are not null and not contain egress ObjectNode objectCP = context.mapper().createObjectNode(); ArrayNode jsonconnectPoints = objectCP.putArray(CP_POINTS); if (intent.ingressPoints() != null) { for (final ConnectPoint cp : intent.ingressPoints()) { jsonconnectPoints.add(connectPointCodec.encode(cp, context)); } result.set(INGRESS_POINT, jsonconnectPoints); } result.set(EGRESS_POINT, egress); return result; }
@Override public ObjectNode encode(Link link, CodecContext context) { checkNotNull(link, "Link cannot be null"); JsonCodec<ConnectPoint> codec = context.codec(ConnectPoint.class); ObjectNode result = context.mapper().createObjectNode(); result.set(SRC, codec.encode(link.src(), context)); result.set(DST, codec.encode(link.dst(), context)); result.put(TYPE, link.type().toString()); if (link.state() != null) { result.put(STATE, link.state().toString()); } return annotate(result, link, context); }
@Override public ObjectNode encode(GroupBucket bucket, CodecContext context) { checkNotNull(bucket, "Driver cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(TYPE, bucket.type().toString()) .put(WEIGHT, bucket.weight()) .put(PACKETS, bucket.packets()) .put(BYTES, bucket.bytes()) .put(BUCKET_ID, bucket.hashCode()); if (bucket.watchPort() != null) { result.put(WATCH_PORT, bucket.watchPort().toString()); } if (bucket.watchGroup() != null) { result.put(WATCH_GROUP, bucket.watchGroup().toString()); } if (bucket.treatment() != null) { result.set(TREATMENT, context.codec(TrafficTreatment.class).encode(bucket.treatment(), context)); } return result; }
/** * Extracts annotations of given Object. * * @param objNode annotated JSON object node * @param context decode context * @return extracted Annotations */ protected Annotations extractAnnotations(ObjectNode objNode, CodecContext context) { JsonCodec<Annotations> codec = context.codec(Annotations.class); if (objNode.has("annotations") && objNode.isObject()) { return codec.decode(get(objNode, "annotations"), context); } else { return DefaultAnnotations.EMPTY; } }
@Override public ObjectNode encode(Meter meter, CodecContext context) { checkNotNull(meter, "Meter cannot be null"); ObjectNode result = context.mapper().createObjectNode() .put(ID, meter.id().toString()) .put(LIFE, meter.life()) .put(PACKETS, meter.packetsSeen()) .put(BYTES, meter.bytesSeen()) .put(REFERENCE_COUNT, meter.referenceCount()) .put(UNIT, meter.unit().toString()) .put(BURST, meter.isBurst()) .put(DEVICE_ID, meter.deviceId().toString()); if (meter.appId() != null) { result.put(APP_ID, meter.appId().name()); } if (meter.state() != null) { result.put(STATE, meter.state().toString()); } ArrayNode bands = context.mapper().createArrayNode(); meter.bands().forEach(band -> { ObjectNode bandJson = context.codec(Band.class).encode(band, context); bands.add(bandJson); }); result.set(BANDS, bands); return result; } }
@Override public ObjectNode encode(Host host, CodecContext context) { checkNotNull(host, "Host cannot be null"); final JsonCodec<HostLocation> locationCodec = context.codec(HostLocation.class); final ObjectNode result = context.mapper().createObjectNode() .put("id", host.id().toString()) .put("mac", host.mac().toString()) .put("vlan", host.vlan().toString()) .put("innerVlan", host.innerVlan().toString()) .put("outerTpid", host.tpid().toString()) .put("configured", host.configured()); final ArrayNode jsonIpAddresses = result.putArray("ipAddresses"); for (final IpAddress ipAddress : host.ipAddresses()) { jsonIpAddresses.add(ipAddress.toString()); } result.set("ipAddresses", jsonIpAddresses); final ArrayNode jsonLocations = result.putArray("locations"); for (final HostLocation location : host.locations()) { jsonLocations.add(locationCodec.encode(location, context)); } result.set("locations", jsonLocations); return annotate(result, host, context); }
@Override public ObjectNode encode(DisjointPath disjointPath, CodecContext context) { checkNotNull(disjointPath, "Path cannot be null"); JsonCodec<Link> codec = context.codec(Link.class); ObjectNode result = context.mapper() .createObjectNode(); ObjectNode primary = context.mapper() .createObjectNode() .put("cost", disjointPath.primary().cost()); result.set("primary", primary); ArrayNode jsonLinks = primary.putArray("links"); for (Link link : disjointPath.primary().links()) { jsonLinks.add(codec.encode(link, context)); } if (disjointPath.backup() != null) { ObjectNode backup = context.mapper() .createObjectNode() .put("cost", disjointPath.backup().cost()); result.set("backup", backup); ArrayNode jsonLinks1 = backup.putArray("links"); for (Link link1 : disjointPath.backup().links()) { jsonLinks1.add(codec.encode(link1, context)); } } return annotate(result, disjointPath, context); }
@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)); }
@Override public PointToPointIntent decode(ObjectNode json, CodecContext context) { PointToPointIntent.Builder builder = PointToPointIntent.builder(); IntentCodec.intentAttributes(json, context, builder); ConnectivityIntentCodec.intentAttributes(json, context, builder); ObjectNode ingressJson = nullIsIllegal(get(json, INGRESS_POINT), INGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); ConnectPoint ingress = context.codec(ConnectPoint.class) .decode(ingressJson, context); builder.filteredIngressPoint(new FilteredConnectPoint(ingress)); ObjectNode egressJson = nullIsIllegal(get(json, EGRESS_POINT), EGRESS_POINT + IntentCodec.MISSING_MEMBER_MESSAGE); ConnectPoint egress = context.codec(ConnectPoint.class) .decode(egressJson, context); builder.filteredEgressPoint(new FilteredConnectPoint(egress)); return builder.build(); } }