blob: 2fdac58b0a722a51112a84d05f9cda1eda953fc9 [file] [log] [blame]
Ray Milkeyb82c42b2015-06-30 09:42:20 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyb82c42b2015-06-30 09:42:20 -07003 *
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
18import java.io.IOException;
19import java.io.InputStream;
20
21import org.junit.Assert;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.Link;
27import org.onosproject.net.intent.Constraint;
28import org.onosproject.net.intent.constraint.AnnotationConstraint;
29import org.onosproject.net.intent.constraint.AsymmetricPathConstraint;
30import org.onosproject.net.intent.constraint.BandwidthConstraint;
Thomas Szyrkowiec7c5f9c92017-07-12 12:42:02 +020031import org.onosproject.net.intent.constraint.DomainConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070032import org.onosproject.net.intent.constraint.LatencyConstraint;
33import org.onosproject.net.intent.constraint.LinkTypeConstraint;
David Glantz05c6f432020-03-19 14:56:12 -050034import org.onosproject.net.intent.constraint.MeteredConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070035import org.onosproject.net.intent.constraint.ObstacleConstraint;
David Glantz05c6f432020-03-19 14:56:12 -050036import org.onosproject.net.intent.constraint.TierConstraint;
Ray Milkeyb82c42b2015-06-30 09:42:20 -070037import org.onosproject.net.intent.constraint.WaypointConstraint;
38
39import com.fasterxml.jackson.databind.JsonNode;
40import com.fasterxml.jackson.databind.node.ObjectNode;
41
42import static com.google.common.base.Preconditions.checkNotNull;
43import static org.easymock.EasyMock.createMock;
44import static org.easymock.EasyMock.expect;
45import static org.easymock.EasyMock.replay;
46import static org.hamcrest.MatcherAssert.assertThat;
47import static org.hamcrest.Matchers.hasItem;
48import static org.hamcrest.Matchers.hasSize;
49import static org.hamcrest.Matchers.instanceOf;
50import static org.hamcrest.Matchers.is;
51import static org.hamcrest.Matchers.notNullValue;
52import static org.onosproject.net.NetTestTools.APP_ID;
53import static org.onosproject.net.NetTestTools.did;
54
55/**
56 * Unit tests for Constraint codec.
57 */
58public class ConstraintCodecTest {
59
60 MockCodecContext context;
61 JsonCodec<Constraint> constraintCodec;
62 final CoreService mockCoreService = createMock(CoreService.class);
63
64 /**
65 * Sets up for each test. Creates a context and fetches the flow rule
66 * codec.
67 */
68 @Before
69 public void setUp() {
70 context = new MockCodecContext();
71 constraintCodec = context.codec(Constraint.class);
72 assertThat(constraintCodec, notNullValue());
73
74 expect(mockCoreService.registerApplication(FlowRuleCodec.REST_APP_ID))
75 .andReturn(APP_ID).anyTimes();
76 replay(mockCoreService);
77 context.registerService(CoreService.class, mockCoreService);
78 }
79
80 /**
81 * Reads in a constraint from the given resource and decodes it.
82 *
83 * @param resourceName resource to use to read the JSON for the constraint
84 * @return decoded constraint
85 */
86 private Constraint getConstraint(String resourceName) {
87 InputStream jsonStream = ConstraintCodecTest.class
88 .getResourceAsStream(resourceName);
89 try {
90 JsonNode json = context.mapper().readTree(jsonStream);
91 assertThat(json, notNullValue());
92 Constraint constraint = constraintCodec.decode((ObjectNode) json, context);
93 assertThat(constraint, notNullValue());
94 return checkNotNull(constraint);
95 } catch (IOException ioe) {
96 Assert.fail(ioe.getMessage());
97 throw new IllegalStateException("cannot happen");
98 }
99 }
100
101
102 /**
103 * Tests link type constraint.
104 */
105 @Test
106 public void linkTypeConstraint() {
107 Constraint constraint = getConstraint("LinkTypeConstraint.json");
108 assertThat(constraint, instanceOf(LinkTypeConstraint.class));
109
110 LinkTypeConstraint linkTypeConstraint = (LinkTypeConstraint) constraint;
111 assertThat(linkTypeConstraint.isInclusive(), is(false));
112 assertThat(linkTypeConstraint.types(), hasSize(2));
113 assertThat(linkTypeConstraint.types(), hasItem(Link.Type.OPTICAL));
114 assertThat(linkTypeConstraint.types(), hasItem(Link.Type.DIRECT));
115 }
116
117 /**
118 * Tests annotation constraint.
119 */
120 @Test
121 public void annotationConstraint() {
122 Constraint constraint = getConstraint("AnnotationConstraint.json");
123 assertThat(constraint, instanceOf(AnnotationConstraint.class));
124
125 AnnotationConstraint annotationConstraint = (AnnotationConstraint) constraint;
126 assertThat(annotationConstraint.key(), is("key"));
127 assertThat(annotationConstraint.threshold(), is(123.0D));
128 }
129
130 /**
131 * Tests bandwidth constraint.
132 */
133 @Test
134 public void bandwidthConstraint() {
135 Constraint constraint = getConstraint("BandwidthConstraint.json");
136 assertThat(constraint, instanceOf(BandwidthConstraint.class));
137
138 BandwidthConstraint bandwidthConstraint = (BandwidthConstraint) constraint;
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800139 assertThat(bandwidthConstraint.bandwidth().bps(), is(345.678D));
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700140 }
141
142 /**
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700143 * Tests latency constraint.
144 */
145 @Test
146 public void latencyConstraint() {
147 Constraint constraint = getConstraint("LatencyConstraint.json");
148 assertThat(constraint, instanceOf(LatencyConstraint.class));
149
150 LatencyConstraint latencyConstraint = (LatencyConstraint) constraint;
151 assertThat(latencyConstraint.latency().toMillis(), is(111L));
152 }
153
154 /**
155 * Tests obstacle constraint.
156 */
157 @Test
158 public void obstacleConstraint() {
159 Constraint constraint = getConstraint("ObstacleConstraint.json");
160 assertThat(constraint, instanceOf(ObstacleConstraint.class));
161
162 ObstacleConstraint obstacleConstraint = (ObstacleConstraint) constraint;
163
164 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev1")));
165 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev2")));
166 assertThat(obstacleConstraint.obstacles(), hasItem(did("dev3")));
167 }
168
169 /**
170 * Tests waypoint constaint.
171 */
172 @Test
173 public void waypointConstraint() {
174 Constraint constraint = getConstraint("WaypointConstraint.json");
175 assertThat(constraint, instanceOf(WaypointConstraint.class));
176
177 WaypointConstraint waypointConstraint = (WaypointConstraint) constraint;
178
179 assertThat(waypointConstraint.waypoints(), hasItem(did("devA")));
180 assertThat(waypointConstraint.waypoints(), hasItem(did("devB")));
181 assertThat(waypointConstraint.waypoints(), hasItem(did("devC")));
182 }
183
184 /**
185 * Tests asymmetric path constraint.
186 */
187 @Test
188 public void asymmetricPathConstraint() {
189 Constraint constraint = getConstraint("AsymmetricPathConstraint.json");
190 assertThat(constraint, instanceOf(AsymmetricPathConstraint.class));
191 }
Thomas Szyrkowiec7c5f9c92017-07-12 12:42:02 +0200192
193 /**
194 * Tests domain constraint.
195 */
196 @Test
197 public void domainConstraint() {
198 Constraint constraint = getConstraint("DomainConstraint.json");
199 assertThat(constraint, instanceOf(DomainConstraint.class));
200 }
David Glantz05c6f432020-03-19 14:56:12 -0500201
202 /**
203 * Tests metered constraint.
204 */
205 @Test
206 public void meteredConstraint() {
207 Constraint constraint = getConstraint("MeteredConstraint.json");
208 assertThat(constraint, instanceOf(MeteredConstraint.class));
209
210 MeteredConstraint meteredConstraint = (MeteredConstraint) constraint;
211
212 assertThat(meteredConstraint.isUseMetered(), is(true));
213 }
214
215 /**
216 * Tests tier constraint.
217 */
218 @Test
219 public void tierConstraint() {
220 Constraint constraint = getConstraint("TierConstraint.json");
221 assertThat(constraint, instanceOf(TierConstraint.class));
222
223 TierConstraint tierConstraint = (TierConstraint) constraint;
224
225 assertThat(tierConstraint.isInclusive(), is(true));
226 assertThat(tierConstraint.costType(), is(TierConstraint.CostType.ORDER));
227
228 assertThat(tierConstraint.tiers().get(0), is(3));
229 assertThat(tierConstraint.tiers().get(1), is(2));
230 assertThat(tierConstraint.tiers().get(2), is(1));
231 }
232
233
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700234}