Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -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 | 0e51fe5 | 2014-11-05 12:04:23 -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 | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 17 | |
Sho SHIMIZU | 14ccab5 | 2014-11-06 11:11:40 -0800 | [diff] [blame] | 18 | import com.google.common.testing.EqualsTester; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 21 | import org.onlab.graph.ScalarWeight; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.DefaultLink; |
| 23 | import org.onosproject.net.DefaultPath; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Path; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.intent.Constraint; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.ResourceContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.provider.ProviderId; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 30 | |
| 31 | import java.util.Arrays; |
| 32 | |
| 33 | import static org.easymock.EasyMock.createMock; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | import static org.junit.Assert.assertThat; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import static org.onosproject.net.DefaultLinkTest.cp; |
| 37 | import static org.onosproject.net.DeviceId.deviceId; |
| 38 | import static org.onosproject.net.Link.Type.DIRECT; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Test for constraint of intermediate elements. |
| 42 | */ |
| 43 | public class WaypointConstraintTest { |
| 44 | |
Sho SHIMIZU | 1332b17 | 2014-11-06 16:03:09 -0800 | [diff] [blame] | 45 | 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 SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 54 | |
| 55 | private WaypointConstraint sut; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 56 | private ResourceContext resourceContext; |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 57 | |
| 58 | private Path path; |
| 59 | private DefaultLink link2; |
| 60 | private DefaultLink link1; |
| 61 | |
| 62 | @Before |
| 63 | public void setUp() { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 64 | resourceContext = createMock(ResourceContext.class); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 65 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 66 | 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 Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 79 | path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10)); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 80 | } |
| 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 SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 89 | assertThat(sut.validate(path, resourceContext), is(true)); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 90 | } |
| 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 SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 99 | assertThat(sut.validate(path, resourceContext), is(false)); |
Sho SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 100 | } |
Sho SHIMIZU | 14ccab5 | 2014-11-06 11:11:40 -0800 | [diff] [blame] | 101 | |
| 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 SHIMIZU | 0e51fe5 | 2014-11-05 12:04:23 -0800 | [diff] [blame] | 116 | } |