ff5d82e5e80baa8a7fc69356f512df4837e417ea
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * archetype-closed-loop-demo-rules
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.controlloop;
22
23 import java.util.List;
24 import java.util.LinkedList;
25 import java.util.Map;
26 import java.util.HashMap;
27 import java.util.UUID;
28
29 import org.onap.policy.controlloop.VirtualControlLoopEvent;
30 import org.onap.policy.controlloop.ControlLoopEventStatus;
31 import org.onap.policy.controlloop.VirtualControlLoopNotification;
32 import org.onap.policy.controlloop.ControlLoopNotificationType;
33 import org.onap.policy.controlloop.ControlLoopOperation;
34 import org.onap.policy.controlloop.ControlLoopOperationWrapper;
35 import org.onap.policy.controlloop.ControlLoopException;
36
37 import org.onap.policy.aai.AAINQF199.AAINQF199CloudRegion;
38 import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperties;
39 import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperty;
40 import org.onap.policy.aai.AAINQF199.AAINQF199GenericVNF;
41 import org.onap.policy.aai.AAINQF199.AAINQF199InstanceFilters;
42 import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItem;
43 import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItems;
44 import org.onap.policy.aai.AAINQF199.AAINQF199Manager;
45 import org.onap.policy.aai.AAINQF199.AAINQF199NamedQuery;
46 import org.onap.policy.aai.AAINQF199.AAINQF199QueryParameters;
47 import org.onap.policy.aai.AAINQF199.AAINQF199Request;
48 import org.onap.policy.aai.AAINQF199.AAINQF199RequestWrapper;
49 import org.onap.policy.aai.AAINQF199.AAINQF199Response;
50 import org.onap.policy.aai.AAINQF199.AAINQF199ResponseWrapper;
51 import org.onap.policy.aai.AAINQF199.AAINQF199ServiceInstance;
52 import org.onap.policy.aai.AAINQF199.AAINQF199Tenant;
53 import org.onap.policy.aai.AAINQF199.AAINQF199VfModule;
54 import org.onap.policy.aai.AAINQF199.AAINQF199VServer;
55 import org.onap.policy.aai.util.Serialization;
56
57 import org.onap.policy.appc.CommonHeader;
58 import org.onap.policy.appc.Request;
59 import org.onap.policy.appc.Response;
60 import org.onap.policy.appc.ResponseCode;
61 import org.onap.policy.appc.ResponseStatus;
62 import org.onap.policy.appc.ResponseValue;
63
64 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
65 import org.onap.policy.vnf.trafficgenerator.PGRequest;
66 import org.onap.policy.vnf.trafficgenerator.PGStream;
67 import org.onap.policy.vnf.trafficgenerator.PGStreams;
68
69 import org.onap.policy.mso.MSOManager;
70 import org.onap.policy.mso.MSORequest;
71 import org.onap.policy.mso.MSORequestStatus;
72 import org.onap.policy.mso.MSORequestDetails;
73 import org.onap.policy.mso.MSOModelInfo;
74 import org.onap.policy.mso.MSOCloudConfiguration;
75 import org.onap.policy.mso.MSORequestInfo;
76 import org.onap.policy.mso.MSORequestParameters;
77 import org.onap.policy.mso.MSORelatedInstanceListElement;
78 import org.onap.policy.mso.MSORelatedInstance;
79 import org.onap.policy.mso.MSOResponse;
80
81 import org.onap.policy.drools.system.PolicyEngine;
82
83 //
84 // These parameters are required to build the runtime policy
85 //
86 declare Params
87     closedLoopControlName : String
88     actor : String
89     aaiURL : String
90     aaiUsername : String
91     aaiPassword : String
92     msoURL : String
93     msoUsername : String
94     msoPassword : String
95     aaiNamedQueryUUID : String
96     aaiPatternMatch : int 
97     notificationTopic : String
98     appcTopic : String
99 end
100
101 /*
102 *
103 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
104 * NOTE:  If this file is to be used as a template to be used with the policy BRMS GW, please comment out this line 
105 * as the BRMS_GW already generates a SETUP rule
106 *
107 */
108 rule "${policyName}.SETUP"
109         when
110         then
111                 System.out.println("rule SETUP is triggered.");
112                 Params params = new Params();
113                 params.setClosedLoopControlName("${closedLoopControlName}");
114                 params.setActor("${actor}");
115                 params.setAaiURL("${aaiURL}");
116                 params.setAaiUsername("${aaiUsername}");
117                 params.setAaiPassword("${aaiPassword}");
118                 params.setMsoURL("${msoURL}");
119                 params.setMsoUsername("${msoUsername}");
120                 params.setMsoPassword("${msoPassword}");
121                 params.setAaiNamedQueryUUID("${aaiNamedQueryUUID}");
122                 params.setAaiPatternMatch(${aaiPatternMatch});
123                 params.setNotificationTopic("${notificationTopic}");
124                 params.setAppcTopic("${appcTopic}");
125                 //
126                 // This stays in memory as long as the rule is alive and running
127                 //
128                 insert(params);
129 end
130
131
132 /*
133 *
134 * This rule responds to DCAE Events
135 *
136 */
137 rule "${policyName}.EVENT"
138         when
139         $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
140         $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
141         not ( ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName ))
142         then
143                 System.out.println("rule EVENT is triggered.");
144                 try {
145                         // 
146                         // Check the requestID in the event to make sure it is not null before we create the EventManager. 
147                         // The EventManager will do extra syntax checking as well check if the closed loop is disabled/
148                         //
149                         if ($event.requestID == null) {
150                                 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
151                                 notification.notification = ControlLoopNotificationType.REJECTED;
152                                 notification.from = "policy";
153                                 notification.message = "Missing requestID from DCAE event";
154                                 notification.policyName = drools.getRule().getName();
155                                 notification.policyScope = "${policyScope}";
156                                 notification.policyVersion = "${policyVersion}";
157                                 //
158                                 // Let interested parties know
159                                 //
160                                 try {
161                                         System.out.println(Serialization.gsonPretty.toJson(notification));
162                                         PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
163                                 } catch (Exception e) {
164                                         e.printStackTrace();
165                                         System.out.println("Can't deliver notification: " + notification);
166                                 }
167                                 //
168                                 // Retract it from memory
169                                 //
170                                 retract($event);
171                                 System.out.println("Event with requestID=null has been retracted.");
172                         } else {
173                                 //
174                                 // Create an EventManager
175                                 //
176                                 ControlLoopEventManager manager = new ControlLoopEventManager($params.getClosedLoopControlName(), $event.requestID);
177                                 //
178                                 // Determine if EventManager can actively process the event (i.e. syntax)
179                                 //
180                                 VirtualControlLoopNotification notification = manager.activate($event);
181                                 notification.from = "policy"; 
182                                 notification.policyName = drools.getRule().getName();
183                                 notification.policyScope = "${policyScope}";
184                                 notification.policyVersion = "${policyVersion}";
185                                 //
186                                 // Are we actively pursuing this event?
187                                 //
188                                 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
189                                         //
190                                         // Insert Event Manager into memory, this will now kick off processing.
191                                         //
192                                         insert(manager);
193                                         //
194                                         // Let interested parties know
195                                         //
196                                         try {
197                                                 System.out.println(Serialization.gsonPretty.toJson(notification));
198                                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
199                                         } catch (Exception e) {
200                                                 e.printStackTrace();
201                                                 System.out.println("Can't deliver notification: " + notification);
202                                         }               
203                                 } else {
204                                         //
205                                         // Let interested parties know
206                                         //
207                                         try {
208                                                 System.out.println(Serialization.gsonPretty.toJson(notification));
209                                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
210                                         } catch (Exception e) {
211                                                 e.printStackTrace();
212                                                 System.out.println("Can't deliver notification: " + notification);
213                                         }
214                                         //
215                                         // Retract it from memory
216                                         //
217                                         retract($event);
218                                 }
219                                 //
220                                 // Now that the manager is inserted into Drools working memory, we'll wait for
221                                 // another rule to fire in order to continue processing. This way we can also
222                                 // then screen for additional ONSET and ABATED events for this same RequestIDs 
223                                 // and for different RequestIDs but with the same closedLoopControlName and target.
224                                 //
225                         }
226                 //
227                 } catch (Exception e) {
228                         e.printStackTrace();
229                         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
230                         notification.notification = ControlLoopNotificationType.REJECTED;
231                         notification.message = "Exception occurred " + e.getMessage();
232                         notification.policyName = drools.getRule().getName();
233                         notification.policyScope = "${policyScope}";
234                         notification.policyVersion = "${policyVersion}";
235                         //
236                         //
237                         //
238                         try {
239                                 System.out.println(Serialization.gsonPretty.toJson(notification));
240                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
241                         } catch (Exception e1) {
242                                 System.out.println("Can't deliver notification: " + notification);
243                                 e1.printStackTrace();
244                         }
245                         //
246                         // Retract the event
247                         //
248                         retract($event);
249                 }
250 end
251
252 /*
253 *
254 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
255 * is created. We can start the operations for this closed loop.
256 *
257 */
258 rule "${policyName}.EVENT.MANAGER"
259         when
260                 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
261                 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
262                 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName, controlLoopResult == null)
263         then
264                 System.out.println("rule EVENT.MANAGER is triggered.");
265                 //
266                 // Check which event this is.
267                 //
268                 ControlLoopEventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
269                 //
270                 // We only want the initial ONSET event in memory,
271                 // all the other events need to be retracted to support
272                 // cleanup and avoid the other rules being fired for this event.
273                 //
274                 if (eventStatus != ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET) {
275                         System.out.println("Retracting "+eventStatus+" Event.");
276                         retract($event);
277                         return;
278                 }
279                 //
280                 // Now the event in memory is first onset event
281                 //
282                 try {
283                         //
284                         // Pull the known AAI field from the Event
285                         //
286                         // generic-vnf is needed for vFirewall case
287                         // vserver-name is needed for vLoadBalancer case
288                         //
289                         String genericVNF = $event.AAI.get("generic-vnf.vnf-id");
290                         String vserver = $event.AAI.get("vserver.vserver-name");
291                         //
292                         // Check if we are implementing a simple pattern match.
293                         //
294                         if ($params.getAaiPatternMatch() == 1) {
295                                 //
296                                 // Yes
297                                 //
298                                 //Basic naming characteristics:
299                                 //VF Name (9 char)+VM name (13 char total)+VFC (19 char total)
300                                 //Example: 
301                                 //VF Name (9 characters):    cscf0001v
302                                 //VM Name(13 characters): cscf0001vm001
303                                 //VFC name(19 characters): cscf0001vm001cfg001
304                                 //
305                                 // zdfw1fwl01fwl02 or zdfw1fwl01fwl01  
306                                 // replaced with
307                                 // zdfw1fwl01pgn02 or zdfw1fwl01pgn01
308                                 //
309                                 int index = genericVNF.lastIndexOf("fwl");
310                                 if (index == -1) {
311                                         System.err.println("The generic-vnf.vnf-id from DCAE Event is not valid.");
312                                 } else {
313                                         genericVNF = genericVNF.substring(0, index) + "pgn" + genericVNF.substring(index+"fwl".length());
314                                 }
315                                 //
316                                 // Construct an APPC request
317                                 //
318                                 ControlLoopOperation operation = new ControlLoopOperation();
319                                 operation.actor = $params.getActor();
320                                 operation.operation = "ModifyConfig";
321                                 operation.target = $event.target;
322                                 //
323                                 // Create operationWrapper
324                                 //
325                                 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
326                                 //
327                                 // insert operationWrapper into memory
328                                 //
329                                 insert(operationWrapper);
330                                 //
331                                 Request request = new Request();
332                                 request.CommonHeader = new CommonHeader();
333                                 request.CommonHeader.RequestID = $event.requestID;
334                                 request.Action = operation.operation;
335                                 request.Payload = new HashMap<String, Object>();
336                                 //
337                                 // Fill in the payload
338                                 //
339                                 request.Payload.put("generic-vnf.vnf-id", genericVNF);
340                                 //
341                                 PGRequest pgRequest = new PGRequest();
342                                 pgRequest.pgStreams = new PGStreams();
343                                 
344                                 PGStream pgStream;
345                                 for(int i = 0; i < 5; i++){
346                                         pgStream = new PGStream();
347                                         pgStream.streamId = "fw_udp"+(i+1);
348                                         pgStream.isEnabled = "true";
349                                         pgRequest.pgStreams.pgStream.add(pgStream);
350                                 }
351                                 request.Payload.put("pg-streams", pgRequest.pgStreams);
352                                 
353                                 if (request != null) {
354                                         //
355                                         // Insert request into memory
356                                         //
357                                         insert(request);
358                                         //
359                                         // Tell interested parties we are performing this Operation
360                                         //
361                                         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
362                                         notification.notification = ControlLoopNotificationType.OPERATION;
363                                         // message and history ??
364                                         notification.from = "policy";
365                                         notification.policyName = drools.getRule().getName();
366                                         notification.policyScope = "${policyScope}";
367                                         notification.policyVersion = "${policyVersion}";
368                                         try {
369                                                 System.out.println(Serialization.gsonPretty.toJson(notification));
370                                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
371                                         } catch (Exception e) {
372                                                 System.out.println("Can't deliver notification: " + notification);
373                                                 e.printStackTrace();
374                                         }
375                                         //
376                                         // Now send the operation request
377                                         //
378                                         if (request instanceof Request) {
379                                                 try {
380                                                         System.out.println("APPC request sent:");
381                                                         System.out.println(Serialization.gsonPretty.toJson(request));
382                                                         PolicyEngine.manager.deliver($params.getAppcTopic(), request);
383                                                 } catch (Exception e) {
384                                                         e.printStackTrace();
385                                                         System.out.println("Can't deliver request: " + request);
386                                                 }
387                                         }
388                                 } else {
389                                         //
390                                         // what happens if it is null
391                                         //
392                                 }
393                                 //
394                         } else {
395                                 //
396                                 // create AAI named-query request with UUID started with "F199"
397                                 //
398                                 AAINQF199Request aainqf199request = new AAINQF199Request();
399                                 AAINQF199QueryParameters aainqf199queryparam = new AAINQF199QueryParameters();
400                                 AAINQF199NamedQuery aainqf199namedquery = new AAINQF199NamedQuery();
401                                 AAINQF199InstanceFilters aainqf199instancefilter = new AAINQF199InstanceFilters();
402                                 //
403                                 // queryParameters
404                                 //
405                                 aainqf199namedquery.namedQueryUUID = UUID.fromString($params.getAaiNamedQueryUUID()); 
406                                 aainqf199queryparam.namedQuery = aainqf199namedquery;
407                                 aainqf199request.queryParameters = aainqf199queryparam;
408                                 //
409                                 // instanceFilters
410                                 //
411                                 Map aainqf199instancefiltermap = new HashMap();
412                                 Map aainqf199instancefiltermapitem = new HashMap();
413                                 aainqf199instancefiltermapitem.put("vserver-name", vserver); 
414                                 aainqf199instancefiltermap.put("vserver", aainqf199instancefiltermapitem);
415                                 aainqf199instancefilter.instanceFilter.add(aainqf199instancefiltermap);
416                                 aainqf199request.instanceFilters = aainqf199instancefilter;
417                                 //
418                                 // print aainqf199request for debug
419                                 //
420                                 System.out.println("AAI Request sent:");
421                                 System.out.println(Serialization.gsonPretty.toJson(aainqf199request));
422                                 //
423                                 // Create AAINQF199RequestWrapper
424                                 //
425                                 AAINQF199RequestWrapper aainqf199RequestWrapper = new AAINQF199RequestWrapper($event.requestID, aainqf199request);
426                                 //
427                                 // insert aainqf199request into memory
428                                 //
429                                 insert(aainqf199RequestWrapper);
430                         }
431                         //
432                 } catch (Exception e) {
433                  e.printStackTrace();
434                 }
435 end
436
437 /*
438 *
439 * This rule happens when we got a valid ONSET, closed loop is enabled, an Event Manager
440 * is created, AAI Manager and AAI Request are ready in memory. We can start sending query to AAI and then wait for response.
441 *
442 */
443 rule "${policyName}.EVENT.MANAGER.AAINQF199REQUEST"
444         when
445                 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
446                 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
447                 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
448                 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
449         then
450                 System.out.println("rule EVENT.MANAGER.AAINQF199REQUEST is triggered.");
451                 //
452                 // send the request
453                 //
454                 AAINQF199Response aainqf199response = AAINQF199Manager.postQuery($params.getAaiURL(), $params.getAaiUsername(), $params.getAaiPassword(),
455                                                                                                            $aainqf199RequestWrapper.aainqf199request, $event.requestID);
456                 
457                 //
458                 // Check AAI response
459                 //
460                 if (aainqf199response == null) {
461                         System.err.println("Failed to get AAI response");
462                         //
463                         // Fail and retract everything
464                         //
465                         retract($event);
466                         retract($manager);
467                         retract($aainqf199RequestWrapper);
468                 } else {
469                         //
470                         // Create AAINQF199ResponseWrapper
471                         //
472                         AAINQF199ResponseWrapper aainqf199ResponseWrapper = new AAINQF199ResponseWrapper($event.requestID, aainqf199response);
473                         //
474                         // insert aainqf199ResponseWrapper to memeory
475                         //
476                         insert(aainqf199ResponseWrapper);
477                 }
478 end
479
480 /*
481 *
482 * This rule happens when we got a valid AAI response. We can start sending request to APPC or MSO now.
483 *
484 */
485 rule "${policyName}.EVENT.MANAGER.AAINQF199RESPONSE"
486         when 
487                 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
488                 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
489                 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
490                 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
491                 $aainqf199ResponseWrapper : AAINQF199ResponseWrapper(requestID == $event.requestID)
492         then
493                 System.out.println("rule EVENT.MANAGER.AAINQF199RESPONSE is triggered.");
494                 //
495                 // Extract related fields out of AAINQF199RESPONSE
496                 //
497                 String vnfItemVnfId, vnfItemVnfType, vnfItemPersonaModelId, vnfItemPersonaModelVersion, vnfItemModelName, 
498                        vnfItemModelVersion, vnfItemModelNameVersionId, serviceItemServiceInstanceId, serviceItemPersonaModelId,
499                        serviceItemModelName, serviceItemModelType, serviceItemModelVersion, serviceItemModelNameVersionId,
500                        vfModuleItemVfModuleName, vfModuleItemPersonaModelId, vfModuleItemPersonaModelVersion, vfModuleItemModelName, 
501                        vfModuleItemModelNameVersionId, tenantItemTenantId, cloudRegionItemCloudRegionId;
502                 try {
503                         //
504                         // vnfItem
505                         //
506                         vnfItemVnfId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfID;
507                         vnfItemVnfType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfType;
508                         vnfItemVnfType = vnfItemVnfType.substring(vnfItemVnfType.lastIndexOf("/")+1);
509                         vnfItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelId;
510                         vnfItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelVersion;
511                         vnfItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
512                         vnfItemModelVersion =           $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(2).propertyValue;
513                         vnfItemModelNameVersionId =     $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
514                         //
515                         // serviceItem
516                         //
517                         serviceItemServiceInstanceId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.serviceInstanceID;
518                         serviceItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelId;
519                         serviceItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
520                         serviceItemModelType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(1).propertyValue;
521                         serviceItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelVersion;
522                         serviceItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
523                         //
524                         // Find the index for base vf module and non-base vf module
525                         //
526                         int baseIndex = -1;
527                         int nonBaseIndex = -1;
528                         List<AAINQF199InventoryResponseItem> inventoryItems = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems;
529                         for (AAINQF199InventoryResponseItem m : inventoryItems) {
530                                 if (m.vfModule != null && m.vfModule.isBaseVfModule == true) {
531                                         baseIndex = inventoryItems.indexOf(m);
532                                 } else if (m.vfModule != null && m.vfModule.isBaseVfModule == false && m.vfModule.orchestrationStatus == null) {
533                                         nonBaseIndex = inventoryItems.indexOf(m);
534                                 }
535                                 //
536                                 if (baseIndex != -1 && nonBaseIndex != -1) {
537                                         break;
538                                 }
539                         }
540                         //
541                         // Report the error if either base vf module or non-base vf module is not found
542                         //
543                         if (baseIndex == -1 || nonBaseIndex == -1) {
544                                 System.err.println("Either base or non-base vf module is not found from AAI response.");
545                                 retract($aainqf199RequestWrapper);
546                                 retract($aainqf199ResponseWrapper);
547                                 retract($manager);
548                                 retract($event);
549                                 return;
550                         }
551                         //
552                         // This comes from the base module
553                         //
554                         vfModuleItemVfModuleName =                      $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(baseIndex).vfModule.vfModuleName;
555                         vfModuleItemVfModuleName = vfModuleItemVfModuleName.replace("Vfmodule", "vDNS");
556                         //
557                         // vfModuleItem - NOT the base module
558                         //
559                         vfModuleItemPersonaModelId =            $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelId;
560                         vfModuleItemPersonaModelVersion =       $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelVersion;
561                         vfModuleItemModelName =                         $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(0).propertyValue;
562                         vfModuleItemModelNameVersionId =        $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(4).propertyValue;
563                         //
564                         // tenantItem
565                         //
566                         tenantItemTenantId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).tenant.tenantId;
567                         //
568                         // cloudRegionItem
569                         //
570                         cloudRegionItemCloudRegionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).items.inventoryResponseItems.get(0).cloudRegion.cloudRegionId;
571                         //
572                 } catch (Exception e) {
573                         e.printStackTrace();
574                         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
575                         notification.notification = ControlLoopNotificationType.REJECTED;
576                         notification.message = "Exception occurred " + e.getMessage();
577                         notification.policyName = drools.getRule().getName();
578                         notification.policyScope = "${policyScope}";
579                         notification.policyVersion = "${policyVersion}";
580                         //
581                         try {
582                                 System.out.println(Serialization.gsonPretty.toJson(notification));
583                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
584                         } catch (Exception e1) {
585                                 System.out.println("Can't deliver notification: " + notification);
586                                 e1.printStackTrace();
587                         }
588                         //
589                         notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
590                         notification.message = "Invalid named-query response from AAI";
591             //
592             try {
593                 System.out.println(Serialization.gsonPretty.toJson(notification));
594                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
595             } catch (Exception e1) {
596                 System.out.println("Can't deliver notification: " + notification);
597                 e1.printStackTrace();
598             }
599                         //
600                         // Retract everything
601                         //
602                         retract($aainqf199RequestWrapper);
603                         retract($aainqf199ResponseWrapper);
604                         retract($manager);
605                         retract($event);
606                         return;
607                 }       
608                 //
609                 // Extracted fields should not be null
610                 //
611                 if ((vnfItemVnfId == null) || (vnfItemVnfType == null) ||
612                     (vnfItemPersonaModelId == null) || (vnfItemModelName == null) ||
613                     (vnfItemModelVersion == null) || (vnfItemModelNameVersionId == null) ||
614                     (serviceItemServiceInstanceId == null) || (serviceItemModelName == null) ||
615                     (serviceItemModelType == null) || (serviceItemModelVersion == null) ||
616                     (serviceItemModelNameVersionId == null) || (vfModuleItemVfModuleName == null) ||
617                     (vfModuleItemPersonaModelId == null) || (vfModuleItemPersonaModelVersion == null) ||
618                     (vfModuleItemModelName == null) || (vfModuleItemModelNameVersionId == null) ||
619                     (tenantItemTenantId == null) || (cloudRegionItemCloudRegionId == null)) {
620                         //
621                         System.err.println("some fields are missing from AAI response.");
622                         //
623                         // Fail and retract everything
624                         //
625                         retract($aainqf199RequestWrapper);
626                         retract($aainqf199ResponseWrapper);
627                         retract($manager);
628                         retract($event);
629                         return;
630                 }
631                 //
632                 // We don't need them any more
633                 //
634                 retract($aainqf199ResponseWrapper);
635                 retract($aainqf199RequestWrapper);  
636                 //
637                 // check the actor of this closed loop
638                 //
639                 switch ($params.getActor()) {
640                         case "APPC":
641                         {
642                                 //
643                                 // Construct an APPC request
644                                 //
645                                 ControlLoopOperation operation = new ControlLoopOperation();
646                                 operation.actor = $params.getActor();
647                                 operation.operation = "ModifyConfig";
648                                 operation.target = $event.target;
649                                 //
650                                 // Create operationWrapper
651                                 //
652                                 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
653                                 //
654                                 // insert operationWrapper into memory
655                                 //
656                                 insert(operationWrapper);
657                                 //
658                                 Request request = new Request();
659                                 request.CommonHeader = new CommonHeader();
660                                 request.CommonHeader.RequestID = $event.requestID;
661                                 request.Action = operation.operation;
662                                 request.Payload = new HashMap<String, Object>();
663                                 //
664                                 // Fill in the payload
665                                 // Hardcode genericVNF for now since AAI has not been ready for vFirewall demo case
666                                 //
667                                 String genericVNF = "zdfw1fwl01pgn02";
668                                 request.Payload.put("generic-vnf.vnf-id", genericVNF);
669                                 //
670                                 PGRequest pgRequest = new PGRequest();
671                                 pgRequest.pgStreams = new PGStreams();
672                                 
673                                 PGStream pgStream;
674                                 for(int i = 0; i < 5; i++){
675                                         pgStream = new PGStream();
676                                         pgStream.streamId = "fw_udp"+(i+1);
677                                         pgStream.isEnabled = "true";
678                                         pgRequest.pgStreams.pgStream.add(pgStream);
679                                 }
680                                 request.Payload.put("pg-streams", pgRequest.pgStreams);
681                                 
682                                 if (request != null) {
683                                         //
684                                         // Insert request into memory
685                                         //
686                                         insert(request);
687                                         //
688                                         // Tell interested parties we are performing this Operation
689                                         //
690                                         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
691                                         notification.notification = ControlLoopNotificationType.OPERATION;
692                                         // message and history ??
693                                         notification.from = "policy";
694                                         notification.policyName = drools.getRule().getName();
695                                         notification.policyScope = "${policyScope}";
696                                         notification.policyVersion = "${policyVersion}";
697                                         try {
698                                                 System.out.println(Serialization.gsonPretty.toJson(notification));
699                                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
700                                         } catch (Exception e) {
701                                                 System.out.println("Can't deliver notification: " + notification);
702                                                 e.printStackTrace();
703                                         }
704                                         //
705                                         // Now send the operation request
706                                         //
707                                         if (request instanceof Request) {
708                                                 try {
709                                                         System.out.println("APPC request sent:");
710                                                         System.out.println(Serialization.gsonPretty.toJson(request));
711                                                         PolicyEngine.manager.deliver($params.getAppcTopic(), request);
712                                                 } catch (Exception e) {
713                                                         e.printStackTrace();
714                                                         System.out.println("Can't deliver request: " + request);
715                                                 }
716                                         }
717                                 } else {
718                                         //
719                                         // what happens if it is null
720                                         //
721                                 }
722                         }
723                         break;
724                         case "MSO":
725                         {
726                                 //
727                                 // Construct an operation
728                                 //
729                                 ControlLoopOperation operation = new ControlLoopOperation();
730                                 operation.actor = $params.getActor();
731                                 operation.operation = "createModuleInstance";
732                                 operation.target = $event.target;
733                                 //
734                                 // Create operationWrapper
735                                 //
736                                 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
737                                 //
738                                 // Construct an MSO request
739                                 //
740                                 MSORequest request = new MSORequest();
741                                 request.requestDetails = new MSORequestDetails();
742                                 request.requestDetails.modelInfo = new MSOModelInfo();
743                                 request.requestDetails.cloudConfiguration = new MSOCloudConfiguration();
744                                 request.requestDetails.requestInfo = new MSORequestInfo();
745                                 request.requestDetails.requestParameters = new MSORequestParameters();
746                                 request.requestDetails.requestParameters.userParams = null;
747                                 //
748                                 // cloudConfiguration
749                                 //
750                                 request.requestDetails.cloudConfiguration.lcpCloudRegionId = cloudRegionItemCloudRegionId;
751                                 request.requestDetails.cloudConfiguration.tenantId = tenantItemTenantId;
752                                 //
753                                 // modelInfo
754                                 //
755                                 request.requestDetails.modelInfo.modelType = "vfModule";
756                                 request.requestDetails.modelInfo.modelInvariantId = vfModuleItemPersonaModelId;
757                                 request.requestDetails.modelInfo.modelNameVersionId = vfModuleItemModelNameVersionId;
758                                 request.requestDetails.modelInfo.modelName = vfModuleItemModelName;
759                                 request.requestDetails.modelInfo.modelVersion = vfModuleItemPersonaModelVersion;
760                                 //
761                                 // requestInfo
762                                 //
763                                 request.requestDetails.requestInfo.instanceName = vfModuleItemVfModuleName;
764                                 request.requestDetails.requestInfo.source = "POLICY";
765                                 request.requestDetails.requestInfo.suppressRollback = false;
766                                 //
767                                 // relatedInstanceList
768                                 //
769                                 MSORelatedInstanceListElement relatedInstanceListElement1 = new MSORelatedInstanceListElement();
770                                 MSORelatedInstanceListElement relatedInstanceListElement2 = new MSORelatedInstanceListElement();
771                                 relatedInstanceListElement1.relatedInstance = new MSORelatedInstance();
772                                 relatedInstanceListElement2.relatedInstance = new MSORelatedInstance();
773                                 //
774                                 relatedInstanceListElement1.relatedInstance.instanceId = serviceItemServiceInstanceId;
775                                 relatedInstanceListElement1.relatedInstance.modelInfo = new MSOModelInfo();
776                                 relatedInstanceListElement1.relatedInstance.modelInfo.modelType = "service";
777                                 relatedInstanceListElement1.relatedInstance.modelInfo.modelInvariantId = serviceItemPersonaModelId;
778                                 relatedInstanceListElement1.relatedInstance.modelInfo.modelNameVersionId = serviceItemModelNameVersionId;
779                                 relatedInstanceListElement1.relatedInstance.modelInfo.modelName = serviceItemModelName;
780                                 relatedInstanceListElement1.relatedInstance.modelInfo.modelVersion = serviceItemModelVersion;
781                                 //
782                                 relatedInstanceListElement2.relatedInstance.instanceId = vnfItemVnfId;
783                                 relatedInstanceListElement2.relatedInstance.modelInfo = new MSOModelInfo();
784                                 relatedInstanceListElement2.relatedInstance.modelInfo.modelType = "vnf";
785                                 relatedInstanceListElement2.relatedInstance.modelInfo.modelInvariantId = vnfItemPersonaModelId;
786                                 relatedInstanceListElement2.relatedInstance.modelInfo.modelNameVersionId = vnfItemModelNameVersionId;
787                                 relatedInstanceListElement2.relatedInstance.modelInfo.modelName = vnfItemModelName;
788                                 relatedInstanceListElement2.relatedInstance.modelInfo.modelVersion = vnfItemModelVersion;
789                                 relatedInstanceListElement2.relatedInstance.modelInfo.modelCustomizationName = vnfItemVnfType;
790                                 //      
791                                 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement1);
792                                 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement2);
793                                 //
794                                 // print MSO request for debug
795                                 //
796                                 System.out.println("MSO request sent:");
797                                 System.out.println(Serialization.gsonPretty.toJson(request));
798                                 //
799                                 //
800                                 //
801                                 if (request != null) {
802                                         //
803                                         // Tell interested parties we are performing this Operation
804                                         //
805                                         VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
806                                         notification.notification = ControlLoopNotificationType.OPERATION;
807                                         notification.from = "policy";
808                                         notification.policyName = drools.getRule().getName();
809                                         notification.policyScope = "${policyScope}";
810                                         notification.policyVersion = "${policyVersion}";
811                                         try {
812                                                 System.out.println(Serialization.gsonPretty.toJson(notification));
813                                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
814                                         } catch (Exception e) {
815                                                 System.out.println("Can't deliver notification: " + notification);
816                                                 e.printStackTrace();
817                                         }
818                                         //
819                                         // Concatenate serviceItemServiceInstanceId and vnfItemVnfId to msoURL
820                                         //
821                                         String MSOUrl = $params.getMsoURL() + "/serviceInstances/v2/" + serviceItemServiceInstanceId + "/vnfs/" + vnfItemVnfId + "/vfModules";
822                                         //
823                                         // Call MSO
824                                         //
825                                         MSOResponse response = MSOManager.createModuleInstance(MSOUrl, $params.getMsoURL(), $params.getMsoUsername(), $params.getMsoPassword(), request);
826                                         
827                                         
828                                         if (response != null) {
829                                                 //
830                                                 // Assign requestId
831                                                 //
832                                                 request.requestId = $event.requestID.toString();                                                
833                                                 response.request.requestId = $event.requestID.toString();
834                                                 //
835                                                 // Insert facts
836                                                 //
837                                                 insert(operationWrapper);
838                                                 insert(request);
839                                                 insert(response);
840                                         } else {
841                                                 //
842                                                 // MSO request not even accepted
843                                                 //
844                                                 notification.message = operationWrapper.operation.toMessage();
845                                                 operationWrapper.operation.message = operationWrapper.operation.toMessage();
846                                                 operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
847                                                 $manager.setControlLoopResult("FAILURE_EXCEPTION");
848                                                 notification.history.add(operationWrapper.operation);
849                                                 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
850                                                 //
851                                                 // Let interested parties know
852                                                 //
853                                                 try {
854                                                         System.out.println(Serialization.gsonPretty.toJson(notification));
855                                                         PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
856                                                 } catch (Exception e) {
857                                                         System.out.println("Can't deliver notification: " + notification);
858                                                         e.printStackTrace();
859                                                 }
860                             notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
861                         try {
862                                 System.out.println(Serialization.gsonPretty.toJson(notification));
863                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
864                         } catch (Exception e) {
865                                 System.out.println("Can't deliver notification: " + notification);
866                                 e.printStackTrace();
867                         }
868                                                 //
869                                                 // Retract everything
870                                                 //
871                                                 retract($event);
872                                                 retract($manager);
873                                         }
874                                 } else {
875                                         System.err.println("constructed MSO request is invalid.");
876                                 }
877                         }
878                         break; 
879                 } 
880 end
881                 
882 /*
883 *
884 * This rule responds to APPC Response Events
885 *
886 */
887 rule "${policyName}.APPC.RESPONSE"
888         when
889                 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
890                 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
891                 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
892                 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
893                 $request : Request( getCommonHeader().RequestID == $event.requestID )
894                 $response : Response( getCommonHeader().RequestID == $event.requestID ) 
895         then
896                 System.out.println("rule APPC.RESPONSE is triggered.");
897                 if ($response.Status == null) {
898                         $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
899                         $manager.setControlLoopResult("FAILURE_EXCEPTION");
900                 } 
901                 //
902                 // Get the Response Code
903                 //
904                 ResponseCode code = ResponseCode.toResponseCode($response.Status.Code);
905                 if (code == null) {
906                         $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
907                         $manager.setControlLoopResult("FAILURE_EXCEPTION");
908                 }
909                 //
910                 // Construct notification
911                 //
912                 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
913                 notification.from = "policy";
914                 notification.policyName = drools.getRule().getName();
915                 notification.policyScope = "${policyScope}";
916                 notification.policyVersion = "${policyVersion}";
917                 notification.message = $operationWrapper.operation.toMessage();
918                 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
919                 //
920                 // Ok, let's figure out what APP-C's response is
921                 //
922                 switch (code) {
923                         case ACCEPT:
924                                 $operationWrapper.operation.outcome = "PROCESSING";
925                                 break;
926                         case ERROR:
927                         case REJECT:
928                                 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
929                                 $manager.setControlLoopResult("FAILURE_EXCEPTION");
930                                 break;
931                         case SUCCESS:
932                                 $operationWrapper.operation.outcome = "SUCCESS";
933                                 $manager.setControlLoopResult("SUCCESS");
934                                 break;
935                         case FAILURE:
936                                 $operationWrapper.operation.outcome = "FAILURE";
937                                 $manager.setControlLoopResult("FAILURE");
938                                 break;
939                 }
940                 if ($operationWrapper.operation.outcome.equals("SUCCESS")) {
941                         notification.history.add($operationWrapper.operation);
942                         notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
943                         //
944                         // Let interested parties know
945                         //
946                         try {
947                                 System.out.println(Serialization.gsonPretty.toJson(notification));
948                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
949                         } catch (Exception e) {
950                                 System.out.println("Can't deliver notification: " + notification);
951                                 e.printStackTrace();
952                         }
953             notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
954             try {
955                 System.out.println(Serialization.gsonPretty.toJson(notification));
956                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
957             } catch (Exception e) {
958                 System.out.println("Can't deliver notification: " + notification);
959                 e.printStackTrace();
960             }
961
962                         //
963                         // We are going to retract these objects from memory
964                         //
965                         System.out.println("Retracting everything");
966                         retract($operationWrapper);
967                         retract($request);
968                         retract($response);
969                         retract($event);
970                         retract($manager);
971                 } else if ($operationWrapper.operation.outcome.equals("PROCESSING")) {
972                         retract($response);
973                 } else {
974                         notification.history.add($operationWrapper.operation);
975                         notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
976                         //
977                         // Let interested parties know
978                         //
979                         try {
980                                 System.out.println(Serialization.gsonPretty.toJson(notification));
981                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
982                         } catch (Exception e) {
983                                 System.out.println("Can't deliver notification: " + notification);
984                                 e.printStackTrace();
985                         }
986             notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
987             //
988             // Let interested parties know
989             //
990             try {
991                 System.out.println(Serialization.gsonPretty.toJson(notification));
992                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
993             } catch (Exception e) {
994                 System.out.println("Can't deliver notification: " + notification);
995                 e.printStackTrace();
996             }
997                         //
998                         // We are going to retract these objects from memory
999                         //
1000                         System.out.println("Retracting everything");
1001                         retract($operationWrapper);
1002                         retract($request);
1003                         retract($response);
1004                         retract($event);
1005                         retract($manager);
1006                 }
1007                 
1008 end             
1009
1010 /*
1011 *
1012 * This rule is used to clean up APPC response
1013 *
1014 */              
1015 rule "${policyName}.APPC.RESPONSE.CLEANUP"
1016         when
1017                 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1018         $response : Response($id : getCommonHeader().RequestID )
1019                 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) ) 
1020         then
1021                 System.out.println("rule APPC.RESPONSE.CLEANUP is triggered.");
1022                 retract($response);
1023 end
1024
1025 /*
1026 *
1027 * This rule responds to MSO Response Events
1028 *
1029 */
1030 rule "${policyName}.MSO.RESPONSE"
1031         when
1032                 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1033                 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1034                 $manager : ControlLoopEventManager( closedLoopControlName == $event.closedLoopControlName )
1035                 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
1036                 $request : MSORequest( requestId == $event.requestID.toString() )
1037                 $response : MSOResponse( request.requestId == $event.requestID.toString() )     
1038         then
1039                 System.out.println("rule MSO.RESPONSE is triggered.");
1040                 //
1041                 // Construct notification
1042                 //
1043                 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1044                 notification.from = "policy";
1045                 notification.policyName = drools.getRule().getName();
1046                 notification.policyScope = "${policyScope}";
1047                 notification.policyVersion = "${policyVersion}";
1048                 notification.message = $operationWrapper.operation.toMessage();
1049                 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
1050                 //
1051                 // The operation can either be succeeded or failed
1052                 // 
1053                 if($response.request.requestStatus.requestState.equals("COMPLETE")) {
1054                         $operationWrapper.operation.outcome = "SUCCESS";
1055                         $manager.setControlLoopResult("SUCCESS");
1056                         notification.history.add($operationWrapper.operation);
1057                         notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
1058                         //
1059                         // Let interested parties know
1060                         //
1061                         try {
1062                                 System.out.println(Serialization.gsonPretty.toJson(notification));
1063                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1064                         } catch (Exception e) {
1065                                 System.out.println("Can't deliver notification: " + notification);
1066                                 e.printStackTrace();
1067                         }
1068                         notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
1069             //
1070             // Let interested parties know
1071             //
1072             try {
1073                 System.out.println(Serialization.gsonPretty.toJson(notification));
1074                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1075             } catch (Exception e) {
1076                 System.out.println("Can't deliver notification: " + notification);
1077                 e.printStackTrace();
1078             }
1079             //
1080                         // We are going to retract these objects from memory
1081                         //
1082                         System.out.println("Retracting everything");
1083                         retract($operationWrapper);
1084                         retract($request);
1085                         retract($response);
1086                         retract($event);
1087                         retract($manager);
1088                 } else {
1089                         $operationWrapper.operation.outcome = "FAILURE";
1090                         $manager.setControlLoopResult("FAILURE");
1091                         notification.history.add($operationWrapper.operation);
1092                         notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1093                         //
1094                         // Let interested parties know
1095                         //
1096                         try {
1097                                 System.out.println(Serialization.gsonPretty.toJson(notification));
1098                                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1099                         } catch (Exception e) {
1100                                 System.out.println("Can't deliver notification: " + notification);
1101                                 e.printStackTrace();
1102                         }
1103                         notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
1104             //
1105             // Let interested parties know
1106             //
1107             try {
1108                 System.out.println(Serialization.gsonPretty.toJson(notification));
1109                 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1110             } catch (Exception e) {
1111                 System.out.println("Can't deliver notification: " + notification);
1112                 e.printStackTrace();
1113             }
1114             //
1115                         // We are going to retract these objects from memory
1116                         //
1117                         System.out.println("Retracting everything");
1118                         retract($operationWrapper);
1119                         retract($request);
1120                         retract($response);
1121                         retract($event);
1122                         retract($manager);
1123                 }               
1124 end
1125
1126 /*
1127  * Sample Queries for illustration with and without arguments
1128  * The results of the query can be introspected (and retracted if desired)
1129  * through REST API.
1130  */
1131 query "${policyName}.QUERY.EVENT.ONSET"
1132         event : VirtualControlLoopEvent( closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1133 end
1134
1135 query "${policyName}.QUERY.MANAGER.RNA" (String aRequestId, Integer numOnsetsLowerBound, Boolean aActivated)
1136         manager : ControlLoopEventManager( closedLoopControlName == "${closedLoopControlName}", 
1137                                 requestID.toString() == aRequestId, 
1138                                 numOnsets > numOnsetsLowerBound,
1139                                 activated == aActivated )
1140 end