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