2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021, 2024 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
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.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNotNull;
29 import static org.junit.jupiter.api.Assertions.assertNull;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import static org.onap.policy.common.message.bus.properties.MessageBusProperties.PROPERTY_NOOP_SINK_TOPICS;
32 import static org.onap.policy.common.message.bus.properties.MessageBusProperties.PROPERTY_NOOP_SOURCE_TOPICS;
34 import java.io.IOException;
35 import java.util.List;
37 import java.util.Properties;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.onap.policy.common.message.bus.event.TopicEndpointManager;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.drools.server.restful.TestConstants;
45 import org.onap.policy.drools.system.PolicyEngineConstants;
46 import org.onap.policy.models.pdp.concepts.PdpStateChange;
47 import org.onap.policy.models.pdp.concepts.PdpUpdate;
48 import org.onap.policy.models.pdp.enums.PdpState;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53 * Lifecycle State Active Test.
55 class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest {
57 private static final String EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "example.controller";
58 private static final String EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME = "example.artifact";
59 private static final String FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "foo.controller";
61 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
62 "src/test/resources/tosca-policy-native-controller-example.json";
63 private static final String EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON =
64 "src/test/resources/tosca-policy-native-artifact-example.json";
65 private static final String FOO_NATIVE_DROOLS_POLICY_JSON =
66 "src/test/resources/tosca-policy-native-controller-foo.json";
69 * Start tests in the Active state.
72 public void startActive() throws CoderException {
73 fsm = makeFsmWithPseudoTime();
75 fsm.setStatusTimerSeconds(15);
76 assertTrue(fsm.start());
78 PdpStateChange change = new PdpStateChange();
79 change.setPdpGroup("A");
80 change.setPdpSubgroup("a");
81 change.setState(PdpState.ACTIVE);
82 change.setName(fsm.getPdpName());
85 fsm.source.offer(new StandardCoder().encode(change));
86 controllerSupport.getController().start();
90 void testMandatoryPolicyTypes() {
91 assertEquals(Set.of("onap.policies.native.drools.Artifact", "onap.policies.native.drools.Controller"),
92 fsm.getMandatoryPolicyTypes());
93 assertEquals(fsm.getMandatoryPolicyTypes(), fsm.getCurrentPolicyTypes());
94 assertTrue(fsm.isMandatoryPolicyTypesCompliant());
95 assertTrue(fsm.status());
97 fsm.mandatoryPolicyTypes.add("blah");
98 assertEquals(Set.of("onap.policies.native.drools.Artifact", "onap.policies.native.drools.Controller", "blah"),
99 fsm.getMandatoryPolicyTypes());
100 assertNotEquals(fsm.getMandatoryPolicyTypes(), fsm.getCurrentPolicyTypes());
101 assertFalse(fsm.isMandatoryPolicyTypesCompliant());
102 assertFalse(fsm.status());
106 void testUpdatePolicies() throws IOException, CoderException {
107 assertEquals(2, fsm.policyTypesMap.size());
108 assertNotNull(fsm.getPolicyTypesMap().get(
109 new ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
110 assertNotNull(fsm.getPolicyTypesMap().get(
111 new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
114 // create controller using native policy
117 ToscaPolicy policyNativeController =
118 getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
120 PdpUpdate update = new PdpUpdate();
121 update.setName(PolicyEngineConstants.getManager().getPdpName());
122 update.setPdpGroup("W");
123 update.setPdpSubgroup("w");
124 update.setPoliciesToBeDeployed(List.of(policyNativeController));
126 assertFalse(fsm.update(update));
127 assertEquals(0, fsm.getPoliciesMap().size());
131 Properties noopTopicProperties = new Properties();
132 noopTopicProperties.put(PROPERTY_NOOP_SOURCE_TOPICS, TestConstants.DCAE_TOPIC);
133 noopTopicProperties.put(PROPERTY_NOOP_SINK_TOPICS, TestConstants.APPC_CL_TOPIC);
134 TopicEndpointManager.getManager().addTopics(noopTopicProperties);
136 assertTrue(fsm.update(update));
137 assertEquals(1, fsm.getPoliciesMap().size());
140 // add an artifact policy
143 ToscaPolicy policyNativeArtifact =
144 getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME);
146 @SuppressWarnings("unchecked")
147 Map<String, String> controllerMap =
148 (Map<String, String>) policyNativeArtifact.getProperties().get("controller");
149 controllerMap.put("name", "xyz987");
150 update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact));
151 assertFalse(fsm.update(update));
153 // add a registered controller
155 controllerMap.put("name", "lifecycle");
156 update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact));
157 assertTrue(fsm.update(update));
159 assertEquals(2, fsm.getPoliciesMap().size());
160 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
161 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
164 // add an operational policy
167 ToscaPolicy opPolicyRestart =
168 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
169 update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact, opPolicyRestart));
170 assertFalse(fsm.update(update));
172 assertEquals(2, fsm.getPoliciesMap().size());
173 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
174 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
176 // register controller
178 fsm.start(controllerSupport.getController());
180 assertEquals(3, fsm.policyTypesMap.size());
181 assertNotNull(fsm.getPolicyTypesMap().get(
182 new ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
183 assertNotNull(fsm.getPolicyTypesMap().get(
184 new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
185 assertNotNull(fsm.getPolicyTypesMap().get(
186 new ToscaConceptIdentifier("onap.policies.controlloop.operational.common.Drools",
189 // invalid controller name
191 opPolicyRestart.getProperties().put("controllerName", "xyz987");
192 assertFalse(fsm.update(update));
193 assertEquals(2, fsm.getPoliciesMap().size());
194 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
195 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
197 // no controller name
199 opPolicyRestart.getProperties().remove("controllerName");
200 assertTrue(fsm.update(update));
201 assertEquals(3, fsm.getPoliciesMap().size());
202 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
203 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
204 assertEquals(opPolicyRestart, fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
206 List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
207 assertEquals(1, factPolicies.size());
208 assertEquals(opPolicyRestart, factPolicies.get(0));
210 // upgrade operational policy with valid controller name
212 ToscaPolicy opPolicyRestartV2 =
213 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
214 opPolicyRestartV2.setVersion("2.0.0");
215 opPolicyRestartV2.getProperties().put("controllerName", "lifecycle");
216 update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
217 update.setPoliciesToBeUndeployed(List.of(opPolicyRestart.getIdentifier()));
218 assertTrue(fsm.update(update));
220 assertEquals(3, fsm.getPoliciesMap().size());
221 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
222 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
223 assertEquals(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
224 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
226 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
227 assertEquals(1, factPolicies.size());
228 assertEquals(opPolicyRestartV2, factPolicies.get(0));
230 update.setPoliciesToBeDeployed(List.of());
231 update.setPoliciesToBeUndeployed(List.of(opPolicyRestartV2.getIdentifier()));
232 assertTrue(fsm.update(update));
234 assertEquals(2, fsm.getPoliciesMap().size());
235 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
236 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
237 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
238 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
240 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
241 assertEquals(0, factPolicies.size());
242 assertTrue(controllerSupport.getController().getDrools().isBrained());
244 update.setPoliciesToBeDeployed(List.of());
245 update.setPoliciesToBeUndeployed(List.of(policyNativeArtifact.getIdentifier(),
246 opPolicyRestartV2.getIdentifier()));
247 assertTrue(fsm.update(update));
248 assertFalse(controllerSupport.getController().getDrools().isBrained());
249 assertEquals(1, fsm.getPoliciesMap().size());
250 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
251 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
252 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
253 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
255 ToscaPolicy policyNativeFooController =
256 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
257 update.setPoliciesToBeUndeployed(List.of());
258 update.setPoliciesToBeDeployed(List.of(policyNativeFooController));
259 assertTrue(fsm.update(update));
260 assertEquals(2, fsm.getPoliciesMap().size());
261 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
262 assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
264 update.setPoliciesToBeDeployed(List.of());
265 update.setPoliciesToBeUndeployed(List.of(policyNativeController.getIdentifier(),
266 policyNativeFooController.getIdentifier()));
267 assertTrue(fsm.update(update));
268 assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
269 assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
270 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
271 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
272 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));