81904b3b10ef57f083564285882e51bbdfe1bc11
[policy/drools-pdp.git] /
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 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;
33
34 import java.io.IOException;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Properties;
38 import java.util.Set;
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;
51
52 /**
53  * Lifecycle State Active Test.
54  */
55 class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest {
56
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";
60
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";
67
68     /**
69      * Start tests in the Active state.
70      */
71     @BeforeEach
72     public void startActive() throws CoderException {
73         fsm = makeFsmWithPseudoTime();
74
75         fsm.setStatusTimerSeconds(15);
76         assertTrue(fsm.start());
77
78         PdpStateChange change = new PdpStateChange();
79         change.setPdpGroup("A");
80         change.setPdpSubgroup("a");
81         change.setState(PdpState.ACTIVE);
82         change.setName(fsm.getPdpName());
83
84         fsm.setSubGroup("a");
85         fsm.source.offer(new StandardCoder().encode(change));
86         controllerSupport.getController().start();
87     }
88
89     @Test
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());
96
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());
103     }
104
105     @Test
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")));
112
113         //
114         // create controller using native policy
115         //
116
117         ToscaPolicy policyNativeController =
118             getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
119
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));
125
126         assertFalse(fsm.update(update));
127         assertEquals(0, fsm.getPoliciesMap().size());
128
129         // add topics
130
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);
135
136         assertTrue(fsm.update(update));
137         assertEquals(1, fsm.getPoliciesMap().size());
138
139         //
140         // add an artifact policy
141         //
142
143         ToscaPolicy policyNativeArtifact =
144                 getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME);
145
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));
152
153         // add a registered controller
154
155         controllerMap.put("name", "lifecycle");
156         update.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact));
157         assertTrue(fsm.update(update));
158
159         assertEquals(2, fsm.getPoliciesMap().size());
160         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
161         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
162
163         //
164         // add an operational policy
165         //
166
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));
171
172         assertEquals(2, fsm.getPoliciesMap().size());
173         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
174         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
175
176         // register controller
177
178         fsm.start(controllerSupport.getController());
179
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",
187                         "1.0.0")));
188
189         // invalid controller name
190
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()));
196
197         // no controller name
198
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()));
205
206         List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
207         assertEquals(1, factPolicies.size());
208         assertEquals(opPolicyRestart, factPolicies.get(0));
209
210         // upgrade operational policy with valid controller name
211
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));
219
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()));
225
226         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
227         assertEquals(1, factPolicies.size());
228         assertEquals(opPolicyRestartV2, factPolicies.get(0));
229
230         update.setPoliciesToBeDeployed(List.of());
231         update.setPoliciesToBeUndeployed(List.of(opPolicyRestartV2.getIdentifier()));
232         assertTrue(fsm.update(update));
233
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()));
239
240         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
241         assertEquals(0, factPolicies.size());
242         assertTrue(controllerSupport.getController().getDrools().isBrained());
243
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()));
254
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()));
263
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()));
273
274         fsm.shutdown();
275     }
276 }