blob: 5a48d8705d2086586eec238b1f8a153ddf6ae899 [file] [log] [blame]
David Glantz05c6f432020-03-19 14:56:12 -05001/*
2 * Copyright 2014-present Open Networking Foundation
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.net.intent.constraint;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.graph.ScalarWeight;
22import org.onosproject.net.Annotations;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.DefaultLink;
25import org.onosproject.net.DefaultPath;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Path;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.intent.Constraint;
30import org.onosproject.net.intent.ResourceContext;
31import org.onosproject.net.provider.ProviderId;
32
33import java.util.Arrays;
34
35import static org.easymock.EasyMock.createMock;
36import static org.hamcrest.Matchers.is;
37import static org.junit.Assert.assertThat;
38import static org.onosproject.net.AnnotationKeys.TIER;
39import static org.onosproject.net.DefaultLinkTest.cp;
40import static org.onosproject.net.DeviceId.deviceId;
41import static org.onosproject.net.Link.Type.DIRECT;
42
43/**
44 * Test for constraint of intermediate elements.
45 */
46public class TierConstraintTest {
47
48 private static final DeviceId DID1 = deviceId("of:1");
49 private static final DeviceId DID2 = deviceId("of:2");
50 private static final DeviceId DID3 = deviceId("of:3");
51 private static final DeviceId DID4 = deviceId("of:4");
52 private static final PortNumber PN1 = PortNumber.portNumber(1);
53 private static final PortNumber PN2 = PortNumber.portNumber(2);
54 private static final PortNumber PN3 = PortNumber.portNumber(3);
55 private static final PortNumber PN4 = PortNumber.portNumber(4);
56 private static final PortNumber PN5 = PortNumber.portNumber(5);
57 private static final PortNumber PN6 = PortNumber.portNumber(6);
58 private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
59 private static final String TIER1 = "1";
60 private static final String TIER2 = "2";
61 private static final String TIER3 = "3";
62
63 private ResourceContext resourceContext;
64
65 private Path path12;
66 private Path path13;
67 private Path path23;
68 private DefaultLink link1;
69 private DefaultLink link2;
70 private DefaultLink link3;
71
72 @Before
73 public void setUp() {
74 resourceContext = createMock(ResourceContext.class);
75
76 Annotations annotations1 = DefaultAnnotations.builder().set(TIER, TIER1).build();
77 Annotations annotations2 = DefaultAnnotations.builder().set(TIER, TIER2).build();
78 Annotations annotations3 = DefaultAnnotations.builder().set(TIER, TIER3).build();
79
80 link1 = DefaultLink.builder()
81 .providerId(PROVIDER_ID)
82 .src(cp(DID1, PN1))
83 .dst(cp(DID2, PN2))
84 .type(DIRECT)
85 .annotations(annotations1)
86 .build();
87 link2 = DefaultLink.builder()
88 .providerId(PROVIDER_ID)
89 .src(cp(DID2, PN3))
90 .dst(cp(DID3, PN4))
91 .type(DIRECT)
92 .annotations(annotations2)
93 .build();
94 link3 = DefaultLink.builder()
95 .providerId(PROVIDER_ID)
96 .src(cp(DID2, PN5))
97 .dst(cp(DID4, PN6))
98 .type(DIRECT)
99 .annotations(annotations3)
100 .build();
101
102 path12 = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10));
103 path13 = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link3), ScalarWeight.toWeight(10));
104 path23 = new DefaultPath(PROVIDER_ID, Arrays.asList(link2, link3), ScalarWeight.toWeight(10));
105 }
106
107 /**
108 * Tests that all of the links in the specified path have a tier included in the specified included tiers.
109 */
110 @Test
111 public void testSatisfyIncludedTiers() {
112 TierConstraint constraint12 = new TierConstraint(true, 1, 2);
113 assertThat(constraint12.validate(path12, resourceContext), is(true));
114
115 TierConstraint constraint13 = new TierConstraint(true, 1, 3);
116 assertThat(constraint13.validate(path13, resourceContext), is(true));
117
118 TierConstraint constraint23 = new TierConstraint(true, 2, 3);
119 assertThat(constraint23.validate(path23, resourceContext), is(true));
120 }
121
122 /**
123 * Tests that at least one link in the specified path has a tier not included the specified included tiers.
124 */
125 @Test
126 public void testNotSatisfyIncludedTiers() {
127 TierConstraint constraint12 = new TierConstraint(true, 1, 2);
128 assertThat(constraint12.validate(path13, resourceContext), is(false));
129 assertThat(constraint12.validate(path23, resourceContext), is(false));
130
131 TierConstraint constraint13 = new TierConstraint(true, 1, 3);
132 assertThat(constraint13.validate(path12, resourceContext), is(false));
133 assertThat(constraint13.validate(path23, resourceContext), is(false));
134
135 TierConstraint constraint23 = new TierConstraint(true, 2, 3);
136 assertThat(constraint23.validate(path12, resourceContext), is(false));
137 assertThat(constraint23.validate(path13, resourceContext), is(false));
138 }
139
140 /**
141 * Tests that all of the links in the specified path do not have a tier in the specified excluded tiers.
142 */
143 @Test
144 public void testSatisfyExcludedTiers() {
145 TierConstraint constraint12 = new TierConstraint(false, 1);
146 assertThat(constraint12.validate(path23, resourceContext), is(true));
147
148 TierConstraint constraint13 = new TierConstraint(false, 2);
149 assertThat(constraint13.validate(path13, resourceContext), is(true));
150
151 TierConstraint constraint23 = new TierConstraint(false, 3);
152 assertThat(constraint23.validate(path12, resourceContext), is(true));
153 }
154
155 /**
156 * Tests that at least one link in the specified path has a tier in the specified excluded tiers.
157 */
158 @Test
159 public void testNotSatisfyExcludedTiers() {
160 TierConstraint constraint12 = new TierConstraint(false, 1);
161 assertThat(constraint12.validate(path12, resourceContext), is(false));
162 assertThat(constraint12.validate(path13, resourceContext), is(false));
163
164 TierConstraint constraint13 = new TierConstraint(false, 2);
165 assertThat(constraint13.validate(path12, resourceContext), is(false));
166 assertThat(constraint13.validate(path23, resourceContext), is(false));
167
168 TierConstraint constraint23 = new TierConstraint(false, 3);
169 assertThat(constraint23.validate(path13, resourceContext), is(false));
170 assertThat(constraint23.validate(path23, resourceContext), is(false));
171 }
172
173 /**
174 * Tests the link cost is equal to order in which a tier was added to the constraint.
175 */
176 @Test
177 public void testOrderCost() {
178 TierConstraint constraint32 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2);
179
180 assertThat(constraint32.cost(link1, resourceContext), is(-1.0));
181 assertThat(constraint32.cost(link2, resourceContext), is(2.0));
182 assertThat(constraint32.cost(link3, resourceContext), is(1.0));
183
184 TierConstraint constraint123 = new TierConstraint(true, TierConstraint.CostType.ORDER, 1, 2, 3);
185
186 assertThat(constraint123.cost(link1, resourceContext), is(1.0));
187 assertThat(constraint123.cost(link2, resourceContext), is(2.0));
188 assertThat(constraint123.cost(link3, resourceContext), is(3.0));
189
190 TierConstraint constraint231 = new TierConstraint(true, TierConstraint.CostType.ORDER, 2, 3, 1);
191
192 assertThat(constraint231.cost(link1, resourceContext), is(3.0));
193 assertThat(constraint231.cost(link2, resourceContext), is(1.0));
194 assertThat(constraint231.cost(link3, resourceContext), is(2.0));
195
196 TierConstraint constraint312 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 1, 2);
197
198 assertThat(constraint312.cost(link1, resourceContext), is(2.0));
199 assertThat(constraint312.cost(link2, resourceContext), is(3.0));
200 assertThat(constraint312.cost(link3, resourceContext), is(1.0));
201 }
202
203 /**
204 * Tests the link cost is equal to order in which a tier was added to the constraint.
205 */
206 @Test
207 public void testOrderCostWithDuplicates() {
208 TierConstraint constraint32 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2, 1, 1, 2, 3);
209
210 assertThat(constraint32.cost(link1, resourceContext), is(3.0));
211 assertThat(constraint32.cost(link2, resourceContext), is(2.0));
212 assertThat(constraint32.cost(link3, resourceContext), is(1.0));
213 }
214
215 /**
216 * Tests the link cost is equal to tier value.
217 */
218 @Test
219 public void testTierCost() {
220 TierConstraint constraint123 = new TierConstraint(true, TierConstraint.CostType.TIER, 3, 1);
221
222 assertThat(constraint123.cost(link1, resourceContext), is(1.0));
223 assertThat(constraint123.cost(link2, resourceContext), is(-1.0));
224 assertThat(constraint123.cost(link3, resourceContext), is(3.0));
225 }
226
227 /**
228 * Tests the link cost is 1 if valid and -1 if invalid.
229 */
230 @Test
231 public void testValidCost() {
232 TierConstraint constraint = new TierConstraint(true, TierConstraint.CostType.VALID, 2, 1);
233
234 assertThat(constraint.cost(link1, resourceContext), is(1.0));
235 assertThat(constraint.cost(link2, resourceContext), is(1.0));
236 assertThat(constraint.cost(link3, resourceContext), is(-1.0));
237 }
238
239 @Test
240 public void testEquality() {
241 Constraint c1 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2, 1);
242 Constraint c2 = new TierConstraint(true, TierConstraint.CostType.ORDER, 3, 2, 1);
243
244 Constraint c3 = new TierConstraint(false, TierConstraint.CostType.TIER, 1);
245 Constraint c4 = new TierConstraint(false, TierConstraint.CostType.TIER, 1);
246
247 new EqualsTester()
248 .addEqualityGroup(c1, c2)
249 .addEqualityGroup(c3, c4)
250 .testEquals();
251 }
252}