Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / LifecycleStateActivePoliciesTest.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * =============LICENSE_END========================================================
20  */
21
22 package org.onap.policy.drools.lifecycle;
23
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
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Properties;
36 import java.util.Set;
37 import org.junit.jupiter.api.BeforeEach;
38 import org.junit.jupiter.api.Test;
39 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
40 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.drools.server.restful.TestConstants;
44 import org.onap.policy.drools.system.PolicyEngineConstants;
45 import org.onap.policy.models.pdp.concepts.PdpStateChange;
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.ToscaConceptIdentifier;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50
51 /**
52  * Lifecycle State Active Test.
53  */
54 class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest {
55
56     private static final String EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "example.controller";
57     private static final String EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME = "example.artifact";
58     private static final String FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "foo.controller";
59
60     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
61             "src/test/resources/tosca-policy-native-controller-example.json";
62     private static final String EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON =
63             "src/test/resources/tosca-policy-native-artifact-example.json";
64     private static final String FOO_NATIVE_DROOLS_POLICY_JSON =
65             "src/test/resources/tosca-policy-native-controller-foo.json";
66
67     /**
68      * Start tests in the Active state.
69      */
70     @BeforeEach
71     public void startActive() throws CoderException {
72         fsm = makeFsmWithPseudoTime();
73
74         fsm.setStatusTimerSeconds(15);
75         assertTrue(fsm.start());
76
77         PdpStateChange change = new PdpStateChange();
78         change.setPdpGroup("A");
79         change.setPdpSubgroup("a");
80         change.setState(PdpState.ACTIVE);
81         change.setName(fsm.getPdpName());
82
83         fsm.setSubGroup("a");
84         fsm.source.offer(new StandardCoder().encode(change));
85         controllerSupport.getController().start();
86     }
87
88     @Test
89     void testMandatoryPolicyTypes() {
90         assertEquals(Set.of("onap.policies.native.drools.Artifact", "onap.policies.native.drools.Controller"),
91             fsm.getMandatoryPolicyTypes());
92         assertEquals(fsm.getMandatoryPolicyTypes(), fsm.getCurrentPolicyTypes());
93         assertTrue(fsm.isMandatoryPolicyTypesCompliant());
94         assertTrue(fsm.status());
95
96         fsm.mandatoryPolicyTypes.add("blah");
97         assertEquals(Set.of("onap.policies.native.drools.Artifact", "onap.policies.native.drools.Controller", "blah"),
98                 fsm.getMandatoryPolicyTypes());
99         assertNotEquals(fsm.getMandatoryPolicyTypes(), fsm.getCurrentPolicyTypes());
100         assertFalse(fsm.isMandatoryPolicyTypesCompliant());
101         assertFalse(fsm.status());
102     }
103
104     @Test
105     void testUpdatePolicies() throws IOException, CoderException {
106         assertEquals(2, fsm.policyTypesMap.size());
107         assertNotNull(fsm.getPolicyTypesMap().get(
108                 new ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
109         assertNotNull(fsm.getPolicyTypesMap().get(
110                 new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
111
112         //
113         // create controller using native policy
114         //
115
116         ToscaPolicy policyNativeController =
117             getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
118
119         PdpUpdate update = new PdpUpdate();
120         update.setName(PolicyEngineConstants.getManager().getPdpName());
121         update.setPdpGroup("W");
122         update.setPdpSubgroup("w");
123         update.setPoliciesToBeDeployed(List.of(policyNativeController));
124
125         assertFalse(fsm.update(update));
126         assertEquals(0, fsm.getPoliciesMap().size());
127
128         // add topics
129
130         Properties noopTopicProperties = new Properties();
131         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, TestConstants.DCAE_TOPIC);
132         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, TestConstants.APPC_CL_TOPIC);
133         TopicEndpointManager.getManager().addTopics(noopTopicProperties);
134
135         assertTrue(fsm.update(update));
136         assertEquals(1, fsm.getPoliciesMap().size());
137
138         //
139         // add an artifact policy
140         //
141
142         ToscaPolicy policyNativeArtifact =
143                 getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME);
144
145         @SuppressWarnings("unchecked")
146         Map<String, String> controllerMap =
147                 (Map<String, String>) policyNativeArtifact.getProperties().get("controller");
148         controllerMap.put("name", "xyz987");
149         update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact));
150         assertFalse(fsm.update(update));
151
152         // add a registered controller
153
154         controllerMap.put("name", "lifecycle");
155         update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact));
156         assertTrue(fsm.update(update));
157
158         assertEquals(2, fsm.getPoliciesMap().size());
159         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
160         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
161
162         //
163         // add an operational policy
164         //
165
166         ToscaPolicy opPolicyRestart =
167             getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
168         update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact, opPolicyRestart));
169         assertFalse(fsm.update(update));
170
171         assertEquals(2, fsm.getPoliciesMap().size());
172         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
173         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
174
175         // register controller
176
177         fsm.start(controllerSupport.getController());
178
179         assertEquals(3, fsm.policyTypesMap.size());
180         assertNotNull(fsm.getPolicyTypesMap().get(
181                 new ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
182         assertNotNull(fsm.getPolicyTypesMap().get(
183                 new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
184         assertNotNull(fsm.getPolicyTypesMap().get(
185                 new ToscaConceptIdentifier("onap.policies.controlloop.operational.common.Drools",
186                         "1.0.0")));
187
188         // invalid controller name
189
190         opPolicyRestart.getProperties().put("controllerName", "xyz987");
191         assertFalse(fsm.update(update));
192         assertEquals(2, fsm.getPoliciesMap().size());
193         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
194         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
195
196         // no controller name
197
198         opPolicyRestart.getProperties().remove("controllerName");
199         assertTrue(fsm.update(update));
200         assertEquals(3, fsm.getPoliciesMap().size());
201         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
202         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
203         assertEquals(opPolicyRestart, fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
204
205         List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
206         assertEquals(1, factPolicies.size());
207         assertEquals(opPolicyRestart, factPolicies.get(0));
208
209         // upgrade operational policy with valid controller name
210
211         ToscaPolicy opPolicyRestartV2 =
212             getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
213         opPolicyRestartV2.setVersion("2.0.0");
214         opPolicyRestartV2.getProperties().put("controllerName", "lifecycle");
215         update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
216         update.setPoliciesToBeUndeployed(List.of(opPolicyRestart.getIdentifier()));
217         assertTrue(fsm.update(update));
218
219         assertEquals(3, fsm.getPoliciesMap().size());
220         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
221         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
222         assertEquals(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
223         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
224
225         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
226         assertEquals(1, factPolicies.size());
227         assertEquals(opPolicyRestartV2, factPolicies.get(0));
228
229         update.setPoliciesToBeDeployed(List.of());
230         update.setPoliciesToBeUndeployed(List.of(opPolicyRestartV2.getIdentifier()));
231         assertTrue(fsm.update(update));
232
233         assertEquals(2, fsm.getPoliciesMap().size());
234         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
235         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
236         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
237         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
238
239         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
240         assertEquals(0, factPolicies.size());
241         assertTrue(controllerSupport.getController().getDrools().isBrained());
242
243         update.setPoliciesToBeDeployed(List.of());
244         update.setPoliciesToBeUndeployed(List.of(policyNativeArtifact.getIdentifier(),
245                 opPolicyRestartV2.getIdentifier()));
246         assertTrue(fsm.update(update));
247         assertFalse(controllerSupport.getController().getDrools().isBrained());
248         assertEquals(1, fsm.getPoliciesMap().size());
249         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
250         assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
251         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
252         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
253
254         ToscaPolicy policyNativeFooController =
255                 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
256         update.setPoliciesToBeUndeployed(List.of());
257         update.setPoliciesToBeDeployed(List.of(policyNativeFooController));
258         assertTrue(fsm.update(update));
259         assertEquals(2, fsm.getPoliciesMap().size());
260         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
261         assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
262
263         update.setPoliciesToBeDeployed(List.of());
264         update.setPoliciesToBeUndeployed(List.of(policyNativeController.getIdentifier(),
265                 policyNativeFooController.getIdentifier()));
266         assertTrue(fsm.update(update));
267         assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
268         assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
269         assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
270         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
271         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
272
273         fsm.shutdown();
274     }
275 }