blob: 07a2cabe709284c568d7199204ecb3853c56499d [file] [log] [blame]
Ray Milkeydb358082015-01-13 16:34:38 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkeydb358082015-01-13 16:34:38 -08003 *
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 */
16package org.onosproject.codec.impl;
17
Ray Milkeyb82c42b2015-06-30 09:42:20 -070018import java.io.IOException;
19import java.io.InputStream;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080020import java.time.Duration;
Ray Milkeydb358082015-01-13 16:34:38 -080021import java.util.List;
22
Ray Milkey1534f8d2015-05-13 15:42:50 -070023import org.junit.Before;
Ray Milkeydb358082015-01-13 16:34:38 -080024import org.junit.Test;
25import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010027import org.onlab.packet.MplsLabel;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070028import org.onlab.util.Bandwidth;
Ray Milkeydb358082015-01-13 16:34:38 -080029import org.onosproject.codec.JsonCodec;
30import org.onosproject.core.ApplicationId;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070031import org.onosproject.core.CoreService;
Ray Milkeydb358082015-01-13 16:34:38 -080032import org.onosproject.core.DefaultApplicationId;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070033import org.onosproject.net.ChannelSpacing;
Ray Milkeydb358082015-01-13 16:34:38 -080034import org.onosproject.net.ConnectPoint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080035import org.onosproject.net.DeviceId;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070036import org.onosproject.net.GridType;
Ray Milkeydb358082015-01-13 16:34:38 -080037import org.onosproject.net.HostId;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070038import org.onosproject.net.Lambda;
Ray Milkeydb358082015-01-13 16:34:38 -080039import org.onosproject.net.NetTestTools;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070040import org.onosproject.net.OchSignalType;
Ray Milkeydb358082015-01-13 16:34:38 -080041import org.onosproject.net.PortNumber;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070046import org.onosproject.net.flow.criteria.Criteria;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070047import org.onosproject.net.flow.criteria.Criterion;
48import org.onosproject.net.flow.criteria.EthCriterion;
49import org.onosproject.net.flow.instructions.Instruction;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070050import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080051import org.onosproject.net.intent.Constraint;
52import org.onosproject.net.intent.HostToHostIntent;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070053import org.onosproject.net.intent.Intent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070054import org.onosproject.net.intent.IntentService;
55import org.onosproject.net.intent.IntentServiceAdapter;
Ray Milkeydb358082015-01-13 16:34:38 -080056import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080057import org.onosproject.net.intent.constraint.AnnotationConstraint;
58import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080059import org.onosproject.net.intent.constraint.BandwidthConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080060import org.onosproject.net.intent.constraint.LatencyConstraint;
61import org.onosproject.net.intent.constraint.ObstacleConstraint;
62import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080063
Ray Milkeyb82c42b2015-06-30 09:42:20 -070064import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeydb358082015-01-13 16:34:38 -080065import com.fasterxml.jackson.databind.node.ObjectNode;
66import com.google.common.collect.ImmutableList;
67
Ray Milkeyb82c42b2015-06-30 09:42:20 -070068import static org.easymock.EasyMock.createMock;
69import static org.easymock.EasyMock.expect;
70import static org.easymock.EasyMock.replay;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070071import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070072import static org.hamcrest.Matchers.hasSize;
73import static org.hamcrest.Matchers.instanceOf;
74import static org.hamcrest.Matchers.is;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070075import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080076import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080077import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080078import static org.onosproject.net.NetTestTools.hid;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070079import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Ray Milkeydb358082015-01-13 16:34:38 -080080
81/**
82 * Unit tests for the host to host intent class codec.
83 */
84public 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 Vachuska02aeb032015-01-06 22:36:30 -080088 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080089 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070090 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080091 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070092 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070093 private final MockCodecContext context = new MockCodecContext();
Ray Milkeyb82c42b2015-06-30 09:42:20 -070094 final CoreService mockCoreService = createMock(CoreService.class);
Ray Milkey1534f8d2015-05-13 15:42:50 -070095
96 @Before
97 public void setUpIntentService() {
98 final IntentService mockIntentService = new IntentServiceAdapter();
99 context.registerService(IntentService.class, mockIntentService);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700100 context.registerService(CoreService.class, mockCoreService);
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700101 expect(mockCoreService.getAppId(appId.name()))
102 .andReturn(appId);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700103 replay(mockCoreService);
Ray Milkey1534f8d2015-05-13 15:42:50 -0700104 }
Ray Milkeydb358082015-01-13 16:34:38 -0800105
106 /**
107 * Tests the encoding of a host to host intent.
108 */
109 @Test
110 public void hostToHostIntent() {
111 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700112 HostToHostIntent.builder()
113 .appId(appId)
114 .one(id1)
115 .two(id2)
116 .build();
Ray Milkeydb358082015-01-13 16:34:38 -0800117
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 Milkey3e3ec5f2015-03-17 17:00:38 -0700135 PointToPointIntent.builder()
136 .appId(appId)
137 .selector(emptySelector)
138 .treatment(emptyTreatment)
139 .ingressPoint(ingress)
140 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800141
Ray Milkeydb358082015-01-13 16:34:38 -0800142 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 Milkeyb9a8a182015-01-20 14:15:35 -0800158 DeviceId did1 = did("device1");
159 DeviceId did2 = did("device2");
160 DeviceId did3 = did("device3");
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700161 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
Ray Milkeydb358082015-01-13 16:34:38 -0800162 final TrafficSelector selector = DefaultTrafficSelector.builder()
163 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100164 .matchMplsLabel(MplsLabel.mplsLabel(4))
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700165 .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700166 .add(Criteria.matchLambda(ochSignal))
Ray Milkeydb358082015-01-13 16:34:38 -0800167 .matchEthDst(MacAddress.BROADCAST)
168 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
169 .build();
170 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100171 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800172 .setOutput(PortNumber.CONTROLLER)
173 .setEthDst(MacAddress.BROADCAST)
174 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800175
176 final List<Constraint> constraints =
177 ImmutableList.of(
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800178 new BandwidthConstraint(Bandwidth.bps(1.0)),
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700179 new AnnotationConstraint("key", 33.0),
180 new AsymmetricPathConstraint(),
181 new LatencyConstraint(Duration.ofSeconds(2)),
182 new ObstacleConstraint(did1, did2),
183 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800184
185 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700186 PointToPointIntent.builder()
187 .appId(appId)
188 .selector(selector)
189 .treatment(treatment)
190 .ingressPoint(ingress)
191 .egressPoint(egress)
192 .constraints(constraints)
193 .build();
194
Ray Milkeydb358082015-01-13 16:34:38 -0800195
Ray Milkeydb358082015-01-13 16:34:38 -0800196 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 Milkeyb82c42b2015-06-30 09:42:20 -0700204
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 Manda2be2f882015-11-11 15:23:40 +0530213 InputStream jsonStream = IntentCodecTest.class
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700214 .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 Milkeydb358082015-01-13 16:34:38 -0800281}