blob: eada7bfbbf87101d6e997052a62903a5c240d212 [file] [log] [blame]
Sho SHIMIZU1df945b2014-11-06 18:36:11 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Sho SHIMIZU1df945b2014-11-06 18:36:11 -08003 *
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;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080021import org.onlab.graph.ScalarWeight;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.Annotations;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.DefaultLink;
25import org.onosproject.net.DefaultPath;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Link;
28import org.onosproject.net.Path;
29import org.onosproject.net.PortNumber;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080030import org.onosproject.net.intent.ResourceContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.provider.ProviderId;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080032
33import java.time.Duration;
34import java.time.temporal.ChronoUnit;
35import java.util.Arrays;
36
37import static org.easymock.EasyMock.createMock;
38import static org.hamcrest.Matchers.closeTo;
39import static org.hamcrest.Matchers.is;
40import static org.junit.Assert.assertThat;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import static org.onosproject.net.AnnotationKeys.LATENCY;
42import static org.onosproject.net.DefaultLinkTest.cp;
43import static org.onosproject.net.DeviceId.deviceId;
44import static org.onosproject.net.Link.Type.DIRECT;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080045
46public 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 SHIMIZU1df945b2014-11-06 18:36:11 -080056 private static final String LATENCY1 = "3.0";
57 private static final String LATENCY2 = "4.0";
58
59 private LatencyConstraint sut;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080060 private ResourceContext resourceContext;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080061
62 private Path path;
63 private Link link1;
64 private Link link2;
65
66 @Before
67 public void setUp() {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080068 resourceContext = createMock(ResourceContext.class);
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080069
Sho SHIMIZUfe129db2014-11-11 14:24:51 -080070 Annotations annotations1 = DefaultAnnotations.builder().set(LATENCY, LATENCY1).build();
71 Annotations annotations2 = DefaultAnnotations.builder().set(LATENCY, LATENCY2).build();
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080072
Ray Milkey2693bda2016-01-22 16:08:14 -080073 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 Milkeya7cf8c82018-02-08 15:07:06 -080087 path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), ScalarWeight.toWeight(10));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080088 }
89
90 /**
91 * Tests the path latency is less than the supplied constraint.
92 */
93 @Test
94 public void testLessThanLatency() {
dingdamud3a0b212017-05-16 11:18:27 +020095 sut = new LatencyConstraint(Duration.of(10, ChronoUnit.NANOS));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080096
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080097 assertThat(sut.validate(path, resourceContext), is(true));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080098 }
99
100 /**
101 * Tests the path latency is more than the supplied constraint.
102 */
103 @Test
104 public void testMoreThanLatency() {
dingdamud3a0b212017-05-16 11:18:27 +0200105 sut = new LatencyConstraint(Duration.of(3, ChronoUnit.NANOS));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -0800106
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800107 assertThat(sut.validate(path, resourceContext), is(false));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -0800108 }
109
110 /**
111 * Tests the link latency is equal to "latency" annotated value.
112 */
113 @Test
114 public void testCost() {
dingdamud3a0b212017-05-16 11:18:27 +0200115 sut = new LatencyConstraint(Duration.of(10, ChronoUnit.NANOS));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -0800116
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800117 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 SHIMIZU1df945b2014-11-06 18:36:11 -0800119 }
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}