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