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