Committed by
Gerrit Code Review
Merge "Add constraint to avoid specified devices"
Showing
2 changed files
with
189 additions
and
0 deletions
| 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 | + */ | ||
| 16 | +package org.onlab.onos.net.intent.constraint; | ||
| 17 | + | ||
| 18 | +import com.google.common.base.MoreObjects; | ||
| 19 | +import com.google.common.collect.ImmutableSet; | ||
| 20 | +import org.onlab.onos.net.DeviceId; | ||
| 21 | +import org.onlab.onos.net.Link; | ||
| 22 | +import org.onlab.onos.net.Path; | ||
| 23 | +import org.onlab.onos.net.resource.LinkResourceService; | ||
| 24 | + | ||
| 25 | +import java.util.Objects; | ||
| 26 | +import java.util.Set; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * Constraint that evaluates elements not passed through. | ||
| 30 | + */ | ||
| 31 | +public class ObstacleConstraint extends BooleanConstraint { | ||
| 32 | + | ||
| 33 | + private final Set<DeviceId> obstacles; | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Creates a new constraint that the specified device are not passed through. | ||
| 37 | + * @param obstacles devices not to be passed | ||
| 38 | + */ | ||
| 39 | + public ObstacleConstraint(DeviceId... obstacles) { | ||
| 40 | + this.obstacles = ImmutableSet.copyOf(obstacles); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + @Override | ||
| 44 | + public boolean isValid(Link link, LinkResourceService resourceService) { | ||
| 45 | + DeviceId src = link.src().deviceId(); | ||
| 46 | + DeviceId dst = link.dst().deviceId(); | ||
| 47 | + | ||
| 48 | + return !(obstacles.contains(src) || obstacles.contains(dst)); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + @Override | ||
| 52 | + public boolean validate(Path path, LinkResourceService resourceService) { | ||
| 53 | + for (Link link : path.links()) { | ||
| 54 | + if (!isValid(link, resourceService)) { | ||
| 55 | + return false; | ||
| 56 | + } | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + return true; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + @Override | ||
| 63 | + public int hashCode() { | ||
| 64 | + return Objects.hash(obstacles); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + @Override | ||
| 68 | + public boolean equals(Object obj) { | ||
| 69 | + if (this == obj) { | ||
| 70 | + return true; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + if (!(obj instanceof ObstacleConstraint)) { | ||
| 74 | + return false; | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + final ObstacleConstraint that = (ObstacleConstraint) obj; | ||
| 78 | + return Objects.equals(this.obstacles, that.obstacles); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + @Override | ||
| 82 | + public String toString() { | ||
| 83 | + return MoreObjects.toStringHelper(this) | ||
| 84 | + .add("obstacles", obstacles) | ||
| 85 | + .toString(); | ||
| 86 | + } | ||
| 87 | +} |
| 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 | + */ | ||
| 16 | +package org.onlab.onos.net.intent.constraint; | ||
| 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; | ||
| 24 | +import org.onlab.onos.net.DefaultLink; | ||
| 25 | +import org.onlab.onos.net.DefaultPath; | ||
| 26 | +import org.onlab.onos.net.DeviceId; | ||
| 27 | +import org.onlab.onos.net.Path; | ||
| 28 | +import org.onlab.onos.net.PortNumber; | ||
| 29 | +import org.onlab.onos.net.provider.ProviderId; | ||
| 30 | +import org.onlab.onos.net.resource.LinkResourceService; | ||
| 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.*; | ||
| 37 | +import static org.onlab.onos.net.DefaultLinkTest.cp; | ||
| 38 | +import static org.onlab.onos.net.DeviceId.deviceId; | ||
| 39 | +import static org.onlab.onos.net.Link.Type.DIRECT; | ||
| 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 | + | ||
| 53 | + private LinkResourceService linkResourceService; | ||
| 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() { | ||
| 63 | + linkResourceService = createMock(LinkResourceService.class); | ||
| 64 | + | ||
| 65 | + link1 = new DefaultLink(PROVIDER_ID, cp(DID1, PN1), cp(DID2, PN2), DIRECT); | ||
| 66 | + link2 = new DefaultLink(PROVIDER_ID, cp(DID2, PN3), cp(DID3, PN4), DIRECT); | ||
| 67 | + path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @Test | ||
| 71 | + public void testEquality() { | ||
| 72 | + ObstacleConstraint o1 = new ObstacleConstraint(DID1, DID2, DID3); | ||
| 73 | + ObstacleConstraint o2 = new ObstacleConstraint(DID3, DID2, DID1); | ||
| 74 | + ObstacleConstraint o3 = new ObstacleConstraint(DID1, DID2); | ||
| 75 | + ObstacleConstraint o4 = new ObstacleConstraint(DID2, DID1); | ||
| 76 | + | ||
| 77 | + new EqualsTester() | ||
| 78 | + .addEqualityGroup(o1, o2) | ||
| 79 | + .addEqualityGroup(o3, o4) | ||
| 80 | + .testEquals(); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /** | ||
| 84 | + * Tests the specified path avoids the specified obstacle. | ||
| 85 | + */ | ||
| 86 | + @Test | ||
| 87 | + public void testPathNotThroughObstacles() { | ||
| 88 | + sut = new ObstacleConstraint(DID4); | ||
| 89 | + | ||
| 90 | + assertThat(sut.validate(path, linkResourceService), is(true)); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * Test the specified path does not avoid the specified obstacle. | ||
| 95 | + */ | ||
| 96 | + @Test | ||
| 97 | + public void testPathThroughObstacle() { | ||
| 98 | + sut = new ObstacleConstraint(DID1); | ||
| 99 | + | ||
| 100 | + assertThat(sut.validate(path, linkResourceService), is(false)); | ||
| 101 | + } | ||
| 102 | +} |
-
Please register or login to post a comment