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.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.Collections;
34 import java.util.List;
36 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 testUpdatePolicies() throws IOException, CoderException {
89 assertEquals(2, fsm.policyTypesMap.size());
90 assertNotNull(fsm.getPolicyTypesMap().get(
91 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
92 assertNotNull(fsm.getPolicyTypesMap().get(
93 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
96 // create controller using native policy
99 ToscaPolicy policyNativeController =
100 getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
102 PdpUpdate update = new PdpUpdate();
103 update.setName(NetworkUtil.getHostname());
104 update.setPdpGroup("W");
105 update.setPdpSubgroup("w");
106 update.setPolicies(List.of(policyNativeController));
108 assertFalse(fsm.update(update));
109 assertEquals(0, fsm.getPoliciesMap().size());
113 Properties noopTopicProperties = new Properties();
114 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, "DCAE_TOPIC");
115 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL");
116 TopicEndpointManager.getManager().addTopics(noopTopicProperties);
118 assertTrue(fsm.update(update));
119 assertEquals(1, fsm.getPoliciesMap().size());
122 // add an artifact policy
125 ToscaPolicy policyNativeArtifact =
126 getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME);
128 @SuppressWarnings("unchecked")
129 Map<String, String> controllerMap =
130 (Map<String, String>) policyNativeArtifact.getProperties().get("controller");
131 controllerMap.put("name", "xyz987");
132 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
133 assertFalse(fsm.update(update));
135 // add a registered controller
137 controllerMap.put("name", "lifecycle");
138 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
139 assertTrue(fsm.update(update));
141 assertEquals(2, fsm.getPoliciesMap().size());
142 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
143 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
146 // add a legacy operational policy
149 String restart = Files.readString(Paths.get("src/test/resources/tosca-policy-operational-restart.json"));
150 ToscaPolicy opPolicyRestart = new StandardCoder().decode(restart, ToscaPolicy.class);
151 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestart));
152 assertFalse(fsm.update(update));
154 assertEquals(2, fsm.getPoliciesMap().size());
155 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
156 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
158 // register controller
160 fsm.start(controllerSupport.getController());
162 assertEquals(4, fsm.policyTypesMap.size());
163 assertNotNull(fsm.getPolicyTypesMap().get(
164 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
165 assertNotNull(fsm.getPolicyTypesMap().get(
166 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
167 assertNotNull(fsm.getPolicyTypesMap().get(
168 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0")));
169 assertNotNull(fsm.getPolicyTypesMap().get(
170 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
173 // invalid controller name
175 opPolicyRestart.getProperties().put("controllerName", "xyz987");
176 assertFalse(fsm.update(update));
177 assertEquals(2, fsm.getPoliciesMap().size());
178 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
179 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
181 // no controller name
183 opPolicyRestart.getProperties().remove("controllerName");
184 assertTrue(fsm.update(update));
185 assertEquals(3, fsm.getPoliciesMap().size());
186 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
187 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
188 assertEquals(opPolicyRestart, fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
190 List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
191 assertEquals(1, factPolicies.size());
192 assertEquals(opPolicyRestart, factPolicies.get(0));
194 // upgrade operational policy with valid controller name
196 String restartV2 = Files.readString(
197 Paths.get("src/test/resources/tosca-policy-operational-restart.v2.json"));
198 ToscaPolicy opPolicyRestartV2 = new StandardCoder().decode(restartV2, ToscaPolicy.class);
199 opPolicyRestartV2.getProperties().put("controllerName", "lifecycle");
200 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
201 assertTrue(fsm.update(update));
203 assertEquals(3, fsm.getPoliciesMap().size());
204 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
205 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
206 assertEquals(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
207 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
209 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
210 assertEquals(1, factPolicies.size());
211 assertEquals(opPolicyRestartV2, factPolicies.get(0));
213 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
214 assertTrue(fsm.update(update));
216 assertEquals(2, fsm.getPoliciesMap().size());
217 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
218 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
219 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
220 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
222 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
223 assertEquals(0, factPolicies.size());
224 assertTrue(controllerSupport.getController().getDrools().isBrained());
226 update.setPolicies(List.of(policyNativeController));
227 assertTrue(fsm.update(update));
228 assertFalse(controllerSupport.getController().getDrools().isBrained());
229 assertEquals(1, fsm.getPoliciesMap().size());
230 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
231 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
232 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
233 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
235 ToscaPolicy policyNativeFooController =
236 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
237 update.setPolicies(List.of(policyNativeController, policyNativeFooController));
238 assertTrue(fsm.update(update));
239 assertEquals(2, fsm.getPoliciesMap().size());
240 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
241 assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
243 update.setPolicies(Collections.emptyList());
244 assertTrue(fsm.update(update));
245 assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
246 assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
247 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
248 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
249 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));