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