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;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.onlab.packet.Ethernet;
import org.onosproject.codec.CodecService;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.Annotations;
......@@ -32,7 +33,12 @@ import org.onosproject.net.Port;
import org.onosproject.net.flow.FlowEntry;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.criteria.Criterion;
import org.onosproject.net.flow.instructions.Instruction;
import org.onosproject.net.intent.ConnectivityIntent;
import org.onosproject.net.intent.HostToHostIntent;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.PointToPointIntent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -61,10 +67,16 @@ public class CodecManager implements CodecService {
registerCodec(Link.class, new LinkCodec());
registerCodec(Host.class, new HostCodec());
registerCodec(HostLocation.class, new HostLocationCodec());
registerCodec(HostToHostIntent.class, new HostToHostIntentCodec());
registerCodec(PointToPointIntent.class, new PointToPointIntentCodec());
registerCodec(Intent.class, new IntentCodec());
registerCodec(ConnectivityIntent.class, new ConnectivityIntentCodec());
registerCodec(FlowEntry.class, new FlowEntryCodec());
registerCodec(TrafficTreatment.class, new TrafficTreatmentCodec());
registerCodec(TrafficSelector.class, new TrafficSelectorCodec());
registerCodec(Instruction.class, new InstructionCodec());
registerCodec(Criterion.class, new CriterionCodec());
registerCodec(Ethernet.class, new EthernetCodec());
log.info("Started");
}
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.intent.ConnectivityIntent;
import org.onosproject.net.intent.Intent;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Connectivity intent codec.
*/
public class ConnectivityIntentCodec extends JsonCodec<ConnectivityIntent> {
@Override
public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
checkNotNull(intent, "Connectivity intent cannot be null");
final JsonCodec<Intent> intentCodec = context.codec(Intent.class);
final ObjectNode result = intentCodec.encode(intent, context);
if (intent.selector() != null) {
final JsonCodec<TrafficSelector> selectorCodec =
context.codec(TrafficSelector.class);
result.set("selector", selectorCodec.encode(intent.selector(), context));
}
if (intent.treatment() != null) {
final JsonCodec<TrafficTreatment> treatmentCodec =
context.codec(TrafficTreatment.class);
result.set("treatment", treatmentCodec.encode(intent.treatment(), context));
}
return result;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.flow.criteria.Criteria;
import org.onosproject.net.flow.criteria.Criterion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Criterion codec.
*/
public class CriterionCodec extends JsonCodec<Criterion> {
protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class);
@Override
public ObjectNode encode(Criterion criterion, CodecContext context) {
checkNotNull(criterion, "Criterion cannot be null");
final ObjectNode result = context.mapper().createObjectNode()
.put("type", criterion.type().toString());
switch (criterion.type()) {
case IN_PORT:
final Criteria.PortCriterion portCriterion = (Criteria.PortCriterion) criterion;
result.put("tcpPort", portCriterion.port().toLong());
break;
case ETH_SRC:
case ETH_DST:
final Criteria.EthCriterion ethCriterion = (Criteria.EthCriterion) criterion;
break;
case ETH_TYPE:
final Criteria.EthTypeCriterion ethTypeCriterion =
(Criteria.EthTypeCriterion) criterion;
result.put("ethType", ethTypeCriterion.ethType());
break;
case IPV4_SRC:
case IPV6_SRC:
final Criteria.IPCriterion iPCriterion = (Criteria.IPCriterion) criterion;
result.put("ip", iPCriterion.ip().toString());
break;
case IP_PROTO:
final Criteria.IPProtocolCriterion iPProtocolCriterion =
(Criteria.IPProtocolCriterion) criterion;
result.put("protocol", iPProtocolCriterion.protocol());
break;
case VLAN_PCP:
final Criteria.VlanPcpCriterion vlanPcpCriterion =
(Criteria.VlanPcpCriterion) criterion;
result.put("priority", vlanPcpCriterion.priority());
break;
case VLAN_VID:
final Criteria.VlanIdCriterion vlanIdCriterion =
(Criteria.VlanIdCriterion) criterion;
result.put("vlanId", vlanIdCriterion.vlanId().toShort());
break;
case TCP_SRC:
case TCP_DST:
final Criteria.TcpPortCriterion tcpPortCriterion =
(Criteria.TcpPortCriterion) criterion;
result.put("tcpPort", tcpPortCriterion.tcpPort().byteValue());
break;
case MPLS_LABEL:
final Criteria.MplsCriterion mplsCriterion =
(Criteria.MplsCriterion) criterion;
result.put("label", mplsCriterion.label());
break;
case OCH_SIGID:
final Criteria.LambdaCriterion lambdaCriterion =
(Criteria.LambdaCriterion) criterion;
result.put("lambda", lambdaCriterion.lambda());
break;
case OCH_SIGTYPE:
final Criteria.OpticalSignalTypeCriterion opticalSignalTypeCriterion =
(Criteria.OpticalSignalTypeCriterion) criterion;
result.put("signalType", opticalSignalTypeCriterion.signalType());
break;
default:
// Don't know how to format this type
log.info("Cannot convert criterion of type {} to JSON",
criterion.type());
break;
}
return result;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import org.onlab.packet.Ethernet;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Ethernet codec.
*/
public class EthernetCodec extends JsonCodec<Ethernet> {
protected static final Logger log = LoggerFactory.getLogger(CriterionCodec.class);
@Override
public ObjectNode encode(Ethernet ethernet, CodecContext context) {
checkNotNull(ethernet, "Ethernet cannot be null");
return context.mapper().createObjectNode()
.put("destinationMacAddress", ethernet.getDestinationMAC().toString())
.put("sourceMacAddress", ethernet.getSourceMAC().toString())
.put("priorityCode", ethernet.getPriorityCode())
.put("vlanId", ethernet.getVlanID())
.put("etherType", ethernet.getEtherType())
.put("pad", ethernet.isPad());
}
}
......@@ -50,13 +50,13 @@ public class FlowEntryCodec extends JsonCodec<FlowEntry> {
if (flowEntry.treatment() != null) {
final JsonCodec<TrafficTreatment> treatmentCodec =
new TrafficTreatmentCodec();
context.codec(TrafficTreatment.class);
result.set("treatment", treatmentCodec.encode(flowEntry.treatment(), context));
}
if (flowEntry.selector() != null) {
final JsonCodec<TrafficSelector> selectorCodec =
new TrafficSelectorCodec();
context.codec(TrafficSelector.class);
result.set("selector", selectorCodec.encode(flowEntry.selector(), context));
}
......
......@@ -34,7 +34,8 @@ public class HostCodec extends AnnotatedCodec<Host> {
@Override
public ObjectNode encode(Host host, CodecContext context) {
checkNotNull(host, "Host cannot be null");
final JsonCodec<HostLocation> locationCodec = new HostLocationCodec();
final JsonCodec<HostLocation> locationCodec =
context.codec(HostLocation.class);
final ObjectNode result = context.mapper().createObjectNode()
.put("id", host.id().toString())
.put("mac", host.mac().toString())
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.intent.ConnectivityIntent;
import org.onosproject.net.intent.HostToHostIntent;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Host to host intent codec.
*/
public class HostToHostIntentCodec extends JsonCodec<HostToHostIntent> {
@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;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import org.onlab.packet.Ethernet;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.flow.instructions.Instruction;
import org.onosproject.net.flow.instructions.Instructions;
import org.onosproject.net.flow.instructions.L0ModificationInstruction;
import org.onosproject.net.flow.instructions.L2ModificationInstruction;
import org.onosproject.net.flow.instructions.L3ModificationInstruction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Instruction codec.
*/
public class InstructionCodec extends JsonCodec<Instruction> {
protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class);
/**
* Encode an L0 modification instruction.
*
* @param result json node that the instruction attributes are added to
* @param instruction The L0 instruction
*/
private void encodeL0(ObjectNode result, L0ModificationInstruction instruction) {
result.put("subtype", instruction.subtype().name());
switch (instruction.subtype()) {
case LAMBDA:
final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
(L0ModificationInstruction.ModLambdaInstruction) instruction;
result.put("lambda", modLambdaInstruction.lambda());
break;
default:
log.info("Cannot convert L0 subtype of {}", instruction.subtype());
}
}
/**
* Encode an L2 modification instruction.
*
* @param result json node that the instruction attributes are added to
* @param instruction The L2 instruction
* @param context context of the request
*/
private void encodeL2(ObjectNode result,
L2ModificationInstruction instruction,
CodecContext context) {
result.put("subtype", instruction.subtype().name());
switch (instruction.subtype()) {
case ETH_SRC:
case ETH_DST:
final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
(L2ModificationInstruction.ModEtherInstruction) instruction;
result.put("mac", modEtherInstruction.mac().toString());
break;
case VLAN_ID:
final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
(L2ModificationInstruction.ModVlanIdInstruction) instruction;
result.put("vlanId", modVlanIdInstruction.vlanId().toShort());
break;
case VLAN_PCP:
final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
(L2ModificationInstruction.ModVlanPcpInstruction) instruction;
result.put("vlanPcp", modVlanPcpInstruction.vlanPcp());
break;
case MPLS_LABEL:
final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
(L2ModificationInstruction.ModMplsLabelInstruction) instruction;
result.put("label", modMplsLabelInstruction.label());
break;
case MPLS_PUSH:
final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
(L2ModificationInstruction.PushHeaderInstructions) instruction;
final JsonCodec<Ethernet> ethernetCodec =
context.codec(Ethernet.class);
result.set("ethernetType",
ethernetCodec.encode(pushHeaderInstructions.ethernetType(),
context));
break;
default:
log.info("Cannot convert L2 subtype of {}", instruction.subtype());
break;
}
}
/**
* Encode an L3 modification instruction.
*
* @param result json node that the instruction attributes are added to
* @param instruction The L3 instruction
*/
private void encodeL3(ObjectNode result, L3ModificationInstruction instruction) {
result.put("subtype", instruction.subtype().name());
switch (instruction.subtype()) {
case IP_SRC:
case IP_DST:
final L3ModificationInstruction.ModIPInstruction modIPInstruction =
(L3ModificationInstruction.ModIPInstruction) instruction;
result.put("ip", modIPInstruction.ip().toString());
break;
default:
log.info("Cannot convert L3 subtype of {}", instruction.subtype());
break;
}
}
@Override
public ObjectNode encode(Instruction instruction, CodecContext context) {
checkNotNull(instruction, "Instruction cannot be null");
final ObjectNode result = context.mapper().createObjectNode()
.put("type", instruction.type().toString());
switch (instruction.type()) {
case OUTPUT:
final Instructions.OutputInstruction outputInstruction =
(Instructions.OutputInstruction) instruction;
result.put("portNumber", outputInstruction.port().toLong());
break;
case DROP:
break;
case L0MODIFICATION:
final L0ModificationInstruction l0ModificationInstruction =
(L0ModificationInstruction) instruction;
encodeL0(result, l0ModificationInstruction);
break;
case L2MODIFICATION:
final L2ModificationInstruction l2ModificationInstruction =
(L2ModificationInstruction) instruction;
encodeL2(result, l2ModificationInstruction, context);
break;
case L3MODIFICATION:
final L3ModificationInstruction l3ModificationInstruction =
(L3ModificationInstruction) instruction;
encodeL3(result, l3ModificationInstruction);
default:
log.info("Cannot convert instruction type of {}", instruction.type());
break;
}
return result;
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.codec.impl;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.intent.ConnectivityIntent;
import org.onosproject.net.intent.PointToPointIntent;
import com.fasterxml.jackson.databind.node.ObjectNode;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Point to point intent codec.
*/
public class PointToPointIntentCodec extends JsonCodec<PointToPointIntent> {
@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.ingressPoint(), context);
final ObjectNode egress =
connectPointCodec.encode(intent.egressPoint(), context);
result.set("ingressPoint", ingress);
result.set("egressPoint", egress);
return result;
}
}
......@@ -37,9 +37,10 @@ public class TrafficSelectorCodec extends JsonCodec<TrafficSelector> {
final ArrayNode jsonCriteria = result.putArray("criteria");
if (selector.criteria() != null) {
for (final Criterion criterion :selector.criteria()) {
// TODO: would be better to have a codec that understands criteria
jsonCriteria.add(criterion.toString());
final JsonCodec<Criterion> criterionCodec =
context.codec(Criterion.class);
for (final Criterion criterion : selector.criteria()) {
jsonCriteria.add(criterionCodec.encode(criterion, context));
}
}
......
......@@ -37,9 +37,10 @@ public class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> {
final ArrayNode jsonInstructions = result.putArray("instructions");
if (treatment.instructions() != null) {
final JsonCodec<Instruction> instructionCodec =
context.codec(Instruction.class);
for (final Instruction instruction : treatment.instructions()) {
// TODO: would be better to have a codec that understands instructions
jsonInstructions.add(instruction.toString());
jsonInstructions.add(instructionCodec.encode(instruction, context));
}
}
......
......@@ -22,9 +22,11 @@ import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.onosproject.net.intent.HostToHostIntent;
import org.onosproject.net.intent.Intent;
import org.onosproject.net.intent.IntentId;
import org.onosproject.net.intent.IntentService;
import org.onosproject.net.intent.PointToPointIntent;
import com.fasterxml.jackson.databind.node.ObjectNode;
......@@ -62,7 +64,14 @@ public class IntentsWebResource extends AbstractWebResource {
final Intent intent = nullIsNotFound(get(IntentService.class)
.getIntent(IntentId.valueOf(id)),
INTENT_NOT_FOUND);
final ObjectNode root = codec(Intent.class).encode(intent, this);
final ObjectNode root;
if (intent instanceof HostToHostIntent) {
root = codec(HostToHostIntent.class).encode((HostToHostIntent) intent, this);
} else if (intent instanceof PointToPointIntent) {
root = codec(PointToPointIntent.class).encode((PointToPointIntent) intent, this);
} else {
root = codec(Intent.class).encode(intent, this);
}
return ok(root.toString()).build();
}
}
......
......@@ -49,7 +49,6 @@ import org.onosproject.net.flow.instructions.L0ModificationInstruction;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.google.common.collect.ImmutableSet;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
......@@ -313,15 +312,17 @@ public class FlowsResourceTest extends JerseyTest {
}
for (final Instruction instruction : flow.treatment().instructions()) {
boolean instructionFound = false;
final String instructionString = instruction.toString();
for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
final JsonValue value = jsonInstructions.get(instructionIndex);
if (value.asString().equals(instructionString)) {
final String jsonType =
jsonInstructions.get(instructionIndex)
.asObject().get("type").asString();
final String instructionType = instruction.type().name();
if (jsonType.equals(instructionType)) {
instructionFound = true;
}
}
if (!instructionFound) {
reason = "instruction " + instructionString;
reason = "instruction " + instruction.toString();
return false;
}
}
......@@ -338,15 +339,18 @@ public class FlowsResourceTest extends JerseyTest {
}
for (final Criterion criterion : flow.selector().criteria()) {
boolean criterionFound = false;
final String criterionString = criterion.toString();
for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
final JsonValue value = jsonCriteria.get(criterionIndex);
if (value.asString().equals(criterionString)) {
final String jsonType =
jsonCriteria.get(criterionIndex)
.asObject().get("type").asString();
final String criterionType = criterion.type().name();
if (jsonType.equals(criterionType)) {
criterionFound = true;
}
}
if (!criterionFound) {
reason = "criterion " + criterionString;
reason = "criterion " + criterion.toString();
return false;
}
}
......