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