2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021, 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.lifecycle;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertSame;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.drools.system.PolicyEngineConstants;
39 import org.onap.policy.models.pdp.concepts.PdpStateChange;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41 import org.onap.policy.models.pdp.concepts.PdpUpdate;
42 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
43 import org.onap.policy.models.pdp.enums.PdpMessageType;
44 import org.onap.policy.models.pdp.enums.PdpState;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 * Lifecycle State Passive Tests.
51 class LifecycleStatePassiveTest extends LifecycleStateRunningTest {
54 * Start tests in the Passive state.
57 public void startPassive() {
58 /* start every test in passive mode */
59 fsm = makeFsmWithPseudoTime();
60 fsm.setStatusTimerSeconds(15L);
66 assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStatePassive(null));
71 void testController() {
72 fsm.start(controllerSupport.getController());
73 assertSame(controllerSupport.getController(),
74 ((PolicyTypeDroolsController) fsm.getController(
75 new ToscaConceptIdentifier(
76 ControllerSupport.POLICY_TYPE_COMPLIANT_OP, ControllerSupport.POLICY_TYPE_VERSION)))
77 .controllers().get(0));
79 fsm.stop(controllerSupport.getController());
80 assertNull(fsm.getController(new ToscaConceptIdentifier(ControllerSupport.POLICY_TYPE_COMPLIANT_OP,
81 ControllerSupport.POLICY_TYPE_VERSION)));
88 assertEquals(0, fsm.client.getSink().getRecentEvents().length);
89 assertFalse(fsm.start());
98 assertBasicTerminated();
101 private void simpleStart() {
102 assertTrue(fsm.start());
103 assertBasicPassive();
106 private void simpleStop() {
107 assertTrue(fsm.stop());
108 assertBasicTerminated();
112 void testShutdown() throws Exception {
116 assertExtendedTerminated();
121 assertTrue(fsm.client.getSink().isAlive());
122 assertTrue(fsm.status());
123 assertSame(1, fsm.client.getSink().getRecentEvents().length);
126 fsm.start(controllerSupport.getController());
128 fsm.stop(controllerSupport.getController());
132 private void status() {
133 waitUntil(5, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1));
134 waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1 + 1));
135 waitUntil(fsm.statusTimerSeconds + 2, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE, 1 + 2));
136 assertTrue(fsm.status());
137 waitUntil(200, TimeUnit.MILLISECONDS, isStatus(PdpState.PASSIVE, 1 + 3));
141 void testUpdate() throws CoderException {
142 controllerSupport.getController().getDrools().delete(ToscaPolicy.class);
143 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
145 PdpUpdate update = new PdpUpdate();
146 update.setName(PolicyEngineConstants.getManager().getPdpName());
147 update.setPdpGroup("Z");
148 update.setPdpSubgroup("z");
150 long interval = 2 * fsm.getStatusTimerSeconds();
151 update.setPdpHeartbeatIntervalMs(interval * 1000L);
153 assertTrue(fsm.update(update));
155 int qlength = fsm.client.getSink().getRecentEvents().length;
156 PdpStatus lastStatus = new StandardCoder().decode(fsm.client.getSink().getRecentEvents()[qlength - 1],
158 assertEquals("foo", lastStatus.getPdpType());
159 assertEquals(update.getRequestId(), lastStatus.getRequestId());
160 assertEquals(update.getRequestId(), lastStatus.getResponse().getResponseTo());
162 assertEquals(PdpState.PASSIVE, fsm.state());
163 assertEquals(interval, fsm.getStatusTimerSeconds());
164 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
165 assertEquals("z", fsm.getSubGroup());
166 assertBasicPassive();
168 ToscaPolicy toscaPolicy =
169 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
170 toscaPolicy.getProperties().put("controllerName", "lifecycle");
171 update.setPoliciesToBeDeployed(List.of(toscaPolicy));
173 assertFalse(fsm.update(update));
175 assertEquals(PdpState.PASSIVE, fsm.state());
176 assertEquals(interval, fsm.getStatusTimerSeconds());
177 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
178 assertEquals("z", fsm.getSubGroup());
179 assertBasicPassive();
181 assertEquals(2, fsm.policyTypesMap.size());
182 assertTrue(fsm.policiesMap.isEmpty());
184 update.setPdpGroup(null);
185 update.setPdpSubgroup(null);
187 assertFalse(fsm.update(update));
189 assertEquals(PdpState.PASSIVE, fsm.state());
190 assertEquals(interval, fsm.getStatusTimerSeconds());
191 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
192 assertNull(fsm.getSubGroup());
193 assertBasicPassive();
194 assertEquals(2, fsm.policyTypesMap.size());
195 assertTrue(fsm.policiesMap.isEmpty());
197 update.setPdpGroup("A");
198 update.setPdpSubgroup("a");
200 assertFalse(fsm.update(update));
202 assertEquals(PdpState.PASSIVE, fsm.state());
203 assertEquals(interval, fsm.getStatusTimerSeconds());
204 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
205 assertEquals("a", fsm.getSubGroup());
206 assertBasicPassive();
207 assertEquals(2, fsm.policyTypesMap.size());
208 assertTrue(fsm.policiesMap.isEmpty());
210 fsm.start(controllerSupport.getController());
211 assertEquals(3, fsm.policyTypesMap.size());
212 assertTrue(fsm.policiesMap.isEmpty());
214 assertTrue(fsm.update(update));
215 assertEquals(3, fsm.policyTypesMap.size());
216 assertEquals(1, fsm.policiesMap.size());
217 assertEquals(fsm.policiesMap.get(toscaPolicy.getIdentifier()), toscaPolicy);
218 assertEquals(PdpState.PASSIVE, fsm.state());
219 assertEquals(interval, fsm.getStatusTimerSeconds());
220 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
221 assertEquals("a", fsm.getSubGroup());
222 assertBasicPassive();
223 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
225 // The "update" event will undeploy "toscaPolicy" and deploy "toscaPolicy2"
226 ToscaPolicy toscaPolicy2 =
227 getExamplesPolicy("policies/vFirewall.policy.operational.input.tosca.json", "operational.modifyconfig");
228 toscaPolicy.getProperties().remove("controllerName");
229 update.setPoliciesToBeUndeployed(List.of(toscaPolicy.getIdentifier()));
230 update.setPoliciesToBeDeployed(List.of(toscaPolicy2));
231 assertTrue(fsm.update(update));
232 assertEquals(3, fsm.policyTypesMap.size());
233 assertEquals(1, fsm.policiesMap.size());
234 assertEquals(toscaPolicy2, fsm.policiesMap.get(toscaPolicy2.getIdentifier()));
235 assertNull(fsm.policiesMap.get(toscaPolicy.getIdentifier()));
236 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
238 update.setPdpGroup(null);
239 update.setPdpSubgroup(null);
240 update.setPoliciesToBeUndeployed(List.of(toscaPolicy2.getIdentifier()));
241 update.setPoliciesToBeDeployed(List.of());
242 assertTrue(fsm.update(update));
243 assertEquals(3, fsm.policyTypesMap.size());
244 assertEquals(0, fsm.policiesMap.size());
245 assertEquals(PdpState.PASSIVE, fsm.state());
246 assertEquals(interval, fsm.getStatusTimerSeconds());
247 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
248 assertNull(fsm.getSubGroup());
249 assertBasicPassive();
250 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
256 void testStateChange() throws CoderException {
258 PdpStateChange change = new PdpStateChange();
259 change.setPdpGroup("A");
260 change.setPdpSubgroup("a");
261 change.setState(PdpState.ACTIVE);
264 change.setName("test");
265 fsm.source.offer(new StandardCoder().encode(change));
267 assertEquals(PdpState.PASSIVE, fsm.state());
268 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
269 assertNull(fsm.getSubGroup());
271 PdpUpdate update = new PdpUpdate();
272 update.setName(PolicyEngineConstants.getManager().getPdpName());
273 update.setPdpGroup("A");
274 update.setPdpSubgroup("a");
276 ToscaPolicy toscaPolicy =
277 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
278 toscaPolicy.getProperties().put("controllerName", "lifecycle");
279 update.setPoliciesToBeDeployed(List.of(toscaPolicy));
281 controllerSupport.getController().start();
282 fsm.start(controllerSupport.getController());
283 assertEquals(3, fsm.policyTypesMap.size());
284 assertTrue(fsm.policiesMap.isEmpty());
286 assertTrue(fsm.update(update));
287 assertEquals(3, fsm.policyTypesMap.size());
288 assertEquals(1, fsm.policiesMap.size());
289 assertEquals(fsm.policiesMap.get(toscaPolicy.getIdentifier()), toscaPolicy);
290 assertEquals(PdpState.PASSIVE, fsm.state());
291 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
292 assertEquals("a", fsm.getSubGroup());
293 assertBasicPassive();
294 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
297 change.setName(fsm.getPdpName());
298 fsm.source.offer(new StandardCoder().encode(change));
300 assertEquals(PdpState.ACTIVE, fsm.state());
301 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
302 assertEquals("a", fsm.getSubGroup());
304 waitUntil(5, TimeUnit.SECONDS, () -> controllerSupport.getController().getDrools().factCount("junits") == 1);
306 assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));
307 assertEquals(0, controllerSupport.getController().getDrools().factCount("junits"));
312 private void assertBasicTerminated() {
313 assertEquals(PdpState.TERMINATED, fsm.state.state());
314 assertFalse(fsm.isAlive());
315 assertFalse(fsm.state.isAlive());
318 private void assertExtendedTerminated() throws Exception {
319 assertBasicTerminated();
320 assertTrue(fsm.statusTask.isCancelled());
321 assertTrue(fsm.statusTask.isDone());
323 // verify there are no outstanding tasks that might change the state
324 assertTrue(time.isEmpty());
326 assertFalse(fsm.client.getSink().isAlive());
328 String[] events = fsm.client.getSink().getRecentEvents();
329 PdpStatus status = new StandardCoder().decode(events[events.length - 1], PdpStatus.class);
330 assertEquals("foo", status.getPdpType());
331 assertEquals(PdpState.TERMINATED, status.getState());
332 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
333 assertEquals(PolicyEngineConstants.getManager().getPdpName(), status.getName());
334 assertEquals(fsm.getPdpName(), status.getName());
335 assertEquals(PdpMessageType.PDP_STATUS, status.getMessageName());
338 private void assertBasicPassive() {
339 assertEquals(PdpState.PASSIVE, fsm.state.state());
340 assertNotNull(fsm.source);
341 assertNotNull(fsm.client);
342 assertNotNull(fsm.statusTask);
344 assertTrue(fsm.isAlive());
345 assertTrue(fsm.source.isAlive());
346 assertTrue(fsm.client.getSink().isAlive());
348 assertFalse(fsm.statusTask.isCancelled());
349 assertFalse(fsm.statusTask.isDone());