blob: 7cbce4d1e4dc3abe217bd87ebb1cb13977ca6589 [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;
62import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080063import org.onosproject.net.intent.constraint.LatencyConstraint;
64import org.onosproject.net.intent.constraint.ObstacleConstraint;
65import org.onosproject.net.intent.constraint.WaypointConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070066import org.onosproject.net.resource.link.BandwidthResource;
67import org.onosproject.net.resource.link.LambdaResource;
Ray Milkeydb358082015-01-13 16:34:38 -080068
Ray Milkeyb82c42b2015-06-30 09:42:20 -070069import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeydb358082015-01-13 16:34:38 -080070import com.fasterxml.jackson.databind.node.ObjectNode;
71import com.google.common.collect.ImmutableList;
72
Ray Milkeyb82c42b2015-06-30 09:42:20 -070073import static org.easymock.EasyMock.createMock;
74import static org.easymock.EasyMock.expect;
75import static org.easymock.EasyMock.replay;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070076import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070077import static org.hamcrest.Matchers.hasSize;
78import static org.hamcrest.Matchers.instanceOf;
79import static org.hamcrest.Matchers.is;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070080import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080081import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080082import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080083import static org.onosproject.net.NetTestTools.hid;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070084import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Ray Milkeydb358082015-01-13 16:34:38 -080085
86/**
87 * Unit tests for the host to host intent class codec.
88 */
89public class IntentCodecTest extends AbstractIntentTest {
90
91 private final HostId id1 = hid("12:34:56:78:91:ab/1");
92 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080093 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080094 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070095 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080096 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070097 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070098 private final MockCodecContext context = new MockCodecContext();
Ray Milkeyb82c42b2015-06-30 09:42:20 -070099 final CoreService mockCoreService = createMock(CoreService.class);
Ray Milkey1534f8d2015-05-13 15:42:50 -0700100
101 @Before
102 public void setUpIntentService() {
103 final IntentService mockIntentService = new IntentServiceAdapter();
104 context.registerService(IntentService.class, mockIntentService);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700105 context.registerService(CoreService.class, mockCoreService);
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700106 expect(mockCoreService.getAppId(appId.name()))
107 .andReturn(appId);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700108 replay(mockCoreService);
Ray Milkey1534f8d2015-05-13 15:42:50 -0700109 }
Ray Milkeydb358082015-01-13 16:34:38 -0800110
111 /**
112 * Tests the encoding of a host to host intent.
113 */
114 @Test
115 public void hostToHostIntent() {
116 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700117 HostToHostIntent.builder()
118 .appId(appId)
119 .one(id1)
120 .two(id2)
121 .build();
Ray Milkeydb358082015-01-13 16:34:38 -0800122
123 final JsonCodec<HostToHostIntent> intentCodec =
124 context.codec(HostToHostIntent.class);
125 assertThat(intentCodec, notNullValue());
126
127 final ObjectNode intentJson = intentCodec.encode(intent, context);
128 assertThat(intentJson, matchesIntent(intent));
129 }
130
131 /**
132 * Tests the encoding of a point to point intent.
133 */
134 @Test
135 public void pointToPointIntent() {
136 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
137 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
138
139 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700140 PointToPointIntent.builder()
141 .appId(appId)
142 .selector(emptySelector)
143 .treatment(emptyTreatment)
144 .ingressPoint(ingress)
145 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800146
Ray Milkeydb358082015-01-13 16:34:38 -0800147 final JsonCodec<PointToPointIntent> intentCodec =
148 context.codec(PointToPointIntent.class);
149 assertThat(intentCodec, notNullValue());
150
151 final ObjectNode intentJson = intentCodec.encode(intent, context);
152 assertThat(intentJson, matchesIntent(intent));
153 }
154
155 /**
156 * Tests the encoding of an intent with treatment, selector and constraints
157 * specified.
158 */
159 @Test
160 public void intentWithTreatmentSelectorAndConstraints() {
161 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
162 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800163 DeviceId did1 = did("device1");
164 DeviceId did2 = did("device2");
165 DeviceId did3 = did("device3");
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700166 Lambda ochSignal = Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8);
Ray Milkeydb358082015-01-13 16:34:38 -0800167 final TrafficSelector selector = DefaultTrafficSelector.builder()
168 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100169 .matchMplsLabel(MplsLabel.mplsLabel(4))
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700170 .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -0700171 .add(Criteria.matchLambda(ochSignal))
Ray Milkeydb358082015-01-13 16:34:38 -0800172 .matchEthDst(MacAddress.BROADCAST)
173 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
174 .build();
175 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700176 .add(Instructions.modL0Lambda(new IndexedLambda(33)))
Michele Santuari4b6019e2014-12-19 11:31:45 +0100177 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800178 .setOutput(PortNumber.CONTROLLER)
179 .setEthDst(MacAddress.BROADCAST)
180 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800181
182 final List<Constraint> constraints =
183 ImmutableList.of(
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700184 new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
185 new LambdaConstraint(LambdaResource.valueOf(3)),
186 new AnnotationConstraint("key", 33.0),
187 new AsymmetricPathConstraint(),
188 new LatencyConstraint(Duration.ofSeconds(2)),
189 new ObstacleConstraint(did1, did2),
190 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800191
192 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700193 PointToPointIntent.builder()
194 .appId(appId)
195 .selector(selector)
196 .treatment(treatment)
197 .ingressPoint(ingress)
198 .egressPoint(egress)
199 .constraints(constraints)
200 .build();
201
Ray Milkeydb358082015-01-13 16:34:38 -0800202
Ray Milkeydb358082015-01-13 16:34:38 -0800203 final JsonCodec<PointToPointIntent> intentCodec =
204 context.codec(PointToPointIntent.class);
205 assertThat(intentCodec, notNullValue());
206
207 final ObjectNode intentJson = intentCodec.encode(intent, context);
208 assertThat(intentJson, matchesIntent(intent));
209
210 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700211
212 /**
213 * Reads in a rule from the given resource and decodes it.
214 *
215 * @param resourceName resource to use to read the JSON for the rule
216 * @return decoded flow rule
217 * @throws IOException if processing the resource fails
218 */
219 private Intent getIntent(String resourceName, JsonCodec intentCodec) throws IOException {
220 InputStream jsonStream = FlowRuleCodecTest.class
221 .getResourceAsStream(resourceName);
222 JsonNode json = context.mapper().readTree(jsonStream);
223 assertThat(json, notNullValue());
224 Intent intent = (Intent) intentCodec.decode((ObjectNode) json, context);
225 assertThat(intent, notNullValue());
226 return intent;
227 }
228
229 /**
230 * Tests the point to point intent JSON codec.
231 *
232 * @throws IOException if JSON processing fails
233 */
234 @Test
235 public void decodePointToPointIntent() throws IOException {
236 JsonCodec<Intent> intentCodec = context.codec(Intent.class);
237 assertThat(intentCodec, notNullValue());
238
239 Intent intent = getIntent("PointToPointIntent.json", intentCodec);
240 assertThat(intent, notNullValue());
241 assertThat(intent, instanceOf(PointToPointIntent.class));
242
243 PointToPointIntent pointIntent = (PointToPointIntent) intent;
244 assertThat(pointIntent.priority(), is(55));
245 assertThat(pointIntent.ingressPoint().deviceId(), is(did("0000000000000001")));
246 assertThat(pointIntent.ingressPoint().port(), is(PortNumber.portNumber(1)));
247 assertThat(pointIntent.egressPoint().deviceId(), is(did("0000000000000007")));
248 assertThat(pointIntent.egressPoint().port(), is(PortNumber.portNumber(2)));
249
250 assertThat(pointIntent.constraints(), hasSize(1));
251
252 assertThat(pointIntent.selector(), notNullValue());
253 assertThat(pointIntent.selector().criteria(), hasSize(1));
254 Criterion criterion1 = pointIntent.selector().criteria().iterator().next();
255 assertThat(criterion1, instanceOf(EthCriterion.class));
256 EthCriterion ethCriterion = (EthCriterion) criterion1;
257 assertThat(ethCriterion.mac().toString(), is("11:22:33:44:55:66"));
258 assertThat(ethCriterion.type().name(), is("ETH_DST"));
259
260 assertThat(pointIntent.treatment(), notNullValue());
261 assertThat(pointIntent.treatment().allInstructions(), hasSize(1));
262 Instruction instruction1 = pointIntent.treatment().allInstructions().iterator().next();
263 assertThat(instruction1, instanceOf(ModEtherInstruction.class));
264 ModEtherInstruction ethInstruction = (ModEtherInstruction) instruction1;
265 assertThat(ethInstruction.mac().toString(), is("22:33:44:55:66:77"));
266 assertThat(ethInstruction.type().toString(), is("L2MODIFICATION"));
267 assertThat(ethInstruction.subtype().toString(), is("ETH_SRC"));
268 }
269
270 /**
271 * Tests the host to host intent JSON codec.
272 *
273 * @throws IOException
274 */
275 @Test
276 public void decodeHostToHostIntent() throws IOException {
277 JsonCodec<Intent> intentCodec = context.codec(Intent.class);
278 assertThat(intentCodec, notNullValue());
279
280 Intent intent = getIntent("HostToHostIntent.json", intentCodec);
281 assertThat(intent, notNullValue());
282 assertThat(intent, instanceOf(HostToHostIntent.class));
283
284 HostToHostIntent hostIntent = (HostToHostIntent) intent;
285 assertThat(hostIntent.priority(), is(7));
286 assertThat(hostIntent.constraints(), hasSize(1));
287 }
Ray Milkeydb358082015-01-13 16:34:38 -0800288}