4dc6b806fc41c3a6e5fa588a024c815f8823c42e
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * demo
4  * ================================================================================
5  * Copyright (C) 2017 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
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.*;
24
25 import java.io.IOException;
26 import java.net.URLEncoder;
27 import java.time.Instant;
28 import java.util.HashMap;
29 import java.util.UUID;
30
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.kie.api.runtime.KieSession;
35 import org.kie.api.runtime.rule.FactHandle;
36 import org.onap.policy.appclcm.LCMRequest;
37 import org.onap.policy.appclcm.LCMRequestWrapper;
38 import org.onap.policy.appclcm.LCMResponse;
39 import org.onap.policy.appclcm.LCMResponseWrapper;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.ControlLoopNotificationType;
42 import org.onap.policy.controlloop.VirtualControlLoopEvent;
43 import org.onap.policy.controlloop.VirtualControlLoopNotification;
44 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
45 import org.onap.policy.drools.PolicyEngineListener;
46 import org.onap.policy.drools.http.server.HttpServletServer;
47 import org.onap.policy.drools.impl.PolicyEngineJUnitImpl;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class VCPEControlLoopTest implements PolicyEngineListener {
52
53     private static final Logger logger = LoggerFactory.getLogger(VCPEControlLoopTest.class);
54     
55     private KieSession kieSession;
56     private Util.Pair<ControlLoopPolicy, String> pair;
57     private PolicyEngineJUnitImpl engine; 
58     private UUID requestID;
59     
60     static {
61         /* Set environment properties */
62         Util.setAAIProps();
63         Util.setGuardProps();
64         Util.setPUProp();
65     }
66     
67     @BeforeClass
68     public static void setUpSimulator() {
69         try {
70             Util.buildAaiSim();
71             Util.buildGuardSim();
72         } catch (Exception e) {
73             fail(e.getMessage());
74         }
75     }
76
77     @AfterClass
78     public static void tearDownSimulator() {
79         HttpServletServer.factory.destroy();
80     }
81     
82     @Test
83     public void successTest() {
84         /*
85          * Start the kie session
86          */
87         try {
88             kieSession = startSession("src/main/resources/ControlLoop_Template_xacml_guard.drl", 
89                         "src/test/resources/yaml/policy_ControlLoop_vCPE.yaml",
90                         "service=ServiceDemo;resource=Res1Demo;type=operational", 
91                         "CL_vCPE", 
92                         "org.onap.closed_loop.ServiceDemo:VNFS:1.0.0");
93         } catch (IOException e) {
94             e.printStackTrace();
95             logger.debug("Could not create kieSession");
96             fail("Could not create kieSession");
97         }
98         
99         /*
100          * Allows the PolicyEngine to callback to this object to
101          * notify that there is an event ready to be pulled 
102          * from the queue
103          */
104         engine.addListener(this);
105         
106         /*
107          * Create a unique requestId
108          */
109         requestID = UUID.randomUUID();
110         
111         /* 
112          * Simulate an onset event the policy engine will 
113          * receive from DCAE to kick off processing through
114          * the rules
115          */
116         sendEvent(pair.a, requestID, ControlLoopEventStatus.ONSET);
117         
118         kieSession.fireUntilHalt();
119         
120         /*
121          * The only fact in memory should be Params
122          */
123         assertEquals(1, kieSession.getFactCount());
124         
125         /*
126          * Print what's left in memory
127          */
128         dumpFacts(kieSession);
129         
130         /*
131          * Gracefully shut down the kie session
132          */
133         kieSession.dispose();
134     }
135
136     /**
137      * This method will start a kie session and instantiate 
138      * the Policy Engine.
139      * 
140      * @param droolsTemplate
141      *          the DRL rules file
142      * @param yamlFile
143      *          the yaml file containing the policies
144      * @param policyScope
145      *          scope for policy
146      * @param policyName
147      *          name of the policy
148      * @param policyVersion
149      *          version of the policy          
150      * @return the kieSession to be used to insert facts 
151      * @throws IOException
152      */
153     private KieSession startSession(String droolsTemplate, 
154             String yamlFile, 
155             String policyScope, 
156             String policyName, 
157             String policyVersion) throws IOException {
158         
159         /*
160          * Load policies from yaml
161          */
162         pair = Util.loadYaml(yamlFile);
163         assertNotNull(pair);
164         assertNotNull(pair.a);
165         assertNotNull(pair.a.getControlLoop());
166         assertNotNull(pair.a.getControlLoop().getControlLoopName());
167         assertTrue(pair.a.getControlLoop().getControlLoopName().length() > 0);
168         
169         /* 
170          * Construct a kie session
171          */
172         final KieSession kieSession = Util.buildContainer(droolsTemplate, 
173                 pair.a.getControlLoop().getControlLoopName(), 
174                 policyScope, 
175                 policyName, 
176                 policyVersion, 
177                 URLEncoder.encode(pair.b, "UTF-8"));
178         
179         /*
180          * Retrieve the Policy Engine
181          */
182         engine = (PolicyEngineJUnitImpl) kieSession.getGlobal("Engine");
183         
184         logger.debug("============");
185         logger.debug(URLEncoder.encode(pair.b, "UTF-8"));
186         logger.debug("============");
187         
188         return kieSession;
189     }
190     
191     /*
192      * (non-Javadoc)
193      * @see org.onap.policy.drools.PolicyEngineListener#newEventNotification(java.lang.String)
194      */
195     public void newEventNotification(String topic) {
196         /*
197          * Pull the object that was sent out to DMAAP and make
198          * sure it is a ControlLoopNoticiation of type active
199          */
200         Object obj = engine.subscribe("UEB", topic);
201         assertNotNull(obj);
202         if (obj instanceof VirtualControlLoopNotification) {
203             VirtualControlLoopNotification notification = (VirtualControlLoopNotification) obj;
204             String policyName = notification.policyName;
205             if (policyName.endsWith("EVENT")) {
206                 logger.debug("Rule Fired: " + notification.policyName);
207                 assertTrue(ControlLoopNotificationType.ACTIVE.equals(notification.notification));
208             }
209             else if (policyName.endsWith("GUARD_NOT_YET_QUERIED")) {
210                 logger.debug("Rule Fired: " + notification.policyName);
211                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
212                 assertNotNull(notification.message);
213                 assertTrue(notification.message.startsWith("Sending guard query"));
214             }
215             else if (policyName.endsWith("GUARD.RESPONSE")) {
216                 logger.debug("Rule Fired: " + notification.policyName);
217                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
218                 assertNotNull(notification.message);
219                 assertTrue(notification.message.endsWith("PERMIT"));
220             }
221             else if (policyName.endsWith("GUARD_PERMITTED")) {
222                 logger.debug("Rule Fired: " + notification.policyName);
223                 assertTrue(ControlLoopNotificationType.OPERATION.equals(notification.notification));
224                 assertNotNull(notification.message);
225                 assertTrue(notification.message.startsWith("actor=APPC"));
226             }
227             else if (policyName.endsWith("OPERATION.TIMEOUT")) {
228                 logger.debug("Rule Fired: " + notification.policyName);
229                 kieSession.halt();
230                 logger.debug("The operation timed out");
231                 fail("Operation Timed Out");
232             }
233             else if (policyName.endsWith("APPC.LCM.RESPONSE")) {
234                 logger.debug("Rule Fired: " + notification.policyName);
235                 assertTrue(ControlLoopNotificationType.OPERATION_SUCCESS.equals(notification.notification));
236                 assertNotNull(notification.message);
237                 assertTrue(notification.message.startsWith("actor=APPC"));
238                 sendEvent(pair.a, requestID, ControlLoopEventStatus.ABATED);
239             }
240             else if (policyName.endsWith("EVENT.MANAGER")) {
241                 logger.debug("Rule Fired: " + notification.policyName);
242                 assertTrue(ControlLoopNotificationType.FINAL_SUCCESS.equals(notification.notification));
243                 kieSession.halt();
244             }
245             else if (policyName.endsWith("EVENT.MANAGER.TIMEOUT")) {
246                 logger.debug("Rule Fired: " + notification.policyName);
247                 kieSession.halt();
248                 logger.debug("The control loop timed out");
249                 fail("Control Loop Timed Out");
250             }
251         }
252         else if (obj instanceof LCMRequestWrapper) {
253             /*
254              * The request should be of type LCMRequestWrapper
255              * and the subrequestid should be 1
256              */
257             LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) obj;
258             LCMRequest appcRequest = dmaapRequest.getBody();
259             assertTrue(appcRequest.getCommonHeader().getSubRequestId().equals("1"));
260             
261             logger.debug("\n============ APPC received the request!!! ===========\n");
262             
263             /*
264              * Simulate a success response from APPC and insert
265              * the response into the working memory
266              */
267             LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
268             LCMResponse appcResponse = new LCMResponse(appcRequest);
269             appcResponse.getStatus().setCode(400);
270             appcResponse.getStatus().setMessage("AppC success");
271             dmaapResponse.setBody(appcResponse);
272             kieSession.insert(dmaapResponse);
273         }        
274     }
275     
276     /**
277      * This method is used to simulate event messages from DCAE
278      * that start the control loop (onset message) or end the
279      * control loop (abatement message).
280      * 
281      * @param policy the controlLoopName comes from the policy 
282      * @param requestID the requestId for this event
283      * @param status could be onset or abated
284      */
285     protected void sendEvent(ControlLoopPolicy policy, UUID requestID, ControlLoopEventStatus status) {
286         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
287         event.closedLoopControlName = policy.getControlLoop().getControlLoopName();
288         event.requestID = requestID;
289         event.target = "generic-vnf.vnf-id";
290         event.closedLoopAlarmStart = Instant.now();
291         event.AAI = new HashMap<>();
292         event.AAI.put("generic-vnf.vnf-id", "testGenericVnfID");
293         event.closedLoopEventStatus = status;
294         kieSession.insert(event);
295     }
296     
297     /**
298      * This method will dump all the facts in the working memory.
299      * 
300      * @param kieSession the session containing the facts
301      */
302     public void dumpFacts(KieSession kieSession) {
303         logger.debug("Fact Count: {}", kieSession.getFactCount());
304         for (FactHandle handle : kieSession.getFactHandles()) {
305             logger.debug("FACT: {}", handle);
306         }
307     }
308 }