blob: 2c2e488a7707aa78aee15ba03601c0a8490d9e35 [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 Milkeyb9a8a182015-01-20 14:15:35 -080018import java.time.Duration;
Ray Milkeydb358082015-01-13 16:34:38 -080019import java.util.List;
20
Ray Milkey1534f8d2015-05-13 15:42:50 -070021import org.junit.Before;
Ray Milkeydb358082015-01-13 16:34:38 -080022import org.junit.Test;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010025import org.onlab.packet.MplsLabel;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070026import org.onlab.util.Bandwidth;
Ray Milkeydb358082015-01-13 16:34:38 -080027import org.onosproject.codec.JsonCodec;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.net.ConnectPoint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080031import org.onosproject.net.DeviceId;
Ray Milkeydb358082015-01-13 16:34:38 -080032import org.onosproject.net.HostId;
33import org.onosproject.net.NetTestTools;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070034import org.onosproject.net.OchSignalType;
Ray Milkeydb358082015-01-13 16:34:38 -080035import org.onosproject.net.PortNumber;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU6c70f642015-05-29 17:27:22 -070040import org.onosproject.net.flow.criteria.Criteria;
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070041import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkeydb358082015-01-13 16:34:38 -080042import org.onosproject.net.intent.Constraint;
43import org.onosproject.net.intent.HostToHostIntent;
Ray Milkey1534f8d2015-05-13 15:42:50 -070044import org.onosproject.net.intent.IntentService;
45import org.onosproject.net.intent.IntentServiceAdapter;
Ray Milkeydb358082015-01-13 16:34:38 -080046import org.onosproject.net.intent.PointToPointIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080047import org.onosproject.net.intent.constraint.AnnotationConstraint;
48import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
Ray Milkeydb358082015-01-13 16:34:38 -080049import org.onosproject.net.intent.constraint.BandwidthConstraint;
50import org.onosproject.net.intent.constraint.LambdaConstraint;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080051import org.onosproject.net.intent.constraint.LatencyConstraint;
52import org.onosproject.net.intent.constraint.ObstacleConstraint;
53import org.onosproject.net.intent.constraint.WaypointConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070054import org.onosproject.net.resource.link.BandwidthResource;
55import org.onosproject.net.resource.link.LambdaResource;
Ray Milkeydb358082015-01-13 16:34:38 -080056
57import com.fasterxml.jackson.databind.node.ObjectNode;
58import com.google.common.collect.ImmutableList;
59
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070060import static org.hamcrest.MatcherAssert.assertThat;
61import static org.hamcrest.Matchers.notNullValue;
Ray Milkeydb358082015-01-13 16:34:38 -080062import static org.onosproject.codec.impl.IntentJsonMatcher.matchesIntent;
Ray Milkeyb9a8a182015-01-20 14:15:35 -080063import static org.onosproject.net.NetTestTools.did;
Ray Milkeydb358082015-01-13 16:34:38 -080064import static org.onosproject.net.NetTestTools.hid;
Ray Milkeydb358082015-01-13 16:34:38 -080065
66/**
67 * Unit tests for the host to host intent class codec.
68 */
69public class IntentCodecTest extends AbstractIntentTest {
70
71 private final HostId id1 = hid("12:34:56:78:91:ab/1");
72 private final HostId id2 = hid("12:34:56:78:92:ab/1");
Thomas Vachuska02aeb032015-01-06 22:36:30 -080073 private final ApplicationId appId = new DefaultApplicationId(3, "test");
Ray Milkeydb358082015-01-13 16:34:38 -080074 final TrafficSelector emptySelector =
Brian O'Connor6b528132015-03-10 16:39:52 -070075 DefaultTrafficSelector.emptySelector();
Ray Milkeydb358082015-01-13 16:34:38 -080076 final TrafficTreatment emptyTreatment =
Brian O'Connor6b528132015-03-10 16:39:52 -070077 DefaultTrafficTreatment.emptyTreatment();
Ray Milkey1534f8d2015-05-13 15:42:50 -070078 private final MockCodecContext context = new MockCodecContext();
79
80 @Before
81 public void setUpIntentService() {
82 final IntentService mockIntentService = new IntentServiceAdapter();
83 context.registerService(IntentService.class, mockIntentService);
84 }
Ray Milkeydb358082015-01-13 16:34:38 -080085
86 /**
87 * Tests the encoding of a host to host intent.
88 */
89 @Test
90 public void hostToHostIntent() {
91 final HostToHostIntent intent =
Ray Milkeyebc5d222015-03-18 15:45:36 -070092 HostToHostIntent.builder()
93 .appId(appId)
94 .one(id1)
95 .two(id2)
96 .build();
Ray Milkeydb358082015-01-13 16:34:38 -080097
98 final JsonCodec<HostToHostIntent> intentCodec =
99 context.codec(HostToHostIntent.class);
100 assertThat(intentCodec, notNullValue());
101
102 final ObjectNode intentJson = intentCodec.encode(intent, context);
103 assertThat(intentJson, matchesIntent(intent));
104 }
105
106 /**
107 * Tests the encoding of a point to point intent.
108 */
109 @Test
110 public void pointToPointIntent() {
111 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
112 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
113
114 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700115 PointToPointIntent.builder()
116 .appId(appId)
117 .selector(emptySelector)
118 .treatment(emptyTreatment)
119 .ingressPoint(ingress)
120 .egressPoint(egress).build();
Ray Milkeydb358082015-01-13 16:34:38 -0800121
Ray Milkeydb358082015-01-13 16:34:38 -0800122 final JsonCodec<PointToPointIntent> intentCodec =
123 context.codec(PointToPointIntent.class);
124 assertThat(intentCodec, notNullValue());
125
126 final ObjectNode intentJson = intentCodec.encode(intent, context);
127 assertThat(intentJson, matchesIntent(intent));
128 }
129
130 /**
131 * Tests the encoding of an intent with treatment, selector and constraints
132 * specified.
133 */
134 @Test
135 public void intentWithTreatmentSelectorAndConstraints() {
136 ConnectPoint ingress = NetTestTools.connectPoint("ingress", 1);
137 ConnectPoint egress = NetTestTools.connectPoint("egress", 2);
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800138 DeviceId did1 = did("device1");
139 DeviceId did2 = did("device2");
140 DeviceId did3 = did("device3");
Ray Milkeydb358082015-01-13 16:34:38 -0800141 final TrafficSelector selector = DefaultTrafficSelector.builder()
142 .matchIPProtocol((byte) 3)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100143 .matchMplsLabel(MplsLabel.mplsLabel(4))
Sho SHIMIZU6c70f642015-05-29 17:27:22 -0700144 .add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID))
Ray Milkeydb358082015-01-13 16:34:38 -0800145 .matchLambda((short) 6)
146 .matchEthDst(MacAddress.BROADCAST)
147 .matchIPDst(IpPrefix.valueOf("1.2.3.4/24"))
148 .build();
149 final TrafficTreatment treatment = DefaultTrafficTreatment.builder()
150 .setLambda((short) 33)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100151 .setMpls(MplsLabel.mplsLabel(44))
Ray Milkeydb358082015-01-13 16:34:38 -0800152 .setOutput(PortNumber.CONTROLLER)
153 .setEthDst(MacAddress.BROADCAST)
154 .build();
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800155
156 final List<Constraint> constraints =
157 ImmutableList.of(
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700158 new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(1.0))),
Sho SHIMIZU94b7ff42015-05-06 17:51:49 -0700159 new LambdaConstraint(LambdaResource.valueOf(3)),
Ray Milkeyb9a8a182015-01-20 14:15:35 -0800160 new AnnotationConstraint("key", 33.0),
161 new AsymmetricPathConstraint(),
162 new LatencyConstraint(Duration.ofSeconds(2)),
163 new ObstacleConstraint(did1, did2),
164 new WaypointConstraint(did3));
Ray Milkeydb358082015-01-13 16:34:38 -0800165
166 final PointToPointIntent intent =
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700167 PointToPointIntent.builder()
168 .appId(appId)
169 .selector(selector)
170 .treatment(treatment)
171 .ingressPoint(ingress)
172 .egressPoint(egress)
173 .constraints(constraints)
174 .build();
175
Ray Milkeydb358082015-01-13 16:34:38 -0800176
Ray Milkeydb358082015-01-13 16:34:38 -0800177 final JsonCodec<PointToPointIntent> intentCodec =
178 context.codec(PointToPointIntent.class);
179 assertThat(intentCodec, notNullValue());
180
181 final ObjectNode intentJson = intentCodec.encode(intent, context);
182 assertThat(intentJson, matchesIntent(intent));
183
184 }
185}