blob: bab174953ab5c696f9314aab7c4936d61ae44092 [file] [log] [blame]
Sho SHIMIZU1df945b2014-11-06 18:36:11 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080017
18import com.google.common.testing.EqualsTester;
19import org.junit.Before;
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.Annotations;
22import org.onosproject.net.DefaultAnnotations;
23import org.onosproject.net.DefaultLink;
24import org.onosproject.net.DefaultPath;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Link;
27import org.onosproject.net.Path;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.provider.ProviderId;
Brian O'Connor6de2e202015-05-21 14:30:41 -070030import org.onosproject.net.resource.link.LinkResourceService;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080031
32import java.time.Duration;
33import java.time.temporal.ChronoUnit;
34import java.util.Arrays;
35
36import static org.easymock.EasyMock.createMock;
37import static org.hamcrest.Matchers.closeTo;
38import static org.hamcrest.Matchers.is;
39import static org.junit.Assert.assertThat;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import static org.onosproject.net.AnnotationKeys.LATENCY;
41import static org.onosproject.net.DefaultLinkTest.cp;
42import static org.onosproject.net.DeviceId.deviceId;
43import static org.onosproject.net.Link.Type.DIRECT;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080044
45public 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 SHIMIZU1df945b2014-11-06 18:36:11 -080055 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 SHIMIZUfe129db2014-11-11 14:24:51 -080069 Annotations annotations1 = DefaultAnnotations.builder().set(LATENCY, LATENCY1).build();
70 Annotations annotations2 = DefaultAnnotations.builder().set(LATENCY, LATENCY2).build();
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080071
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}