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 /* Set environment properties */
82 SupportUtil.setAaiProps();
83 SupportUtil.setGuardProps();
84 SupportUtil.setSdncProps();
85 SupportUtil.setSoProps();
86 SupportUtil.setVfcProps();
87 SupportUtil.setPuProp();
88 SupportUtil.setCdsProps();
90 LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "INFO");
92 PolicyEngineConstants.getManager().configure(new Properties());
93 assertTrue(PolicyEngineConstants.getManager().start());
94 Properties noopSinkProperties = new Properties();
95 noopSinkProperties.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS,
96 "APPC-LCM-READ,APPC-CL,SDNR-CL,POLICY-CL-MGT");
97 noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events", "org.onap.policy.appclcm.LcmRequestWrapper");
98 noopSinkProperties.put("noop.sink.topics.APPC-LCM-READ.events.custom.gson",
99 "org.onap.policy.appclcm.util.Serialization,gson");
100 noopSinkProperties.put("noop.sink.topics.APPC-CL.events", "org.onap.policy.appc.Response");
101 noopSinkProperties.put("noop.sink.topics.APPC-CL.events.custom.gson",
102 "org.onap.policy.appc.util.Serialization,gsonPretty");
103 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events",
104 "org.onap.policy.controlloop.VirtualControlLoopNotification");
105 noopSinkProperties.put("noop.sink.topics.POLICY-CL-MGT.events.custom.gson",
106 "org.onap.policy.controlloop.util.Serialization,gsonPretty");
107 noopTopics = TopicEndpointManager.getManager().addTopicSinks(noopSinkProperties);
109 EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
110 .groupId(JUNIT_GROUP_ID)
111 .artifactId(JUNIT_ARTIFACT_ID)
112 .topic("POLICY-CL-MGT")
113 .eventClass("org.onap.policy.controlloop.VirtualControlLoopNotification")
114 .protocolFilter(new JsonProtocolFilter())
115 .modelClassLoaderHash(1111));
116 EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
117 .groupId(JUNIT_GROUP_ID)
118 .artifactId(JUNIT_ARTIFACT_ID)
119 .topic("APPC-LCM-READ")
120 .eventClass("org.onap.policy.appclcm.LcmRequestWrapper")
121 .protocolFilter(new JsonProtocolFilter())
122 .modelClassLoaderHash(1111));
123 EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
124 .groupId(JUNIT_GROUP_ID)
125 .artifactId(JUNIT_ARTIFACT_ID)
127 .eventClass("org.onap.policy.appc.Request")
128 .protocolFilter(new JsonProtocolFilter())
129 .modelClassLoaderHash(1111));
130 EventProtocolCoderConstants.getManager().addEncoder(EventProtocolParams.builder()
131 .groupId(JUNIT_GROUP_ID)
132 .artifactId(JUNIT_ARTIFACT_ID)
134 .eventClass("org.onap.policy.sdnr.PciRequestWrapper")
135 .protocolFilter(new JsonProtocolFilter())
136 .modelClassLoaderHash(1111));
138 SupportUtil.buildAaiSim();
139 SupportUtil.buildSdncSim();
140 SupportUtil.buildSoSim();
141 SupportUtil.buildVfcSim();
142 SupportUtil.buildGuardSim();
143 } catch (Exception e) {
144 fail(e.getMessage());
149 * Start the kie session
152 kieSession = startSession(droolsTemplate, yamlFile, policyScope,
153 policyName, policyVersion);
154 } catch (IOException e) {
156 logger.debug("Could not create kieSession");
157 fail("Could not create kieSession");
162 * Stops the simulators and the kie session.
165 public static void tearDownAfterClass() {
167 * Gracefully shut down the kie session
169 kieSession.dispose();
171 PolicyEngineConstants.getManager().stop();
172 HttpServletServerFactoryInstance.getServerFactory().destroy();
173 PolicyControllerConstants.getFactory().shutdown();
174 TopicEndpointManager.getManager().shutdown();
178 * This method will start a kie session and instantiate the Policy Engine.
180 * @param droolsTemplate the DRL rules file
181 * @param yamlFile the yaml file containing the policies
182 * @param policyScope scope for policy
183 * @param policyName name of the policy
184 * @param policyVersion version of the policy
185 * @return the kieSession to be used to insert facts
186 * @throws IOException IO exception
188 private static KieSession startSession(String droolsTemplate, String yamlFile, String policyScope,
189 String policyName, String policyVersion) throws IOException {
192 * Load policies from yaml
194 pair = SupportUtil.loadYaml(yamlFile);
196 assertNotNull(pair.first);
197 assertNotNull(pair.first.getControlLoop());
198 assertNotNull(pair.first.getControlLoop().getControlLoopName());
199 assertTrue(pair.first.getControlLoop().getControlLoopName().length() > 0);
202 * Construct a kie session
204 final KieSession kieSession = SupportUtil.buildContainer(droolsTemplate,
205 pair.first.getControlLoop().getControlLoopName(),
206 policyScope, policyName, policyVersion, URLEncoder.encode(pair.second, "UTF-8"));
209 * Retrieve the Policy Engine
212 logger.debug("============");
213 logger.debug(URLEncoder.encode(pair.second, "UTF-8"));
214 logger.debug("============");
220 * This method will dump all the facts in the working memory.
222 * @param kieSession the session containing the facts
224 public void dumpFacts(KieSession kieSession) {
225 logger.debug("Fact Count: {}", kieSession.getFactCount());
226 for (FactHandle handle : kieSession.getFactHandles()) {
227 logger.debug("FACT: {}", handle);