2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * =============LICENSE_END========================================================
21 package org.onap.policy.drools.lifecycle;
23 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Objects;
38 import java.util.concurrent.TimeUnit;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.models.pdp.concepts.PdpStateChange;
45 import org.onap.policy.models.pdp.concepts.PdpStatus;
46 import org.onap.policy.models.pdp.concepts.PdpUpdate;
47 import org.onap.policy.models.pdp.enums.PdpState;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
52 * Lifecycle State Active Test.
54 public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
57 * Start tests in the Active state.
60 public void startActive() throws CoderException {
61 fsm = makeFsmWithPseudoTime();
63 fsm.setStatusTimerSeconds(15);
64 assertTrue(fsm.start());
70 private void goActive() throws CoderException {
71 PdpStateChange change = new PdpStateChange();
72 change.setPdpGroup("A");
73 change.setPdpSubgroup("a");
74 change.setState(PdpState.ACTIVE);
75 change.setName(fsm.getName());
77 fsm.setSubGroupAction("a");
78 fsm.source.offer(new StandardCoder().encode(change));
79 controllerSupport.getController().start();
83 public void constructor() {
84 assertThatIllegalArgumentException().isThrownBy(() -> new LifecycleStateActive(null));
89 public void testStart() {
91 assertFalse(fsm.start());
97 private void assertActive() {
98 assertEquals(PdpState.ACTIVE, fsm.state());
99 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
100 assertEquals("a", fsm.getSubgroup());
101 assertTrue(fsm.isAlive());
102 waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
106 public void testStop() {
107 assertTrue(fsm.stop());
108 assertBasicTerminated();
113 private void assertBasicTerminated() {
114 assertEquals(PdpState.TERMINATED, fsm.state());
115 assertFalse(fsm.isAlive());
116 assertFalse(fsm.state.isAlive());
117 waitUntil(1, TimeUnit.SECONDS, isStatus(PdpState.TERMINATED));
121 public void testShutdown() {
124 assertBasicTerminated();
126 assertTrue(fsm.statusTask.isCancelled());
127 assertTrue(fsm.statusTask.isDone());
131 public void testStatus() {
132 waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
133 int preCount = fsm.client.getSink().getRecentEvents().length;
135 assertTrue(fsm.status());
136 assertEquals(preCount + 1, fsm.client.getSink().getRecentEvents().length);
138 fsm.start(controllerSupport.getController());
139 assertTrue(fsm.status());
140 assertEquals(preCount + 2, fsm.client.getSink().getRecentEvents().length);
142 fsm.stop(controllerSupport.getController());
147 public void testStateChange() throws CoderException {
150 /* no name and mismatching group info */
151 PdpStateChange change = new PdpStateChange();
152 change.setPdpGroup("B");
153 change.setPdpSubgroup("b");
154 change.setState(PdpState.ACTIVE);
156 fsm.source.offer(new StandardCoder().encode(change));
157 assertEquals(PdpState.ACTIVE, fsm.state());
158 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
159 assertNotEquals("b", fsm.getSubgroup());
161 change.setName(fsm.getName());
162 fsm.source.offer(new StandardCoder().encode(change));
163 assertEquals(PdpState.ACTIVE, fsm.state());
164 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
165 assertEquals("a", fsm.getSubgroup());
167 change.setState(PdpState.SAFE);
168 fsm.source.offer(new StandardCoder().encode(change));
169 assertEquals(PdpState.ACTIVE, fsm.state());
171 change.setState(PdpState.TERMINATED);
172 fsm.source.offer(new StandardCoder().encode(change));
173 assertEquals(PdpState.ACTIVE, fsm.state());
175 change.setState(PdpState.PASSIVE);
176 fsm.source.offer(new StandardCoder().encode(change));
177 assertEquals(PdpState.PASSIVE, fsm.state());
178 waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE));
184 public void testUpdate() throws IOException, CoderException {
186 // TODO: extract repeated similar assertion blocks into their own helper methods
188 PdpUpdate update = new PdpUpdate();
189 update.setName(NetworkUtil.getHostname());
190 update.setPdpGroup("W");
191 update.setPdpSubgroup("w");
192 update.setPolicies(Collections.emptyList());
194 fsm.start(controllerSupport.getController());
195 assertTrue(fsm.update(update));
197 assertEquals(PdpState.ACTIVE, fsm.state());
198 assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
199 assertEquals("w", fsm.getSubgroup());
202 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
203 ToscaPolicy toscaPolicyRestartV1 = new StandardCoder().decode(restartV1, ToscaPolicy.class);
204 update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
206 int qlength = fsm.client.getSink().getRecentEvents().length;
208 // update with an operational.restart policy
210 assertTrue(fsm.update(update));
211 assertEquals(qlength + 1, fsm.client.getSink().getRecentEvents().length);
212 assertEquals(4, fsm.policyTypesMap.size());
213 assertNotNull(fsm.getPolicyTypesMap().get(
214 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
215 assertNotNull(fsm.getPolicyTypesMap().get(
216 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
217 assertNotNull(fsm.getPolicyTypesMap().get(
218 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0")));
219 assertNotNull(fsm.getPolicyTypesMap().get(
220 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
222 PdpStatus cachedStatus = new StandardCoder()
223 .decode(fsm.client.getSink().getRecentEvents()[qlength], PdpStatus.class);
224 assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
226 List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
227 assertEquals(1, factPolicies.size());
228 assertEquals(toscaPolicyRestartV1, factPolicies.get(0));
229 assertEquals(1, fsm.policiesMap.size());
231 // dup update with the same operational.restart policy - nothing changes
233 assertTrue(fsm.update(update));
234 assertEquals(qlength + 2, fsm.client.getSink().getRecentEvents().length);
235 assertEquals(4, fsm.policyTypesMap.size());
236 cachedStatus = new StandardCoder()
237 .decode(fsm.client.getSink().getRecentEvents()[qlength + 1], PdpStatus.class);
238 assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
241 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
242 assertEquals(1, factPolicies.size());
243 assertEquals(toscaPolicyRestartV1, factPolicies.get(0));
244 assertEquals(1, fsm.policiesMap.size());
246 // undeploy operational.restart policy
248 update.setPolicies(Collections.emptyList());
249 assertTrue(fsm.update(update));
250 assertEquals(qlength + 3, fsm.client.getSink().getRecentEvents().length);
251 assertEquals(4, fsm.policyTypesMap.size());
252 cachedStatus = new StandardCoder()
253 .decode(fsm.client.getSink().getRecentEvents()[qlength + 2], PdpStatus.class);
254 assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
256 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
257 assertEquals(0, factPolicies.size());
258 assertEquals(0, fsm.policiesMap.size());
260 // redeploy operational.restart policy
262 update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
263 assertTrue(fsm.update(update));
264 assertEquals(qlength + 4, fsm.client.getSink().getRecentEvents().length);
265 assertEquals(4, fsm.policyTypesMap.size());
266 cachedStatus = new StandardCoder()
267 .decode(fsm.client.getSink().getRecentEvents()[qlength + 3], PdpStatus.class);
268 assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
270 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
271 assertEquals(1, factPolicies.size());
272 assertEquals(toscaPolicyRestartV1, factPolicies.get(0));
273 assertEquals(1, fsm.policiesMap.size());
275 // deploy a new version of the operational.restart policy
278 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.v2.json")));
279 ToscaPolicy toscaPolicyRestartV2 = new StandardCoder().decode(restartV2, ToscaPolicy.class);
280 update.setPolicies(Arrays.asList(toscaPolicyRestartV2));
281 assertTrue(fsm.update(update));
282 assertEquals(qlength + 5, fsm.client.getSink().getRecentEvents().length);
283 assertEquals(4, fsm.policyTypesMap.size());
284 cachedStatus = new StandardCoder()
285 .decode(fsm.client.getSink().getRecentEvents()[qlength + 4], PdpStatus.class);
286 assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
288 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
289 assertEquals(1, factPolicies.size());
290 assertNotEquals(toscaPolicyRestartV1, factPolicies.get(0));
291 assertEquals(toscaPolicyRestartV2, factPolicies.get(0));
292 assertEquals(1, fsm.policiesMap.size());
294 // deploy another policy : firewall
297 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-firewall.json")));
298 ToscaPolicy toscaPolicyFirewall = new StandardCoder().decode(firewall, ToscaPolicy.class);
299 update.setPolicies(Arrays.asList(toscaPolicyRestartV2, toscaPolicyFirewall));
300 assertTrue(fsm.update(update));
301 assertEquals(qlength + 6, fsm.client.getSink().getRecentEvents().length);
302 assertEquals(4, fsm.policyTypesMap.size());
303 cachedStatus = new StandardCoder()
304 .decode(fsm.client.getSink().getRecentEvents()[qlength + 5], PdpStatus.class);
305 assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
307 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
308 assertEquals(2, factPolicies.size());
309 assertTrue(factPolicies.stream().noneMatch((ff) -> Objects.equals(toscaPolicyRestartV1, ff)));
310 assertTrue(factPolicies.stream().anyMatch((ff) -> Objects.equals(toscaPolicyRestartV2, ff)));
311 assertTrue(factPolicies.stream().anyMatch((ff) -> Objects.equals(toscaPolicyFirewall, ff)));
312 assertEquals(2, fsm.policiesMap.size());
314 long originalInterval = fsm.getStatusTimerSeconds();
315 long interval = 10 * originalInterval;
316 update.setPdpHeartbeatIntervalMs(interval * 1000L);
318 assertTrue(fsm.update(update));
320 assertEquals(PdpState.ACTIVE, fsm.state());
321 assertEquals(interval, fsm.getStatusTimerSeconds());
323 assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));