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