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 Map<String, String> controllerMap =
129 (Map<String, String>) policyNativeArtifact.getProperties().get("controller");
130 controllerMap.put("name", "xyz987");
131 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
132 assertFalse(fsm.update(update));
134 // add a registered controller
136 controllerMap.put("name", "lifecycle");
137 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
138 assertTrue(fsm.update(update));
140 assertEquals(2, fsm.getPoliciesMap().size());
141 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
142 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
145 // add a legacy operational policy
148 String restart = Files.readString(Paths.get("src/test/resources/tosca-policy-operational-restart.json"));
149 ToscaPolicy opPolicyRestart = new StandardCoder().decode(restart, ToscaPolicy.class);
150 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestart));
151 assertFalse(fsm.update(update));
153 assertEquals(2, fsm.getPoliciesMap().size());
154 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
155 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
157 // register controller
159 fsm.start(controllerSupport.getController());
161 assertEquals(4, fsm.policyTypesMap.size());
162 assertNotNull(fsm.getPolicyTypesMap().get(
163 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
164 assertNotNull(fsm.getPolicyTypesMap().get(
165 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
166 assertNotNull(fsm.getPolicyTypesMap().get(
167 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0")));
168 assertNotNull(fsm.getPolicyTypesMap().get(
169 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
172 // invalid controller name
174 opPolicyRestart.getProperties().put("controllerName", "xyz987");
175 assertFalse(fsm.update(update));
176 assertEquals(2, fsm.getPoliciesMap().size());
177 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
178 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
180 // no controller name
182 opPolicyRestart.getProperties().remove("controllerName");
183 assertTrue(fsm.update(update));
184 assertEquals(3, fsm.getPoliciesMap().size());
185 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
186 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
187 assertEquals(opPolicyRestart, fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
189 List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
190 assertEquals(1, factPolicies.size());
191 assertEquals(opPolicyRestart, factPolicies.get(0));
193 // upgrade operational policy with valid controller name
195 String restartV2 = Files.readString(
196 Paths.get("src/test/resources/tosca-policy-operational-restart.v2.json"));
197 ToscaPolicy opPolicyRestartV2 = new StandardCoder().decode(restartV2, ToscaPolicy.class);
198 opPolicyRestartV2.getProperties().put("controllerName", "lifecycle");
199 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
200 assertTrue(fsm.update(update));
202 assertEquals(3, fsm.getPoliciesMap().size());
203 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
204 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
205 assertEquals(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
206 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
208 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
209 assertEquals(1, factPolicies.size());
210 assertEquals(opPolicyRestartV2, factPolicies.get(0));
212 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
213 assertTrue(fsm.update(update));
215 assertEquals(2, fsm.getPoliciesMap().size());
216 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
217 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
218 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
219 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
221 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
222 assertEquals(0, factPolicies.size());
223 assertTrue(controllerSupport.getController().getDrools().isBrained());
225 update.setPolicies(List.of(policyNativeController));
226 assertTrue(fsm.update(update));
227 assertFalse(controllerSupport.getController().getDrools().isBrained());
228 assertEquals(1, fsm.getPoliciesMap().size());
229 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
230 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
231 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
232 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
234 ToscaPolicy policyNativeFooController =
235 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
236 update.setPolicies(List.of(policyNativeController, policyNativeFooController));
237 assertTrue(fsm.update(update));
238 assertEquals(2, fsm.getPoliciesMap().size());
239 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
240 assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
242 update.setPolicies(Collections.EMPTY_LIST);
243 assertTrue(fsm.update(update));
244 assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
245 assertNull(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()));