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 | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 20 | import java.time.Duration; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 21 | import java.util.List; |
| 22 | |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 23 | import org.junit.Before; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 27 | import org.onlab.packet.MplsLabel; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 28 | import org.onlab.util.Bandwidth; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 29 | import org.onosproject.codec.JsonCodec; |
| 30 | import org.onosproject.core.ApplicationId; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 31 | import org.onosproject.core.CoreService; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 32 | import org.onosproject.core.DefaultApplicationId; |
Sho SHIMIZU | c44c0c3 | 2015-06-01 11:40:48 -0700 | [diff] [blame] | 33 | import org.onosproject.net.ChannelSpacing; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | c44c0c3 | 2015-06-01 11:40:48 -0700 | [diff] [blame] | 36 | import org.onosproject.net.GridType; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 37 | import org.onosproject.net.HostId; |
Sho SHIMIZU | 9553bb8 | 2015-06-30 16:02:45 -0700 | [diff] [blame] | 38 | import org.onosproject.net.IndexedLambda; |
Sho SHIMIZU | c44c0c3 | 2015-06-01 11:40:48 -0700 | [diff] [blame] | 39 | import org.onosproject.net.Lambda; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 40 | import org.onosproject.net.NetTestTools; |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 41 | import org.onosproject.net.OchSignalType; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 42 | import org.onosproject.net.PortNumber; |
| 43 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 44 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 45 | import org.onosproject.net.flow.TrafficSelector; |
| 46 | import org.onosproject.net.flow.TrafficTreatment; |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 47 | import org.onosproject.net.flow.criteria.Criteria; |
Sho SHIMIZU | 9553bb8 | 2015-06-30 16:02:45 -0700 | [diff] [blame] | 48 | import org.onosproject.net.flow.instructions.Instructions; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 49 | import org.onosproject.net.flow.criteria.Criterion; |
| 50 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 51 | import org.onosproject.net.flow.instructions.Instruction; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 52 | import org.onosproject.net.intent.AbstractIntentTest; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 53 | import org.onosproject.net.intent.Constraint; |
| 54 | import org.onosproject.net.intent.HostToHostIntent; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 55 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 56 | import org.onosproject.net.intent.IntentService; |
| 57 | import org.onosproject.net.intent.IntentServiceAdapter; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 58 | import org.onosproject.net.intent.PointToPointIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 59 | import org.onosproject.net.intent.constraint.AnnotationConstraint; |
| 60 | import org.onosproject.net.intent.constraint.AsymmetricPathConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 61 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 62 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 63 | import org.onosproject.net.intent.constraint.LatencyConstraint; |
| 64 | import org.onosproject.net.intent.constraint.ObstacleConstraint; |
| 65 | import org.onosproject.net.intent.constraint.WaypointConstraint; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 66 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 67 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 68 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 69 | import com.google.common.collect.ImmutableList; |
| 70 | |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 71 | import static org.easymock.EasyMock.createMock; |
| 72 | import static org.easymock.EasyMock.expect; |
| 73 | import static org.easymock.EasyMock.replay; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 74 | import static org.hamcrest.MatcherAssert.assertThat; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 75 | import static org.hamcrest.Matchers.hasSize; |
| 76 | import static org.hamcrest.Matchers.instanceOf; |
| 77 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 78 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 79 | import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent; |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 80 | import static org.onosproject.net.NetTestTools.did; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 81 | import static org.onosproject.net.NetTestTools.hid; |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 82 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Unit tests for the host to host intent class codec. |
| 86 | */ |
| 87 | public class IntentCodecTest extends AbstractIntentTest { |
| 88 | |
| 89 | private final HostId id1 = hid("12:34:56:78:91:ab/1"); |
| 90 | private final HostId id2 = hid("12:34:56:78:92:ab/1"); |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 91 | private final ApplicationId appId = new DefaultApplicationId(3, "test"); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 92 | final TrafficSelector emptySelector = |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 93 | DefaultTrafficSelector.emptySelector(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 94 | final TrafficTreatment emptyTreatment = |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 95 | DefaultTrafficTreatment.emptyTreatment(); |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 96 | private final MockCodecContext context = new MockCodecContext(); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 97 | final CoreService mockCoreService = createMock(CoreService.class); |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 98 | |
| 99 | @Before |
| 100 | public void setUpIntentService() { |
| 101 | final IntentService mockIntentService = new IntentServiceAdapter(); |
| 102 | context.registerService(IntentService.class, mockIntentService); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 103 | context.registerService(CoreService.class, mockCoreService); |
Ray Milkey | f7cb401 | 2015-07-20 13:01:07 -0700 | [diff] [blame] | 104 | expect(mockCoreService.getAppId(appId.name())) |
| 105 | .andReturn(appId); |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 106 | replay(mockCoreService); |
Ray Milkey | 1534f8d | 2015-05-13 15:42:50 -0700 | [diff] [blame] | 107 | } |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Tests the encoding of a host to host intent. |
| 111 | */ |
| 112 | @Test |
| 113 | public void hostToHostIntent() { |
| 114 | final HostToHostIntent intent = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 115 | HostToHostIntent.builder() |
| 116 | .appId(appId) |
| 117 | .one(id1) |
| 118 | .two(id2) |
| 119 | .build(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 120 | |
| 121 | final JsonCodec<HostToHostIntent> intentCodec = |
| 122 | context.codec(HostToHostIntent.class); |
| 123 | assertThat(intentCodec, notNullValue()); |
| 124 | |
| 125 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 126 | assertThat(intentJson, matchesIntent(intent)); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Tests the encoding of a point to point intent. |
| 131 | */ |
| 132 | @Test |
| 133 | public void pointToPointIntent() { |
| 134 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 135 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
| 136 | |
| 137 | final PointToPointIntent intent = |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 138 | PointToPointIntent.builder() |
| 139 | .appId(appId) |
| 140 | .selector(emptySelector) |
| 141 | .treatment(emptyTreatment) |
| 142 | .ingressPoint(ingress) |
| 143 | .egressPoint(egress).build(); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 144 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 145 | final JsonCodec<PointToPointIntent> intentCodec = |
| 146 | context.codec(PointToPointIntent.class); |
| 147 | assertThat(intentCodec, notNullValue()); |
| 148 | |
| 149 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 150 | assertThat(intentJson, matchesIntent(intent)); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Tests the encoding of an intent with treatment, selector and constraints |
| 155 | * specified. |
| 156 | */ |
| 157 | @Test |
| 158 | public void intentWithTreatmentSelectorAndConstraints() { |
| 159 | ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1); |
| 160 | ConnectPoint egress = NetTestTools.connectPoint("egress", 2); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 161 | DeviceId did1 = did("device1"); |
| 162 | DeviceId did2 = did("device2"); |
| 163 | DeviceId did3 = did("device3"); |
Sho SHIMIZU | c44c0c3 | 2015-06-01 11:40:48 -0700 | [diff] [blame] | 164 | Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 165 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 166 | .matchIPProtocol((byte) 3) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 167 | .matchMplsLabel(MplsLabel.mplsLabel(4)) |
Sho SHIMIZU | 6c70f64 | 2015-05-29 17:27:22 -0700 | [diff] [blame] | 168 | .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID)) |
Sho SHIMIZU | c44c0c3 | 2015-06-01 11:40:48 -0700 | [diff] [blame] | 169 | .add(Criteria.matchLambda(ochSignal)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 170 | .matchEthDst(MacAddress.BROADCAST) |
| 171 | .matchIPDst(IpPrefix.valueOf("1.2.3.4/24")) |
| 172 | .build(); |
| 173 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Sho SHIMIZU | 9553bb8 | 2015-06-30 16:02:45 -0700 | [diff] [blame] | 174 | .add(Instructions.modL0Lambda(new IndexedLambda(33))) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 175 | .setMpls(MplsLabel.mplsLabel(44)) |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 176 | .setOutput(PortNumber.CONTROLLER) |
| 177 | .setEthDst(MacAddress.BROADCAST) |
| 178 | .build(); |
Ray Milkey | b9a8a18 | 2015-01-20 14:15:35 -0800 | [diff] [blame] | 179 | |
| 180 | final List<Constraint> constraints = |
| 181 | ImmutableList.of( |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 182 | new BandwidthConstraint(Bandwidth.bps(1.0)), |
Sho SHIMIZU | 03d4253 | 2015-11-23 17:16:30 -0800 | [diff] [blame] | 183 | new LambdaConstraint(new IndexedLambda(3)), |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 184 | new AnnotationConstraint("key", 33.0), |
| 185 | new AsymmetricPathConstraint(), |
| 186 | new LatencyConstraint(Duration.ofSeconds(2)), |
| 187 | new ObstacleConstraint(did1, did2), |
| 188 | new WaypointConstraint(did3)); |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 189 | |
| 190 | final PointToPointIntent intent = |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 191 | PointToPointIntent.builder() |
| 192 | .appId(appId) |
| 193 | .selector(selector) |
| 194 | .treatment(treatment) |
| 195 | .ingressPoint(ingress) |
| 196 | .egressPoint(egress) |
| 197 | .constraints(constraints) |
| 198 | .build(); |
| 199 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 200 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 201 | final JsonCodec<PointToPointIntent> intentCodec = |
| 202 | context.codec(PointToPointIntent.class); |
| 203 | assertThat(intentCodec, notNullValue()); |
| 204 | |
| 205 | final ObjectNode intentJson = intentCodec.encode(intent, context); |
| 206 | assertThat(intentJson, matchesIntent(intent)); |
| 207 | |
| 208 | } |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Reads in a rule from the given resource and decodes it. |
| 212 | * |
| 213 | * @param resourceName resource to use to read the JSON for the rule |
| 214 | * @return decoded flow rule |
| 215 | * @throws IOException if processing the resource fails |
| 216 | */ |
| 217 | private Intent getIntent(String resourceName, JsonCodec intentCodec) throws IOException { |
Phaneendra Manda | 2be2f88 | 2015-11-11 15:23:40 +0530 | [diff] [blame] | 218 | InputStream jsonStream = IntentCodecTest.class |
Ray Milkey | b82c42b | 2015-06-30 09:42:20 -0700 | [diff] [blame] | 219 | .getResourceAsStream(resourceName); |
| 220 | JsonNode json = context.mapper().readTree(jsonStream); |
| 221 | assertThat(json, notNullValue()); |
| 222 | Intent intent = (Intent) intentCodec.decode((ObjectNode) json, context); |
| 223 | assertThat(intent, notNullValue()); |
| 224 | return intent; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Tests the point to point intent JSON codec. |
| 229 | * |
| 230 | * @throws IOException if JSON processing fails |
| 231 | */ |
| 232 | @Test |
| 233 | public void decodePointToPointIntent() throws IOException { |
| 234 | JsonCodec<Intent> intentCodec = context.codec(Intent.class); |
| 235 | assertThat(intentCodec, notNullValue()); |
| 236 | |
| 237 | Intent intent = getIntent("PointToPointIntent.json", intentCodec); |
| 238 | assertThat(intent, notNullValue()); |
| 239 | assertThat(intent, instanceOf(PointToPointIntent.class)); |
| 240 | |
| 241 | PointToPointIntent pointIntent = (PointToPointIntent) intent; |
| 242 | assertThat(pointIntent.priority(), is(55)); |
| 243 | assertThat(pointIntent.ingressPoint().deviceId(), is(did("0000000000000001"))); |
| 244 | assertThat(pointIntent.ingressPoint().port(), is(PortNumber.portNumber(1))); |
| 245 | assertThat(pointIntent.egressPoint().deviceId(), is(did("0000000000000007"))); |
| 246 | assertThat(pointIntent.egressPoint().port(), is(PortNumber.portNumber(2))); |
| 247 | |
| 248 | assertThat(pointIntent.constraints(), hasSize(1)); |
| 249 | |
| 250 | assertThat(pointIntent.selector(), notNullValue()); |
| 251 | assertThat(pointIntent.selector().criteria(), hasSize(1)); |
| 252 | Criterion criterion1 = pointIntent.selector().criteria().iterator().next(); |
| 253 | assertThat(criterion1, instanceOf(EthCriterion.class)); |
| 254 | EthCriterion ethCriterion = (EthCriterion) criterion1; |
| 255 | assertThat(ethCriterion.mac().toString(), is("11:22:33:44:55:66")); |
| 256 | assertThat(ethCriterion.type().name(), is("ETH_DST")); |
| 257 | |
| 258 | assertThat(pointIntent.treatment(), notNullValue()); |
| 259 | assertThat(pointIntent.treatment().allInstructions(), hasSize(1)); |
| 260 | Instruction instruction1 = pointIntent.treatment().allInstructions().iterator().next(); |
| 261 | assertThat(instruction1, instanceOf(ModEtherInstruction.class)); |
| 262 | ModEtherInstruction ethInstruction = (ModEtherInstruction) instruction1; |
| 263 | assertThat(ethInstruction.mac().toString(), is("22:33:44:55:66:77")); |
| 264 | assertThat(ethInstruction.type().toString(), is("L2MODIFICATION")); |
| 265 | assertThat(ethInstruction.subtype().toString(), is("ETH_SRC")); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Tests the host to host intent JSON codec. |
| 270 | * |
| 271 | * @throws IOException |
| 272 | */ |
| 273 | @Test |
| 274 | public void decodeHostToHostIntent() throws IOException { |
| 275 | JsonCodec<Intent> intentCodec = context.codec(Intent.class); |
| 276 | assertThat(intentCodec, notNullValue()); |
| 277 | |
| 278 | Intent intent = getIntent("HostToHostIntent.json", intentCodec); |
| 279 | assertThat(intent, notNullValue()); |
| 280 | assertThat(intent, instanceOf(HostToHostIntent.class)); |
| 281 | |
| 282 | HostToHostIntent hostIntent = (HostToHostIntent) intent; |
| 283 | assertThat(hostIntent.priority(), is(7)); |
| 284 | assertThat(hostIntent.constraints(), hasSize(1)); |
| 285 | } |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 286 | } |