blob: f16f272d63e55017849f3d2a8721fbd32b282802 [file] [log] [blame]
Ray Milkeydb358082015-01-13 16:34:38 -08001/*
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 */
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 SHIMIZU9553bb82015-06-30 16:02:45 -070038import org.onosproject.net.IndexedLambda;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070039import org.onosproject.net.Lambda;
Ray Milkeydb358082015-01-13 16:34:38 -080040import org.onosproject.net.NetTestTools;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070041import org.onosproject.net.OchSignalType;
Ray Milkeydb358082015-01-13 16:34:38 -080042import org.onosproject.net.PortNumber;
43import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070047import org.onosproject.net.flow.criteria.Criteria;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070048import org.onosproject.net.flow.instructions.Instructions;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070049import org.onosproject.net.flow.criteria.Criterion;
50import org.onosproject.net.flow.criteria.EthCriterion;
51import org.onosproject.net.flow.instructions.Instruction;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070052import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080053import org.onosproject.net.intent.Constraint;
54import org.onosproject.net.intent.HostToHostIntent;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070055import org.onosproject.net.intent.Intent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070056import org.onosproject.net.intent.IntentService;
57import org.onosproject.net.intent.IntentServiceAdapter;
Ray Milkeydb358082015-01-13 16:34:38 -080058import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080059import org.onosproject.net.intent.constraint.AnnotationConstraint;
60import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080061import org.onosproject.net.intent.constraint.BandwidthConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080062import org.onosproject.net.intent.constraint.LatencyConstraint;
63import org.onosproject.net.intent.constraint.ObstacleConstraint;
64import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080065
Ray Milkeyb82c42b2015-06-30 09:42:20 -070066import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeydb358082015-01-13 16:34:38 -080067import com.fasterxml.jackson.databind.node.ObjectNode;
68import com.google.common.collect.ImmutableList;
69
Ray Milkeyb82c42b2015-06-30 09:42:20 -070070import static org.easymock.EasyMock.createMock;
71import static org.easymock.EasyMock.expect;
72import static org.easymock.EasyMock.replay;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070073import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070074import static org.hamcrest.Matchers.hasSize;
75import static org.hamcrest.Matchers.instanceOf;
76import static org.hamcrest.Matchers.is;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070077import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080078import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080079import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080080import static org.onosproject.net.NetTestTools.hid;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070081import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Ray Milkeydb358082015-01-13 16:34:38 -080082
83/**
84 * Unit tests for the host to host intent class codec.
85 */
86public class IntentCodecTest extends AbstractIntentTest {
87
88 private final HostId id1 = hid("12:34:56:78:91:ab/1");
89 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080090 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080091 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070092 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080093 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070094 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070095 private final MockCodecContext context = new MockCodecContext();
Ray Milkeyb82c42b2015-06-30 09:42:20 -070096 final CoreService mockCoreService = createMock(CoreService.class);
Ray Milkey1534f8d2015-05-13 15:42:50 -070097
98 @Before
99 public void setUpIntentService() {
100 final IntentService mockIntentService = new IntentServiceAdapter();
101 context.registerService(IntentService.class, mockIntentService);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700102 context.registerService(CoreService.class, mockCoreService);
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700103 expect(mockCoreService.getAppId(appId.name()))
104 .andReturn(appId);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700105 replay(mockCoreService);
Ray Milkey1534f8d2015-05-13 15:42:50 -0700106 }
Ray Milkeydb358082015-01-13 16:34:38 -0800107
108 /**
109 * Tests the encoding of a host to host intent.
110 */
111 @Test
112 public void hostToHostIntent() {
113 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700114 HostToHostIntent.builder()
115 .appId(appId)
116 .one(id1)
117 .two(id2)
118 .build();
Ray Milkeydb358082015-01-13 16:34:38 -0800119
120 final JsonCodec<HostToHostIntent> intentCodec =
121 context.codec(HostToHostIntent.class);
122 assertThat(intentCodec, notNullValue());
123
124 final ObjectNode intentJson = intentCodec.encode(intent, context);
125 assertThat(intentJson, matchesIntent(intent));
126 }
127
128 /**
129 * Tests the encoding of a point to point intent.
130 */
131 @Test
132 public void pointToPointIntent() {
133 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
134 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
135
136 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700137 PointToPointIntent.builder()
138 .appId(appId)
139 .selector(emptySelector)
140 .treatment(emptyTreatment)
141 .ingressPoint(ingress)
142 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800143
Ray Milkeydb358082015-01-13 16:34:38 -0800144 final JsonCodec<PointToPointIntent> intentCodec =
145 context.codec(PointToPointIntent.class);
146 assertThat(intentCodec, notNullValue());
147
148 final ObjectNode intentJson = intentCodec.encode(intent, context);
149 assertThat(intentJson, matchesIntent(intent));
150 }
151
152 /**
153 * Tests the encoding of an intent with treatment, selector and constraints
154 * specified.
155 */
156 @Test
157 public void intentWithTreatmentSelectorAndConstraints() {
158 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
159 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800160 DeviceId did1 = did("device1");
161 DeviceId did2 = did("device2");
162 DeviceId did3 = did("device3");
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700163 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
Ray Milkeydb358082015-01-13 16:34:38 -0800164 final TrafficSelector selector = DefaultTrafficSelector.builder()
165 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100166 .matchMplsLabel(MplsLabel.mplsLabel(4))
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700167 .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700168 .add(Criteria.matchLambda(ochSignal))
Ray Milkeydb358082015-01-13 16:34:38 -0800169 .matchEthDst(MacAddress.BROADCAST)
170 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
171 .build();
172 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700173 .add(Instructions.modL0Lambda(new IndexedLambda(33)))
Michele Santuari4b6019e2014-12-19 11:31:45 +0100174 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800175 .setOutput(PortNumber.CONTROLLER)
176 .setEthDst(MacAddress.BROADCAST)
177 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800178
179 final List<Constraint> constraints =
180 ImmutableList.of(
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800181 new BandwidthConstraint(Bandwidth.bps(1.0)),
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700182 new AnnotationConstraint("key", 33.0),
183 new AsymmetricPathConstraint(),
184 new LatencyConstraint(Duration.ofSeconds(2)),
185 new ObstacleConstraint(did1, did2),
186 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800187
188 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700189 PointToPointIntent.builder()
190 .appId(appId)
191 .selector(selector)
192 .treatment(treatment)
193 .ingressPoint(ingress)
194 .egressPoint(egress)
195 .constraints(constraints)
196 .build();
197
Ray Milkeydb358082015-01-13 16:34:38 -0800198
Ray Milkeydb358082015-01-13 16:34:38 -0800199 final JsonCodec<PointToPointIntent> intentCodec =
200 context.codec(PointToPointIntent.class);
201 assertThat(intentCodec, notNullValue());
202
203 final ObjectNode intentJson = intentCodec.encode(intent, context);
204 assertThat(intentJson, matchesIntent(intent));
205
206 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700207
208 /**
209 * Reads in a rule from the given resource and decodes it.
210 *
211 * @param resourceName resource to use to read the JSON for the rule
212 * @return decoded flow rule
213 * @throws IOException if processing the resource fails
214 */
215 private Intent getIntent(String resourceName, JsonCodec intentCodec) throws IOException {
Phaneendra Manda2be2f882015-11-11 15:23:40 +0530216 InputStream jsonStream = IntentCodecTest.class
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700217 .getResourceAsStream(resourceName);
218 JsonNode json = context.mapper().readTree(jsonStream);
219 assertThat(json, notNullValue());
220 Intent intent = (Intent) intentCodec.decode((ObjectNode) json, context);
221 assertThat(intent, notNullValue());
222 return intent;
223 }
224
225 /**
226 * Tests the point to point intent JSON codec.
227 *
228 * @throws IOException if JSON processing fails
229 */
230 @Test
231 public void decodePointToPointIntent() throws IOException {
232 JsonCodec<Intent> intentCodec = context.codec(Intent.class);
233 assertThat(intentCodec, notNullValue());
234
235 Intent intent = getIntent("PointToPointIntent.json", intentCodec);
236 assertThat(intent, notNullValue());
237 assertThat(intent, instanceOf(PointToPointIntent.class));
238
239 PointToPointIntent pointIntent = (PointToPointIntent) intent;
240 assertThat(pointIntent.priority(), is(55));
241 assertThat(pointIntent.ingressPoint().deviceId(), is(did("0000000000000001")));
242 assertThat(pointIntent.ingressPoint().port(), is(PortNumber.portNumber(1)));
243 assertThat(pointIntent.egressPoint().deviceId(), is(did("0000000000000007")));
244 assertThat(pointIntent.egressPoint().port(), is(PortNumber.portNumber(2)));
245
246 assertThat(pointIntent.constraints(), hasSize(1));
247
248 assertThat(pointIntent.selector(), notNullValue());
249 assertThat(pointIntent.selector().criteria(), hasSize(1));
250 Criterion criterion1 = pointIntent.selector().criteria().iterator().next();
251 assertThat(criterion1, instanceOf(EthCriterion.class));
252 EthCriterion ethCriterion = (EthCriterion) criterion1;
253 assertThat(ethCriterion.mac().toString(), is("11:22:33:44:55:66"));
254 assertThat(ethCriterion.type().name(), is("ETH_DST"));
255
256 assertThat(pointIntent.treatment(), notNullValue());
257 assertThat(pointIntent.treatment().allInstructions(), hasSize(1));
258 Instruction instruction1 = pointIntent.treatment().allInstructions().iterator().next();
259 assertThat(instruction1, instanceOf(ModEtherInstruction.class));
260 ModEtherInstruction ethInstruction = (ModEtherInstruction) instruction1;
261 assertThat(ethInstruction.mac().toString(), is("22:33:44:55:66:77"));
262 assertThat(ethInstruction.type().toString(), is("L2MODIFICATION"));
263 assertThat(ethInstruction.subtype().toString(), is("ETH_SRC"));
264 }
265
266 /**
267 * Tests the host to host intent JSON codec.
268 *
269 * @throws IOException
270 */
271 @Test
272 public void decodeHostToHostIntent() throws IOException {
273 JsonCodec<Intent> intentCodec = context.codec(Intent.class);
274 assertThat(intentCodec, notNullValue());
275
276 Intent intent = getIntent("HostToHostIntent.json", intentCodec);
277 assertThat(intent, notNullValue());
278 assertThat(intent, instanceOf(HostToHostIntent.class));
279
280 HostToHostIntent hostIntent = (HostToHostIntent) intent;
281 assertThat(hostIntent.priority(), is(7));
282 assertThat(hostIntent.constraints(), hasSize(1));
283 }
Ray Milkeydb358082015-01-13 16:34:38 -0800284}