Sho SHIMIZU | 1df945b | 2014-11-06 18:36:11 -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 | 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; |
| 29 | import org.onosproject.net.provider.ProviderId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 30 | import org.onosproject.net.resource.link.LinkResourceService; |
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; |
| 59 | private LinkResourceService linkResourceService; |
| 60 | |
| 61 | private Path path; |
| 62 | private Link link1; |
| 63 | private Link link2; |
| 64 | |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | linkResourceService = createMock(LinkResourceService.class); |
| 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 | |
| 72 | link1 = new DefaultLink(PROVIDER_ID, cp(DID1, PN1), cp(DID2, PN2), DIRECT, annotations1); |
| 73 | link2 = new DefaultLink(PROVIDER_ID, cp(DID2, PN3), cp(DID3, PN4), DIRECT, annotations2); |
| 74 | path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests the path latency is less than the supplied constraint. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testLessThanLatency() { |
| 82 | sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS)); |
| 83 | |
| 84 | assertThat(sut.validate(path, linkResourceService), is(true)); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests the path latency is more than the supplied constraint. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testMoreThanLatency() { |
| 92 | sut = new LatencyConstraint(Duration.of(3, ChronoUnit.MICROS)); |
| 93 | |
| 94 | assertThat(sut.validate(path, linkResourceService), is(false)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Tests the link latency is equal to "latency" annotated value. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testCost() { |
| 102 | sut = new LatencyConstraint(Duration.of(10, ChronoUnit.MICROS)); |
| 103 | |
| 104 | assertThat(sut.cost(link1, linkResourceService), is(closeTo(Double.parseDouble(LATENCY1), 1.0e-6))); |
| 105 | assertThat(sut.cost(link2, linkResourceService), is(closeTo(Double.parseDouble(LATENCY2), 1.0e-6))); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Tests equality of the instances. |
| 110 | */ |
| 111 | @Test |
| 112 | public void testEquality() { |
| 113 | LatencyConstraint c1 = new LatencyConstraint(Duration.of(1, ChronoUnit.SECONDS)); |
| 114 | LatencyConstraint c2 = new LatencyConstraint(Duration.of(1000, ChronoUnit.MILLIS)); |
| 115 | |
| 116 | LatencyConstraint c3 = new LatencyConstraint(Duration.of(2, ChronoUnit.SECONDS)); |
| 117 | LatencyConstraint c4 = new LatencyConstraint(Duration.of(2000, ChronoUnit.MILLIS)); |
| 118 | |
| 119 | new EqualsTester() |
| 120 | .addEqualityGroup(c1, c2) |
| 121 | .addEqualityGroup(c3, c4) |
| 122 | .testEquals(); |
| 123 | } |
| 124 | } |