blob: 060607b00d4730a7ee5f2a3b286be6d24a2204f7 [file] [log] [blame]
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080017
Sho SHIMIZU14ccab52014-11-06 11:11:40 -080018import com.google.common.testing.EqualsTester;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080019import org.junit.Before;
20import org.junit.Test;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080021import org.onlab.graph.ScalarWeight;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.DefaultLink;
23import org.onosproject.net.DefaultPath;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Path;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.intent.Constraint;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080028import org.onosproject.net.intent.ResourceContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.provider.ProviderId;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080030
31import java.util.Arrays;
32
33import static org.easymock.EasyMock.createMock;
34import static org.hamcrest.Matchers.is;
35import static org.junit.Assert.assertThat;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import static org.onosproject.net.DefaultLinkTest.cp;
37import static org.onosproject.net.DeviceId.deviceId;
38import static org.onosproject.net.Link.Type.DIRECT;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080039
40/**
41 * Test for constraint of intermediate elements.
42 */
43public class WaypointConstraintTest {
44
Sho SHIMIZU1332b172014-11-06 16:03:09 -080045 private static final DeviceId DID1 = deviceId("of:1");
46 private static final DeviceId DID2 = deviceId("of:2");
47 private static final DeviceId DID3 = deviceId("of:3");
48 private static final DeviceId DID4 = deviceId("of:4");
49 private static final PortNumber PN1 = PortNumber.portNumber(1);
50 private static final PortNumber PN2 = PortNumber.portNumber(2);
51 private static final PortNumber PN3 = PortNumber.portNumber(3);
52 private static final PortNumber PN4 = PortNumber.portNumber(4);
53 private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080054
55 private WaypointConstraint sut;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080056 private ResourceContext resourceContext;
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080057
58 private Path path;
59 private DefaultLink link2;
60 private DefaultLink link1;
61
62 @Before
63 public void setUp() {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080064 resourceContext = createMock(ResourceContext.class);
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080065
Ray Milkey2693bda2016-01-22 16:08:14 -080066 link1 = DefaultLink.builder()
67 .providerId(PROVIDER_ID)
68 .src(cp(DID1, PN1))
69 .dst(cp(DID2, PN2))
70 .type(DIRECT)
71 .build();
72 link2 = DefaultLink.builder()
73 .providerId(PROVIDER_ID)
74 .src(cp(DID2, PN3))
75 .dst(cp(DID3, PN4))
76 .type(DIRECT)
77 .build();
78
Ray Milkeya7cf8c82018-02-08 15:07:06 -080079 path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10));
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080080 }
81
82 /**
83 * Tests that all of the specified waypoints are included in the specified path in order.
84 */
85 @Test
86 public void testSatisfyWaypoints() {
87 sut = new WaypointConstraint(DID1, DID2, DID3);
88
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080089 assertThat(sut.validate(path, resourceContext), is(true));
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -080090 }
91
92 /**
93 * Tests that the specified path does not includes the specified waypoint.
94 */
95 @Test
96 public void testNotSatisfyWaypoint() {
97 sut = new WaypointConstraint(DID4);
98
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080099 assertThat(sut.validate(path, resourceContext), is(false));
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -0800100 }
Sho SHIMIZU14ccab52014-11-06 11:11:40 -0800101
102 @Test
103 public void testEquality() {
104 Constraint c1 = new WaypointConstraint(DID1, DID2);
105 Constraint c2 = new WaypointConstraint(DID1, DID2);
106
107 Constraint c3 = new WaypointConstraint(DID2);
108 Constraint c4 = new WaypointConstraint(DID3);
109
110 new EqualsTester()
111 .addEqualityGroup(c1, c2)
112 .addEqualityGroup(c3)
113 .addEqualityGroup(c4)
114 .testEquals();
115 }
Sho SHIMIZU0e51fe52014-11-05 12:04:23 -0800116}