blob: 3aba8767d6b0af5c9ee7d3edcd1d500003749680 [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
18import org.junit.Test;
HIGUCHI Yuta501c4652015-10-29 14:21:48 -070019import org.onlab.util.DataRateUnit;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.TestApplicationId;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.HostId;
Luca Prete670ac5d2017-02-03 15:55:43 -080023import org.onosproject.net.ResourceGroup;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.flow.TrafficSelector;
HIGUCHI Yuta584107a2015-10-27 18:12:44 -070025import org.onosproject.net.intent.constraint.BandwidthConstraint;
HIGUCHI Yuta584107a2015-10-27 18:12:44 -070026import com.google.common.collect.ImmutableList;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080027import com.google.common.testing.EqualsTester;
28
Ray Milkey37f6a382014-11-25 14:54:42 -080029import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.equalTo;
HIGUCHI Yuta584107a2015-10-27 18:12:44 -070031import static org.hamcrest.Matchers.hasItem;
Ray Milkey37f6a382014-11-25 14:54:42 -080032import static org.hamcrest.Matchers.is;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080033import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import static org.onosproject.net.NetTestTools.hid;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080035
36/**
37 * Unit tests for the HostToHostIntent class.
38 */
Brian O'Connor520c0522014-11-23 23:50:47 -080039public class HostToHostIntentTest extends IntentTest {
40 private final TrafficSelector selector = new IntentTestsMocks.MockSelector();
41 private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
42 private final HostId id1 = hid("12:34:56:78:91:ab/1");
43 private final HostId id2 = hid("12:34:56:78:92:ab/1");
44 private final HostId id3 = hid("12:34:56:78:93:ab/1");
Luca Prete670ac5d2017-02-03 15:55:43 -080045 private final ResourceGroup resourceGrouop = ResourceGroup.of(0L);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080046
Ray Milkey37f6a382014-11-25 14:54:42 -080047 private static final ApplicationId APPID = new TestApplicationId("foo");
48
49 private HostToHostIntent makeHostToHost(HostId one, HostId two) {
Ray Milkeyebc5d222015-03-18 15:45:36 -070050 return HostToHostIntent.builder()
51 .appId(APPID)
52 .one(one)
53 .two(two)
54 .selector(selector)
55 .treatment(treatment)
56 .build();
Ray Milkey37f6a382014-11-25 14:54:42 -080057 }
58
59 /**
60 * Tests the equals() method where two HostToHostIntents have references
61 * to the same hosts. These should compare equal.
62 */
63 @Test
64 public void testSameEquals() {
65
66 HostId one = hid("00:00:00:00:00:01/-1");
67 HostId two = hid("00:00:00:00:00:02/-1");
68 HostToHostIntent i1 = makeHostToHost(one, two);
69 HostToHostIntent i2 = makeHostToHost(one, two);
70
71 assertThat(i1.one(), is(equalTo(i2.one())));
72 assertThat(i1.two(), is(equalTo(i2.two())));
73 }
74
Ray Milkeyc8f481f2014-11-18 15:37:12 -080075 /**
76 * Checks that the HostToHostIntent class is immutable.
77 */
78 @Test
79 public void testImmutability() {
80 assertThatClassIsImmutable(HostToHostIntent.class);
81 }
82
83 /**
84 * Tests equals(), hashCode() and toString() methods.
85 */
Ray Milkey37f6a382014-11-25 14:54:42 -080086 @Test
Ray Milkeyc8f481f2014-11-18 15:37:12 -080087 public void testEquals() {
Ray Milkeyebc5d222015-03-18 15:45:36 -070088 final HostToHostIntent intent1 = HostToHostIntent.builder()
89 .appId(APPID)
90 .one(id1)
91 .two(id2)
92 .selector(selector)
93 .treatment(treatment)
94 .build();
Ray Milkey37f6a382014-11-25 14:54:42 -080095
Ray Milkeyebc5d222015-03-18 15:45:36 -070096 final HostToHostIntent intent2 = HostToHostIntent.builder()
97 .appId(APPID)
98 .one(id2)
99 .two(id3)
100 .selector(selector)
101 .treatment(treatment)
102 .build();
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800103
104 new EqualsTester()
Ray Milkey37f6a382014-11-25 14:54:42 -0800105 .addEqualityGroup(intent1)
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800106 .addEqualityGroup(intent2)
107 .testEquals();
108 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800109
HIGUCHI Yuta584107a2015-10-27 18:12:44 -0700110 @Test
111 public void testImplicitConstraintsAreAdded() {
HIGUCHI Yuta501c4652015-10-29 14:21:48 -0700112 final Constraint other = BandwidthConstraint.of(1, DataRateUnit.GBPS);
HIGUCHI Yuta584107a2015-10-27 18:12:44 -0700113 final HostToHostIntent intent = HostToHostIntent.builder()
114 .appId(APPID)
115 .one(id1)
116 .two(id2)
117 .selector(selector)
118 .treatment(treatment)
119 .constraints(ImmutableList.of(other))
120 .build();
121
122 assertThat(intent.constraints(), hasItem(HostToHostIntent.NOT_OPTICAL));
123 }
124
125 @Test
126 public void testImplicitConstraints() {
127 final HostToHostIntent implicit = HostToHostIntent.builder()
128 .appId(APPID)
129 .one(id1)
130 .two(id2)
131 .selector(selector)
132 .treatment(treatment)
133 .build();
134 final HostToHostIntent empty = HostToHostIntent.builder()
135 .appId(APPID)
136 .one(id1)
137 .two(id2)
138 .selector(selector)
139 .treatment(treatment)
140 .constraints(ImmutableList.of())
141 .build();
142 final HostToHostIntent exact = HostToHostIntent.builder()
143 .appId(APPID)
144 .one(id1)
145 .two(id2)
146 .selector(selector)
147 .treatment(treatment)
148 .constraints(ImmutableList.of(HostToHostIntent.NOT_OPTICAL))
149 .build();
150
151 new EqualsTester()
152 .addEqualityGroup(implicit.constraints(),
153 empty.constraints(),
154 exact.constraints())
155 .testEquals();
156
157 }
158
Luca Prete670ac5d2017-02-03 15:55:43 -0800159 @Test
160 public void testResourceGroup() {
161 final HostToHostIntent intent = (HostToHostIntent) createWithResourceGroup();
162 assertThat("incorrect app id", intent.appId(), is(APPID));
163 assertThat("incorrect host one", intent.one(), is(id1));
164 assertThat("incorrect host two", intent.two(), is(id3));
165 assertThat("incorrect selector", intent.selector(), is(selector));
166 assertThat("incorrect treatment", intent.treatment(), is(treatment));
167 assertThat("incorrect resource group", intent.resourceGroup(), is(resourceGrouop));
168
169 }
170
Brian O'Connor520c0522014-11-23 23:50:47 -0800171 @Override
172 protected Intent createOne() {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700173 return HostToHostIntent.builder()
174 .appId(APPID)
175 .one(id1)
176 .two(id2)
177 .selector(selector)
178 .treatment(treatment)
179 .build();
Brian O'Connor520c0522014-11-23 23:50:47 -0800180 }
181
182 @Override
183 protected Intent createAnother() {
Ray Milkeyebc5d222015-03-18 15:45:36 -0700184 return HostToHostIntent.builder()
185 .appId(APPID)
186 .one(id1)
187 .two(id3)
188 .selector(selector)
189 .treatment(treatment)
190 .build();
Brian O'Connor520c0522014-11-23 23:50:47 -0800191 }
Luca Prete670ac5d2017-02-03 15:55:43 -0800192
193 protected Intent createWithResourceGroup() {
194 return HostToHostIntent.builder()
195 .appId(APPID)
196 .one(id1)
197 .two(id3)
198 .selector(selector)
199 .treatment(treatment)
200 .resourceGroup(resourceGrouop)
201 .build();
202 }
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800203}