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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.controlloop;
23 import java.util.List;
24 import java.util.LinkedList;
26 import java.util.HashMap;
27 import java.util.UUID;
29 import org.openecomp.policy.controlloop.VirtualControlLoopEvent;
30 import org.openecomp.policy.controlloop.ControlLoopEventStatus;
31 import org.openecomp.policy.controlloop.VirtualControlLoopNotification;
32 import org.openecomp.policy.controlloop.ControlLoopNotificationType;
33 import org.openecomp.policy.controlloop.ControlLoopOperation;
34 import org.openecomp.policy.controlloop.ControlLoopOperationWrapper;
35 import org.openecomp.policy.template.demo.ControlLoopException;
37 import org.openecomp.policy.aai.AAINQF199.AAINQF199CloudRegion;
38 import org.openecomp.policy.aai.AAINQF199.AAINQF199ExtraProperties;
39 import org.openecomp.policy.aai.AAINQF199.AAINQF199ExtraProperty;
40 import org.openecomp.policy.aai.AAINQF199.AAINQF199GenericVNF;
41 import org.openecomp.policy.aai.AAINQF199.AAINQF199InstanceFilters;
42 import org.openecomp.policy.aai.AAINQF199.AAINQF199InventoryResponseItem;
43 import org.openecomp.policy.aai.AAINQF199.AAINQF199InventoryResponseItems;
44 import org.openecomp.policy.aai.AAINQF199.AAINQF199Manager;
45 import org.openecomp.policy.aai.AAINQF199.AAINQF199NamedQuery;
46 import org.openecomp.policy.aai.AAINQF199.AAINQF199QueryParameters;
47 import org.openecomp.policy.aai.AAINQF199.AAINQF199Request;
48 import org.openecomp.policy.aai.AAINQF199.AAINQF199RequestWrapper;
49 import org.openecomp.policy.aai.AAINQF199.AAINQF199Response;
50 import org.openecomp.policy.aai.AAINQF199.AAINQF199ResponseWrapper;
51 import org.openecomp.policy.aai.AAINQF199.AAINQF199ServiceInstance;
52 import org.openecomp.policy.aai.AAINQF199.AAINQF199Tenant;
53 import org.openecomp.policy.aai.AAINQF199.AAINQF199VfModule;
54 import org.openecomp.policy.aai.AAINQF199.AAINQF199VServer;
55 import org.openecomp.policy.aai.util.Serialization;
57 import org.openecomp.policy.appc.CommonHeader;
58 import org.openecomp.policy.appc.Request;
59 import org.openecomp.policy.appc.Response;
60 import org.openecomp.policy.appc.ResponseCode;
61 import org.openecomp.policy.appc.ResponseStatus;
62 import org.openecomp.policy.appc.ResponseValue;
64 import org.openecomp.policy.template.demo.EventManager;
65 import org.openecomp.policy.vnf.trafficgenerator.PGRequest;
66 import org.openecomp.policy.vnf.trafficgenerator.PGStream;
67 import org.openecomp.policy.vnf.trafficgenerator.PGStreams;
69 import org.openecomp.policy.mso.MSOManager;
70 import org.openecomp.policy.mso.MSORequest;
71 import org.openecomp.policy.mso.MSORequestStatus;
72 import org.openecomp.policy.mso.MSORequestDetails;
73 import org.openecomp.policy.mso.MSOModelInfo;
74 import org.openecomp.policy.mso.MSOCloudConfiguration;
75 import org.openecomp.policy.mso.MSORequestInfo;
76 import org.openecomp.policy.mso.MSORequestParameters;
77 import org.openecomp.policy.mso.MSORelatedInstanceListElement;
78 import org.openecomp.policy.mso.MSORelatedInstance;
79 import org.openecomp.policy.mso.MSOResponse;
81 import org.openecomp.policy.drools.system.PolicyEngine;
84 // These parameters are required to build the runtime policy
87 closedLoopControlName : String
95 aaiNamedQueryUUID : String
97 notificationTopic : String
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
108 rule "${policyName}.SETUP"
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}");
126 // This stays in memory as long as the rule is alive and running
134 * This rule responds to DCAE Events
137 rule "${policyName}.EVENT"
139 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
140 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
141 not ( EventManager( closedLoopControlName == $event.closedLoopControlName ))
143 System.out.println("rule EVENT is triggered.");
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/
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}";
158 // Let interested parties know
161 System.out.println(Serialization.gsonPretty.toJson(notification));
162 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
163 } catch (Exception e) {
165 System.out.println("Can't deliver notification: " + notification);
168 // Retract it from memory
171 System.out.println("Event with requestID=null has been retracted.");
174 // Create an EventManager
176 EventManager manager = new EventManager($params.getClosedLoopControlName(), $event.requestID, $event.target);
178 // Determine if EventManager can actively process the event (i.e. syntax)
180 VirtualControlLoopNotification notification = manager.activate($event);
181 notification.from = "policy";
182 notification.policyName = drools.getRule().getName();
183 notification.policyScope = "${policyScope}";
184 notification.policyVersion = "${policyVersion}";
186 // Are we actively pursuing this event?
188 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
190 // Insert Event Manager into memory, this will now kick off processing.
194 // Let interested parties know
197 System.out.println(Serialization.gsonPretty.toJson(notification));
198 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
199 } catch (Exception e) {
201 System.out.println("Can't deliver notification: " + notification);
205 // Let interested parties know
208 System.out.println(Serialization.gsonPretty.toJson(notification));
209 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
210 } catch (Exception e) {
212 System.out.println("Can't deliver notification: " + notification);
215 // Retract it from memory
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.
227 } catch (Exception e) {
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}";
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();
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.
258 rule "${policyName}.EVENT.MANAGER"
260 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
261 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
262 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName, controlLoopResult == null)
264 System.out.println("rule EVENT.MANAGER is triggered.");
266 // Check which event this is.
268 EventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
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.
274 if (eventStatus != EventManager.NEW_EVENT_STATUS.FIRST_ONSET) {
275 System.out.println("Retracting "+eventStatus+" Event.");
280 // Now the event in memory is first onset event
284 // Pull the known AAI field from the Event
286 // generic-vnf is needed for vFirewall case
287 // vserver-name is needed for vLoadBalancer case
289 String genericVNF = $event.AAI.get("generic-vnf.vnf-id");
290 String vserver = $event.AAI.get("vserver.vserver-name");
292 // Check if we are implementing a simple pattern match.
294 if ($params.getAaiPatternMatch() == 1) {
298 //Basic naming characteristics:
299 //VF Name (9 char)+VM name (13 char total)+VFC (19 char total)
301 //VF Name (9 characters): cscf0001v
302 //VM Name(13 characters): cscf0001vm001
303 //VFC name(19 characters): cscf0001vm001cfg001
305 // zdfw1fwl01fwl02 or zdfw1fwl01fwl01
307 // zdfw1fwl01pgn02 or zdfw1fwl01pgn01
309 int index = genericVNF.lastIndexOf("fwl");
311 System.err.println("The generic-vnf.vnf-id from DCAE Event is not valid.");
313 genericVNF = genericVNF.substring(0, index) + "pgn" + genericVNF.substring(index+"fwl".length());
316 // Construct an APPC request
318 ControlLoopOperation operation = new ControlLoopOperation();
319 operation.actor = $params.getActor();
320 operation.operation = "ModifyConfig";
321 operation.target = $event.target;
323 // Create operationWrapper
325 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
327 // insert operationWrapper into memory
329 insert(operationWrapper);
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>();
337 // Fill in the payload
339 request.Payload.put("generic-vnf.vnf-id", genericVNF);
341 PGRequest pgRequest = new PGRequest();
342 pgRequest.pgStreams = new PGStreams();
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);
351 request.Payload.put("pg-streams", pgRequest.pgStreams);
353 if (request != null) {
355 // Insert request into memory
359 // Tell interested parties we are performing this Operation
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}";
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);
376 // Now send the operation request
378 if (request instanceof Request) {
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) {
385 System.out.println("Can't deliver request: " + request);
390 // what happens if it is null
396 // create AAI named-query request with UUID started with "F199"
398 AAINQF199Request aainqf199request = new AAINQF199Request();
399 AAINQF199QueryParameters aainqf199queryparam = new AAINQF199QueryParameters();
400 AAINQF199NamedQuery aainqf199namedquery = new AAINQF199NamedQuery();
401 AAINQF199InstanceFilters aainqf199instancefilter = new AAINQF199InstanceFilters();
405 aainqf199namedquery.namedQueryUUID = UUID.fromString($params.getAaiNamedQueryUUID());
406 aainqf199queryparam.namedQuery = aainqf199namedquery;
407 aainqf199request.queryParameters = aainqf199queryparam;
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;
418 // print aainqf199request for debug
420 System.out.println("AAI Request sent:");
421 System.out.println(Serialization.gsonPretty.toJson(aainqf199request));
423 // Create AAINQF199RequestWrapper
425 AAINQF199RequestWrapper aainqf199RequestWrapper = new AAINQF199RequestWrapper($event.requestID, aainqf199request);
427 // insert aainqf199request into memory
429 insert(aainqf199RequestWrapper);
432 } catch (Exception e) {
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.
443 rule "${policyName}.EVENT.MANAGER.AAINQF199REQUEST"
445 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
446 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
447 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
448 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
450 System.out.println("rule EVENT.MANAGER.AAINQF199REQUEST is triggered.");
454 AAINQF199Response aainqf199response = AAINQF199Manager.postQuery($params.getAaiURL(), $params.getAaiUsername(), $params.getAaiPassword(),
455 $aainqf199RequestWrapper.aainqf199request, $event.requestID);
457 // Check AAI response
459 if (aainqf199response == null) {
460 System.err.println("Failed to get AAI response");
462 // Fail and retract everything
466 retract($aainqf199RequestWrapper);
469 // Create AAINQF199ResponseWrapper
471 AAINQF199ResponseWrapper aainqf199ResponseWrapper = new AAINQF199ResponseWrapper($event.requestID, aainqf199response);
473 // insert aainqf199ResponseWrapper to memeory
475 insert(aainqf199ResponseWrapper);
481 * This rule happens when we got a valid AAI response. We can start sending request to APPC or MSO now.
484 rule "${policyName}.EVENT.MANAGER.AAINQF199RESPONSE"
486 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
487 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
488 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
489 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
490 $aainqf199ResponseWrapper : AAINQF199ResponseWrapper(requestID == $event.requestID)
492 System.out.println("rule EVENT.MANAGER.AAINQF199RESPONSE is triggered.");
494 // Extract related fields out of AAINQF199RESPONSE
496 String vnfItemVnfId, vnfItemVnfType, vnfItemPersonaModelId, vnfItemPersonaModelVersion, vnfItemModelName,
497 vnfItemModelVersion, vnfItemModelNameVersionId, serviceItemServiceInstanceId, serviceItemPersonaModelId,
498 serviceItemModelName, serviceItemModelType, serviceItemModelVersion, serviceItemModelNameVersionId,
499 vfModuleItemVfModuleName, vfModuleItemPersonaModelId, vfModuleItemPersonaModelVersion, vfModuleItemModelName,
500 vfModuleItemModelNameVersionId, tenantItemTenantId, cloudRegionItemCloudRegionId;
505 vnfItemVnfId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfID;
506 vnfItemVnfType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfType;
507 vnfItemVnfType = vnfItemVnfType.substring(vnfItemVnfType.lastIndexOf("/")+1);
508 vnfItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelId;
509 vnfItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelVersion;
510 vnfItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
511 vnfItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(2).propertyValue;
512 vnfItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
516 serviceItemServiceInstanceId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.serviceInstanceID;
517 serviceItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelId;
518 serviceItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
519 serviceItemModelType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(1).propertyValue;
520 serviceItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelVersion;
521 serviceItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
523 // Find the index for base vf module and non-base vf module
526 int nonBaseIndex = -1;
527 List<AAINQF199InventoryResponseItem> inventoryItems = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems;
528 for (AAINQF199InventoryResponseItem m : inventoryItems) {
529 if (m.vfModule != null && m.vfModule.isBaseVfModule == true) {
530 baseIndex = inventoryItems.indexOf(m);
531 } else if (m.vfModule != null && m.vfModule.isBaseVfModule == false && m.vfModule.orchestrationStatus == null) {
532 nonBaseIndex = inventoryItems.indexOf(m);
535 if (baseIndex != -1 && nonBaseIndex != -1) {
540 // Report the error if either base vf module or non-base vf module is not found
542 if (baseIndex == -1 || nonBaseIndex == -1) {
543 System.err.println("Either base or non-base vf module is not found from AAI response.");
544 retract($aainqf199RequestWrapper);
545 retract($aainqf199ResponseWrapper);
551 // This comes from the base module
553 vfModuleItemVfModuleName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(baseIndex).vfModule.vfModuleName;
554 vfModuleItemVfModuleName = vfModuleItemVfModuleName.replace("Vfmodule", "vDNS");
556 // vfModuleItem - NOT the base module
558 vfModuleItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelId;
559 vfModuleItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelVersion;
560 vfModuleItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(0).propertyValue;
561 vfModuleItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(4).propertyValue;
565 tenantItemTenantId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).tenant.tenantId;
569 cloudRegionItemCloudRegionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).items.inventoryResponseItems.get(0).cloudRegion.cloudRegionId;
571 } catch (Exception e) {
573 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
574 notification.notification = ControlLoopNotificationType.REJECTED;
575 notification.message = "Exception occurred " + e.getMessage();
576 notification.policyName = drools.getRule().getName();
577 notification.policyScope = "${policyScope}";
578 notification.policyVersion = "${policyVersion}";
581 System.out.println(Serialization.gsonPretty.toJson(notification));
582 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
583 } catch (Exception e1) {
584 System.out.println("Can't deliver notification: " + notification);
585 e1.printStackTrace();
588 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
589 notification.message = "Invalid named-query response from AAI";
592 System.out.println(Serialization.gsonPretty.toJson(notification));
593 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
594 } catch (Exception e1) {
595 System.out.println("Can't deliver notification: " + notification);
596 e1.printStackTrace();
599 // Retract everything
601 retract($aainqf199RequestWrapper);
602 retract($aainqf199ResponseWrapper);
608 // Extracted fields should not be null
610 if ((vnfItemVnfId == null) || (vnfItemVnfType == null) ||
611 (vnfItemPersonaModelId == null) || (vnfItemModelName == null) ||
612 (vnfItemModelVersion == null) || (vnfItemModelNameVersionId == null) ||
613 (serviceItemServiceInstanceId == null) || (serviceItemModelName == null) ||
614 (serviceItemModelType == null) || (serviceItemModelVersion == null) ||
615 (serviceItemModelNameVersionId == null) || (vfModuleItemVfModuleName == null) ||
616 (vfModuleItemPersonaModelId == null) || (vfModuleItemPersonaModelVersion == null) ||
617 (vfModuleItemModelName == null) || (vfModuleItemModelNameVersionId == null) ||
618 (tenantItemTenantId == null) || (cloudRegionItemCloudRegionId == null)) {
620 System.err.println("some fields are missing from AAI response.");
622 // Fail and retract everything
624 retract($aainqf199RequestWrapper);
625 retract($aainqf199ResponseWrapper);
631 // We don't need them any more
633 retract($aainqf199ResponseWrapper);
634 retract($aainqf199RequestWrapper);
636 // check the actor of this closed loop
638 switch ($params.getActor()) {
642 // Construct an APPC request
644 ControlLoopOperation operation = new ControlLoopOperation();
645 operation.actor = $params.getActor();
646 operation.operation = "ModifyConfig";
647 operation.target = $event.target;
649 // Create operationWrapper
651 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
653 // insert operationWrapper into memory
655 insert(operationWrapper);
657 Request request = new Request();
658 request.CommonHeader = new CommonHeader();
659 request.CommonHeader.RequestID = $event.requestID;
660 request.Action = operation.operation;
661 request.Payload = new HashMap<String, Object>();
663 // Fill in the payload
664 // Hardcode genericVNF for now since AAI has not been ready for vFirewall demo case
666 String genericVNF = "zdfw1fwl01pgn02";
667 request.Payload.put("generic-vnf.vnf-id", genericVNF);
669 PGRequest pgRequest = new PGRequest();
670 pgRequest.pgStreams = new PGStreams();
673 for(int i = 0; i < 5; i++){
674 pgStream = new PGStream();
675 pgStream.streamId = "fw_udp"+(i+1);
676 pgStream.isEnabled = "true";
677 pgRequest.pgStreams.pgStream.add(pgStream);
679 request.Payload.put("pg-streams", pgRequest.pgStreams);
681 if (request != null) {
683 // Insert request into memory
687 // Tell interested parties we are performing this Operation
689 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
690 notification.notification = ControlLoopNotificationType.OPERATION;
691 // message and history ??
692 notification.from = "policy";
693 notification.policyName = drools.getRule().getName();
694 notification.policyScope = "${policyScope}";
695 notification.policyVersion = "${policyVersion}";
697 System.out.println(Serialization.gsonPretty.toJson(notification));
698 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
699 } catch (Exception e) {
700 System.out.println("Can't deliver notification: " + notification);
704 // Now send the operation request
706 if (request instanceof Request) {
708 System.out.println("APPC request sent:");
709 System.out.println(Serialization.gsonPretty.toJson(request));
710 PolicyEngine.manager.deliver($params.getAppcTopic(), request);
711 } catch (Exception e) {
713 System.out.println("Can't deliver request: " + request);
718 // what happens if it is null
726 // Construct an operation
728 ControlLoopOperation operation = new ControlLoopOperation();
729 operation.actor = $params.getActor();
730 operation.operation = "createModuleInstance";
731 operation.target = $event.target;
733 // Create operationWrapper
735 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
737 // Construct an MSO request
739 MSORequest request = new MSORequest();
740 request.requestDetails = new MSORequestDetails();
741 request.requestDetails.modelInfo = new MSOModelInfo();
742 request.requestDetails.cloudConfiguration = new MSOCloudConfiguration();
743 request.requestDetails.requestInfo = new MSORequestInfo();
744 request.requestDetails.requestParameters = new MSORequestParameters();
745 request.requestDetails.requestParameters.userParams = null;
747 // cloudConfiguration
749 request.requestDetails.cloudConfiguration.lcpCloudRegionId = cloudRegionItemCloudRegionId;
750 request.requestDetails.cloudConfiguration.tenantId = tenantItemTenantId;
754 request.requestDetails.modelInfo.modelType = "vfModule";
755 request.requestDetails.modelInfo.modelInvariantId = vfModuleItemPersonaModelId;
756 request.requestDetails.modelInfo.modelNameVersionId = vfModuleItemModelNameVersionId;
757 request.requestDetails.modelInfo.modelName = vfModuleItemModelName;
758 request.requestDetails.modelInfo.modelVersion = vfModuleItemPersonaModelVersion;
762 request.requestDetails.requestInfo.instanceName = vfModuleItemVfModuleName;
763 request.requestDetails.requestInfo.source = "POLICY";
764 request.requestDetails.requestInfo.suppressRollback = false;
766 // relatedInstanceList
768 MSORelatedInstanceListElement relatedInstanceListElement1 = new MSORelatedInstanceListElement();
769 MSORelatedInstanceListElement relatedInstanceListElement2 = new MSORelatedInstanceListElement();
770 relatedInstanceListElement1.relatedInstance = new MSORelatedInstance();
771 relatedInstanceListElement2.relatedInstance = new MSORelatedInstance();
773 relatedInstanceListElement1.relatedInstance.instanceId = serviceItemServiceInstanceId;
774 relatedInstanceListElement1.relatedInstance.modelInfo = new MSOModelInfo();
775 relatedInstanceListElement1.relatedInstance.modelInfo.modelType = "service";
776 relatedInstanceListElement1.relatedInstance.modelInfo.modelInvariantId = serviceItemPersonaModelId;
777 relatedInstanceListElement1.relatedInstance.modelInfo.modelNameVersionId = serviceItemModelNameVersionId;
778 relatedInstanceListElement1.relatedInstance.modelInfo.modelName = serviceItemModelName;
779 relatedInstanceListElement1.relatedInstance.modelInfo.modelVersion = serviceItemModelVersion;
781 relatedInstanceListElement2.relatedInstance.instanceId = vnfItemVnfId;
782 relatedInstanceListElement2.relatedInstance.modelInfo = new MSOModelInfo();
783 relatedInstanceListElement2.relatedInstance.modelInfo.modelType = "vnf";
784 relatedInstanceListElement2.relatedInstance.modelInfo.modelInvariantId = vnfItemPersonaModelId;
785 relatedInstanceListElement2.relatedInstance.modelInfo.modelNameVersionId = vnfItemModelNameVersionId;
786 relatedInstanceListElement2.relatedInstance.modelInfo.modelName = vnfItemModelName;
787 relatedInstanceListElement2.relatedInstance.modelInfo.modelVersion = vnfItemModelVersion;
788 relatedInstanceListElement2.relatedInstance.modelInfo.modelCustomizationName = vnfItemVnfType;
790 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement1);
791 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement2);
793 // print MSO request for debug
795 System.out.println("MSO request sent:");
796 System.out.println(Serialization.gsonPretty.toJson(request));
800 if (request != null) {
802 // Tell interested parties we are performing this Operation
804 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
805 notification.notification = ControlLoopNotificationType.OPERATION;
806 notification.from = "policy";
807 notification.policyName = drools.getRule().getName();
808 notification.policyScope = "${policyScope}";
809 notification.policyVersion = "${policyVersion}";
811 System.out.println(Serialization.gsonPretty.toJson(notification));
812 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
813 } catch (Exception e) {
814 System.out.println("Can't deliver notification: " + notification);
818 // Concatenate serviceItemServiceInstanceId and vnfItemVnfId to msoURL
820 String MSOUrl = $params.getMsoURL() + "/serviceInstances/v2/" + serviceItemServiceInstanceId + "/vnfs/" + vnfItemVnfId + "/vfModules";
824 MSOResponse response = MSOManager.createModuleInstance(MSOUrl, $params.getMsoURL(), $params.getMsoUsername(), $params.getMsoPassword(), request);
826 if (response != null) {
830 request.requestId = $event.requestID.toString();
831 response.request.requestId = $event.requestID.toString();
835 insert(operationWrapper);
840 // MSO request not even accepted
842 notification.message = operationWrapper.operation.toMessage();
843 operationWrapper.operation.message = operationWrapper.operation.toMessage();
844 operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
845 $manager.setControlLoopResult("FAILURE_EXCEPTION");
846 notification.history.add(operationWrapper.operation);
847 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
849 // Let interested parties know
852 System.out.println(Serialization.gsonPretty.toJson(notification));
853 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
854 } catch (Exception e) {
855 System.out.println("Can't deliver notification: " + notification);
858 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
860 System.out.println(Serialization.gsonPretty.toJson(notification));
861 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
862 } catch (Exception e) {
863 System.out.println("Can't deliver notification: " + notification);
867 // Retract everything
873 System.err.println("constructed MSO request is invalid.");
882 * This rule responds to APPC Response Events
885 rule "${policyName}.APPC.RESPONSE"
887 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
888 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
889 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
890 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
891 $request : Request( getCommonHeader().RequestID == $event.requestID )
892 $response : Response( getCommonHeader().RequestID == $event.requestID )
894 System.out.println("rule APPC.RESPONSE is triggered.");
895 if ($response.Status == null) {
896 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
897 $manager.setControlLoopResult("FAILURE_EXCEPTION");
900 // Get the Response Code
902 ResponseCode code = ResponseCode.toResponseCode($response.Status.Code);
904 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
905 $manager.setControlLoopResult("FAILURE_EXCEPTION");
908 // Construct notification
910 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
911 notification.from = "policy";
912 notification.policyName = drools.getRule().getName();
913 notification.policyScope = "${policyScope}";
914 notification.policyVersion = "${policyVersion}";
915 notification.message = $operationWrapper.operation.toMessage();
916 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
918 // Ok, let's figure out what APP-C's response is
922 $operationWrapper.operation.outcome = "PROCESSING";
926 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
927 $manager.setControlLoopResult("FAILURE_EXCEPTION");
930 $operationWrapper.operation.outcome = "SUCCESS";
931 $manager.setControlLoopResult("SUCCESS");
934 $operationWrapper.operation.outcome = "FAILURE";
935 $manager.setControlLoopResult("FAILURE");
938 if ($operationWrapper.operation.outcome.equals("SUCCESS")) {
939 notification.history.add($operationWrapper.operation);
940 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
942 // Let interested parties know
945 System.out.println(Serialization.gsonPretty.toJson(notification));
946 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
947 } catch (Exception e) {
948 System.out.println("Can't deliver notification: " + notification);
951 notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
953 System.out.println(Serialization.gsonPretty.toJson(notification));
954 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
955 } catch (Exception e) {
956 System.out.println("Can't deliver notification: " + notification);
961 // We are going to retract these objects from memory
963 System.out.println("Retracting everything");
964 retract($operationWrapper);
969 } else if ($operationWrapper.operation.outcome.equals("PROCESSING")) {
972 notification.history.add($operationWrapper.operation);
973 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
975 // Let interested parties know
978 System.out.println(Serialization.gsonPretty.toJson(notification));
979 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
980 } catch (Exception e) {
981 System.out.println("Can't deliver notification: " + notification);
984 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
986 // Let interested parties know
989 System.out.println(Serialization.gsonPretty.toJson(notification));
990 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
991 } catch (Exception e) {
992 System.out.println("Can't deliver notification: " + notification);
996 // We are going to retract these objects from memory
998 System.out.println("Retracting everything");
999 retract($operationWrapper);
1010 * This rule is used to clean up APPC response
1013 rule "${policyName}.APPC.RESPONSE.CLEANUP"
1015 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1016 $response : Response($id : getCommonHeader().RequestID )
1017 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1019 System.out.println("rule APPC.RESPONSE.CLEANUP is triggered.");
1025 * This rule responds to MSO Response Events
1028 rule "${policyName}.MSO.RESPONSE"
1030 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1031 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1032 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
1033 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
1034 $request : MSORequest( requestId == $event.requestID.toString() )
1035 $response : MSOResponse( request.requestId == $event.requestID.toString() )
1037 System.out.println("rule MSO.RESPONSE is triggered.");
1039 // Construct notification
1041 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1042 notification.from = "policy";
1043 notification.policyName = drools.getRule().getName();
1044 notification.policyScope = "${policyScope}";
1045 notification.policyVersion = "${policyVersion}";
1046 notification.message = $operationWrapper.operation.toMessage();
1047 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
1049 // The operation can either be succeeded or failed
1051 if($response.request.requestStatus.requestState.equals("COMPLETE")) {
1052 $operationWrapper.operation.outcome = "SUCCESS";
1053 $manager.setControlLoopResult("SUCCESS");
1054 notification.history.add($operationWrapper.operation);
1055 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
1057 // Let interested parties know
1060 System.out.println(Serialization.gsonPretty.toJson(notification));
1061 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1062 } catch (Exception e) {
1063 System.out.println("Can't deliver notification: " + notification);
1064 e.printStackTrace();
1066 notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
1068 // Let interested parties know
1071 System.out.println(Serialization.gsonPretty.toJson(notification));
1072 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1073 } catch (Exception e) {
1074 System.out.println("Can't deliver notification: " + notification);
1075 e.printStackTrace();
1078 // We are going to retract these objects from memory
1080 System.out.println("Retracting everything");
1081 retract($operationWrapper);
1087 $operationWrapper.operation.outcome = "FAILURE";
1088 $manager.setControlLoopResult("FAILURE");
1089 notification.history.add($operationWrapper.operation);
1090 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1092 // Let interested parties know
1095 System.out.println(Serialization.gsonPretty.toJson(notification));
1096 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1097 } catch (Exception e) {
1098 System.out.println("Can't deliver notification: " + notification);
1099 e.printStackTrace();
1101 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
1103 // Let interested parties know
1106 System.out.println(Serialization.gsonPretty.toJson(notification));
1107 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1108 } catch (Exception e) {
1109 System.out.println("Can't deliver notification: " + notification);
1110 e.printStackTrace();
1113 // We are going to retract these objects from memory
1115 System.out.println("Retracting everything");
1116 retract($operationWrapper);
1125 * Sample Queries for illustration with and without arguments
1126 * The results of the query can be introspected (and retracted if desired)
1129 query "${policyName}.QUERY.EVENT.ONSET"
1130 event : VirtualControlLoopEvent( closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1133 query "${policyName}.QUERY.MANAGER.RNA" (String aRequestId, Integer numOnsetsLowerBound, Boolean aActivated)
1134 manager : EventManager( closedLoopControlName == "${closedLoopControlName}",
1135 requestID.toString() == aRequestId,
1136 numOnsets > numOnsetsLowerBound,
1137 activated == aActivated )