Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
| 21 | import com.google.common.testing.EqualsTester; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.DefaultLink; |
| 25 | import org.onosproject.net.DefaultPath; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Path; |
| 28 | import org.onosproject.net.PortNumber; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame^] | 29 | import org.onosproject.net.intent.ResourceContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.provider.ProviderId; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 31 | |
| 32 | import java.util.Arrays; |
| 33 | |
| 34 | import static org.easymock.EasyMock.createMock; |
| 35 | import static org.hamcrest.Matchers.is; |
| 36 | import static org.junit.Assert.*; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import static org.onosproject.net.DefaultLinkTest.cp; |
| 38 | import static org.onosproject.net.DeviceId.deviceId; |
| 39 | import static org.onosproject.net.Link.Type.DIRECT; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 40 | |
| 41 | public class ObstacleConstraintTest { |
| 42 | |
| 43 | private static final DeviceId DID1 = deviceId("of:1"); |
| 44 | private static final DeviceId DID2 = deviceId("of:2"); |
| 45 | private static final DeviceId DID3 = deviceId("of:3"); |
| 46 | private static final DeviceId DID4 = deviceId("of:4"); |
| 47 | private static final PortNumber PN1 = PortNumber.portNumber(1); |
| 48 | private static final PortNumber PN2 = PortNumber.portNumber(2); |
| 49 | private static final PortNumber PN3 = PortNumber.portNumber(3); |
| 50 | private static final PortNumber PN4 = PortNumber.portNumber(4); |
| 51 | private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo"); |
| 52 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame^] | 53 | private ResourceContext resourceContext; |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 54 | |
| 55 | private Path path; |
| 56 | private DefaultLink link2; |
| 57 | private DefaultLink link1; |
| 58 | |
| 59 | private ObstacleConstraint sut; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame^] | 63 | resourceContext = createMock(ResourceContext.class); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 64 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 65 | link1 = DefaultLink.builder() |
| 66 | .providerId(PROVIDER_ID) |
| 67 | .src(cp(DID1, PN1)) |
| 68 | .dst(cp(DID2, PN2)) |
| 69 | .type(DIRECT) |
| 70 | .build(); |
| 71 | link2 = DefaultLink.builder() |
| 72 | .providerId(PROVIDER_ID) |
| 73 | .src(cp(DID2, PN3)) |
| 74 | .dst(cp(DID3, PN4)) |
| 75 | .type(DIRECT) |
| 76 | .build(); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 77 | path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testEquality() { |
| 82 | ObstacleConstraint o1 = new ObstacleConstraint(DID1, DID2, DID3); |
| 83 | ObstacleConstraint o2 = new ObstacleConstraint(DID3, DID2, DID1); |
| 84 | ObstacleConstraint o3 = new ObstacleConstraint(DID1, DID2); |
| 85 | ObstacleConstraint o4 = new ObstacleConstraint(DID2, DID1); |
| 86 | |
| 87 | new EqualsTester() |
| 88 | .addEqualityGroup(o1, o2) |
| 89 | .addEqualityGroup(o3, o4) |
| 90 | .testEquals(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests the specified path avoids the specified obstacle. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testPathNotThroughObstacles() { |
| 98 | sut = new ObstacleConstraint(DID4); |
| 99 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame^] | 100 | assertThat(sut.validate(path, resourceContext), is(true)); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Test the specified path does not avoid the specified obstacle. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testPathThroughObstacle() { |
| 108 | sut = new ObstacleConstraint(DID1); |
| 109 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame^] | 110 | assertThat(sut.validate(path, resourceContext), is(false)); |
Sho SHIMIZU | 075268b | 2014-11-05 15:16:32 -0800 | [diff] [blame] | 111 | } |
| 112 | } |