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
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * =============LICENSE_END========================================================
 
  22 package org.onap.policy.drools.lifecycle;
 
  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;
 
  32 import java.io.IOException;
 
  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.ToscaConceptIdentifier;
 
  48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 
  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());
 
  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 ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
 
 108         assertNotNull(fsm.getPolicyTypesMap().get(
 
 109                 new ToscaConceptIdentifier("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.setPoliciesToBeDeployed(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.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact));
 
 149         assertFalse(fsm.update(update));
 
 151         // add a registered controller
 
 153         controllerMap.put("name", "lifecycle");
 
 154         update.setPoliciesToBeDeployed(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.setPoliciesToBeDeployed(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 ToscaConceptIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
 
 181         assertNotNull(fsm.getPolicyTypesMap().get(
 
 182                 new ToscaConceptIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
 
 183         assertNotNull(fsm.getPolicyTypesMap().get(
 
 184                 new ToscaConceptIdentifier("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.setPoliciesToBeDeployed(List.of(policyNativeController, policyNativeArtifact, opPolicyRestartV2));
 
 215         update.setPoliciesToBeUndeployed(List.of(opPolicyRestart.getIdentifier()));
 
 216         assertTrue(fsm.update(update));
 
 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()));
 
 224         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
 
 225         assertEquals(1, factPolicies.size());
 
 226         assertEquals(opPolicyRestartV2, factPolicies.get(0));
 
 228         update.setPoliciesToBeDeployed(List.of());
 
 229         update.setPoliciesToBeUndeployed(List.of(opPolicyRestartV2.getIdentifier()));
 
 230         assertTrue(fsm.update(update));
 
 232         assertEquals(2, fsm.getPoliciesMap().size());
 
 233         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
 
 234         assertEquals(policyNativeArtifact, fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
 
 235         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
 
 236         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
 
 238         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
 
 239         assertEquals(0, factPolicies.size());
 
 240         assertTrue(controllerSupport.getController().getDrools().isBrained());
 
 242         update.setPoliciesToBeDeployed(List.of());
 
 243         update.setPoliciesToBeUndeployed(List.of(policyNativeArtifact.getIdentifier(),
 
 244                 opPolicyRestartV2.getIdentifier()));
 
 245         assertTrue(fsm.update(update));
 
 246         assertFalse(controllerSupport.getController().getDrools().isBrained());
 
 247         assertEquals(1, fsm.getPoliciesMap().size());
 
 248         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
 
 249         assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
 
 250         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
 
 251         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));
 
 253         ToscaPolicy policyNativeFooController =
 
 254                 getPolicyFromFile(FOO_NATIVE_DROOLS_POLICY_JSON, FOO_NATIVE_DROOLS_CONTROLLER_POLICY_NAME);
 
 255         update.setPoliciesToBeUndeployed(List.of());
 
 256         update.setPoliciesToBeDeployed(List.of(policyNativeFooController));
 
 257         assertTrue(fsm.update(update));
 
 258         assertEquals(2, fsm.getPoliciesMap().size());
 
 259         assertEquals(policyNativeController, fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
 
 260         assertEquals(policyNativeFooController, fsm.getPoliciesMap().get(policyNativeFooController.getIdentifier()));
 
 262         update.setPoliciesToBeDeployed(List.of());
 
 263         update.setPoliciesToBeUndeployed(List.of(policyNativeController.getIdentifier(),
 
 264                 policyNativeFooController.getIdentifier()));
 
 265         assertTrue(fsm.update(update));
 
 266         assertThatIllegalArgumentException().isThrownBy(() -> controllerSupport.getController().getDrools());
 
 267         assertNull(fsm.getPoliciesMap().get(policyNativeController.getIdentifier()));
 
 268         assertNull(fsm.getPoliciesMap().get(policyNativeArtifact.getIdentifier()));
 
 269         assertNull(fsm.getPoliciesMap().get(opPolicyRestartV2.getIdentifier()));
 
 270         assertNull(fsm.getPoliciesMap().get(opPolicyRestart.getIdentifier()));