blob: a66d827b9be2519e158445481bce7e3697fdd59b [file] [log] [blame]
Claudine Chiu1f036b82017-03-09 16:45:56 -05001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.cluster.NodeId;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.packet.DefaultPacketRequest;
28import org.onosproject.net.packet.PacketPriority;
29import org.onosproject.net.packet.PacketRequest;
30
31import java.util.Optional;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34import static org.onlab.util.Tools.nullIsIllegal;
35
36/**
37 * Codec for the PacketRequest class.
38 */
39
40// TODO: Needs unit test
41
42public class PacketRequestCodec extends JsonCodec<PacketRequest> {
43
44 // JSON field names
45 static final String TRAFFIC_SELECTOR = "selector";
46 static final String PRIORITY = "priority";
47 static final String APP_ID = "appId";
48 static final String NODE_ID = "nodeId";
49 static final String DEVICE_ID = "deviceId";
50
51 private static final String NULL_OBJECT_MSG = "PacketRequest cannot be null";
52 private static final String MISSING_MEMBER_MSG = " member is required in PacketRequest";
53 public static final String REST_APP_ID = "org.onosproject.rest";
54
55 @Override
56 public ObjectNode encode(PacketRequest packetRequest, CodecContext context) {
57 checkNotNull(packetRequest, NULL_OBJECT_MSG);
58
59 final JsonCodec<TrafficSelector> trafficSelectorCodec =
60 context.codec(TrafficSelector.class);
61 final ObjectNode result = context.mapper().createObjectNode()
62 .put(NODE_ID, packetRequest.nodeId().toString())
63 .put(PRIORITY, packetRequest.priority().name())
64 .put(APP_ID, packetRequest.appId().toString());
65 if (packetRequest.deviceId().isPresent()) {
66 result.put(DEVICE_ID, packetRequest.deviceId().get().toString());
67 }
68
69 result.set(TRAFFIC_SELECTOR, trafficSelectorCodec.encode(packetRequest.selector(), context));
70
71 return result;
72 }
73
74 @Override
75 public PacketRequest decode(ObjectNode json, CodecContext context) {
76 if (json == null || !json.isObject()) {
77 return null;
78 }
79
80 final JsonCodec<TrafficSelector> trafficSelectorCodec =
81 context.codec(TrafficSelector.class);
82 TrafficSelector trafficSelector = trafficSelectorCodec.decode(
83 get(json, TRAFFIC_SELECTOR), context);
84 NodeId nodeId = NodeId.nodeId(extractMember(NODE_ID, json));
85 PacketPriority priority = PacketPriority.valueOf(extractMember(PRIORITY, json));
86
87 CoreService coreService = context.getService(CoreService.class);
88 // TODO check appId (currently hardcoded - should it be read from json node?)
89 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
90
91 DeviceId deviceId = null;
92 JsonNode node = json.get(DEVICE_ID);
93 if (node != null) {
94 deviceId = DeviceId.deviceId(node.asText());
95 }
96
97 return new DefaultPacketRequest(trafficSelector, priority, appId, nodeId, Optional.ofNullable(deviceId));
98 }
99
100 /**
101 * Extract member from JSON ObjectNode.
102 *
103 * @param key key for which value is needed
104 * @param json JSON ObjectNode
105 * @return member value
106 */
107 private String extractMember(String key, ObjectNode json) {
108 return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
109 }
110}