Sho SHIMIZU

Fix cast error on execution of "flow -j" command

This resolves ONOS-2037.

Change-Id: Ie6bf0c7ca0644ae8029df72d3ea7084fa0297aca
......@@ -19,6 +19,7 @@ import java.util.EnumMap;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.OchSignal;
import org.onosproject.net.flow.criteria.Criterion;
import org.onosproject.net.flow.criteria.EthCriterion;
import org.onosproject.net.flow.criteria.EthTypeCriterion;
......@@ -34,9 +35,9 @@ import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
import org.onosproject.net.flow.criteria.LambdaCriterion;
import org.onosproject.net.flow.criteria.MetadataCriterion;
import org.onosproject.net.flow.criteria.MplsCriterion;
import org.onosproject.net.flow.criteria.OchSignalCriterion;
import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
import org.onosproject.net.flow.criteria.PortCriterion;
import org.onosproject.net.flow.criteria.SctpPortCriterion;
......@@ -325,9 +326,15 @@ public final class CriterionCodec extends JsonCodec<Criterion> {
private static class FormatOchSigId implements CriterionTypeFormatter {
@Override
public ObjectNode encodeCriterion(ObjectNode root, Criterion criterion) {
final LambdaCriterion lambdaCriterion =
(LambdaCriterion) criterion;
return root.put("lambda", lambdaCriterion.lambda());
OchSignal ochSignal = ((OchSignalCriterion) criterion).lambda();
ObjectNode child = root.putObject("ochSignalId");
child.put("gridType", ochSignal.gridType().name());
child.put("channelSpacing", ochSignal.channelSpacing().name());
child.put("spacingMultiplier", ochSignal.spacingMultiplier());
child.put("slotGranularity", ochSignal.slotGranularity());
return root;
}
}
......
......@@ -26,6 +26,9 @@ import org.onlab.packet.MplsLabel;
import org.onlab.packet.VlanId;
import org.onosproject.codec.CodecContext;
import org.onosproject.codec.JsonCodec;
import org.onosproject.net.ChannelSpacing;
import org.onosproject.net.GridType;
import org.onosproject.net.Lambda;
import org.onosproject.net.OchSignalType;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.criteria.Criteria;
......@@ -406,8 +409,9 @@ public class CriterionCodecTest {
* Tests lambda criterion.
*/
@Test
public void matchLambdaTest() {
Criterion criterion = Criteria.matchLambda((short) 40000);
public void matchOchSignal() {
Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
Criterion criterion = Criteria.matchLambda(ochSignal);
ObjectNode result = criterionCodec.encode(criterion, context);
assertThat(result, matchesCriterion(criterion));
}
......
......@@ -15,8 +15,10 @@
*/
package org.onosproject.codec.impl;
import com.google.common.base.Joiner;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.onosproject.net.OchSignal;
import org.onosproject.net.flow.criteria.Criterion;
import com.fasterxml.jackson.databind.JsonNode;
......@@ -34,9 +36,9 @@ import org.onosproject.net.flow.criteria.IcmpCodeCriterion;
import org.onosproject.net.flow.criteria.IcmpTypeCriterion;
import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
import org.onosproject.net.flow.criteria.LambdaCriterion;
import org.onosproject.net.flow.criteria.MetadataCriterion;
import org.onosproject.net.flow.criteria.MplsCriterion;
import org.onosproject.net.flow.criteria.OchSignalCriterion;
import org.onosproject.net.flow.criteria.OchSignalTypeCriterion;
import org.onosproject.net.flow.criteria.PortCriterion;
import org.onosproject.net.flow.criteria.SctpPortCriterion;
......@@ -45,6 +47,8 @@ import org.onosproject.net.flow.criteria.UdpPortCriterion;
import org.onosproject.net.flow.criteria.VlanIdCriterion;
import org.onosproject.net.flow.criteria.VlanPcpCriterion;
import java.util.Objects;
/**
* Hamcrest matcher for criterion objects.
*/
......@@ -448,16 +452,29 @@ public final class CriterionJsonMatcher extends
}
/**
* Matches a lambda criterion object.
* Matches an Och signal criterion object.
*
* @param criterion criterion to match
* @return true if the JSON matches the criterion, false otherwise.
*/
private boolean matchCriterion(LambdaCriterion criterion) {
final int lambda = criterion.lambda();
final int jsonLambda = jsonCriterion.get("lambda").intValue();
if (lambda != jsonLambda) {
description.appendText("lambda was " + Integer.toString(lambda));
private boolean matchCriterion(OchSignalCriterion criterion) {
final OchSignal ochSignal = criterion.lambda();
final JsonNode jsonOchSignal = jsonCriterion.get("ochSignalId");
String jsonGridType = jsonOchSignal.get("gridType").textValue();
String jsonChannelSpacing = jsonOchSignal.get("channelSpacing").textValue();
int jsonSpacingMultiplier = jsonOchSignal.get("spacingMultiplier").intValue();
int jsonSlotGranularity = jsonOchSignal.get("slotGranularity").intValue();
boolean equality = Objects.equals(ochSignal.gridType().name(), jsonGridType)
&& Objects.equals(ochSignal.channelSpacing().name(), jsonChannelSpacing)
&& Objects.equals(ochSignal.spacingMultiplier(), jsonSpacingMultiplier)
&& Objects.equals(ochSignal.slotGranularity(), jsonSlotGranularity);
if (!equality) {
String joined = Joiner.on(", ")
.join(jsonGridType, jsonChannelSpacing, jsonSpacingMultiplier, jsonSlotGranularity);
description.appendText("och signal id was " + joined);
return false;
}
return true;
......@@ -572,7 +589,7 @@ public final class CriterionJsonMatcher extends
(IPv6ExthdrFlagsCriterion) criterion);
case OCH_SIGID:
return matchCriterion((LambdaCriterion) criterion);
return matchCriterion((OchSignalCriterion) criterion);
case OCH_SIGTYPE:
return matchCriterion((OchSignalTypeCriterion) criterion);
......
......@@ -27,9 +27,12 @@ import org.onlab.util.Bandwidth;
import org.onosproject.codec.JsonCodec;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.DefaultApplicationId;
import org.onosproject.net.ChannelSpacing;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.GridType;
import org.onosproject.net.HostId;
import org.onosproject.net.Lambda;
import org.onosproject.net.NetTestTools;
import org.onosproject.net.OchSignalType;
import org.onosproject.net.PortNumber;
......@@ -138,11 +141,12 @@ public class IntentCodecTest extends AbstractIntentTest {
DeviceId did1 = did("device1");
DeviceId did2 = did("device2");
DeviceId did3 = did("device3");
Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
final TrafficSelector selector = DefaultTrafficSelector.builder()
.matchIPProtocol((byte) 3)
.matchMplsLabel(MplsLabel.mplsLabel(4))
.add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
.matchLambda((short) 6)
.add(Criteria.matchLambda(ochSignal))
.matchEthDst(MacAddress.BROADCAST)
.matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
.build();
......