blob: 359555b7360abf34cf5773fb184dc1e8db6ee238 [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;
Michele Santuarib1b44892017-05-19 09:42:31 +020025import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.net.DefaultAnnotations;
28import org.onosproject.net.DefaultHost;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.DefaultLink;
30import org.onosproject.net.DefaultPath;
31import org.onosproject.net.DeviceId;
Michele Santuarib1b44892017-05-19 09:42:31 +020032import org.onosproject.net.HostId;
33import org.onosproject.net.HostLocation;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.Path;
35import org.onosproject.net.PortNumber;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080036import org.onosproject.net.intent.ResourceContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.provider.ProviderId;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080038
39import java.util.Arrays;
40
41import static org.easymock.EasyMock.createMock;
42import static org.hamcrest.Matchers.is;
43import static org.junit.Assert.*;
Michele Santuarib1b44892017-05-19 09:42:31 +020044import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.DefaultLinkTest.cp;
46import static org.onosproject.net.DeviceId.deviceId;
47import static org.onosproject.net.Link.Type.DIRECT;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080048
49public class ObstacleConstraintTest {
50
51 private static final DeviceId DID1 = deviceId("of:1");
52 private static final DeviceId DID2 = deviceId("of:2");
53 private static final DeviceId DID3 = deviceId("of:3");
54 private static final DeviceId DID4 = deviceId("of:4");
Michele Santuarib1b44892017-05-19 09:42:31 +020055 private static final DeviceId DID5 = deviceId("of:5");
56 private static final DeviceId DID6 = deviceId("of:6");
Sho SHIMIZU075268b2014-11-05 15:16:32 -080057 private static final PortNumber PN1 = PortNumber.portNumber(1);
58 private static final PortNumber PN2 = PortNumber.portNumber(2);
59 private static final PortNumber PN3 = PortNumber.portNumber(3);
60 private static final PortNumber PN4 = PortNumber.portNumber(4);
61 private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo");
62
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080063 private ResourceContext resourceContext;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080064
65 private Path path;
Michele Santuarib1b44892017-05-19 09:42:31 +020066 private Path pathWithEdgeLink;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080067 private DefaultLink link1;
Michele Santuarib1b44892017-05-19 09:42:31 +020068 private DefaultLink link2;
69 private DefaultLink edgelink1;
70 private DefaultLink edgelink2;
71 private DefaultHost host1;
72 private DefaultHost host2;
Sho SHIMIZU075268b2014-11-05 15:16:32 -080073
74 private ObstacleConstraint sut;
75
76 @Before
77 public void setUp() {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080078 resourceContext = createMock(ResourceContext.class);
Sho SHIMIZU075268b2014-11-05 15:16:32 -080079
Ray Milkey2693bda2016-01-22 16:08:14 -080080 link1 = DefaultLink.builder()
81 .providerId(PROVIDER_ID)
82 .src(cp(DID1, PN1))
83 .dst(cp(DID2, PN2))
84 .type(DIRECT)
85 .build();
86 link2 = DefaultLink.builder()
87 .providerId(PROVIDER_ID)
88 .src(cp(DID2, PN3))
89 .dst(cp(DID3, PN4))
90 .type(DIRECT)
91 .build();
Michele Santuarib1b44892017-05-19 09:42:31 +020092 host1 = new DefaultHost(PROVIDER_ID, HostId.hostId("00:00:00:00:00:01/None"),
93 MacAddress.valueOf(0), VlanId.vlanId(),
94 new HostLocation(DID5, PN1, 1),
95 ImmutableSet.of(), DefaultAnnotations.EMPTY);
96 host2 = new DefaultHost(PROVIDER_ID, HostId.hostId("00:00:00:00:00:02/None"),
97 MacAddress.valueOf(0), VlanId.vlanId(),
98 new HostLocation(DID6, PN1, 1),
99 ImmutableSet.of(), DefaultAnnotations.EMPTY);
100 edgelink1 = createEdgeLink(host1, true);
101 edgelink2 = createEdgeLink(host2, false);
102
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800103 path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10);
Michele Santuarib1b44892017-05-19 09:42:31 +0200104 pathWithEdgeLink = new DefaultPath(PROVIDER_ID,
105 Arrays.asList(edgelink1, link1, link2, edgelink2), 10);
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800106 }
107
108 @Test
109 public void testEquality() {
110 ObstacleConstraint o1 = new ObstacleConstraint(DID1, DID2, DID3);
111 ObstacleConstraint o2 = new ObstacleConstraint(DID3, DID2, DID1);
112 ObstacleConstraint o3 = new ObstacleConstraint(DID1, DID2);
113 ObstacleConstraint o4 = new ObstacleConstraint(DID2, DID1);
114
115 new EqualsTester()
116 .addEqualityGroup(o1, o2)
117 .addEqualityGroup(o3, o4)
118 .testEquals();
119 }
120
121 /**
122 * Tests the specified path avoids the specified obstacle.
123 */
124 @Test
125 public void testPathNotThroughObstacles() {
126 sut = new ObstacleConstraint(DID4);
127
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800128 assertThat(sut.validate(path, resourceContext), is(true));
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800129 }
130
131 /**
132 * Test the specified path does not avoid the specified obstacle.
133 */
134 @Test
135 public void testPathThroughObstacle() {
136 sut = new ObstacleConstraint(DID1);
137
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800138 assertThat(sut.validate(path, resourceContext), is(false));
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800139 }
Michele Santuarib1b44892017-05-19 09:42:31 +0200140
141 /**
142 * Test the specified path does not avoid the specified obstacle including edge links.
143 */
144 @Test
145 public void testPathThroughObstacleWithEdgeLink() {
146 sut = new ObstacleConstraint(DID1);
147
148 assertThat(sut.validate(pathWithEdgeLink, resourceContext), is(false));
149 }
Sho SHIMIZU075268b2014-11-05 15:16:32 -0800150}