Ray Milkey
Committed by Gerrit Code Review

ONOS-574 - Complete REST APIs for flows

- codecs for all objects referenced by flows
- codecs for HostToHost and PointToPoint intents
- standardized on context.codec() rather than direct calls to new codec()

Change-Id: I94fcb6e31a9161132c6efc2793b9c46fa3cc4570
...@@ -20,6 +20,7 @@ import org.apache.felix.scr.annotations.Activate; ...@@ -20,6 +20,7 @@ import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component; 20 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate; 21 import org.apache.felix.scr.annotations.Deactivate;
22 import org.apache.felix.scr.annotations.Service; 22 import org.apache.felix.scr.annotations.Service;
23 +import org.onlab.packet.Ethernet;
23 import org.onosproject.codec.CodecService; 24 import org.onosproject.codec.CodecService;
24 import org.onosproject.codec.JsonCodec; 25 import org.onosproject.codec.JsonCodec;
25 import org.onosproject.net.Annotations; 26 import org.onosproject.net.Annotations;
...@@ -32,7 +33,12 @@ import org.onosproject.net.Port; ...@@ -32,7 +33,12 @@ import org.onosproject.net.Port;
32 import org.onosproject.net.flow.FlowEntry; 33 import org.onosproject.net.flow.FlowEntry;
33 import org.onosproject.net.flow.TrafficSelector; 34 import org.onosproject.net.flow.TrafficSelector;
34 import org.onosproject.net.flow.TrafficTreatment; 35 import org.onosproject.net.flow.TrafficTreatment;
36 +import org.onosproject.net.flow.criteria.Criterion;
37 +import org.onosproject.net.flow.instructions.Instruction;
38 +import org.onosproject.net.intent.ConnectivityIntent;
39 +import org.onosproject.net.intent.HostToHostIntent;
35 import org.onosproject.net.intent.Intent; 40 import org.onosproject.net.intent.Intent;
41 +import org.onosproject.net.intent.PointToPointIntent;
36 import org.slf4j.Logger; 42 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory; 43 import org.slf4j.LoggerFactory;
38 44
...@@ -61,10 +67,16 @@ public class CodecManager implements CodecService { ...@@ -61,10 +67,16 @@ public class CodecManager implements CodecService {
61 registerCodec(Link.class, new LinkCodec()); 67 registerCodec(Link.class, new LinkCodec());
62 registerCodec(Host.class, new HostCodec()); 68 registerCodec(Host.class, new HostCodec());
63 registerCodec(HostLocation.class, new HostLocationCodec()); 69 registerCodec(HostLocation.class, new HostLocationCodec());
70 + registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
71 + registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
64 registerCodec(Intent.class, new IntentCodec()); 72 registerCodec(Intent.class, new IntentCodec());
73 + registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
65 registerCodec(FlowEntry.class, new FlowEntryCodec()); 74 registerCodec(FlowEntry.class, new FlowEntryCodec());
66 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec()); 75 registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
67 registerCodec(TrafficSelector.class, new TrafficSelectorCodec()); 76 registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
77 + registerCodec(Instruction.class, new InstructionCodec());
78 + registerCodec(Criterion.class, new CriterionCodec());
79 + registerCodec(Ethernet.class, new EthernetCodec());
68 log.info("Started"); 80 log.info("Started");
69 } 81 }
70 82
......
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.flow.TrafficSelector;
21 +import org.onosproject.net.flow.TrafficTreatment;
22 +import org.onosproject.net.intent.ConnectivityIntent;
23 +import org.onosproject.net.intent.Intent;
24 +
25 +import com.fasterxml.jackson.databind.node.ObjectNode;
26 +
27 +import static com.google.common.base.Preconditions.checkNotNull;
28 +
29 +/**
30 + * Connectivity intent codec.
31 + */
32 +public class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {
33 +
34 + @Override
35 + public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
36 + checkNotNull(intent, "Connectivity intent cannot be null");
37 +
38 + final JsonCodec<Intent> intentCodec = context.codec(Intent.class);
39 + final ObjectNode result = intentCodec.encode(intent, context);
40 +
41 + if (intent.selector() != null) {
42 + final JsonCodec<TrafficSelector> selectorCodec =
43 + context.codec(TrafficSelector.class);
44 + result.set("selector", selectorCodec.encode(intent.selector(), context));
45 + }
46 +
47 + if (intent.treatment() != null) {
48 + final JsonCodec<TrafficTreatment> treatmentCodec =
49 + context.codec(TrafficTreatment.class);
50 + result.set("treatment", treatmentCodec.encode(intent.treatment(), context));
51 + }
52 +
53 + return result;
54 + }
55 +}
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.flow.criteria.Criteria;
21 +import org.onosproject.net.flow.criteria.Criterion;
22 +import org.slf4j.Logger;
23 +import org.slf4j.LoggerFactory;
24 +
25 +import com.fasterxml.jackson.databind.node.ObjectNode;
26 +
27 +import static com.google.common.base.Preconditions.checkNotNull;
28 +
29 +/**
30 + * Criterion codec.
31 + */
32 +public class CriterionCodec extends JsonCodec<Criterion> {
33 +
34 + protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class);
35 +
36 + @Override
37 + public ObjectNode encode(Criterion criterion, CodecContext context) {
38 + checkNotNull(criterion, "Criterion cannot be null");
39 +
40 + final ObjectNode result = context.mapper().createObjectNode()
41 + .put("type", criterion.type().toString());
42 +
43 + switch (criterion.type()) {
44 +
45 + case IN_PORT:
46 + final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion;
47 + result.put("tcpPort", portCriterion.port().toLong());
48 + break;
49 +
50 + case ETH_SRC:
51 + case ETH_DST:
52 + final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion;
53 + break;
54 +
55 + case ETH_TYPE:
56 + final Criteria.EthTypeCriterion ethTypeCriterion =
57 + (Criteria.EthTypeCriterion) criterion;
58 + result.put("ethType", ethTypeCriterion.ethType());
59 + break;
60 +
61 + case IPV4_SRC:
62 + case IPV6_SRC:
63 + final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion;
64 + result.put("ip", iPCriterion.ip().toString());
65 + break;
66 +
67 + case IP_PROTO:
68 + final Criteria.IPProtocolCriterion iPProtocolCriterion =
69 + (Criteria.IPProtocolCriterion) criterion;
70 + result.put("protocol", iPProtocolCriterion.protocol());
71 + break;
72 +
73 + case VLAN_PCP:
74 + final Criteria.VlanPcpCriterion vlanPcpCriterion =
75 + (Criteria.VlanPcpCriterion) criterion;
76 + result.put("priority", vlanPcpCriterion.priority());
77 + break;
78 +
79 + case VLAN_VID:
80 + final Criteria.VlanIdCriterion vlanIdCriterion =
81 + (Criteria.VlanIdCriterion) criterion;
82 + result.put("vlanId", vlanIdCriterion.vlanId().toShort());
83 + break;
84 +
85 + case TCP_SRC:
86 + case TCP_DST:
87 + final Criteria.TcpPortCriterion tcpPortCriterion =
88 + (Criteria.TcpPortCriterion) criterion;
89 + result.put("tcpPort", tcpPortCriterion.tcpPort().byteValue());
90 + break;
91 +
92 + case MPLS_LABEL:
93 + final Criteria.MplsCriterion mplsCriterion =
94 + (Criteria.MplsCriterion) criterion;
95 + result.put("label", mplsCriterion.label());
96 + break;
97 +
98 + case OCH_SIGID:
99 + final Criteria.LambdaCriterion lambdaCriterion =
100 + (Criteria.LambdaCriterion) criterion;
101 + result.put("lambda", lambdaCriterion.lambda());
102 + break;
103 +
104 + case OCH_SIGTYPE:
105 + final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
106 + (Criteria.OpticalSignalTypeCriterion) criterion;
107 + result.put("signalType", opticalSignalTypeCriterion.signalType());
108 + break;
109 +
110 + default:
111 + // Don't know how to format this type
112 + log.info("Cannot convert criterion of type {} to JSON",
113 + criterion.type());
114 + break;
115 + }
116 +
117 + return result;
118 + }
119 +}
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onlab.packet.Ethernet;
19 +import org.onosproject.codec.CodecContext;
20 +import org.onosproject.codec.JsonCodec;
21 +import org.slf4j.Logger;
22 +import org.slf4j.LoggerFactory;
23 +
24 +import com.fasterxml.jackson.databind.node.ObjectNode;
25 +
26 +import static com.google.common.base.Preconditions.checkNotNull;
27 +
28 +/**
29 + * Ethernet codec.
30 + */
31 +public class EthernetCodec extends JsonCodec<Ethernet> {
32 +
33 + protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class);
34 +
35 + @Override
36 + public ObjectNode encode(Ethernet ethernet, CodecContext context) {
37 + checkNotNull(ethernet, "Ethernet cannot be null");
38 +
39 + return context.mapper().createObjectNode()
40 + .put("destinationMacAddress", ethernet.getDestinationMAC().toString())
41 + .put("sourceMacAddress", ethernet.getSourceMAC().toString())
42 + .put("priorityCode", ethernet.getPriorityCode())
43 + .put("vlanId", ethernet.getVlanID())
44 + .put("etherType", ethernet.getEtherType())
45 + .put("pad", ethernet.isPad());
46 + }
47 +
48 +}
...@@ -50,13 +50,13 @@ public class FlowEntryCodec extends JsonCodec<FlowEntry> { ...@@ -50,13 +50,13 @@ public class FlowEntryCodec extends JsonCodec<FlowEntry> {
50 50
51 if (flowEntry.treatment() != null) { 51 if (flowEntry.treatment() != null) {
52 final JsonCodec<TrafficTreatment> treatmentCodec = 52 final JsonCodec<TrafficTreatment> treatmentCodec =
53 - new TrafficTreatmentCodec(); 53 + context.codec(TrafficTreatment.class);
54 result.set("treatment", treatmentCodec.encode(flowEntry.treatment(), context)); 54 result.set("treatment", treatmentCodec.encode(flowEntry.treatment(), context));
55 } 55 }
56 56
57 if (flowEntry.selector() != null) { 57 if (flowEntry.selector() != null) {
58 final JsonCodec<TrafficSelector> selectorCodec = 58 final JsonCodec<TrafficSelector> selectorCodec =
59 - new TrafficSelectorCodec(); 59 + context.codec(TrafficSelector.class);
60 result.set("selector", selectorCodec.encode(flowEntry.selector(), context)); 60 result.set("selector", selectorCodec.encode(flowEntry.selector(), context));
61 } 61 }
62 62
......
...@@ -34,7 +34,8 @@ public class HostCodec extends AnnotatedCodec<Host> { ...@@ -34,7 +34,8 @@ public class HostCodec extends AnnotatedCodec<Host> {
34 @Override 34 @Override
35 public ObjectNode encode(Host host, CodecContext context) { 35 public ObjectNode encode(Host host, CodecContext context) {
36 checkNotNull(host, "Host cannot be null"); 36 checkNotNull(host, "Host cannot be null");
37 - final JsonCodec<HostLocation> locationCodec = new HostLocationCodec(); 37 + final JsonCodec<HostLocation> locationCodec =
38 + context.codec(HostLocation.class);
38 final ObjectNode result = context.mapper().createObjectNode() 39 final ObjectNode result = context.mapper().createObjectNode()
39 .put("id", host.id().toString()) 40 .put("id", host.id().toString())
40 .put("mac", host.mac().toString()) 41 .put("mac", host.mac().toString())
......
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.intent.ConnectivityIntent;
21 +import org.onosproject.net.intent.HostToHostIntent;
22 +
23 +import com.fasterxml.jackson.databind.node.ObjectNode;
24 +
25 +import static com.google.common.base.Preconditions.checkNotNull;
26 +
27 +/**
28 + * Host to host intent codec.
29 + */
30 +public class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> {
31 +
32 + @Override
33 + public ObjectNode encode(HostToHostIntent intent, CodecContext context) {
34 + checkNotNull(intent, "Host to host intent cannot be null");
35 +
36 + final JsonCodec<ConnectivityIntent> connectivityIntentCodec =
37 + context.codec(ConnectivityIntent.class);
38 + final ObjectNode result = connectivityIntentCodec.encode(intent, context);
39 +
40 + final String one = intent.one().toString();
41 + final String two = intent.two().toString();
42 + result.put("one", one);
43 + result.put("two", two);
44 +
45 + return result;
46 + }
47 +}
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onlab.packet.Ethernet;
19 +import org.onosproject.codec.CodecContext;
20 +import org.onosproject.codec.JsonCodec;
21 +import org.onosproject.net.flow.instructions.Instruction;
22 +import org.onosproject.net.flow.instructions.Instructions;
23 +import org.onosproject.net.flow.instructions.L0ModificationInstruction;
24 +import org.onosproject.net.flow.instructions.L2ModificationInstruction;
25 +import org.onosproject.net.flow.instructions.L3ModificationInstruction;
26 +import org.slf4j.Logger;
27 +import org.slf4j.LoggerFactory;
28 +
29 +import com.fasterxml.jackson.databind.node.ObjectNode;
30 +
31 +import static com.google.common.base.Preconditions.checkNotNull;
32 +
33 +/**
34 + * Instruction codec.
35 + */
36 +public class InstructionCodec extends JsonCodec<Instruction> {
37 +
38 + protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class);
39 +
40 + /**
41 + * Encode an L0 modification instruction.
42 + *
43 + * @param result json node that the instruction attributes are added to
44 + * @param instruction The L0 instruction
45 + */
46 + private void encodeL0(ObjectNode result, L0ModificationInstruction instruction) {
47 + result.put("subtype", instruction.subtype().name());
48 +
49 + switch (instruction.subtype()) {
50 + case LAMBDA:
51 + final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
52 + (L0ModificationInstruction.ModLambdaInstruction) instruction;
53 + result.put("lambda", modLambdaInstruction.lambda());
54 + break;
55 +
56 + default:
57 + log.info("Cannot convert L0 subtype of {}", instruction.subtype());
58 + }
59 + }
60 +
61 + /**
62 + * Encode an L2 modification instruction.
63 + *
64 + * @param result json node that the instruction attributes are added to
65 + * @param instruction The L2 instruction
66 + * @param context context of the request
67 + */
68 + private void encodeL2(ObjectNode result,
69 + L2ModificationInstruction instruction,
70 + CodecContext context) {
71 + result.put("subtype", instruction.subtype().name());
72 +
73 + switch (instruction.subtype()) {
74 + case ETH_SRC:
75 + case ETH_DST:
76 + final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
77 + (L2ModificationInstruction.ModEtherInstruction) instruction;
78 + result.put("mac", modEtherInstruction.mac().toString());
79 + break;
80 +
81 + case VLAN_ID:
82 + final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
83 + (L2ModificationInstruction.ModVlanIdInstruction) instruction;
84 + result.put("vlanId", modVlanIdInstruction.vlanId().toShort());
85 + break;
86 +
87 + case VLAN_PCP:
88 + final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
89 + (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
90 + result.put("vlanPcp", modVlanPcpInstruction.vlanPcp());
91 + break;
92 +
93 + case MPLS_LABEL:
94 + final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
95 + (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
96 + result.put("label", modMplsLabelInstruction.label());
97 + break;
98 +
99 + case MPLS_PUSH:
100 + final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
101 + (L2ModificationInstruction.PushHeaderInstructions) instruction;
102 +
103 + final JsonCodec<Ethernet> ethernetCodec =
104 + context.codec(Ethernet.class);
105 + result.set("ethernetType",
106 + ethernetCodec.encode(pushHeaderInstructions.ethernetType(),
107 + context));
108 + break;
109 +
110 + default:
111 + log.info("Cannot convert L2 subtype of {}", instruction.subtype());
112 + break;
113 + }
114 + }
115 +
116 + /**
117 + * Encode an L3 modification instruction.
118 + *
119 + * @param result json node that the instruction attributes are added to
120 + * @param instruction The L3 instruction
121 + */
122 + private void encodeL3(ObjectNode result, L3ModificationInstruction instruction) {
123 + result.put("subtype", instruction.subtype().name());
124 + switch (instruction.subtype()) {
125 + case IP_SRC:
126 + case IP_DST:
127 + final L3ModificationInstruction.ModIPInstruction modIPInstruction =
128 + (L3ModificationInstruction.ModIPInstruction) instruction;
129 + result.put("ip", modIPInstruction.ip().toString());
130 + break;
131 +
132 + default:
133 + log.info("Cannot convert L3 subtype of {}", instruction.subtype());
134 + break;
135 + }
136 + }
137 +
138 + @Override
139 + public ObjectNode encode(Instruction instruction, CodecContext context) {
140 + checkNotNull(instruction, "Instruction cannot be null");
141 +
142 + final ObjectNode result = context.mapper().createObjectNode()
143 + .put("type", instruction.type().toString());
144 +
145 +
146 + switch (instruction.type()) {
147 + case OUTPUT:
148 + final Instructions.OutputInstruction outputInstruction =
149 + (Instructions.OutputInstruction) instruction;
150 + result.put("portNumber", outputInstruction.port().toLong());
151 + break;
152 +
153 + case DROP:
154 + break;
155 +
156 + case L0MODIFICATION:
157 + final L0ModificationInstruction l0ModificationInstruction =
158 + (L0ModificationInstruction) instruction;
159 + encodeL0(result, l0ModificationInstruction);
160 + break;
161 +
162 + case L2MODIFICATION:
163 + final L2ModificationInstruction l2ModificationInstruction =
164 + (L2ModificationInstruction) instruction;
165 + encodeL2(result, l2ModificationInstruction, context);
166 + break;
167 +
168 + case L3MODIFICATION:
169 + final L3ModificationInstruction l3ModificationInstruction =
170 + (L3ModificationInstruction) instruction;
171 + encodeL3(result, l3ModificationInstruction);
172 +
173 + default:
174 + log.info("Cannot convert instruction type of {}", instruction.type());
175 + break;
176 + }
177 + return result;
178 + }
179 +}
1 +/*
2 + * Copyright 2015 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 + */
16 +package org.onosproject.codec.impl;
17 +
18 +import org.onosproject.codec.CodecContext;
19 +import org.onosproject.codec.JsonCodec;
20 +import org.onosproject.net.ConnectPoint;
21 +import org.onosproject.net.intent.ConnectivityIntent;
22 +import org.onosproject.net.intent.PointToPointIntent;
23 +
24 +import com.fasterxml.jackson.databind.node.ObjectNode;
25 +
26 +import static com.google.common.base.Preconditions.checkNotNull;
27 +
28 +/**
29 + * Point to point intent codec.
30 + */
31 +public class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> {
32 +
33 + @Override
34 + public ObjectNode encode(PointToPointIntent intent, CodecContext context) {
35 + checkNotNull(intent, "Point to point intent cannot be null");
36 +
37 + final JsonCodec<ConnectivityIntent> connectivityIntentCodec =
38 + context.codec(ConnectivityIntent.class);
39 + final ObjectNode result = connectivityIntentCodec.encode(intent, context);
40 +
41 + final JsonCodec<ConnectPoint> connectPointCodec =
42 + context.codec(ConnectPoint.class);
43 + final ObjectNode ingress =
44 + connectPointCodec.encode(intent.ingressPoint(), context);
45 + final ObjectNode egress =
46 + connectPointCodec.encode(intent.egressPoint(), context);
47 +
48 + result.set("ingressPoint", ingress);
49 + result.set("egressPoint", egress);
50 +
51 + return result;
52 + }
53 +}
...@@ -37,9 +37,10 @@ public class TrafficSelectorCodec extends JsonCodec<TrafficSelector> { ...@@ -37,9 +37,10 @@ public class TrafficSelectorCodec extends JsonCodec<TrafficSelector> {
37 final ArrayNode jsonCriteria = result.putArray("criteria"); 37 final ArrayNode jsonCriteria = result.putArray("criteria");
38 38
39 if (selector.criteria() != null) { 39 if (selector.criteria() != null) {
40 - for (final Criterion criterion :selector.criteria()) { 40 + final JsonCodec<Criterion> criterionCodec =
41 - // TODO: would be better to have a codec that understands criteria 41 + context.codec(Criterion.class);
42 - jsonCriteria.add(criterion.toString()); 42 + for (final Criterion criterion : selector.criteria()) {
43 + jsonCriteria.add(criterionCodec.encode(criterion, context));
43 } 44 }
44 } 45 }
45 46
......
...@@ -37,9 +37,10 @@ public class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> { ...@@ -37,9 +37,10 @@ public class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> {
37 final ArrayNode jsonInstructions = result.putArray("instructions"); 37 final ArrayNode jsonInstructions = result.putArray("instructions");
38 38
39 if (treatment.instructions() != null) { 39 if (treatment.instructions() != null) {
40 + final JsonCodec<Instruction> instructionCodec =
41 + context.codec(Instruction.class);
40 for (final Instruction instruction : treatment.instructions()) { 42 for (final Instruction instruction : treatment.instructions()) {
41 - // TODO: would be better to have a codec that understands instructions 43 + jsonInstructions.add(instructionCodec.encode(instruction, context));
42 - jsonInstructions.add(instruction.toString());
43 } 44 }
44 } 45 }
45 46
......
...@@ -22,9 +22,11 @@ import javax.ws.rs.Produces; ...@@ -22,9 +22,11 @@ import javax.ws.rs.Produces;
22 import javax.ws.rs.core.MediaType; 22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response; 23 import javax.ws.rs.core.Response;
24 24
25 +import org.onosproject.net.intent.HostToHostIntent;
25 import org.onosproject.net.intent.Intent; 26 import org.onosproject.net.intent.Intent;
26 import org.onosproject.net.intent.IntentId; 27 import org.onosproject.net.intent.IntentId;
27 import org.onosproject.net.intent.IntentService; 28 import org.onosproject.net.intent.IntentService;
29 +import org.onosproject.net.intent.PointToPointIntent;
28 30
29 import com.fasterxml.jackson.databind.node.ObjectNode; 31 import com.fasterxml.jackson.databind.node.ObjectNode;
30 32
...@@ -62,7 +64,14 @@ public class IntentsWebResource extends AbstractWebResource { ...@@ -62,7 +64,14 @@ public class IntentsWebResource extends AbstractWebResource {
62 final Intent intent = nullIsNotFound(get(IntentService.class) 64 final Intent intent = nullIsNotFound(get(IntentService.class)
63 .getIntent(IntentId.valueOf(id)), 65 .getIntent(IntentId.valueOf(id)),
64 INTENT_NOT_FOUND); 66 INTENT_NOT_FOUND);
65 - final ObjectNode root = codec(Intent.class).encode(intent, this); 67 + final ObjectNode root;
68 + if (intent instanceof HostToHostIntent) {
69 + root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this);
70 + } else if (intent instanceof PointToPointIntent) {
71 + root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this);
72 + } else {
73 + root = codec(Intent.class).encode(intent, this);
74 + }
66 return ok(root.toString()).build(); 75 return ok(root.toString()).build();
67 } 76 }
68 } 77 }
......
...@@ -49,7 +49,6 @@ import org.onosproject.net.flow.instructions.L0ModificationInstruction; ...@@ -49,7 +49,6 @@ import org.onosproject.net.flow.instructions.L0ModificationInstruction;
49 49
50 import com.eclipsesource.json.JsonArray; 50 import com.eclipsesource.json.JsonArray;
51 import com.eclipsesource.json.JsonObject; 51 import com.eclipsesource.json.JsonObject;
52 -import com.eclipsesource.json.JsonValue;
53 import com.google.common.collect.ImmutableSet; 52 import com.google.common.collect.ImmutableSet;
54 import com.sun.jersey.api.client.UniformInterfaceException; 53 import com.sun.jersey.api.client.UniformInterfaceException;
55 import com.sun.jersey.api.client.WebResource; 54 import com.sun.jersey.api.client.WebResource;
...@@ -313,15 +312,17 @@ public class FlowsResourceTest extends JerseyTest { ...@@ -313,15 +312,17 @@ public class FlowsResourceTest extends JerseyTest {
313 } 312 }
314 for (final Instruction instruction : flow.treatment().instructions()) { 313 for (final Instruction instruction : flow.treatment().instructions()) {
315 boolean instructionFound = false; 314 boolean instructionFound = false;
316 - final String instructionString = instruction.toString();
317 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) { 315 for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
318 - final JsonValue value = jsonInstructions.get(instructionIndex); 316 + final String jsonType =
319 - if (value.asString().equals(instructionString)) { 317 + jsonInstructions.get(instructionIndex)
318 + .asObject().get("type").asString();
319 + final String instructionType = instruction.type().name();
320 + if (jsonType.equals(instructionType)) {
320 instructionFound = true; 321 instructionFound = true;
321 } 322 }
322 } 323 }
323 if (!instructionFound) { 324 if (!instructionFound) {
324 - reason = "instruction " + instructionString; 325 + reason = "instruction " + instruction.toString();
325 return false; 326 return false;
326 } 327 }
327 } 328 }
...@@ -338,15 +339,18 @@ public class FlowsResourceTest extends JerseyTest { ...@@ -338,15 +339,18 @@ public class FlowsResourceTest extends JerseyTest {
338 } 339 }
339 for (final Criterion criterion : flow.selector().criteria()) { 340 for (final Criterion criterion : flow.selector().criteria()) {
340 boolean criterionFound = false; 341 boolean criterionFound = false;
341 - final String criterionString = criterion.toString(); 342 +
342 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) { 343 for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
343 - final JsonValue value = jsonCriteria.get(criterionIndex); 344 + final String jsonType =
344 - if (value.asString().equals(criterionString)) { 345 + jsonCriteria.get(criterionIndex)
346 + .asObject().get("type").asString();
347 + final String criterionType = criterion.type().name();
348 + if (jsonType.equals(criterionType)) {
345 criterionFound = true; 349 criterionFound = true;
346 } 350 }
347 } 351 }
348 if (!criterionFound) { 352 if (!criterionFound) {
349 - reason = "criterion " + criterionString; 353 + reason = "criterion " + criterion.toString();
350 return false; 354 return false;
351 } 355 }
352 } 356 }
......