Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 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 | |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 18 | import java.time.Duration; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 19 | import java.util.List; |
| 20 | |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame^] | 24 | import org.onlab.packet.MplsLabel; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.DefaultApplicationId; |
| 29 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 31 | import org.onosproject.net.HostId; |
| 32 | import org.onosproject.net.NetTestTools; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.intent.Constraint; |
| 39 | import org.onosproject.net.intent.HostToHostIntent; |
| 40 | import org.onosproject.net.intent.AbstractIntentTest; |
| 41 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.constraint.AnnotationConstraint; |
| 43 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 45 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.constraint.LatencyConstraint; |
| 47 | import org.onosproject.net.intent.constraint.ObstacleConstraint; |
| 48 | import org.onosproject.net.intent.constraint.WaypointConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 49 | import org.onosproject.net.resource.Bandwidth; |
| 50 | import org.onosproject.net.resource.Lambda; |
| 51 | |
| 52 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 53 | import com.google.common.collect.ImmutableList; |
| 54 | |
| 55 | import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 56 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 57 | import static org.onosproject.net.NetTestTools.hid; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 58 | import static org.hamcrest.MatcherAssert.assertThat; |
| 59 | import static org.hamcrest.Matchers.notNullValue; |
| 60 | |
| 61 | /** |
| 62 | * Unit tests for the host to host intent class codec. |
| 63 | */ |
| 64 | public class IntentCodecTest extends AbstractIntentTest { |
| 65 | |
| 66 | private final HostId id1 = hid("12:34:56:78:91:ab/1"); |
| 67 | private final HostId id2 = hid("12:34:56:78:92:ab/1"); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 68 | private final ApplicationId appId = new DefaultApplicationId(3, "test"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 69 | final TrafficSelector emptySelector = |
| 70 | DefaultTrafficSelector.builder().build(); |
| 71 | final TrafficTreatment emptyTreatment = |
| 72 | DefaultTrafficTreatment.builder().build(); |
| 73 | private final CodecContext context = new MockCodecContext(); |
| 74 | |
| 75 | /** |
| 76 | * Tests the encoding of a host to host intent. |
| 77 | */ |
| 78 | @Test |
| 79 | public void hostToHostIntent() { |
| 80 | final HostToHostIntent intent = |
| 81 | new HostToHostIntent(appId, id1, id2); |
| 82 | |
| 83 | final JsonCodec<HostToHostIntent> intentCodec = |
| 84 | context.codec(HostToHostIntent.class); |
| 85 | assertThat(intentCodec, notNullValue()); |
| 86 | |
| 87 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 88 | assertThat(intentJson, matchesIntent(intent)); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tests the encoding of a point to point intent. |
| 93 | */ |
| 94 | @Test |
| 95 | public void pointToPointIntent() { |
| 96 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 97 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
| 98 | |
| 99 | final PointToPointIntent intent = |
| 100 | new PointToPointIntent(appId, emptySelector, |
| 101 | emptyTreatment, ingress, egress); |
| 102 | |
| 103 | final CodecContext context = new MockCodecContext(); |
| 104 | final JsonCodec<PointToPointIntent> intentCodec = |
| 105 | context.codec(PointToPointIntent.class); |
| 106 | assertThat(intentCodec, notNullValue()); |
| 107 | |
| 108 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 109 | assertThat(intentJson, matchesIntent(intent)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests the encoding of an intent with treatment, selector and constraints |
| 114 | * specified. |
| 115 | */ |
| 116 | @Test |
| 117 | public void intentWithTreatmentSelectorAndConstraints() { |
| 118 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 119 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 120 | DeviceId did1 = did("device1"); |
| 121 | DeviceId did2 = did("device2"); |
| 122 | DeviceId did3 = did("device3"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 123 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 124 | .matchIPProtocol((byte) 3) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame^] | 125 | .matchMplsLabel(MplsLabel.mplsLabel(4)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 126 | .matchOpticalSignalType((short) 5) |
| 127 | .matchLambda((short) 6) |
| 128 | .matchEthDst(MacAddress.BROADCAST) |
| 129 | .matchIPDst(IpPrefix.valueOf("1.2.3.4/24")) |
| 130 | .build(); |
| 131 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 132 | .setLambda((short) 33) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame^] | 133 | .setMpls(MplsLabel.mplsLabel(44)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 134 | .setOutput(PortNumber.CONTROLLER) |
| 135 | .setEthDst(MacAddress.BROADCAST) |
| 136 | .build(); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 137 | |
| 138 | final List<Constraint> constraints = |
| 139 | ImmutableList.of( |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 140 | new BandwidthConstraint(Bandwidth.bps(1.0)), |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 141 | new LambdaConstraint(Lambda.valueOf(3)), |
| 142 | new AnnotationConstraint("key", 33.0), |
| 143 | new AsymmetricPathConstraint(), |
| 144 | new LatencyConstraint(Duration.ofSeconds(2)), |
| 145 | new ObstacleConstraint(did1, did2), |
| 146 | new WaypointConstraint(did3)); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 147 | |
| 148 | final PointToPointIntent intent = |
| 149 | new PointToPointIntent(appId, selector, treatment, |
| 150 | ingress, egress, constraints); |
| 151 | |
| 152 | final CodecContext context = new MockCodecContext(); |
| 153 | final JsonCodec<PointToPointIntent> intentCodec = |
| 154 | context.codec(PointToPointIntent.class); |
| 155 | assertThat(intentCodec, notNullValue()); |
| 156 | |
| 157 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 158 | assertThat(intentJson, matchesIntent(intent)); |
| 159 | |
| 160 | } |
| 161 | } |