2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.template.demo;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
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;
51 * Common super class used by various Control Loop test classes. It manages the simulators
52 * and the kie session.
54 public class ControlLoopBase {
56 private static final String JUNIT_ARTIFACT_ID = "junit.artifactId";
58 private static final String JUNIT_GROUP_ID = "junit.groupId";
60 protected static final Logger logger = LoggerFactory.getLogger(ControlLoopBase.class);
62 protected static List<? extends TopicSink> noopTopics;
64 protected static KieSession kieSession;
65 protected static SupportUtil.Pair<ControlLoopPolicy, String> pair;
67 protected UUID requestId;
70 * Starts the simulator and the kie session.
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
78 public static void setUpBeforeClass(String droolsTemplate, String yamlFile, String policyScope,
79 String policyName, String policyVersion) {
81 SupportUtil.setCustomQuery("false");
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();
92 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
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.AppcLcmDmaapWrapper");
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);
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.AppcLcmDmaapWrapper")
123 .protocolFilter(new JsonProtocolFilter())
124 .modelClassLoaderHash(1111));
125 EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
126 .groupId(JUNIT_GROUP_ID)
127 .artifactId(JUNIT_ARTIFACT_ID)
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)
136 .eventClass("org.onap.policy.sdnr.PciRequestWrapper")
137 .protocolFilter(new JsonProtocolFilter())
138 .modelClassLoaderHash(1111));
140 SupportUtil.buildAaiSim();
141 SupportUtil.buildSdncSim();
142 SupportUtil.buildSoSim();
143 SupportUtil.buildVfcSim();
144 SupportUtil.buildGuardSim();
145 } catch (Exception e) {
146 fail(e.getMessage());
151 * Start the kie session
154 kieSession = startSession(droolsTemplate, yamlFile, policyScope,
155 policyName, policyVersion);
156 } catch (IOException e) {
158 logger.debug("Could not create kieSession");
159 fail("Could not create kieSession");
164 * Stops the simulators and the kie session.
167 public static void tearDownAfterClass() {
169 SupportUtil.setCustomQuery("false");
172 * Gracefully shut down the kie session
174 kieSession.dispose();
176 PolicyEngineConstants.getManager().stop();
177 HttpServletServerFactoryInstance.getServerFactory().destroy();
178 PolicyControllerConstants.getFactory().shutdown();
179 TopicEndpointManager.getManager().shutdown();
183 * This method will start a kie session and instantiate the Policy Engine.
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
193 private static KieSession startSession(String droolsTemplate, String yamlFile, String policyScope,
194 String policyName, String policyVersion) throws IOException {
197 * Load policies from yaml
199 pair = SupportUtil.loadYaml(yamlFile);
201 assertNotNull(pair.first);
202 assertNotNull(pair.first.getControlLoop());
203 assertNotNull(pair.first.getControlLoop().getControlLoopName());
204 assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
207 * Construct a kie session
209 final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate,
210 pair.first.getControlLoop().getControlLoopName(),
211 policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8"));
214 * Retrieve the Policy Engine
217 logger.debug("============");
218 logger.debug(URLEncoder.encode(pair.second, "UTF-8"));
219 logger.debug("============");
225 * This method will dump all the facts in the working memory.
227 * @param kieSession the session containing the facts
229 public void dumpFacts(KieSession kieSession) {
230 logger.debug("Fact Count: {}", kieSession.getFactCount());
231 for (FactHandle handle : kieSession.getFactHandles()) {
232 logger.debug("FACT: {}", handle);