Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Test for constraint of intermediate nodes not passed. |
| 20 | */ |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 22 | import com.google.common.testing.EqualsTester; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 25 | import org.onlab.graph.ScalarWeight; |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.net.DefaultAnnotations; |
| 29 | import org.onosproject.net.DefaultHost; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DefaultLink; |
| 31 | import org.onosproject.net.DefaultPath; |
| 32 | import org.onosproject.net.DeviceId; |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 33 | import org.onosproject.net.HostId; |
| 34 | import org.onosproject.net.HostLocation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Path; |
| 36 | import org.onosproject.net.PortNumber; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.ResourceContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.provider.ProviderId; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 39 | |
| 40 | import java.util.Arrays; |
| 41 | |
| 42 | import static org.easymock.EasyMock.createMock; |
| 43 | import static org.hamcrest.Matchers.is; |
| 44 | import static org.junit.Assert.*; |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 45 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import static org.onosproject.net.DefaultLinkTest.cp; |
| 47 | import static org.onosproject.net.DeviceId.deviceId; |
| 48 | import static org.onosproject.net.Link.Type.DIRECT; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 49 | |
| 50 | public 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 Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 56 | private static final DeviceId DID5 = deviceId("of:5"); |
| 57 | private static final DeviceId DID6 = deviceId("of:6"); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 58 | 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 SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 64 | private ResourceContext resourceContext; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 65 | |
| 66 | private Path path; |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 67 | private Path pathWithEdgeLink; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 68 | private DefaultLink link1; |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 69 | private DefaultLink link2; |
| 70 | private DefaultLink edgelink1; |
| 71 | private DefaultLink edgelink2; |
| 72 | private DefaultHost host1; |
| 73 | private DefaultHost host2; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 74 | |
| 75 | private ObstacleConstraint sut; |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 79 | resourceContext = createMock(ResourceContext.class); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 80 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 81 | 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 Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 93 | 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 Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 104 | path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10)); |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 105 | pathWithEdgeLink = new DefaultPath(PROVIDER_ID, |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 106 | Arrays.asList(edgelink1, link1, link2, edgelink2), ScalarWeight.toWeight(10)); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 107 | } |
| 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 SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 129 | assertThat(sut.validate(path, resourceContext), is(true)); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 130 | } |
| 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 SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 139 | assertThat(sut.validate(path, resourceContext), is(false)); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 140 | } |
Michele Santuari | b1b4489 | 2017-05-19 09:42:31 +0200 | [diff] [blame] | 141 | |
| 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 SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 151 | } |