2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.assertNull;
29 import static org.junit.Assert.assertTrue;
31 import java.io.IOException;
32 import java.util.Collections;
33 import java.util.List;
35 import java.util.Properties;
37 import org.junit.Before;
38 import org.junit.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.common.utils.network.NetworkUtil;
44 import org.onap.policy.models.pdp.concepts.PdpStateChange;
45 import org.onap.policy.models.pdp.concepts.PdpUpdate;
46 import org.onap.policy.models.pdp.enums.PdpState;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
51 * Lifecycle State Active Test.
53 public class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest {
55 private static final String EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "example.controller";
56 private static final String EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME = "example.artifact";
57 private static final String FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "foo.controller";
59 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
60 "src/test/resources/tosca-policy-native-controller-example.json";
61 private static final String EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON =
62 "src/test/resources/tosca-policy-native-artifact-example.json";
63 private static final String FOO_NATIVE_DROOLS_POLICY_JSON =
64 "src/test/resources/tosca-policy-native-controller-foo.json";
67 * Start tests in the Active state.
70 public void startActive() throws CoderException {
71 fsm = makeFsmWithPseudoTime();
73 fsm.setStatusTimerSeconds(15);
74 assertTrue(fsm.start());
76 PdpStateChange change = new PdpStateChange();
77 change.setPdpGroup("A");
78 change.setPdpSubgroup("a");
79 change.setState(PdpState.ACTIVE);
80 change.setName(fsm.getName());
82 fsm.setSubGroupAction("a");
83 fsm.source.offer(new StandardCoder().encode(change));
84 controllerSupport.getController().start();
88 public void testMandatoryPolicyTypes() {
89 assertEquals(Set.of("onap.policies.native.drools.Artifact", "onap.policies.native.drools.Controller"),
90 fsm.getMandatoryPolicyTypes());
91 assertEquals(fsm.getMandatoryPolicyTypes(), fsm.getCurrentPolicyTypes());
92 assertTrue(fsm.isMandatoryPolicyTypesCompliant());
93 assertTrue(fsm.status());
95 fsm.mandatoryPolicyTypes.add("blah");
96 assertEquals(Set.of("onap.policies.native.drools.Artifact", "onap.policies.native.drools.Controller", "blah"),
97 fsm.getMandatoryPolicyTypes());
98 assertNotEquals(fsm.getMandatoryPolicyTypes(), fsm.getCurrentPolicyTypes());
99 assertFalse(fsm.isMandatoryPolicyTypesCompliant());
100 assertFalse(fsm.status());
104 public void testUpdatePolicies() throws IOException, CoderException {
105 assertEquals(2, fsm.policyTypesMap.size());
106 assertNotNull(fsm.getPolicyTypesMap().get(
107 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
108 assertNotNull(fsm.getPolicyTypesMap().get(
109 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
112 // create controller using native policy
115 ToscaPolicy policyNativeController =
116 getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
118 PdpUpdate update = new PdpUpdate();
119 update.setName(NetworkUtil.getHostname());
120 update.setPdpGroup("W");
121 update.setPdpSubgroup("w");
122 update.setPolicies(List.of(policyNativeController));
124 assertFalse(fsm.update(update));
125 assertEquals(0, fsm.getPoliciesMap().size());
129 Properties noopTopicProperties = new Properties();
130 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, "DCAE_TOPIC");
131 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL");
132 TopicEndpointManager.getManager().addTopics(noopTopicProperties);
134 assertTrue(fsm.update(update));
135 assertEquals(1, fsm.getPoliciesMap().size());
138 // add an artifact policy
141 ToscaPolicy policyNativeArtifact =
142 getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME);
144 @SuppressWarnings("unchecked")
145 Map<String, String> controllerMap =
146 (Map<String, String>) policyNativeArtifact.getProperties().get("controller");
147 controllerMap.put("name", "xyz987");
148 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
149 assertFalse(fsm.update(update));
151 // add a registered controller
153 controllerMap.put("name", "lifecycle");
154 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
155 assertTrue(fsm.update(update));
157 assertEquals(2, fsm.getPoliciesMap().size());
158 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
159 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
162 // add an operational policy
165 ToscaPolicy opPolicyRestart =
166 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
167 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestart));
168 assertFalse(fsm.update(update));
170 assertEquals(2, fsm.getPoliciesMap().size());
171 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
172 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
174 // register controller
176 fsm.start(controllerSupport.getController());
178 assertEquals(3, fsm.policyTypesMap.size());
179 assertNotNull(fsm.getPolicyTypesMap().get(
180 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
181 assertNotNull(fsm.getPolicyTypesMap().get(
182 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
183 assertNotNull(fsm.getPolicyTypesMap().get(
184 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
187 // invalid controller name
189 opPolicyRestart.getProperties().put("controllerName", "xyz987");
190 assertFalse(fsm.update(update));
191 assertEquals(2, fsm.getPoliciesMap().size());
192 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
193 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
195 // no controller name
197 opPolicyRestart.getProperties().remove("controllerName");
198 assertTrue(fsm.update(update));
199 assertEquals(3, fsm.getPoliciesMap().size());
200 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
201 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
202 assertEquals(opPolicyRestart, fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
204 List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
205 assertEquals(1, factPolicies.size());
206 assertEquals(opPolicyRestart, factPolicies.get(0));
208 // upgrade operational policy with valid controller name
210 ToscaPolicy opPolicyRestartV2 =
211 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
212 opPolicyRestartV2.setVersion("2.0.0");
213 opPolicyRestartV2.getProperties().put("controllerName", "lifecycle");
214 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
215 assertTrue(fsm.update(update));
217 assertEquals(3, fsm.getPoliciesMap().size());
218 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
219 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
220 assertEquals(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
221 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
223 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
224 assertEquals(1, factPolicies.size());
225 assertEquals(opPolicyRestartV2, factPolicies.get(0));
227 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
228 assertTrue(fsm.update(update));
230 assertEquals(2, fsm.getPoliciesMap().size());
231 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
232 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
233 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
234 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
236 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
237 assertEquals(0, factPolicies.size());
238 assertTrue(controllerSupport.getController().getDrools().isBrained());
240 update.setPolicies(List.of(policyNativeController));
241 assertTrue(fsm.update(update));
242 assertFalse(controllerSupport.getController().getDrools().isBrained());
243 assertEquals(1, fsm.getPoliciesMap().size());
244 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
245 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
246 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
247 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
249 ToscaPolicy policyNativeFooController =
250 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
251 update.setPolicies(List.of(policyNativeController, policyNativeFooController));
252 assertTrue(fsm.update(update));
253 assertEquals(2, fsm.getPoliciesMap().size());
254 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
255 assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
257 update.setPolicies(Collections.emptyList());
258 assertTrue(fsm.update(update));
259 assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
260 assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
261 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
262 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
263 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));