blob: c1ee3aa4aed9ec56da46598563828579ec61c288 [file] [log] [blame]
Sho SHIMIZU1df945b2014-11-06 18:36:11 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
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;
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;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080029import org.onosproject.net.intent.ResourceContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.provider.ProviderId;
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;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080059 private ResourceContext resourceContext;
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080060
61 private Path path;
62 private Link link1;
63 private Link link2;
64
65 @Before
66 public void setUp() {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080067 resourceContext = createMock(ResourceContext.class);
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080068
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
Ray Milkey2693bda2016-01-22 16:08:14 -080072 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 SHIMIZU1df945b2014-11-06 18:36:11 -080086 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 SHIMIZUb1681bd2016-02-22 12:47:50 -080096 assertThat(sut.validate(path, resourceContext), is(true));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -080097 }
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 SHIMIZUb1681bd2016-02-22 12:47:50 -0800106 assertThat(sut.validate(path, resourceContext), is(false));
Sho SHIMIZU1df945b2014-11-06 18:36:11 -0800107 }
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 SHIMIZUb1681bd2016-02-22 12:47:50 -0800116 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 SHIMIZU1df945b2014-11-06 18:36:11 -0800118 }
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}