blob: 2bdf200c53b259795e9c3e624cfee46364e4c5a2 [file] [log] [blame]
Yi Tsengc927a062017-05-02 15:02:37 -07001/*
2 * Copyright 2017-present 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 */
16
17package org.onosproject.net.intent.impl;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.junit.TestTools;
25import org.onosproject.TestApplicationId;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.IdGenerator;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.FilteredConnectPoint;
30import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentData;
32import org.onosproject.net.intent.IntentInstaller;
33import org.onosproject.net.intent.IntentOperationContext;
34import org.onosproject.net.intent.IntentState;
35import org.onosproject.net.intent.Key;
36import org.onosproject.net.intent.MockIdGenerator;
37import org.onosproject.net.intent.PointToPointIntent;
38import org.onosproject.net.intent.TestInstallableIntent;
39import org.onosproject.store.intent.impl.IntentStoreAdapter;
40import org.onosproject.store.service.WallClockTimestamp;
41
42import java.util.List;
43import java.util.Optional;
44import java.util.stream.IntStream;
45
46import static org.junit.Assert.*;
47
48/**
49 * Tests for install coordinator.
50 */
51public class InstallCoordinatorTest {
52 private static final int INSTALL_DELAY = 100;
53 private static final int INSTALL_DURATION = 1000;
54 private static final IdGenerator ID_GENERATOR = new MockIdGenerator();
55
56 private InstallCoordinator installCoordinator;
57 private InstallerRegistry installerRegistry;
58 private TestIntentStore intentStore;
59 private TestIntentInstaller intentInstaller;
60
61
62 @Before
63 public void setup() {
64 Intent.unbindIdGenerator(ID_GENERATOR);
65 Intent.bindIdGenerator(ID_GENERATOR);
66 installerRegistry = new InstallerRegistry();
67 intentStore = new TestIntentStore();
68 intentInstaller = new TestIntentInstaller();
69 installerRegistry.registerInstaller(TestInstallableIntent.class, intentInstaller);
70
71 installCoordinator = new InstallCoordinator(installerRegistry, intentStore);
72 }
73
74 @After
75 public void tearDown() {
76 installerRegistry.unregisterInstaller(TestInstallableIntent.class);
77 Intent.unbindIdGenerator(ID_GENERATOR);
78 }
79
80 /**
81 * Installs test Intents.
82 */
83 @Test
84 public void testInstallIntent() {
85 IntentData toInstall = new IntentData(createTestIntent(),
86 IntentState.INSTALLING,
87 new WallClockTimestamp());
88 List<Intent> intents = Lists.newArrayList();
89 IntStream.range(0, 10).forEach(val -> {
90 intents.add(new TestInstallableIntent(val));
91 });
92 toInstall = new IntentData(toInstall, intents);
93 installCoordinator.installIntents(Optional.empty(), Optional.of(toInstall));
94 Intent toInstallIntent = toInstall.intent();
95 TestTools.assertAfter(INSTALL_DELAY, INSTALL_DURATION, () -> {
96 IntentData newData = intentStore.newData;
97 assertEquals(toInstallIntent, newData.intent());
98 assertEquals(IntentState.INSTALLED, newData.state());
99 assertEquals(intents, newData.installables());
100 });
101 }
102
103 /**
104 * Uninstalls test Intents.
105 */
106 @Test
107 public void testUninstallIntent() {
108 IntentData toUninstall = new IntentData(createTestIntent(),
109 IntentState.WITHDRAWING,
110 new WallClockTimestamp());
111 List<Intent> intents = Lists.newArrayList();
112
113 IntStream.range(0, 10).forEach(val -> {
114 intents.add(new TestInstallableIntent(val));
115 });
116
117 toUninstall = new IntentData(toUninstall, intents);
118
119 installCoordinator.installIntents(Optional.of(toUninstall), Optional.empty());
120 Intent toUninstallIntent = toUninstall.intent();
121 TestTools.assertAfter(INSTALL_DELAY, INSTALL_DURATION, () -> {
122 IntentData newData = intentStore.newData;
123 assertEquals(toUninstallIntent, newData.intent());
124 assertEquals(IntentState.WITHDRAWN, newData.state());
125 assertEquals(ImmutableList.of(), newData.installables());
126 });
127
128 }
129
130 /**
131 * Do both uninstall and install test Intents.
132 */
133 @Test
134 public void testUninstallAndInstallIntent() {
135 IntentData toUninstall = new IntentData(createTestIntent(),
136 IntentState.INSTALLED,
137 new WallClockTimestamp());
138 IntentData toInstall = new IntentData(createTestIntent(),
139 IntentState.INSTALLING,
140 new WallClockTimestamp());
141 List<Intent> intentsToUninstall = Lists.newArrayList();
142 List<Intent> intentsToInstall = Lists.newArrayList();
143
144 IntStream.range(0, 10).forEach(val -> {
145 intentsToUninstall.add(new TestInstallableIntent(val));
146 });
147
148 IntStream.range(10, 20).forEach(val -> {
149 intentsToInstall.add(new TestInstallableIntent(val));
150 });
151
152 toUninstall = new IntentData(toUninstall, intentsToUninstall);
153 toInstall = new IntentData(toInstall, intentsToInstall);
154
155 installCoordinator.installIntents(Optional.of(toUninstall), Optional.of(toInstall));
156 Intent toInstallIntent = toInstall.intent();
157
158 TestTools.assertAfter(INSTALL_DELAY, INSTALL_DURATION, () -> {
159 IntentData newData = intentStore.newData;
160 assertEquals(toInstallIntent, newData.intent());
161 assertEquals(IntentState.INSTALLED, newData.state());
162 assertEquals(intentsToInstall, newData.installables());
163 });
164 }
165
166 /**
167 * Not uninstall nor install anything.
168 */
169 @Test
170 public void testInstallNothing() {
171 installCoordinator.installIntents(Optional.empty(), Optional.empty());
172 assertNull(intentStore.newData);
173 }
174
175 /**
176 * Test Intent install failed.
177 */
178 @Test
179 public void testInstallFailed() {
180 installerRegistry.unregisterInstaller(TestInstallableIntent.class);
181 installerRegistry.registerInstaller(TestInstallableIntent.class, new TestFailedIntentInstaller());
182 IntentData toUninstall = new IntentData(createTestIntent(),
183 IntentState.INSTALLED,
184 new WallClockTimestamp());
185 IntentData toInstall = new IntentData(createTestIntent(),
186 IntentState.INSTALLING,
187 new WallClockTimestamp());
188 List<Intent> intentsToUninstall = Lists.newArrayList();
189 List<Intent> intentsToInstall = Lists.newArrayList();
190
191 IntStream.range(0, 10).forEach(val -> {
192 intentsToUninstall.add(new TestInstallableIntent(val));
193 });
194
195 IntStream.range(10, 20).forEach(val -> {
196 intentsToInstall.add(new TestInstallableIntent(val));
197 });
198
199 toUninstall = new IntentData(toUninstall, intentsToUninstall);
200 toInstall = new IntentData(toInstall, intentsToInstall);
201
202 installCoordinator.installIntents(Optional.of(toUninstall), Optional.of(toInstall));
203
204 Intent toUninstallIntent = toUninstall.intent();
205 TestTools.assertAfter(INSTALL_DELAY, INSTALL_DURATION, () -> {
206 IntentData newData = intentStore.newData;
207 assertEquals(toUninstallIntent, newData.intent());
208 assertEquals(IntentState.CORRUPT, newData.state());
209 assertEquals(intentsToUninstall, newData.installables());
210 });
211 }
212
213 /**
214 * Creates a test Intent.
215 *
216 * @return the test Intent.
217 */
218 private PointToPointIntent createTestIntent() {
219 FilteredConnectPoint ingress = new FilteredConnectPoint(ConnectPoint.deviceConnectPoint("s1/1"));
220 FilteredConnectPoint egress = new FilteredConnectPoint(ConnectPoint.deviceConnectPoint("s1/2"));
221 ApplicationId appId = TestApplicationId.create("test App");
222 return PointToPointIntent.builder()
223 .filteredIngressPoint(ingress)
224 .filteredEgressPoint(egress)
225 .appId(appId)
226 .key(Key.of("Test key", appId))
227 .build();
228 }
229
230 /**
231 * Test Intent store; records the newest Intent data.
232 */
233 class TestIntentStore extends IntentStoreAdapter {
234 IntentData newData;
235 @Override
236 public void write(IntentData newData) {
237 this.newData = newData;
238 }
239 }
240
241 /**
242 * Test Intent installer; always success for every Intent operation.
243 */
244 class TestIntentInstaller implements IntentInstaller<TestInstallableIntent> {
245 @Override
246 public void apply(IntentOperationContext<TestInstallableIntent> context) {
247 installCoordinator.success(context);
248 }
249 }
250
251 /**
252 * Test Intent installer; always failed for every Intent operation.
253 */
254 class TestFailedIntentInstaller implements IntentInstaller<TestInstallableIntent> {
255 @Override
256 public void apply(IntentOperationContext<TestInstallableIntent> context) {
257 installCoordinator.failed(context);
258 }
259 }
260}