blob: 006ca57615d9af7635e7748a8ad7ae0ef6741670 [file] [log] [blame]
Ray Milkeyc8f481f2014-11-18 15:37:12 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Ray Milkeyc8f481f2014-11-18 15:37:12 -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;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080017
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080018import java.time.Duration;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080019import java.util.HashSet;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Set;
23
24import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.Link;
27import org.onosproject.net.NetTestTools;
28import org.onosproject.net.flow.TrafficSelector;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080029
Michele Santuari4a338072014-11-05 18:38:55 +010030import com.google.common.collect.ImmutableSet;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080031import com.google.common.testing.EqualsTester;
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080032import org.onosproject.net.intent.constraint.LatencyConstraint;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080033
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.hasSize;
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -080036import static org.hamcrest.Matchers.instanceOf;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080037import static org.hamcrest.Matchers.is;
38import static org.hamcrest.Matchers.nullValue;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080039import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import static org.onosproject.net.NetTestTools.APP_ID;
41import static org.onosproject.net.NetTestTools.link;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080042
43/**
44 * Unit tests for the LinkCollectionIntent class.
45 */
Brian O'Connor520c0522014-11-23 23:50:47 -080046public class LinkCollectionIntentTest extends IntentTest {
Ray Milkeyc8f481f2014-11-18 15:37:12 -080047
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080048 final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 2);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080049 final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
50 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
51 final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
52
53 /**
54 * Checks that the LinkCollectionIntent class is immutable.
55 */
56 @Test
57 public void testImmutability() {
58 assertThatClassIsImmutable(LinkCollectionIntent.class);
59 }
60
61 /**
62 * Tests equals(), hashCode() and toString() methods.
63 */
Ray Milkey37f6a382014-11-25 14:54:42 -080064 @Test
Ray Milkeyc8f481f2014-11-18 15:37:12 -080065 public void testEquals() {
66
67 final HashSet<Link> links1 = new HashSet<>();
68 links1.add(link("src", 1, "dst", 2));
69 final LinkCollectionIntent collectionIntent1 =
Ray Milkeyebc5d222015-03-18 15:45:36 -070070 LinkCollectionIntent.builder()
71 .appId(APP_ID)
72 .selector(selector)
73 .treatment(treatment)
74 .links(links1)
75 .ingressPoints(ImmutableSet.of(ingress))
76 .egressPoints(ImmutableSet.of(egress))
77 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -080078
79 final HashSet<Link> links2 = new HashSet<>();
80 links2.add(link("src", 1, "dst", 3));
81 final LinkCollectionIntent collectionIntent2 =
Ray Milkeyebc5d222015-03-18 15:45:36 -070082 LinkCollectionIntent.builder()
83 .appId(APP_ID)
84 .selector(selector)
85 .treatment(treatment)
86 .links(links2)
87 .ingressPoints(ImmutableSet.of(ingress))
88 .egressPoints(ImmutableSet.of(egress))
89 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -080090
91 new EqualsTester()
Ray Milkey37f6a382014-11-25 14:54:42 -080092 .addEqualityGroup(collectionIntent1)
Ray Milkeyc8f481f2014-11-18 15:37:12 -080093 .addEqualityGroup(collectionIntent2)
94 .testEquals();
95 }
96
97 /**
98 * Tests constructor without constraints.
99 */
100 @Test
101 public void testConstructor() {
102 final HashSet<Link> links1 = new HashSet<>();
103 links1.add(link("src", 1, "dst", 2));
104 final LinkCollectionIntent collectionIntent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700105 LinkCollectionIntent.builder()
106 .appId(APP_ID)
107 .selector(selector)
108 .treatment(treatment)
109 .links(links1)
110 .ingressPoints(ImmutableSet.of(ingress))
111 .egressPoints(ImmutableSet.of(egress))
112 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800113
114 final Set<Link> createdLinks = collectionIntent.links();
115 assertThat(createdLinks, hasSize(1));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800116 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800117 assertThat(collectionIntent.treatment(), is(treatment));
118 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800119 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100120 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800121 assertThat(collectionIntent.resources(), hasSize(1));
122 final List<Constraint> createdConstraints = collectionIntent.constraints();
123 assertThat(createdConstraints, hasSize(0));
124 }
125
126 /**
127 * Tests constructor with constraints.
128 */
129 @Test
130 public void testConstructorWithConstraints() {
131 final HashSet<Link> links1 = new HashSet<>();
132 final LinkedList<Constraint> constraints = new LinkedList<>();
133
134 links1.add(link("src", 1, "dst", 2));
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -0800135 constraints.add(new LatencyConstraint(Duration.ofMillis(100)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800136 final LinkCollectionIntent collectionIntent =
Ray Milkeyebc5d222015-03-18 15:45:36 -0700137 LinkCollectionIntent.builder()
138 .appId(APP_ID)
139 .selector(selector)
140 .treatment(treatment)
141 .links(links1)
142 .ingressPoints(ImmutableSet.of(ingress))
143 .egressPoints(ImmutableSet.of(egress))
144 .constraints(constraints)
145 .priority(8888)
146 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800147
148 final Set<Link> createdLinks = collectionIntent.links();
149 assertThat(createdLinks, hasSize(1));
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800150 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800151 assertThat(collectionIntent.treatment(), is(treatment));
152 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800153 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100154 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800155
156 final List<Constraint> createdConstraints = collectionIntent.constraints();
157 assertThat(createdConstraints, hasSize(1));
Sho SHIMIZU4e2149e2016-02-22 14:42:34 -0800158 assertThat(createdConstraints.get(0), instanceOf(LatencyConstraint.class));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800159 }
160
161 /**
162 * Tests constructor with constraints.
163 */
164 @Test
165 public void testSerializerConstructor() {
166
167 final LinkCollectionIntent collectionIntent =
168 new LinkCollectionIntent();
169
170 final Set<Link> createdLinks = collectionIntent.links();
171 assertThat(createdLinks, nullValue());
Sho SHIMIZUee2aa652015-02-25 18:56:43 -0800172 assertThat(collectionIntent.isInstallable(), is(false));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800173 assertThat(collectionIntent.treatment(), nullValue());
174 assertThat(collectionIntent.selector(), nullValue());
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800175 assertThat(collectionIntent.ingressPoints(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100176 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800177
178 final List<Constraint> createdConstraints = collectionIntent.constraints();
179 assertThat(createdConstraints, hasSize(0));
180 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800181
182 @Override
183 protected Intent createOne() {
184 HashSet<Link> links1 = new HashSet<>();
185 links1.add(link("src", 1, "dst", 2));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700186 return LinkCollectionIntent.builder()
187 .appId(APP_ID)
188 .selector(selector)
189 .treatment(treatment)
190 .links(links1)
191 .ingressPoints(ImmutableSet.of(ingress))
192 .egressPoints(ImmutableSet.of(egress))
193 .build();
Brian O'Connor520c0522014-11-23 23:50:47 -0800194 }
195
196 @Override
197 protected Intent createAnother() {
198 HashSet<Link> links2 = new HashSet<>();
199 links2.add(link("src", 1, "dst", 3));
Ray Milkeyebc5d222015-03-18 15:45:36 -0700200 return LinkCollectionIntent.builder()
201 .appId(APP_ID)
202 .selector(selector)
203 .treatment(treatment)
204 .links(links2)
205 .ingressPoints(ImmutableSet.of(ingress))
206 .egressPoints(ImmutableSet.of(egress))
207 .build();
Brian O'Connor520c0522014-11-23 23:50:47 -0800208 }
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800209}