716452f73969cbdce18aabe0e7a301194240b190
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Bell Canada.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.template.demo;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.io.IOException;
29 import java.net.URLEncoder;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.UUID;
33 import org.junit.AfterClass;
34 import org.kie.api.runtime.KieSession;
35 import org.kie.api.runtime.rule.FactHandle;
36 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
37 import org.onap.policy.common.endpoints.event.comm.TopicSink;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
40 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
41 import org.onap.policy.drools.protocol.coders.EventProtocolCoderConstants;
42 import org.onap.policy.drools.protocol.coders.EventProtocolParams;
43 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
44 import org.onap.policy.drools.system.PolicyControllerConstants;
45 import org.onap.policy.drools.system.PolicyEngineConstants;
46 import org.onap.policy.drools.utils.logging.LoggerUtil;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * Common super class used by various Control Loop test classes. It manages the simulators
52  * and the kie session.
53  */
54 public class ControlLoopBase {
55
56     private static final String JUNIT_ARTIFACT_ID = "junit.artifactId";
57
58     private static final String JUNIT_GROUP_ID = "junit.groupId";
59
60     protected static final Logger logger = LoggerFactory.getLogger(ControlLoopBase.class);
61
62     protected static List<? extends TopicSink> noopTopics;
63
64     protected static KieSession kieSession;
65     protected static SupportUtil.Pair<ControlLoopPolicy, String> pair;
66
67     protected UUID requestId;
68
69     /**
70      * Starts the simulator and the kie session.
71      *
72      * @param droolsTemplate the DRL rules file
73      * @param yamlFile the yaml file containing the policies
74      * @param policyScope scope for policy
75      * @param policyName name of the policy
76      * @param policyVersion version of the policy
77      */
78     public static void setUpBeforeClass(String droolsTemplate, String yamlFile, String policyScope,
79                     String policyName, String policyVersion) {
80
81         SupportUtil.setCustomQuery("false");
82
83         /* Set environment properties */
84         SupportUtil.setAaiProps();
85         SupportUtil.setGuardProps();
86         SupportUtil.setSdncProps();
87         SupportUtil.setSoProps();
88         SupportUtil.setVfcProps();
89         SupportUtil.setPuProp();
90         SupportUtil.setCdsProps();
91
92         LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
93
94         PolicyEngineConstants.getManager().configure(new Properties());
95         assertTrue(PolicyEngineConstants.getManager().start());
96         Properties noopSinkProperties = new Properties();
97         noopSinkProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS,
98                         "APPC-LCM-READ,APPC-CL,SDNR-CL,POLICY-CL-MGT");
99         noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events", "org.onap.policy.appclcm.LcmRequestWrapper");
100         noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events.custom.gson",
101                 "org.onap.policy.appclcm.util.Serialization,gson");
102         noopSinkProperties.put("noop.sink.topics.APPC-CL.events", "org.onap.policy.appc.Response");
103         noopSinkProperties.put("noop.sink.topics.APPC-CL.events.custom.gson",
104                 "org.onap.policy.appc.util.Serialization,gsonPretty");
105         noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events",
106                 "org.onap.policy.controlloop.VirtualControlLoopNotification");
107         noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson",
108                 "org.onap.policy.controlloop.util.Serialization,gsonPretty");
109         noopTopics = TopicEndpointManager.getManager().addTopicSinks(noopSinkProperties);
110
111         EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
112                 .groupId(JUNIT_GROUP_ID)
113                 .artifactId(JUNIT_ARTIFACT_ID)
114                 .topic("POLICY-CL-MGT")
115                 .eventClass("org.onap.policy.controlloop.VirtualControlLoopNotification")
116                 .protocolFilter(new JsonProtocolFilter())
117                 .modelClassLoaderHash(1111));
118         EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
119                 .groupId(JUNIT_GROUP_ID)
120                 .artifactId(JUNIT_ARTIFACT_ID)
121                 .topic("APPC-LCM-READ")
122                 .eventClass("org.onap.policy.appclcm.LcmRequestWrapper")
123                 .protocolFilter(new JsonProtocolFilter())
124                 .modelClassLoaderHash(1111));
125         EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
126                 .groupId(JUNIT_GROUP_ID)
127                 .artifactId(JUNIT_ARTIFACT_ID)
128                 .topic("APPC-CL")
129                 .eventClass("org.onap.policy.appc.Request")
130                 .protocolFilter(new JsonProtocolFilter())
131                 .modelClassLoaderHash(1111));
132         EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
133                 .groupId(JUNIT_GROUP_ID)
134                 .artifactId(JUNIT_ARTIFACT_ID)
135                 .topic("SDNR-CL")
136                 .eventClass("org.onap.policy.sdnr.PciRequestWrapper")
137                 .protocolFilter(new JsonProtocolFilter())
138                 .modelClassLoaderHash(1111));
139         try {
140             SupportUtil.buildAaiSim();
141             SupportUtil.buildSdncSim();
142             SupportUtil.buildSoSim();
143             SupportUtil.buildVfcSim();
144             SupportUtil.buildGuardSim();
145         } catch (Exception e) {
146             fail(e.getMessage());
147         }
148
149
150         /*
151          * Start the kie session
152          */
153         try {
154             kieSession = startSession(droolsTemplate, yamlFile, policyScope,
155                             policyName, policyVersion);
156         } catch (IOException e) {
157             e.printStackTrace();
158             logger.debug("Could not create kieSession");
159             fail("Could not create kieSession");
160         }
161     }
162
163     /**
164      * Stops the simulators and the kie session.
165      */
166     @AfterClass
167     public static void tearDownAfterClass() {
168
169         SupportUtil.setCustomQuery("false");
170
171         /*
172          * Gracefully shut down the kie session
173          */
174         kieSession.dispose();
175
176         PolicyEngineConstants.getManager().stop();
177         HttpServletServerFactoryInstance.getServerFactory().destroy();
178         PolicyControllerConstants.getFactory().shutdown();
179         TopicEndpointManager.getManager().shutdown();
180     }
181
182     /**
183      * This method will start a kie session and instantiate the Policy Engine.
184      *
185      * @param droolsTemplate the DRL rules file
186      * @param yamlFile the yaml file containing the policies
187      * @param policyScope scope for policy
188      * @param policyName name of the policy
189      * @param policyVersion version of the policy
190      * @return the kieSession to be used to insert facts
191      * @throws IOException IO exception
192      */
193     private static KieSession startSession(String droolsTemplate, String yamlFile, String policyScope,
194             String policyName, String policyVersion) throws IOException {
195
196         /*
197          * Load policies from yaml
198          */
199         pair = SupportUtil.loadYaml(yamlFile);
200         assertNotNull(pair);
201         assertNotNull(pair.first);
202         assertNotNull(pair.first.getControlLoop());
203         assertNotNull(pair.first.getControlLoop().getControlLoopName());
204         assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
205
206         /*
207          * Construct a kie session
208          */
209         final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate,
210                 pair.first.getControlLoop().getControlLoopName(),
211                 policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8"));
212
213         /*
214          * Retrieve the Policy Engine
215          */
216
217         logger.debug("============");
218         logger.debug(URLEncoder.encode(pair.second, "UTF-8"));
219         logger.debug("============");
220
221         return kieSession;
222     }
223
224     /**
225      * This method will dump all the facts in the working memory.
226      *
227      * @param kieSession the session containing the facts
228      */
229     public void dumpFacts(KieSession kieSession) {
230         logger.debug("Fact Count: {}", kieSession.getFactCount());
231         for (FactHandle handle : kieSession.getFactHandles()) {
232             logger.debug("FACT: {}", handle);
233         }
234     }
235
236 }