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.util.Collections;
32 import java.util.List;
34 import java.util.Properties;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
38 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.models.pdp.concepts.PdpStateChange;
43 import org.onap.policy.models.pdp.concepts.PdpUpdate;
44 import org.onap.policy.models.pdp.enums.PdpState;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 * Lifecycle State Active Test.
51 public class LifecycleStateActivePoliciesTest extends LifecycleStateRunningTest {
53 private static final String EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "example.controller";
54 private static final String EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME = "example.artifact";
55 private static final String FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME = "foo.controller";
57 private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
58 "src/test/resources/tosca-policy-native-controller-example.json";
59 private static final String EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON =
60 "src/test/resources/tosca-policy-native-artifact-example.json";
61 private static final String FOO_NATIVE_DROOLS_POLICY_JSON =
62 "src/test/resources/tosca-policy-native-controller-foo.json";
65 * Start tests in the Active state.
68 public void startActive() throws CoderException {
69 fsm = makeFsmWithPseudoTime();
71 fsm.setStatusTimerSeconds(15);
72 assertTrue(fsm.start());
74 PdpStateChange change = new PdpStateChange();
75 change.setPdpGroup("A");
76 change.setPdpSubgroup("a");
77 change.setState(PdpState.ACTIVE);
78 change.setName(fsm.getName());
80 fsm.setSubGroupAction("a");
81 fsm.source.offer(new StandardCoder().encode(change));
82 controllerSupport.getController().start();
86 public void testUpdatePolicies() throws IOException, CoderException {
87 assertEquals(2, fsm.policyTypesMap.size());
88 assertNotNull(fsm.getPolicyTypesMap().get(
89 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
90 assertNotNull(fsm.getPolicyTypesMap().get(
91 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
94 // create controller using native policy
97 ToscaPolicy policyNativeController =
98 getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
100 PdpUpdate update = new PdpUpdate();
101 update.setName(NetworkUtil.getHostname());
102 update.setPdpGroup("W");
103 update.setPdpSubgroup("w");
104 update.setPolicies(List.of(policyNativeController));
106 assertFalse(fsm.update(update));
107 assertEquals(0, fsm.getPoliciesMap().size());
111 Properties noopTopicProperties = new Properties();
112 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, "DCAE_TOPIC");
113 noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL");
114 TopicEndpointManager.getManager().addTopics(noopTopicProperties);
116 assertTrue(fsm.update(update));
117 assertEquals(1, fsm.getPoliciesMap().size());
120 // add an artifact policy
123 ToscaPolicy policyNativeArtifact =
124 getPolicyFromFile(EXAMPLE_NATIVE_ARTIFACT_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_ARTIFACT_POLICY_NAME);
126 @SuppressWarnings("unchecked")
127 Map<String, String> controllerMap =
128 (Map<String, String>) policyNativeArtifact.getProperties().get("controller");
129 controllerMap.put("name", "xyz987");
130 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
131 assertFalse(fsm.update(update));
133 // add a registered controller
135 controllerMap.put("name", "lifecycle");
136 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
137 assertTrue(fsm.update(update));
139 assertEquals(2, fsm.getPoliciesMap().size());
140 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
141 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
144 // add an operational policy
147 ToscaPolicy opPolicyRestart =
148 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
149 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestart));
150 assertFalse(fsm.update(update));
152 assertEquals(2, fsm.getPoliciesMap().size());
153 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
154 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
156 // register controller
158 fsm.start(controllerSupport.getController());
160 assertEquals(3, fsm.policyTypesMap.size());
161 assertNotNull(fsm.getPolicyTypesMap().get(
162 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
163 assertNotNull(fsm.getPolicyTypesMap().get(
164 new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
165 assertNotNull(fsm.getPolicyTypesMap().get(
166 new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
169 // invalid controller name
171 opPolicyRestart.getProperties().put("controllerName", "xyz987");
172 assertFalse(fsm.update(update));
173 assertEquals(2, fsm.getPoliciesMap().size());
174 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
175 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
177 // no controller name
179 opPolicyRestart.getProperties().remove("controllerName");
180 assertTrue(fsm.update(update));
181 assertEquals(3, fsm.getPoliciesMap().size());
182 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
183 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
184 assertEquals(opPolicyRestart, fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
186 List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
187 assertEquals(1, factPolicies.size());
188 assertEquals(opPolicyRestart, factPolicies.get(0));
190 // upgrade operational policy with valid controller name
192 ToscaPolicy opPolicyRestartV2 =
193 getExamplesPolicy("policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
194 opPolicyRestartV2.setVersion("2.0.0");
195 opPolicyRestartV2.getProperties().put("controllerName", "lifecycle");
196 update.setPolicies(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
197 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(opPolicyRestartV2, fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
203 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
205 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
206 assertEquals(1, factPolicies.size());
207 assertEquals(opPolicyRestartV2, factPolicies.get(0));
209 update.setPolicies(List.of(policyNativeController, policyNativeArtifact));
210 assertTrue(fsm.update(update));
212 assertEquals(2, fsm.getPoliciesMap().size());
213 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
214 assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
215 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
216 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
218 factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
219 assertEquals(0, factPolicies.size());
220 assertTrue(controllerSupport.getController().getDrools().isBrained());
222 update.setPolicies(List.of(policyNativeController));
223 assertTrue(fsm.update(update));
224 assertFalse(controllerSupport.getController().getDrools().isBrained());
225 assertEquals(1, fsm.getPoliciesMap().size());
226 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
227 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
228 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
229 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
231 ToscaPolicy policyNativeFooController =
232 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
233 update.setPolicies(List.of(policyNativeController, policyNativeFooController));
234 assertTrue(fsm.update(update));
235 assertEquals(2, fsm.getPoliciesMap().size());
236 assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
237 assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
239 update.setPolicies(Collections.emptyList());
240 assertTrue(fsm.update(update));
241 assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
242 assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
243 assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
244 assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
245 assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));