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.onap.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.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.template.demo.ControlLoopException;
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;
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;
64 import org.onap.policy.template.demo.EventManager;
65 import org.onap.policy.vnf.trafficgenerator.PGRequest;
66 import org.onap.policy.vnf.trafficgenerator.PGStream;
67 import org.onap.policy.vnf.trafficgenerator.PGStreams;
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;
81 import org.onap.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 * (Comment SETUP rule out for the first ONAP opensource release since policy BRMS_GW already puts a Params fact in there)
107 *rule "${policyName}.SETUP"
110 * System.out.println("rule SETUP is triggered.");
111 * Params params = new Params();
112 * params.setClosedLoopControlName("${closedLoopControlName}");
113 * params.setActor("${actor}");
114 * params.setAaiURL("${aaiURL}");
115 * params.setAaiUsername("${aaiUsername}");
116 * params.setAaiPassword("${aaiPassword}");
117 * params.setMsoURL("${msoURL}");
118 * params.setMsoUsername("${msoUsername}");
119 * params.setMsoPassword("${msoPassword}");
120 * params.setAaiNamedQueryUUID("${aaiNamedQueryUUID}");
121 * params.setAaiPatternMatch(${aaiPatternMatch});
122 * params.setNotificationTopic("${notificationTopic}");
123 * params.setAppcTopic("${appcTopic}");
125 * // This stays in memory as long as the rule is alive and running
132 * This rule responds to DCAE Events
135 rule "${policyName}.EVENT"
137 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
138 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
139 not ( EventManager( closedLoopControlName == $event.closedLoopControlName ))
141 System.out.println("rule EVENT is triggered.");
144 // Check the requestID in the event to make sure it is not null before we create the EventManager.
145 // The EventManager will do extra syntax checking as well check if the closed loop is disabled/
147 if ($event.requestID == null) {
148 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
149 notification.notification = ControlLoopNotificationType.REJECTED;
150 notification.from = "policy";
151 notification.message = "Missing requestID from DCAE event";
152 notification.policyName = drools.getRule().getName();
153 notification.policyScope = "${policyScope}";
154 notification.policyVersion = "${policyVersion}";
156 // Let interested parties know
159 System.out.println(Serialization.gsonPretty.toJson(notification));
160 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
161 } catch (Exception e) {
163 System.out.println("Can't deliver notification: " + notification);
166 // Retract it from memory
169 System.out.println("Event with requestID=null has been retracted.");
172 // Create an EventManager
174 EventManager manager = new EventManager($params.getClosedLoopControlName(), $event.requestID, $event.target);
176 // Determine if EventManager can actively process the event (i.e. syntax)
178 VirtualControlLoopNotification notification = manager.activate($event);
179 notification.from = "policy";
180 notification.policyName = drools.getRule().getName();
181 notification.policyScope = "${policyScope}";
182 notification.policyVersion = "${policyVersion}";
184 // Are we actively pursuing this event?
186 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
188 // Insert Event Manager into memory, this will now kick off processing.
192 // Let interested parties know
195 System.out.println(Serialization.gsonPretty.toJson(notification));
196 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
197 } catch (Exception e) {
199 System.out.println("Can't deliver notification: " + notification);
203 // Let interested parties know
206 System.out.println(Serialization.gsonPretty.toJson(notification));
207 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
208 } catch (Exception e) {
210 System.out.println("Can't deliver notification: " + notification);
213 // Retract it from memory
218 // Now that the manager is inserted into Drools working memory, we'll wait for
219 // another rule to fire in order to continue processing. This way we can also
220 // then screen for additional ONSET and ABATED events for this same RequestIDs
221 // and for different RequestIDs but with the same closedLoopControlName and target.
225 } catch (Exception e) {
227 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
228 notification.notification = ControlLoopNotificationType.REJECTED;
229 notification.message = "Exception occurred " + e.getMessage();
230 notification.policyName = drools.getRule().getName();
231 notification.policyScope = "${policyScope}";
232 notification.policyVersion = "${policyVersion}";
237 System.out.println(Serialization.gsonPretty.toJson(notification));
238 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
239 } catch (Exception e1) {
240 System.out.println("Can't deliver notification: " + notification);
241 e1.printStackTrace();
252 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
253 * is created. We can start the operations for this closed loop.
256 rule "${policyName}.EVENT.MANAGER"
258 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
259 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
260 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName, controlLoopResult == null)
262 System.out.println("rule EVENT.MANAGER is triggered.");
264 // Check which event this is.
266 EventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
268 // We only want the initial ONSET event in memory,
269 // all the other events need to be retracted to support
270 // cleanup and avoid the other rules being fired for this event.
272 if (eventStatus != EventManager.NEW_EVENT_STATUS.FIRST_ONSET) {
273 System.out.println("Retracting "+eventStatus+" Event.");
278 // Now the event in memory is first onset event
282 // Pull the known AAI field from the Event
284 // generic-vnf is needed for vFirewall case
285 // vserver-name is needed for vLoadBalancer case
287 String genericVNF = $event.AAI.get("generic-vnf.vnf-id");
288 String vserver = $event.AAI.get("vserver.vserver-name");
290 // Check if we are implementing a simple pattern match.
292 if ($params.getAaiPatternMatch() == 1) {
296 //Basic naming characteristics:
297 //VF Name (9 char)+VM name (13 char total)+VFC (19 char total)
299 //VF Name (9 characters): cscf0001v
300 //VM Name(13 characters): cscf0001vm001
301 //VFC name(19 characters): cscf0001vm001cfg001
303 // zdfw1fwl01fwl02 or zdfw1fwl01fwl01
305 // zdfw1fwl01pgn02 or zdfw1fwl01pgn01
307 int index = genericVNF.lastIndexOf("fwl");
309 System.err.println("The generic-vnf.vnf-id from DCAE Event is not valid.");
311 genericVNF = genericVNF.substring(0, index) + "pgn" + genericVNF.substring(index+"fwl".length());
314 // Construct an APPC request
316 ControlLoopOperation operation = new ControlLoopOperation();
317 operation.actor = $params.getActor();
318 operation.operation = "ModifyConfig";
319 operation.target = $event.target;
321 // Create operationWrapper
323 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
325 // insert operationWrapper into memory
327 insert(operationWrapper);
329 Request request = new Request();
330 request.CommonHeader = new CommonHeader();
331 request.CommonHeader.RequestID = $event.requestID;
332 request.Action = operation.operation;
333 request.Payload = new HashMap<String, Object>();
335 // Fill in the payload
337 request.Payload.put("generic-vnf.vnf-id", genericVNF);
339 PGRequest pgRequest = new PGRequest();
340 pgRequest.pgStreams = new PGStreams();
343 for(int i = 0; i < 5; i++){
344 pgStream = new PGStream();
345 pgStream.streamId = "fw_udp"+(i+1);
346 pgStream.isEnabled = "true";
347 pgRequest.pgStreams.pgStream.add(pgStream);
349 request.Payload.put("pg-streams", pgRequest.pgStreams);
351 if (request != null) {
353 // Insert request into memory
357 // Tell interested parties we are performing this Operation
359 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
360 notification.notification = ControlLoopNotificationType.OPERATION;
361 // message and history ??
362 notification.from = "policy";
363 notification.policyName = drools.getRule().getName();
364 notification.policyScope = "${policyScope}";
365 notification.policyVersion = "${policyVersion}";
367 System.out.println(Serialization.gsonPretty.toJson(notification));
368 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
369 } catch (Exception e) {
370 System.out.println("Can't deliver notification: " + notification);
374 // Now send the operation request
376 if (request instanceof Request) {
378 System.out.println("APPC request sent:");
379 System.out.println(Serialization.gsonPretty.toJson(request));
380 PolicyEngine.manager.deliver($params.getAppcTopic(), request);
381 } catch (Exception e) {
383 System.out.println("Can't deliver request: " + request);
388 // what happens if it is null
394 // create AAI named-query request with UUID started with "F199"
396 AAINQF199Request aainqf199request = new AAINQF199Request();
397 AAINQF199QueryParameters aainqf199queryparam = new AAINQF199QueryParameters();
398 AAINQF199NamedQuery aainqf199namedquery = new AAINQF199NamedQuery();
399 AAINQF199InstanceFilters aainqf199instancefilter = new AAINQF199InstanceFilters();
403 aainqf199namedquery.namedQueryUUID = UUID.fromString($params.getAaiNamedQueryUUID());
404 aainqf199queryparam.namedQuery = aainqf199namedquery;
405 aainqf199request.queryParameters = aainqf199queryparam;
409 Map aainqf199instancefiltermap = new HashMap();
410 Map aainqf199instancefiltermapitem = new HashMap();
411 aainqf199instancefiltermapitem.put("vserver-name", vserver);
412 aainqf199instancefiltermap.put("vserver", aainqf199instancefiltermapitem);
413 aainqf199instancefilter.instanceFilter.add(aainqf199instancefiltermap);
414 aainqf199request.instanceFilters = aainqf199instancefilter;
416 // print aainqf199request for debug
418 System.out.println("AAI Request sent:");
419 System.out.println(Serialization.gsonPretty.toJson(aainqf199request));
421 // Create AAINQF199RequestWrapper
423 AAINQF199RequestWrapper aainqf199RequestWrapper = new AAINQF199RequestWrapper($event.requestID, aainqf199request);
425 // insert aainqf199request into memory
427 insert(aainqf199RequestWrapper);
430 } catch (Exception e) {
437 * This rule happens when we got a valid ONSET, closed loop is enabled, an Event Manager
438 * is created, AAI Manager and AAI Request are ready in memory. We can start sending query to AAI and then wait for response.
441 rule "${policyName}.EVENT.MANAGER.AAINQF199REQUEST"
443 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
444 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
445 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
446 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
448 System.out.println("rule EVENT.MANAGER.AAINQF199REQUEST is triggered.");
452 AAINQF199Response aainqf199response = AAINQF199Manager.postQuery($params.getAaiURL(), $params.getAaiUsername(), $params.getAaiPassword(),
453 $aainqf199RequestWrapper.aainqf199request, $event.requestID);
455 // Check AAI response
457 if (aainqf199response == null) {
458 System.err.println("Failed to get AAI response");
460 // Fail and retract everything
464 retract($aainqf199RequestWrapper);
467 // Create AAINQF199ResponseWrapper
469 AAINQF199ResponseWrapper aainqf199ResponseWrapper = new AAINQF199ResponseWrapper($event.requestID, aainqf199response);
471 // insert aainqf199ResponseWrapper to memeory
473 insert(aainqf199ResponseWrapper);
479 * This rule happens when we got a valid AAI response. We can start sending request to APPC or MSO now.
482 rule "${policyName}.EVENT.MANAGER.AAINQF199RESPONSE"
484 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
485 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
486 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
487 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
488 $aainqf199ResponseWrapper : AAINQF199ResponseWrapper(requestID == $event.requestID)
490 System.out.println("rule EVENT.MANAGER.AAINQF199RESPONSE is triggered.");
492 // Extract related fields out of AAINQF199RESPONSE
494 String vnfItemVnfId, vnfItemVnfType, vnfItemPersonaModelId, vnfItemPersonaModelVersion, vnfItemModelName,
495 vnfItemModelVersion, vnfItemModelNameVersionId, serviceItemServiceInstanceId, serviceItemPersonaModelId,
496 serviceItemModelName, serviceItemModelType, serviceItemModelVersion, serviceItemModelNameVersionId,
497 vfModuleItemVfModuleName, vfModuleItemPersonaModelId, vfModuleItemPersonaModelVersion, vfModuleItemModelName,
498 vfModuleItemModelNameVersionId, tenantItemTenantId, cloudRegionItemCloudRegionId;
503 vnfItemVnfId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfID;
504 vnfItemVnfType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfType;
505 vnfItemVnfType = vnfItemVnfType.substring(vnfItemVnfType.lastIndexOf("/")+1);
506 vnfItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelId;
507 vnfItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelVersion;
508 vnfItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
509 vnfItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(2).propertyValue;
510 vnfItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
514 serviceItemServiceInstanceId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.serviceInstanceID;
515 serviceItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelId;
516 serviceItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
517 serviceItemModelType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(1).propertyValue;
518 serviceItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelVersion;
519 serviceItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
521 // Find the index for base vf module and non-base vf module
524 int nonBaseIndex = -1;
525 List<AAINQF199InventoryResponseItem> inventoryItems = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems;
526 for (AAINQF199InventoryResponseItem m : inventoryItems) {
527 if (m.vfModule != null && m.vfModule.isBaseVfModule == true) {
528 baseIndex = inventoryItems.indexOf(m);
529 } else if (m.vfModule != null && m.vfModule.isBaseVfModule == false && m.vfModule.orchestrationStatus == null) {
530 nonBaseIndex = inventoryItems.indexOf(m);
533 if (baseIndex != -1 && nonBaseIndex != -1) {
538 // Report the error if either base vf module or non-base vf module is not found
540 if (baseIndex == -1 || nonBaseIndex == -1) {
541 System.err.println("Either base or non-base vf module is not found from AAI response.");
542 retract($aainqf199RequestWrapper);
543 retract($aainqf199ResponseWrapper);
549 // This comes from the base module
551 vfModuleItemVfModuleName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(baseIndex).vfModule.vfModuleName;
552 vfModuleItemVfModuleName = vfModuleItemVfModuleName.replace("Vfmodule", "vDNS");
554 // vfModuleItem - NOT the base module
556 vfModuleItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelId;
557 vfModuleItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).vfModule.personaModelVersion;
558 vfModuleItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(0).propertyValue;
559 vfModuleItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(nonBaseIndex).extraProperties.extraProperty.get(4).propertyValue;
563 tenantItemTenantId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).tenant.tenantId;
567 cloudRegionItemCloudRegionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).items.inventoryResponseItems.get(0).cloudRegion.cloudRegionId;
569 } catch (Exception e) {
571 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
572 notification.notification = ControlLoopNotificationType.REJECTED;
573 notification.message = "Exception occurred " + e.getMessage();
574 notification.policyName = drools.getRule().getName();
575 notification.policyScope = "${policyScope}";
576 notification.policyVersion = "${policyVersion}";
579 System.out.println(Serialization.gsonPretty.toJson(notification));
580 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
581 } catch (Exception e1) {
582 System.out.println("Can't deliver notification: " + notification);
583 e1.printStackTrace();
586 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
587 notification.message = "Invalid named-query response from AAI";
590 System.out.println(Serialization.gsonPretty.toJson(notification));
591 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
592 } catch (Exception e1) {
593 System.out.println("Can't deliver notification: " + notification);
594 e1.printStackTrace();
597 // Retract everything
599 retract($aainqf199RequestWrapper);
600 retract($aainqf199ResponseWrapper);
606 // Extracted fields should not be null
608 if ((vnfItemVnfId == null) || (vnfItemVnfType == null) ||
609 (vnfItemPersonaModelId == null) || (vnfItemModelName == null) ||
610 (vnfItemModelVersion == null) || (vnfItemModelNameVersionId == null) ||
611 (serviceItemServiceInstanceId == null) || (serviceItemModelName == null) ||
612 (serviceItemModelType == null) || (serviceItemModelVersion == null) ||
613 (serviceItemModelNameVersionId == null) || (vfModuleItemVfModuleName == null) ||
614 (vfModuleItemPersonaModelId == null) || (vfModuleItemPersonaModelVersion == null) ||
615 (vfModuleItemModelName == null) || (vfModuleItemModelNameVersionId == null) ||
616 (tenantItemTenantId == null) || (cloudRegionItemCloudRegionId == null)) {
618 System.err.println("some fields are missing from AAI response.");
620 // Fail and retract everything
622 retract($aainqf199RequestWrapper);
623 retract($aainqf199ResponseWrapper);
629 // We don't need them any more
631 retract($aainqf199ResponseWrapper);
632 retract($aainqf199RequestWrapper);
634 // check the actor of this closed loop
636 switch ($params.getActor()) {
640 // Construct an APPC request
642 ControlLoopOperation operation = new ControlLoopOperation();
643 operation.actor = $params.getActor();
644 operation.operation = "ModifyConfig";
645 operation.target = $event.target;
647 // Create operationWrapper
649 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
651 // insert operationWrapper into memory
653 insert(operationWrapper);
655 Request request = new Request();
656 request.CommonHeader = new CommonHeader();
657 request.CommonHeader.RequestID = $event.requestID;
658 request.Action = operation.operation;
659 request.Payload = new HashMap<String, Object>();
661 // Fill in the payload
662 // Hardcode genericVNF for now since AAI has not been ready for vFirewall demo case
664 String genericVNF = "zdfw1fwl01pgn02";
665 request.Payload.put("generic-vnf.vnf-id", genericVNF);
667 PGRequest pgRequest = new PGRequest();
668 pgRequest.pgStreams = new PGStreams();
671 for(int i = 0; i < 5; i++){
672 pgStream = new PGStream();
673 pgStream.streamId = "fw_udp"+(i+1);
674 pgStream.isEnabled = "true";
675 pgRequest.pgStreams.pgStream.add(pgStream);
677 request.Payload.put("pg-streams", pgRequest.pgStreams);
679 if (request != null) {
681 // Insert request into memory
685 // Tell interested parties we are performing this Operation
687 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
688 notification.notification = ControlLoopNotificationType.OPERATION;
689 // message and history ??
690 notification.from = "policy";
691 notification.policyName = drools.getRule().getName();
692 notification.policyScope = "${policyScope}";
693 notification.policyVersion = "${policyVersion}";
695 System.out.println(Serialization.gsonPretty.toJson(notification));
696 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
697 } catch (Exception e) {
698 System.out.println("Can't deliver notification: " + notification);
702 // Now send the operation request
704 if (request instanceof Request) {
706 System.out.println("APPC request sent:");
707 System.out.println(Serialization.gsonPretty.toJson(request));
708 PolicyEngine.manager.deliver($params.getAppcTopic(), request);
709 } catch (Exception e) {
711 System.out.println("Can't deliver request: " + request);
716 // what happens if it is null
724 // Construct an operation
726 ControlLoopOperation operation = new ControlLoopOperation();
727 operation.actor = $params.getActor();
728 operation.operation = "createModuleInstance";
729 operation.target = $event.target;
731 // Create operationWrapper
733 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
735 // Construct an MSO request
737 MSORequest request = new MSORequest();
738 request.requestDetails = new MSORequestDetails();
739 request.requestDetails.modelInfo = new MSOModelInfo();
740 request.requestDetails.cloudConfiguration = new MSOCloudConfiguration();
741 request.requestDetails.requestInfo = new MSORequestInfo();
742 request.requestDetails.requestParameters = new MSORequestParameters();
743 request.requestDetails.requestParameters.userParams = null;
745 // cloudConfiguration
747 request.requestDetails.cloudConfiguration.lcpCloudRegionId = cloudRegionItemCloudRegionId;
748 request.requestDetails.cloudConfiguration.tenantId = tenantItemTenantId;
752 request.requestDetails.modelInfo.modelType = "vfModule";
753 request.requestDetails.modelInfo.modelInvariantId = vfModuleItemPersonaModelId;
754 request.requestDetails.modelInfo.modelNameVersionId = vfModuleItemModelNameVersionId;
755 request.requestDetails.modelInfo.modelName = vfModuleItemModelName;
756 request.requestDetails.modelInfo.modelVersion = vfModuleItemPersonaModelVersion;
760 request.requestDetails.requestInfo.instanceName = vfModuleItemVfModuleName;
761 request.requestDetails.requestInfo.source = "POLICY";
762 request.requestDetails.requestInfo.suppressRollback = false;
764 // relatedInstanceList
766 MSORelatedInstanceListElement relatedInstanceListElement1 = new MSORelatedInstanceListElement();
767 MSORelatedInstanceListElement relatedInstanceListElement2 = new MSORelatedInstanceListElement();
768 relatedInstanceListElement1.relatedInstance = new MSORelatedInstance();
769 relatedInstanceListElement2.relatedInstance = new MSORelatedInstance();
771 relatedInstanceListElement1.relatedInstance.instanceId = serviceItemServiceInstanceId;
772 relatedInstanceListElement1.relatedInstance.modelInfo = new MSOModelInfo();
773 relatedInstanceListElement1.relatedInstance.modelInfo.modelType = "service";
774 relatedInstanceListElement1.relatedInstance.modelInfo.modelInvariantId = serviceItemPersonaModelId;
775 relatedInstanceListElement1.relatedInstance.modelInfo.modelNameVersionId = serviceItemModelNameVersionId;
776 relatedInstanceListElement1.relatedInstance.modelInfo.modelName = serviceItemModelName;
777 relatedInstanceListElement1.relatedInstance.modelInfo.modelVersion = serviceItemModelVersion;
779 relatedInstanceListElement2.relatedInstance.instanceId = vnfItemVnfId;
780 relatedInstanceListElement2.relatedInstance.modelInfo = new MSOModelInfo();
781 relatedInstanceListElement2.relatedInstance.modelInfo.modelType = "vnf";
782 relatedInstanceListElement2.relatedInstance.modelInfo.modelInvariantId = vnfItemPersonaModelId;
783 relatedInstanceListElement2.relatedInstance.modelInfo.modelNameVersionId = vnfItemModelNameVersionId;
784 relatedInstanceListElement2.relatedInstance.modelInfo.modelName = vnfItemModelName;
785 relatedInstanceListElement2.relatedInstance.modelInfo.modelVersion = vnfItemModelVersion;
786 relatedInstanceListElement2.relatedInstance.modelInfo.modelCustomizationName = vnfItemVnfType;
788 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement1);
789 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement2);
791 // print MSO request for debug
793 System.out.println("MSO request sent:");
794 System.out.println(Serialization.gsonPretty.toJson(request));
798 if (request != null) {
800 // Tell interested parties we are performing this Operation
802 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
803 notification.notification = ControlLoopNotificationType.OPERATION;
804 notification.from = "policy";
805 notification.policyName = drools.getRule().getName();
806 notification.policyScope = "${policyScope}";
807 notification.policyVersion = "${policyVersion}";
809 System.out.println(Serialization.gsonPretty.toJson(notification));
810 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
811 } catch (Exception e) {
812 System.out.println("Can't deliver notification: " + notification);
816 // Concatenate serviceItemServiceInstanceId and vnfItemVnfId to msoURL
818 String MSOUrl = $params.getMsoURL() + "/serviceInstances/v2/" + serviceItemServiceInstanceId + "/vnfs/" + vnfItemVnfId + "/vfModules";
822 MSOResponse response = MSOManager.createModuleInstance(MSOUrl, $params.getMsoURL(), $params.getMsoUsername(), $params.getMsoPassword(), request);
824 if (response != null) {
828 request.requestId = $event.requestID.toString();
829 response.request.requestId = $event.requestID.toString();
833 insert(operationWrapper);
838 // MSO request not even accepted
840 notification.message = operationWrapper.operation.toMessage();
841 operationWrapper.operation.message = operationWrapper.operation.toMessage();
842 operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
843 $manager.setControlLoopResult("FAILURE_EXCEPTION");
844 notification.history.add(operationWrapper.operation);
845 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
847 // Let interested parties know
850 System.out.println(Serialization.gsonPretty.toJson(notification));
851 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
852 } catch (Exception e) {
853 System.out.println("Can't deliver notification: " + notification);
856 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
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);
865 // Retract everything
871 System.err.println("constructed MSO request is invalid.");
880 * This rule responds to APPC Response Events
883 rule "${policyName}.APPC.RESPONSE"
885 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
886 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
887 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
888 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
889 $request : Request( getCommonHeader().RequestID == $event.requestID )
890 $response : Response( getCommonHeader().RequestID == $event.requestID )
892 System.out.println("rule APPC.RESPONSE is triggered.");
893 if ($response.Status == null) {
894 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
895 $manager.setControlLoopResult("FAILURE_EXCEPTION");
898 // Get the Response Code
900 ResponseCode code = ResponseCode.toResponseCode($response.Status.Code);
902 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
903 $manager.setControlLoopResult("FAILURE_EXCEPTION");
906 // Construct notification
908 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
909 notification.from = "policy";
910 notification.policyName = drools.getRule().getName();
911 notification.policyScope = "${policyScope}";
912 notification.policyVersion = "${policyVersion}";
913 notification.message = $operationWrapper.operation.toMessage();
914 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
916 // Ok, let's figure out what APP-C's response is
920 $operationWrapper.operation.outcome = "PROCESSING";
924 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
925 $manager.setControlLoopResult("FAILURE_EXCEPTION");
928 $operationWrapper.operation.outcome = "SUCCESS";
929 $manager.setControlLoopResult("SUCCESS");
932 $operationWrapper.operation.outcome = "FAILURE";
933 $manager.setControlLoopResult("FAILURE");
936 if ($operationWrapper.operation.outcome.equals("SUCCESS")) {
937 notification.history.add($operationWrapper.operation);
938 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
940 // Let interested parties know
943 System.out.println(Serialization.gsonPretty.toJson(notification));
944 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
945 } catch (Exception e) {
946 System.out.println("Can't deliver notification: " + notification);
949 notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
951 System.out.println(Serialization.gsonPretty.toJson(notification));
952 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
953 } catch (Exception e) {
954 System.out.println("Can't deliver notification: " + notification);
959 // We are going to retract these objects from memory
961 System.out.println("Retracting everything");
962 retract($operationWrapper);
967 } else if ($operationWrapper.operation.outcome.equals("PROCESSING")) {
970 notification.history.add($operationWrapper.operation);
971 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
973 // Let interested parties know
976 System.out.println(Serialization.gsonPretty.toJson(notification));
977 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
978 } catch (Exception e) {
979 System.out.println("Can't deliver notification: " + notification);
982 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
984 // Let interested parties know
987 System.out.println(Serialization.gsonPretty.toJson(notification));
988 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
989 } catch (Exception e) {
990 System.out.println("Can't deliver notification: " + notification);
994 // We are going to retract these objects from memory
996 System.out.println("Retracting everything");
997 retract($operationWrapper);
1008 * This rule is used to clean up APPC response
1011 rule "${policyName}.APPC.RESPONSE.CLEANUP"
1013 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1014 $response : Response($id : getCommonHeader().RequestID )
1015 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
1017 System.out.println("rule APPC.RESPONSE.CLEANUP is triggered.");
1023 * This rule responds to MSO Response Events
1026 rule "${policyName}.MSO.RESPONSE"
1028 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1029 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1030 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
1031 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
1032 $request : MSORequest( requestId == $event.requestID.toString() )
1033 $response : MSOResponse( request.requestId == $event.requestID.toString() )
1035 System.out.println("rule MSO.RESPONSE is triggered.");
1037 // Construct notification
1039 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1040 notification.from = "policy";
1041 notification.policyName = drools.getRule().getName();
1042 notification.policyScope = "${policyScope}";
1043 notification.policyVersion = "${policyVersion}";
1044 notification.message = $operationWrapper.operation.toMessage();
1045 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
1047 // The operation can either be succeeded or failed
1049 if($response.request.requestStatus.requestState.equals("Completed")) {
1050 $operationWrapper.operation.outcome = "SUCCESS";
1051 $manager.setControlLoopResult("SUCCESS");
1052 notification.history.add($operationWrapper.operation);
1053 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
1055 // Let interested parties know
1058 System.out.println(Serialization.gsonPretty.toJson(notification));
1059 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1060 } catch (Exception e) {
1061 System.out.println("Can't deliver notification: " + notification);
1062 e.printStackTrace();
1064 notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
1066 // Let interested parties know
1069 System.out.println(Serialization.gsonPretty.toJson(notification));
1070 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1071 } catch (Exception e) {
1072 System.out.println("Can't deliver notification: " + notification);
1073 e.printStackTrace();
1076 // We are going to retract these objects from memory
1078 System.out.println("Retracting everything");
1079 retract($operationWrapper);
1085 $operationWrapper.operation.outcome = "FAILURE";
1086 $manager.setControlLoopResult("FAILURE");
1087 notification.history.add($operationWrapper.operation);
1088 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1090 // Let interested parties know
1093 System.out.println(Serialization.gsonPretty.toJson(notification));
1094 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1095 } catch (Exception e) {
1096 System.out.println("Can't deliver notification: " + notification);
1097 e.printStackTrace();
1099 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
1101 // Let interested parties know
1104 System.out.println(Serialization.gsonPretty.toJson(notification));
1105 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1106 } catch (Exception e) {
1107 System.out.println("Can't deliver notification: " + notification);
1108 e.printStackTrace();
1111 // We are going to retract these objects from memory
1113 System.out.println("Retracting everything");
1114 retract($operationWrapper);