blob: 29e7c987da42b690999d48057243890b882ab85d [file] [log] [blame]
Sho SHIMIZU075268b2014-11-05 15:16:32 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Sho SHIMIZU075268b2014-11-05 15:16:32 -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 SHIMIZU075268b2014-11-05 15:16:32 -080017
18/**
19 * Test for constraint of intermediate nodes not passed.
20 */
Michele Santuarib1b44892017-05-19 09:42:31 +020021import com.google.common.collect.ImmutableSet;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080022import com.google.common.testing.EqualsTester;
23import org.junit.Before;
24import org.junit.Test;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080025import org.onlab.graph.ScalarWeight;
Michele Santuarib1b44892017-05-19 09:42:31 +020026import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
28import org.onosproject.net.DefaultAnnotations;
29import org.onosproject.net.DefaultHost;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.DefaultLink;
31import org.onosproject.net.DefaultPath;
32import org.onosproject.net.DeviceId;
Michele Santuarib1b44892017-05-19 09:42:31 +020033import org.onosproject.net.HostId;
34import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.Path;
36import org.onosproject.net.PortNumber;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080037import org.onosproject.net.intent.ResourceContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.provider.ProviderId;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080039
40import java.util.Arrays;
41
42import static org.easymock.EasyMock.createMock;
43import static org.hamcrest.Matchers.is;
44import static org.junit.Assert.*;
Michele Santuarib1b44892017-05-19 09:42:31 +020045import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.net.DefaultLinkTest.cp;
47import static org.onosproject.net.DeviceId.deviceId;
48import static org.onosproject.net.Link.Type.DIRECT;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080049
50public class ObstacleConstraintTest {
51
52 private static final DeviceId DID1 = deviceId("of:1");
53 private static final DeviceId DID2 = deviceId("of:2");
54 private static final DeviceId DID3 = deviceId("of:3");
55 private static final DeviceId DID4 = deviceId("of:4");
Michele Santuarib1b44892017-05-19 09:42:31 +020056 private static final DeviceId DID5 = deviceId("of:5");
57 private static final DeviceId DID6 = deviceId("of:6");
Sho SHIMIZU075268b2014-11-05 15:16:32 -080058 private static final PortNumber PN1 = PortNumber.portNumber(1);
59 private static final PortNumber PN2 = PortNumber.portNumber(2);
60 private static final PortNumber PN3 = PortNumber.portNumber(3);
61 private static final PortNumber PN4 = PortNumber.portNumber(4);
62 private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
63
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080064 private ResourceContext resourceContext;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080065
66 private Path path;
Michele Santuarib1b44892017-05-19 09:42:31 +020067 private Path pathWithEdgeLink;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080068 private DefaultLink link1;
Michele Santuarib1b44892017-05-19 09:42:31 +020069 private DefaultLink link2;
70 private DefaultLink edgelink1;
71 private DefaultLink edgelink2;
72 private DefaultHost host1;
73 private DefaultHost host2;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080074
75 private ObstacleConstraint sut;
76
77 @Before
78 public void setUp() {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080079 resourceContext = createMock(ResourceContext.class);
Sho SHIMIZU075268b2014-11-05 15:16:32 -080080
Ray Milkey2693bda2016-01-22 16:08:14 -080081 link1 = DefaultLink.builder()
82 .providerId(PROVIDER_ID)
83 .src(cp(DID1, PN1))
84 .dst(cp(DID2, PN2))
85 .type(DIRECT)
86 .build();
87 link2 = DefaultLink.builder()
88 .providerId(PROVIDER_ID)
89 .src(cp(DID2, PN3))
90 .dst(cp(DID3, PN4))
91 .type(DIRECT)
92 .build();
Michele Santuarib1b44892017-05-19 09:42:31 +020093 host1 = new DefaultHost(PROVIDER_ID, HostId.hostId("00:00:00:00:00:01/None"),
94 MacAddress.valueOf(0), VlanId.vlanId(),
95 new HostLocation(DID5, PN1, 1),
96 ImmutableSet.of(), DefaultAnnotations.EMPTY);
97 host2 = new DefaultHost(PROVIDER_ID, HostId.hostId("00:00:00:00:00:02/None"),
98 MacAddress.valueOf(0), VlanId.vlanId(),
99 new HostLocation(DID6, PN1, 1),
100 ImmutableSet.of(), DefaultAnnotations.EMPTY);
101 edgelink1 = createEdgeLink(host1, true);
102 edgelink2 = createEdgeLink(host2, false);
103
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800104 path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10));
Michele Santuarib1b44892017-05-19 09:42:31 +0200105 pathWithEdgeLink = new DefaultPath(PROVIDER_ID,
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800106 Arrays.asList(edgelink1, link1, link2, edgelink2), ScalarWeight.toWeight(10));
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800107 }
108
109 @Test
110 public void testEquality() {
111 ObstacleConstraint o1 = new ObstacleConstraint(DID1, DID2, DID3);
112 ObstacleConstraint o2 = new ObstacleConstraint(DID3, DID2, DID1);
113 ObstacleConstraint o3 = new ObstacleConstraint(DID1, DID2);
114 ObstacleConstraint o4 = new ObstacleConstraint(DID2, DID1);
115
116 new EqualsTester()
117 .addEqualityGroup(o1, o2)
118 .addEqualityGroup(o3, o4)
119 .testEquals();
120 }
121
122 /**
123 * Tests the specified path avoids the specified obstacle.
124 */
125 @Test
126 public void testPathNotThroughObstacles() {
127 sut = new ObstacleConstraint(DID4);
128
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800129 assertThat(sut.validate(path, resourceContext), is(true));
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800130 }
131
132 /**
133 * Test the specified path does not avoid the specified obstacle.
134 */
135 @Test
136 public void testPathThroughObstacle() {
137 sut = new ObstacleConstraint(DID1);
138
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800139 assertThat(sut.validate(path, resourceContext), is(false));
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800140 }
Michele Santuarib1b44892017-05-19 09:42:31 +0200141
142 /**
143 * Test the specified path does not avoid the specified obstacle including edge links.
144 */
145 @Test
146 public void testPathThroughObstacleWithEdgeLink() {
147 sut = new ObstacleConstraint(DID1);
148
149 assertThat(sut.validate(pathWithEdgeLink, resourceContext), is(false));
150 }
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800151}