caab96572ba1da08006fde349a7e7f2cf60b8958
[policy/drools-pdp.git] /
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * =============LICENSE_END========================================================
19  */
20
21 package org.onap.policy.drools.lifecycle;
22
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.assertNull;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.IOException;
31 import java.util.Properties;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
35 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.ResourceUtils;
39 import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
40 import org.onap.policy.drools.system.PolicyControllerConstants;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
43
44 /**
45  * Native Controller Policy Test.
46  */
47 public class PolicyTypeNativeDroolsControllerTest extends LifecycleStateRunningTest {
48     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_NAME = "example.controller";
49     private static final String EXAMPLE_NATIVE_DROOLS_POLICY_JSON =
50             "src/test/resources/tosca-policy-native-controller-example.json";
51
52     /**
53      * Test initialization.
54      */
55     @Before
56     public void init() throws IOException, CoderException {
57         fsm = makeFsmWithPseudoTime();
58     }
59
60     @Test
61     public void testDeployUndeploy() throws IOException, CoderException {
62         fsm = makeFsmWithPseudoTime();
63
64         assertTrue(controllerSupport.getController().getDrools().isBrained());
65         assertFalse(controllerSupport.getController().isAlive());
66         assertFalse(controllerSupport.getController().getDrools().isAlive());
67         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
68
69         assertTrue(controllerSupport.getController().start());
70
71         assertTrue(controllerSupport.getController().isAlive());
72         assertTrue(controllerSupport.getController().getDrools().isAlive());
73         assertTrue(controllerSupport.getController().getDrools().isBrained());
74         assertSame(controllerSupport.getController(), PolicyControllerConstants.getFactory().get("lifecycle"));
75
76         ToscaPolicy policy = getPolicyFromFile(EXAMPLE_NATIVE_DROOLS_POLICY_JSON, EXAMPLE_NATIVE_DROOLS_POLICY_NAME);
77         ControllerPolicy controllerPolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
78         PolicyTypeNativeDroolsController controller =
79                 new PolicyTypeNativeDroolsController(fsm, policy.getTypeIdentifier());
80         assertTrue(controller.undeploy(policy));
81         assertThatIllegalArgumentException().isThrownBy(
82             () -> PolicyControllerConstants.getFactory().get(controllerPolicy.getName()));
83
84         Properties noopTopicProperties = new Properties();
85         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS, "DCAE_TOPIC");
86         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, "APPC-CL");
87         TopicEndpointManager.getManager().addTopics(noopTopicProperties);
88
89         assertTrue(controller.deploy(policy));
90         /* this should be ok too */
91         assertTrue(controller.deploy(policy));
92     }
93
94     @Test
95     public void testControllerProperties() throws CoderException {
96         Properties noopTopicProperties = new Properties();
97         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SOURCE_TOPICS,
98                 "DCAE_TOPIC,APPC-CL,APPC-LCM-WRITE,SDNR-CL-RSP");
99         noopTopicProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS,
100                 "APPC-CL,APPC-LCM-READ,POLICY-CL-MGT,DCAE_CL_RSP");
101         TopicEndpointManager.getManager().addTopics(noopTopicProperties);
102
103         ToscaPolicy nativeControllerPolicy =
104                 getExamplesPolicy("policies/usecases.native.controller.policy.input.tosca.json", "usecases");
105         PolicyTypeNativeDroolsController controller =
106                 new PolicyTypeNativeDroolsController(fsm, nativeControllerPolicy.getTypeIdentifier());
107         assertTrue(controller.deploy(nativeControllerPolicy));
108         Properties properties = PolicyControllerConstants.getFactory().get("usecases").getProperties();
109
110         assertEquals("usecases", properties.getProperty("controller.name"));
111
112         assertNull(properties.getProperty("rules.groupId"));
113         assertNull(properties.getProperty("rules.artifactId"));
114         assertNull(properties.getProperty("rules.version"));
115
116         assertEquals("DCAE_TOPIC,APPC-CL,APPC-LCM-WRITE,SDNR-CL-RSP",
117                 properties.getProperty("noop.source.topics"));
118         assertEquals("APPC-CL,APPC-LCM-READ,POLICY-CL-MGT,DCAE_CL_RSP",
119                 properties.getProperty("noop.sink.topics"));
120
121         assertEquals("org.onap.policy.controlloop.CanonicalOnset,org.onap.policy.controlloop.CanonicalAbated",
122                 properties.getProperty("noop.source.topics.DCAE_TOPIC.events"));
123         assertEquals("[?($.closedLoopEventStatus == 'ONSET')]",
124             properties
125                 .getProperty("noop.source.topics.DCAE_TOPIC.events.org.onap.policy.controlloop.CanonicalOnset.filter"));
126         assertEquals("[?($.closedLoopEventStatus == 'ABATED')]",
127             properties
128                 .getProperty("noop.source.topics.DCAE_TOPIC.events."
129                                      + "org.onap.policy.controlloop.CanonicalAbated.filter"));
130         assertEquals("org.onap.policy.controlloop.util.Serialization,gson",
131                 properties.getProperty("noop.source.topics.DCAE_TOPIC.events.custom.gson"));
132
133         assertEquals("org.onap.policy.appc.Response", properties.getProperty("noop.source.topics.APPC-CL.events"));
134         assertEquals("[?($.CommonHeader && $.Status)]",
135                 properties
136                         .getProperty("noop.source.topics.APPC-CL.events.org.onap.policy.appc.Response.filter"));
137         assertEquals("org.onap.policy.appc.util.Serialization,gsonPretty",
138                 properties.getProperty("noop.source.topics.APPC-CL.events.custom.gson"));
139
140         assertEquals("org.onap.policy.appclcm.AppcLcmDmaapWrapper",
141                 properties.getProperty("noop.source.topics.APPC-LCM-WRITE.events"));
142         assertEquals("[?($.type == 'response')]",
143             properties
144                 .getProperty("noop.source.topics.APPC-LCM-WRITE.events."
145                         + "org.onap.policy.appclcm.AppcLcmDmaapWrapper.filter"));
146         assertEquals("org.onap.policy.appclcm.util.Serialization,gson",
147                 properties.getProperty("noop.source.topics.APPC-LCM-WRITE.events.custom.gson"));
148
149         assertEquals("org.onap.policy.sdnr.PciResponseWrapper",
150                 properties.getProperty("noop.source.topics.SDNR-CL-RSP.events"));
151         assertEquals("[?($.type == 'response')]",
152                 properties
153                         .getProperty("noop.source.topics.SDNR-CL-RSP.events."
154                                              + "org.onap.policy.sdnr.PciResponseWrapper.filter"));
155         assertEquals("org.onap.policy.sdnr.util.Serialization,gson",
156                 properties.getProperty("noop.source.topics.SDNR-CL-RSP.events.custom.gson"));
157
158         assertEquals("org.onap.policy.appc.Request", properties.getProperty("noop.sink.topics.APPC-CL.events"));
159         assertEquals("org.onap.policy.appc.util.Serialization,gsonPretty",
160                 properties.getProperty("noop.sink.topics.APPC-CL.events.custom.gson"));
161
162         assertEquals("org.onap.policy.appclcm.AppcLcmDmaapWrapper",
163                 properties.getProperty("noop.sink.topics.APPC-LCM-READ.events"));
164         assertEquals("org.onap.policy.appclcm.util.Serialization,gson",
165                 properties.getProperty("noop.sink.topics.APPC-LCM-READ.events.custom.gson"));
166
167         assertEquals("org.onap.policy.controlloop.VirtualControlLoopNotification",
168                 properties.getProperty("noop.sink.topics.POLICY-CL-MGT.events"));
169         assertEquals("org.onap.policy.controlloop.util.Serialization,gsonPretty",
170                 properties.getProperty("noop.sink.topics.POLICY-CL-MGT.events.custom.gson"));
171
172         assertEquals("org.onap.policy.controlloop.ControlLoopResponse",
173                 properties.getProperty("noop.sink.topics.DCAE_CL_RSP.events"));
174         assertEquals("org.onap.policy.controlloop.util.Serialization,gsonPretty",
175                 properties.getProperty("noop.sink.topics.DCAE_CL_RSP.events.custom.gson"));
176
177         assertEquals("test", properties.getProperty("notes"));
178         assertEquals("auto", properties.getProperty("persistence.type"));
179
180         assertTrue(controller.undeploy(nativeControllerPolicy));
181     }
182
183     private ToscaPolicy getExamplesPolicy(String resourcePath, String policyName) throws CoderException {
184         String policyJson = ResourceUtils.getResourceAsString(resourcePath);
185         ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyJson, ToscaServiceTemplate.class);
186         return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
187     }
188 }