Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -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 | 1df945b | 2014-11-06 18:36:11 -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 | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 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.Annotations; |
| 23 | import org.onosproject.net.DefaultAnnotations; |
| 24 | import org.onosproject.net.DefaultLink; |
| 25 | import org.onosproject.net.DefaultPath; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.Path; |
| 29 | import org.onosproject.net.PortNumber; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.ResourceContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.provider.ProviderId; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 32 | |
| 33 | import java.time.Duration; |
| 34 | import java.time.temporal.ChronoUnit; |
| 35 | import java.util.Arrays; |
| 36 | |
| 37 | import static org.easymock.EasyMock.createMock; |
| 38 | import static org.hamcrest.Matchers.closeTo; |
| 39 | import static org.hamcrest.Matchers.is; |
| 40 | import static org.junit.Assert.assertThat; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import static org.onosproject.net.AnnotationKeys.LATENCY; |
| 42 | import static org.onosproject.net.DefaultLinkTest.cp; |
| 43 | import static org.onosproject.net.DeviceId.deviceId; |
| 44 | import static org.onosproject.net.Link.Type.DIRECT; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 45 | |
| 46 | public class LatencyConstraintTest { |
| 47 | |
| 48 | private static final DeviceId DID1 = deviceId("of:1"); |
| 49 | private static final DeviceId DID2 = deviceId("of:2"); |
| 50 | private static final DeviceId DID3 = deviceId("of:3"); |
| 51 | private static final PortNumber PN1 = PortNumber.portNumber(1); |
| 52 | private static final PortNumber PN2 = PortNumber.portNumber(2); |
| 53 | private static final PortNumber PN3 = PortNumber.portNumber(3); |
| 54 | private static final PortNumber PN4 = PortNumber.portNumber(4); |
| 55 | private static final ProviderId PROVIDER_ID = new ProviderId("of", "foo"); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 56 | private static final String LATENCY1 = "3.0"; |
| 57 | private static final String LATENCY2 = "4.0"; |
| 58 | |
| 59 | private LatencyConstraint sut; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 60 | private ResourceContext resourceContext; |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 61 | |
| 62 | private Path path; |
| 63 | private Link link1; |
| 64 | private Link link2; |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 68 | resourceContext = createMock(ResourceContext.class); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 69 | |
Sho SHIMIZU | fe129db | 2014-11-11 14:24:51 -0800 | [diff] [blame] | 70 | Annotations annotations1 = DefaultAnnotations.builder().set(LATENCY, LATENCY1).build(); |
| 71 | Annotations annotations2 = DefaultAnnotations.builder().set(LATENCY, LATENCY2).build(); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 72 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 73 | link1 = DefaultLink.builder() |
| 74 | .providerId(PROVIDER_ID) |
| 75 | .src(cp(DID1, PN1)) |
| 76 | .dst(cp(DID2, PN2)) |
| 77 | .type(DIRECT) |
| 78 | .annotations(annotations1) |
| 79 | .build(); |
| 80 | link2 = DefaultLink.builder() |
| 81 | .providerId(PROVIDER_ID) |
| 82 | .src(cp(DID2, PN3)) |
| 83 | .dst(cp(DID3, PN4)) |
| 84 | .type(DIRECT) |
| 85 | .annotations(annotations2) |
| 86 | .build(); |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 87 | path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10)); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests the path latency is less than the supplied constraint. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testLessThanLatency() { |
dingdamu | d3a0b21 | 2017-05-16 11:18:27 +0200 | [diff] [blame] | 95 | sut = new LatencyConstraint(Duration.of(10, ChronoUnit.NANOS)); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 96 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 97 | assertThat(sut.validate(path, resourceContext), is(true)); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests the path latency is more than the supplied constraint. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testMoreThanLatency() { |
dingdamu | d3a0b21 | 2017-05-16 11:18:27 +0200 | [diff] [blame] | 105 | sut = new LatencyConstraint(Duration.of(3, ChronoUnit.NANOS)); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 106 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 107 | assertThat(sut.validate(path, resourceContext), is(false)); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests the link latency is equal to "latency" annotated value. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testCost() { |
dingdamu | d3a0b21 | 2017-05-16 11:18:27 +0200 | [diff] [blame] | 115 | sut = new LatencyConstraint(Duration.of(10, ChronoUnit.NANOS)); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 116 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 117 | assertThat(sut.cost(link1, resourceContext), is(closeTo(Double.parseDouble(LATENCY1), 1.0e-6))); |
| 118 | assertThat(sut.cost(link2, resourceContext), is(closeTo(Double.parseDouble(LATENCY2), 1.0e-6))); |
Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Tests equality of the instances. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testEquality() { |
| 126 | LatencyConstraint c1 = new LatencyConstraint(Duration.of(1, ChronoUnit.SECONDS)); |
| 127 | LatencyConstraint c2 = new LatencyConstraint(Duration.of(1000, ChronoUnit.MILLIS)); |
| 128 | |
| 129 | LatencyConstraint c3 = new LatencyConstraint(Duration.of(2, ChronoUnit.SECONDS)); |
| 130 | LatencyConstraint c4 = new LatencyConstraint(Duration.of(2000, ChronoUnit.MILLIS)); |
| 131 | |
| 132 | new EqualsTester() |
| 133 | .addEqualityGroup(c1, c2) |
| 134 | .addEqualityGroup(c3, c4) |
| 135 | .testEquals(); |
| 136 | } |
| 137 | } |