2b105f91ef53bc3644d35d50d07708b3ac63fdf1
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2021 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.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.io.IOException;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Properties;
37 import java.util.Set;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
41 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.network.NetworkUtil;
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 public 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     @Before
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.getName());
82
83         fsm.setSubGroup("a");
84         fsm.source.offer(new StandardCoder().encode(change));
85         controllerSupport.getController().start();
86     }
87
88     @Test
89     public 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     public 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(NetworkUtil.getHostname());
121         update.setPdpGroup("W");
122         update.setPdpSubgroup("w");
123         update.setPolicies(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, "DCAE_TOPIC");
132         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL");
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.setPolicies(List.of(policyNativeController, policyNativeArtifact));
150         assertFalse(fsm.update(update));
151
152         // add a registered controller
153
154         controllerMap.put("name", "lifecycle");
155         update.setPolicies(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.setPolicies(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.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
216         assertTrue(fsm.update(update));
217
218         assertEquals(3, fsm.getPoliciesMap().size());
219         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
220         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
221         assertEquals(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
222         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
223
224         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
225         assertEquals(1, factPolicies.size());
226         assertEquals(opPolicyRestartV2, factPolicies.get(0));
227
228         update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
229         assertTrue(fsm.update(update));
230
231         assertEquals(2, fsm.getPoliciesMap().size());
232         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
233         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
234         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
235         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
236
237         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
238         assertEquals(0, factPolicies.size());
239         assertTrue(controllerSupport.getController().getDrools().isBrained());
240
241         update.setPolicies(List.of(policyNativeController));
242         assertTrue(fsm.update(update));
243         assertFalse(controllerSupport.getController().getDrools().isBrained());
244         assertEquals(1, fsm.getPoliciesMap().size());
245         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
246         assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
247         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
248         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
249
250         ToscaPolicy policyNativeFooController =
251                 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
252         update.setPolicies(List.of(policyNativeController, policyNativeFooController));
253         assertTrue(fsm.update(update));
254         assertEquals(2, fsm.getPoliciesMap().size());
255         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
256         assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
257
258         update.setPolicies(Collections.emptyList());
259         assertTrue(fsm.update(update));
260         assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
261         assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
262         assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
263         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
264         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
265
266         fsm.shutdown();
267     }
268 }