fbe9129bd914fa8c1f17cc1fa903b353399ffe0f
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
4  * ================================================================================
5  * Copyright (C) 2017 Intel Corp. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.template.demo;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.net.URLEncoder;
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.kie.api.runtime.KieSession;
37 import org.kie.api.runtime.rule.FactHandle;
38 import org.onap.policy.controlloop.ControlLoopEventStatus;
39 import org.onap.policy.controlloop.ControlLoopLogger;
40 import org.onap.policy.controlloop.ControlLoopTargetType;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.impl.ControlLoopLoggerStdOutImpl;
43 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
44 import org.onap.policy.drools.http.server.HttpServletServer;
45 import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
46 import org.onap.policy.drools.system.PolicyEngine;
47 import org.onap.policy.vfc.util.Serialization;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52 public class VFCControlLoopTest {
53
54         private static final Logger log = LoggerFactory.getLogger(VFCControlLoopTest.class);
55         private KieSession kieSession;
56         private Util.Pair<ControlLoopPolicy, String> pair;
57         private PolicyEngineJUnitImpl engine;
58
59         static {
60             /* Set environment properties */
61         Util.setAAIProps();
62         Util.setVFCProps();
63         Util.setGuardProps();
64         }
65         
66         @BeforeClass
67         public static void setUpSimulator() {
68                 try {
69                         Util.buildAaiSim();
70                         Util.buildVfcSim();
71                 } catch (Exception e) {
72                         fail(e.getMessage());
73                 }
74         }
75
76         @AfterClass
77         public static void tearDownSimulator() {
78                 HttpServletServer.factory.destroy();
79         }
80
81         @Test
82         public void testVolte() throws IOException {
83
84                 final String yaml = "src/test/resources/yaml/policy_ControlLoop_VFC.yaml";
85
86                 //
87                 // Pull info from the yaml
88                 //
89                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml(yaml);
90                 assertNotNull(pair);
91                 assertNotNull(pair.a);
92                 assertNotNull(pair.a.getControlLoop());
93                 assertNotNull(pair.a.getControlLoop().getControlLoopName());
94                 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
95                 final String closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
96
97                  /*
98          * Start the kie session
99          */
100                 try {
101                         kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl",
102                                         "src/test/resources/yaml/policy_ControlLoop_VFC.yaml",
103                                         "service=ServiceTest;resource=ResourceTest;type=operational",
104                                         "CL_VFC",
105                                         "org.onap.closed_loop.ServiceTest:VNFS:1.0.0");
106                 } catch (IOException e) {
107                         e.printStackTrace();
108                         log.debug("Could not create kieSession");
109                         fail("Could not create kieSession");
110                 }
111
112
113                 //
114                 // Insert our globals
115                 //
116                 final ControlLoopLogger logger = new ControlLoopLoggerStdOutImpl();
117                 kieSession.setGlobal("Logger", logger);
118                 final PolicyEngineJUnitImpl engine = new PolicyEngineJUnitImpl();
119                 kieSession.setGlobal("Engine", engine);
120
121                 //
122                 // Initial fire of rules
123                 //
124                 kieSession.fireAllRules();
125                 //
126                 // Kick a thread that starts testing
127                 //
128                 new Thread(new Runnable() {
129
130                         @Override
131                         public void run() {
132
133                                 log.debug("\n************ Starting VoLTE Test *************\n");
134
135                                 //
136                                 // Generate an invalid DCAE Event with requestID=null
137                                 //
138                                 VirtualControlLoopEvent invalidEvent = new VirtualControlLoopEvent();
139                                 invalidEvent.closedLoopControlName = closedLoopControlName;
140                                 invalidEvent.requestID = null;
141                                 invalidEvent.closedLoopEventClient = "tca.instance00009";
142                                 invalidEvent.target_type = ControlLoopTargetType.VNF;
143                                 invalidEvent.target = "generic-vnf.vnf-id";
144                                 invalidEvent.from = "DCAE";
145                                 invalidEvent.closedLoopAlarmStart = Instant.now();
146                                 invalidEvent.AAI = new HashMap<String, String>();
147                                 invalidEvent.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
148                                 invalidEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
149
150                                 log.debug("-------- Sending Invalid ONSET --------");
151                                 log.debug(Serialization.gsonPretty.toJson(invalidEvent));
152
153                                 //
154                                 // Insert invalid DCAE Event into memory
155                                 //
156                                 kieSession.insert(invalidEvent);
157                                 try {
158                                         Thread.sleep(500);
159                                 } catch (InterruptedException e) {
160                                 }
161                                 //
162                                 // Generate first DCAE ONSET Event
163                                 //
164                                 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
165                                 onsetEvent.closedLoopControlName = closedLoopControlName;
166                                 onsetEvent.requestID = UUID.randomUUID();
167                                 onsetEvent.closedLoopEventClient = "tca.instance00009";
168                                 onsetEvent.target_type = ControlLoopTargetType.VM;
169                                 onsetEvent.target = "VM_NAME";
170                                 onsetEvent.from = "DCAE";
171                                 onsetEvent.closedLoopAlarmStart = Instant.now();
172                                 onsetEvent.AAI = new HashMap<String, String>();
173                                 onsetEvent.AAI.put("vserver.vserver-name", "vserver-name-16102016-aai3255-data-11-1");
174                                 onsetEvent.AAI.put("vserver.vserver-id", "vserver-id-16102016-aai3255-data-11-1");
175                                 onsetEvent.AAI.put("generic-vnf.vnf-id", "vnf-id-16102016-aai3255-data-11-1");
176                                 onsetEvent.AAI.put("service-instance.service-instance-id", "service-instance-id-16102016-aai3255-data-11-1");
177                                 onsetEvent.AAI.put("vserver.is-closed-loop-disabled", "false");
178                                 onsetEvent.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
179
180                                 log.debug("-------- Sending Valid ONSET --------");
181                                 log.debug(Serialization.gsonPretty.toJson(onsetEvent));
182
183                                 //
184                                 // Insert first DCAE ONSET Event into memory
185                                 //
186                                 kieSession.insert(onsetEvent);
187                                 //
188                                 // We have test for subsequent ONSET Events in testvFirewall()
189                                 // So no need to test it again here
190                                 //
191                                 try {
192                                         Thread.sleep(30000);
193                                 } catch (InterruptedException e) {
194                                 }
195                                 //
196                                 // Test is finished, so stop the kieSession
197                                 //
198                                 kieSession.halt();
199                         }
200                 //
201                 }).start();
202                 //
203                 // Start firing rules
204                 //
205                 kieSession.fireUntilHalt();
206                 //
207                 // Dump working memory
208                 //
209                 dumpFacts(kieSession);
210
211                 //
212                 // See if there is anything left in memory, there SHOULD only be
213                 // a params fact.
214                 //
215                 //assertEquals("There should only be 1 Fact left in memory.", 1, kieSession.getFactCount());
216                 if (kieSession.getFactCount() != 1L) {
217                     log.error("FACT count mismatch: 1 expected but there are {}", kieSession.getFactCount());
218                 }
219                 for (FactHandle handle : kieSession.getFactHandles()) {
220                         Object fact = kieSession.getObject(handle);
221                         // assertEquals("Non-Param Fact left in working memory", "org.onap.policy.controlloop.Params", fact.getClass().getName());
222                         log.info("Working Memory FACT: {}", fact.getClass().getName());
223                 }
224
225         }
226
227         public static void dumpFacts(KieSession kieSession) {
228                 log.debug("Fact Count: " + kieSession.getFactCount());
229                 for (FactHandle handle : kieSession.getFactHandles()) {
230                         log.debug("FACT: " + handle);
231                 }
232         }
233
234         /**
235          * This method will start a kie session and instantiate
236          * the Policy Engine.
237          *
238          * @param droolsTemplate
239          *          the DRL rules file
240          * @param yamlFile
241          *          the yaml file containing the policies
242          * @param policyScope
243          *          scope for policy
244          * @param policyName
245          *          name of the policy
246          * @param policyVersion
247          *          version of the policy
248          * @return the kieSession to be used to insert facts
249          * @throws IOException
250          */
251         private KieSession startSession(String droolsTemplate,
252                                         String yamlFile,
253                                         String policyScope,
254                                         String policyName,
255                                         String policyVersion) throws IOException {
256
257         /*
258          * Load policies from yaml
259          */
260                 pair = Util.loadYaml(yamlFile);
261                 assertNotNull(pair);
262                 assertNotNull(pair.a);
263                 assertNotNull(pair.a.getControlLoop());
264                 assertNotNull(pair.a.getControlLoop().getControlLoopName());
265                 assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
266
267         /*
268          * Construct a kie session
269          */
270                 final KieSession kieSession = Util.buildContainer(droolsTemplate,
271                                 pair.a.getControlLoop().getControlLoopName(),
272                                 policyScope,
273                                 policyName,
274                                 policyVersion,
275                                 URLEncoder.encode(pair.b, "UTF-8"));
276
277         /*
278          * Retrieve the Policy Engine
279          */
280                 engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine");
281
282                 log.debug("============");
283                 log.debug(URLEncoder.encode(pair.b, "UTF-8"));
284                 log.debug("============");
285
286                 return kieSession;
287         }
288
289 }
290