blob: 2542322600a9cb5006aef5ab4a6257c90eb12059 [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.installer;
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.onosproject.net.FilteredConnectPoint;
25import org.onosproject.net.domain.DomainIntent;
26import org.onosproject.net.domain.DomainIntentOperations;
27import org.onosproject.net.domain.DomainIntentService;
28import org.onosproject.net.domain.DomainPointToPointIntent;
29import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentData;
31import org.onosproject.net.intent.IntentInstallationContext;
32import org.onosproject.net.intent.IntentOperationContext;
33import org.onosproject.net.intent.IntentState;
34import org.onosproject.store.service.WallClockTimestamp;
35
36import java.util.List;
37
38import static org.junit.Assert.*;
39
40/**
41 * Tests for domain Intent installer.
42 */
43public class DomainIntentInstallerTest extends AbstractIntentInstallerTest {
44 protected DomainIntentInstaller installer;
45 protected TestDomainIntentService domainIntentService;
46
47 @Before
48 public void setup() {
49 super.setup();
50 domainIntentService = new TestDomainIntentService();
51 installer = new DomainIntentInstaller();
52 installer.domainIntentService = domainIntentService;
53 installer.trackerService = trackerService;
54 installer.intentExtensionService = intentExtensionService;
55 installer.intentInstallCoordinator = intentInstallCoordinator;
56
57 installer.activated();
58 }
59
60 @After
61 public void tearDown() {
62 super.tearDown();
63 installer.deactivated();
64 }
65
66 /**
67 * Installs domain Intents.
68 */
69 @Test
70 public void testInstall() {
71 List<Intent> intentsToUninstall = Lists.newArrayList();
72 List<Intent> intentsToInstall = createDomainIntents();
73 IntentData toUninstall = null;
74 IntentData toInstall = new IntentData(createP2PIntent(),
75 IntentState.INSTALLING,
76 new WallClockTimestamp());
77 toInstall = new IntentData(toInstall, intentsToInstall);
78 IntentOperationContext<DomainIntent> operationContext;
79 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
80 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
81 installer.apply(operationContext);
82 assertEquals(intentInstallCoordinator.successContext, operationContext);
83 }
84
85 /**
86 * Uninstall domain Intents.
87 */
88 @Test
89 public void testUninstall() {
90 List<Intent> intentsToUninstall = createDomainIntents();
91 List<Intent> intentsToInstall = Lists.newArrayList();
92 IntentData toUninstall = new IntentData(createP2PIntent(),
93 IntentState.WITHDRAWING,
94 new WallClockTimestamp());
95 IntentData toInstall = null;
96 toUninstall = new IntentData(toUninstall, intentsToUninstall);
97 IntentOperationContext<DomainIntent> operationContext;
98 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
99 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
100 installer.apply(operationContext);
101 assertEquals(intentInstallCoordinator.successContext, operationContext);
102 }
103
104 /**
105 * Do both uninstall and install domain Intents.
106 */
107 @Test
108 public void testUninstallAndInstall() {
109 List<Intent> intentsToUninstall = createDomainIntents();
110 List<Intent> intentsToInstall = createAnotherDomainIntents();
111 IntentData toUninstall = new IntentData(createP2PIntent(),
112 IntentState.INSTALLED,
113 new WallClockTimestamp());
114 toUninstall = new IntentData(toUninstall, intentsToUninstall);
115 IntentData toInstall = new IntentData(createP2PIntent(),
116 IntentState.INSTALLING,
117 new WallClockTimestamp());
118 toInstall = new IntentData(toInstall, intentsToInstall);
119 IntentOperationContext<DomainIntent> operationContext;
120 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
121 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
122 installer.apply(operationContext);
123 assertEquals(intentInstallCoordinator.successContext, operationContext);
124 }
125
126 /**
127 * Nothing to uninstall or install.
128 */
129 @Test
130 public void testNoAnyIntentToApply() {
131 IntentData toInstall = null;
132 IntentData toUninstall = null;
133 IntentOperationContext<DomainIntent> operationContext;
134 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
135 operationContext = new IntentOperationContext<>(ImmutableList.of(), ImmutableList.of(), context);
136 installer.apply(operationContext);
137 IntentOperationContext successContext = intentInstallCoordinator.successContext;
138 assertEquals(successContext, operationContext);
139 }
140
141 /**
142 * Test if domain Intent installation operations failed.
143 */
144 @Test
145 public void testInstallFailed() {
146 domainIntentService = new TestFailedDomainIntentService();
147 installer.domainIntentService = domainIntentService;
148 List<Intent> intentsToUninstall = Lists.newArrayList();
149 List<Intent> intentsToInstall = createDomainIntents();
150 IntentData toUninstall = null;
151 IntentData toInstall = new IntentData(createP2PIntent(),
152 IntentState.INSTALLING,
153 new WallClockTimestamp());
154 toInstall = new IntentData(toInstall, intentsToInstall);
155 IntentOperationContext<DomainIntent> operationContext;
156 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
157 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
158 installer.apply(operationContext);
159 assertEquals(intentInstallCoordinator.failedContext, operationContext);
160 }
161
162 /**
163 * Creates domain Intents.
164 *
165 * @return the domain Intents
166 */
167 private List<Intent> createDomainIntents() {
168 FilteredConnectPoint ingress = new FilteredConnectPoint(CP1);
169 FilteredConnectPoint egress = new FilteredConnectPoint(CP2);
170 DomainPointToPointIntent intent = DomainPointToPointIntent.builder()
171 .appId(APP_ID)
172 .key(KEY1)
173 .priority(DEFAULT_PRIORITY)
174 .filteredIngressPoint(ingress)
175 .filteredEgressPoint(egress)
176 .links(ImmutableList.of())
177 .build();
178
179 return ImmutableList.of(intent);
180 }
181
182 /**
183 * Create another domain Intents.
184 *
185 * @return the domain Intents
186 */
187 private List<Intent> createAnotherDomainIntents() {
188 FilteredConnectPoint ingress = new FilteredConnectPoint(CP1);
189 FilteredConnectPoint egress = new FilteredConnectPoint(CP3);
190 DomainPointToPointIntent intent = DomainPointToPointIntent.builder()
191 .appId(APP_ID)
192 .key(KEY1)
193 .priority(DEFAULT_PRIORITY)
194 .filteredIngressPoint(ingress)
195 .filteredEgressPoint(egress)
196 .links(ImmutableList.of())
197 .build();
198
199 return ImmutableList.of(intent);
200 }
201
202 /**
203 * Test domain Intent service; always success for all domain Intent operations.
204 */
205 class TestDomainIntentService implements DomainIntentService {
206 @Override
207 public void sumbit(DomainIntentOperations domainOperations) {
208 domainOperations.callback().onSuccess(domainOperations);
209 }
210 }
211
212 /**
213 * Test domain Intent service; always failed of any domain Intent operation.
214 */
215 class TestFailedDomainIntentService extends TestDomainIntentService {
216
217 @Override
218 public void sumbit(DomainIntentOperations domainOperations) {
219 domainOperations.callback().onError(domainOperations);
220 }
221 }
222
223}