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