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