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