blob: b257c5aa39315cba1b04d3b9ada62351cc7d9890 [file] [log] [blame]
Ray Milkeydb358082015-01-13 16:34:38 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Ray Milkeya2cf3a12018-02-15 16:13:56 -080036import org.onosproject.net.FilteredConnectPoint;
Sho SHIMIZUc44c0c32015-06-01 11:40:48 -070037import org.onosproject.net.GridType;
Ray Milkeydb358082015-01-13 16:34:38 -080038import org.onosproject.net.HostId;
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;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070048import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.instructions.Instruction;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070051import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080052import org.onosproject.net.intent.Constraint;
53import org.onosproject.net.intent.HostToHostIntent;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070054import org.onosproject.net.intent.Intent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070055import org.onosproject.net.intent.IntentService;
56import org.onosproject.net.intent.IntentServiceAdapter;
Ray Milkeydb358082015-01-13 16:34:38 -080057import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080058import org.onosproject.net.intent.constraint.AnnotationConstraint;
59import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080060import org.onosproject.net.intent.constraint.BandwidthConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080061import org.onosproject.net.intent.constraint.LatencyConstraint;
62import org.onosproject.net.intent.constraint.ObstacleConstraint;
63import org.onosproject.net.intent.constraint.WaypointConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080064
Ray Milkeyb82c42b2015-06-30 09:42:20 -070065import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeydb358082015-01-13 16:34:38 -080066import com.fasterxml.jackson.databind.node.ObjectNode;
67import com.google.common.collect.ImmutableList;
68
Ray Milkeyb82c42b2015-06-30 09:42:20 -070069import static org.easymock.EasyMock.createMock;
70import static org.easymock.EasyMock.expect;
71import static org.easymock.EasyMock.replay;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070072import static org.hamcrest.MatcherAssert.assertThat;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070073import static org.hamcrest.Matchers.hasSize;
74import static org.hamcrest.Matchers.instanceOf;
75import static org.hamcrest.Matchers.is;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070076import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080077import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080078import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080079import static org.onosproject.net.NetTestTools.hid;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070080import static org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Ray Milkeydb358082015-01-13 16:34:38 -080081
82/**
83 * Unit tests for the host to host intent class codec.
84 */
85public class IntentCodecTest extends AbstractIntentTest {
86
87 private final HostId id1 = hid("12:34:56:78:91:ab/1");
88 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080089 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080090 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070091 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080092 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070093 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070094 private final MockCodecContext context = new MockCodecContext();
Ray Milkeyb82c42b2015-06-30 09:42:20 -070095 final CoreService mockCoreService = createMock(CoreService.class);
Ray Milkey1534f8d2015-05-13 15:42:50 -070096
97 @Before
98 public void setUpIntentService() {
99 final IntentService mockIntentService = new IntentServiceAdapter();
100 context.registerService(IntentService.class, mockIntentService);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700101 context.registerService(CoreService.class, mockCoreService);
Ray Milkeyf7cb4012015-07-20 13:01:07 -0700102 expect(mockCoreService.getAppId(appId.name()))
103 .andReturn(appId);
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700104 replay(mockCoreService);
Ray Milkey1534f8d2015-05-13 15:42:50 -0700105 }
Ray Milkeydb358082015-01-13 16:34:38 -0800106
107 /**
108 * Tests the encoding of a host to host intent.
109 */
110 @Test
111 public void hostToHostIntent() {
112 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700113 HostToHostIntent.builder()
114 .appId(appId)
115 .one(id1)
116 .two(id2)
117 .build();
Ray Milkeydb358082015-01-13 16:34:38 -0800118
119 final JsonCodec<HostToHostIntent> intentCodec =
120 context.codec(HostToHostIntent.class);
121 assertThat(intentCodec, notNullValue());
122
123 final ObjectNode intentJson = intentCodec.encode(intent, context);
124 assertThat(intentJson, matchesIntent(intent));
125 }
126
127 /**
128 * Tests the encoding of a point to point intent.
129 */
130 @Test
131 public void pointToPointIntent() {
132 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
133 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
134
135 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700136 PointToPointIntent.builder()
137 .appId(appId)
138 .selector(emptySelector)
139 .treatment(emptyTreatment)
Ray Milkeya2cf3a12018-02-15 16:13:56 -0800140 .filteredIngressPoint(new FilteredConnectPoint(ingress))
141 .filteredEgressPoint(new FilteredConnectPoint(egress))
142 .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()
Michele Santuari4b6019e2014-12-19 11:31:45 +0100173 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800174 .setOutput(PortNumber.CONTROLLER)
175 .setEthDst(MacAddress.BROADCAST)
176 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800177
178 final List<Constraint> constraints =
179 ImmutableList.of(
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800180 new BandwidthConstraint(Bandwidth.bps(1.0)),
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700181 new AnnotationConstraint("key", 33.0),
182 new AsymmetricPathConstraint(),
183 new LatencyConstraint(Duration.ofSeconds(2)),
184 new ObstacleConstraint(did1, did2),
185 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800186
187 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700188 PointToPointIntent.builder()
189 .appId(appId)
190 .selector(selector)
191 .treatment(treatment)
Ray Milkeya2cf3a12018-02-15 16:13:56 -0800192 .filteredIngressPoint(new FilteredConnectPoint(ingress))
193 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700194 .constraints(constraints)
195 .build();
196
Ray Milkeydb358082015-01-13 16:34:38 -0800197
Ray Milkeydb358082015-01-13 16:34:38 -0800198 final JsonCodec<PointToPointIntent> intentCodec =
199 context.codec(PointToPointIntent.class);
200 assertThat(intentCodec, notNullValue());
201
202 final ObjectNode intentJson = intentCodec.encode(intent, context);
203 assertThat(intentJson, matchesIntent(intent));
204
205 }
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700206
207 /**
208 * Reads in a rule from the given resource and decodes it.
209 *
210 * @param resourceName resource to use to read the JSON for the rule
211 * @return decoded flow rule
212 * @throws IOException if processing the resource fails
213 */
214 private Intent getIntent(String resourceName, JsonCodec intentCodec) throws IOException {
Phaneendra Manda2be2f882015-11-11 15:23:40 +0530215 InputStream jsonStream = IntentCodecTest.class
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700216 .getResourceAsStream(resourceName);
217 JsonNode json = context.mapper().readTree(jsonStream);
218 assertThat(json, notNullValue());
219 Intent intent = (Intent) intentCodec.decode((ObjectNode) json, context);
220 assertThat(intent, notNullValue());
221 return intent;
222 }
223
224 /**
225 * Tests the point to point intent JSON codec.
226 *
227 * @throws IOException if JSON processing fails
228 */
229 @Test
230 public void decodePointToPointIntent() throws IOException {
231 JsonCodec<Intent> intentCodec = context.codec(Intent.class);
232 assertThat(intentCodec, notNullValue());
233
234 Intent intent = getIntent("PointToPointIntent.json", intentCodec);
235 assertThat(intent, notNullValue());
236 assertThat(intent, instanceOf(PointToPointIntent.class));
237
238 PointToPointIntent pointIntent = (PointToPointIntent) intent;
239 assertThat(pointIntent.priority(), is(55));
Ray Milkeya2cf3a12018-02-15 16:13:56 -0800240 assertThat(pointIntent.filteredIngressPoint().connectPoint().deviceId(), is(did("0000000000000001")));
241 assertThat(pointIntent.filteredIngressPoint().connectPoint().port(), is(PortNumber.portNumber(1)));
242 assertThat(pointIntent.filteredEgressPoint().connectPoint().deviceId(), is(did("0000000000000007")));
243 assertThat(pointIntent.filteredEgressPoint().connectPoint().port(), is(PortNumber.portNumber(2)));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700244
245 assertThat(pointIntent.constraints(), hasSize(1));
246
247 assertThat(pointIntent.selector(), notNullValue());
248 assertThat(pointIntent.selector().criteria(), hasSize(1));
249 Criterion criterion1 = pointIntent.selector().criteria().iterator().next();
250 assertThat(criterion1, instanceOf(EthCriterion.class));
251 EthCriterion ethCriterion = (EthCriterion) criterion1;
252 assertThat(ethCriterion.mac().toString(), is("11:22:33:44:55:66"));
253 assertThat(ethCriterion.type().name(), is("ETH_DST"));
254
255 assertThat(pointIntent.treatment(), notNullValue());
256 assertThat(pointIntent.treatment().allInstructions(), hasSize(1));
257 Instruction instruction1 = pointIntent.treatment().allInstructions().iterator().next();
258 assertThat(instruction1, instanceOf(ModEtherInstruction.class));
259 ModEtherInstruction ethInstruction = (ModEtherInstruction) instruction1;
260 assertThat(ethInstruction.mac().toString(), is("22:33:44:55:66:77"));
261 assertThat(ethInstruction.type().toString(), is("L2MODIFICATION"));
262 assertThat(ethInstruction.subtype().toString(), is("ETH_SRC"));
263 }
264
265 /**
266 * Tests the host to host intent JSON codec.
267 *
268 * @throws IOException
269 */
270 @Test
271 public void decodeHostToHostIntent() throws IOException {
272 JsonCodec<Intent> intentCodec = context.codec(Intent.class);
273 assertThat(intentCodec, notNullValue());
274
275 Intent intent = getIntent("HostToHostIntent.json", intentCodec);
276 assertThat(intent, notNullValue());
277 assertThat(intent, instanceOf(HostToHostIntent.class));
278
279 HostToHostIntent hostIntent = (HostToHostIntent) intent;
280 assertThat(hostIntent.priority(), is(7));
281 assertThat(hostIntent.constraints(), hasSize(1));
282 }
Ray Milkeydb358082015-01-13 16:34:38 -0800283}