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.LinkedList;
25 import java.util.HashMap;
26 import java.util.UUID;
28 import org.openecomp.policy.controlloop.VirtualControlLoopEvent;
29 import org.openecomp.policy.controlloop.ControlLoopEventStatus;
30 import org.openecomp.policy.controlloop.VirtualControlLoopNotification;
31 import org.openecomp.policy.controlloop.ControlLoopNotificationType;
32 import org.openecomp.policy.controlloop.ControlLoopOperation;
33 import org.openecomp.policy.controlloop.ControlLoopOperationWrapper;
34 import org.openecomp.policy.template.demo.ControlLoopException;
36 import org.openecomp.policy.aai.AAINQF199.AAINQF199CloudRegion;
37 import org.openecomp.policy.aai.AAINQF199.AAINQF199ExtraProperties;
38 import org.openecomp.policy.aai.AAINQF199.AAINQF199ExtraProperty;
39 import org.openecomp.policy.aai.AAINQF199.AAINQF199GenericVNF;
40 import org.openecomp.policy.aai.AAINQF199.AAINQF199InstanceFilters;
41 import org.openecomp.policy.aai.AAINQF199.AAINQF199InventoryResponseItem;
42 import org.openecomp.policy.aai.AAINQF199.AAINQF199InventoryResponseItems;
43 import org.openecomp.policy.aai.AAINQF199.AAINQF199Manager;
44 import org.openecomp.policy.aai.AAINQF199.AAINQF199NamedQuery;
45 import org.openecomp.policy.aai.AAINQF199.AAINQF199QueryParameters;
46 import org.openecomp.policy.aai.AAINQF199.AAINQF199Request;
47 import org.openecomp.policy.aai.AAINQF199.AAINQF199RequestWrapper;
48 import org.openecomp.policy.aai.AAINQF199.AAINQF199Response;
49 import org.openecomp.policy.aai.AAINQF199.AAINQF199ResponseWrapper;
50 import org.openecomp.policy.aai.AAINQF199.AAINQF199ServiceInstance;
51 import org.openecomp.policy.aai.AAINQF199.AAINQF199Tenant;
52 import org.openecomp.policy.aai.AAINQF199.AAINQF199VfModule;
53 import org.openecomp.policy.aai.AAINQF199.AAINQF199VServer;
54 import org.openecomp.policy.aai.util.Serialization;
56 import org.openecomp.policy.appc.CommonHeader;
57 import org.openecomp.policy.appc.Request;
58 import org.openecomp.policy.appc.Response;
59 import org.openecomp.policy.appc.ResponseCode;
60 import org.openecomp.policy.appc.ResponseStatus;
61 import org.openecomp.policy.appc.ResponseValue;
63 import org.openecomp.policy.template.demo.EventManager;
64 import org.openecomp.policy.vnf.trafficgenerator.PGRequest;
65 import org.openecomp.policy.vnf.trafficgenerator.PGStream;
66 import org.openecomp.policy.vnf.trafficgenerator.PGStreams;
68 import org.openecomp.policy.mso.MSOManager;
69 import org.openecomp.policy.mso.MSORequest;
70 import org.openecomp.policy.mso.MSORequestStatus;
71 import org.openecomp.policy.mso.MSORequestDetails;
72 import org.openecomp.policy.mso.MSOModelInfo;
73 import org.openecomp.policy.mso.MSOCloudConfiguration;
74 import org.openecomp.policy.mso.MSORequestInfo;
75 import org.openecomp.policy.mso.MSORequestParameters;
76 import org.openecomp.policy.mso.MSORelatedInstanceListElement;
77 import org.openecomp.policy.mso.MSORelatedInstance;
78 import org.openecomp.policy.mso.MSOResponse;
80 import org.openecomp.policy.drools.system.PolicyEngine;
83 // These parameters are required to build the runtime policy
86 closedLoopControlName : String
94 aaiNamedQueryUUID : String
96 notificationTopic : String
102 * Called once and only once to insert the parameters into working memory for this Closed Loop policy.
103 * (Comment SETUP rule out for the first ECOMP opensource release since policy BRMS_GW already puts a Params fact in there)
106 *rule "${policyName}.SETUP"
109 * System.out.println("rule SETUP is triggered.");
110 * Params params = new Params();
111 * params.setClosedLoopControlName("${closedLoopControlName}");
112 * params.setActor("${actor}");
113 * params.setAaiURL("${aaiURL}");
114 * params.setAaiUsername("${aaiUsername}");
115 * params.setAaiPassword("${aaiPassword}");
116 * params.setMsoURL("${msoURL}");
117 * params.setMsoUsername("${msoUsername}");
118 * params.setMsoPassword("${msoPassword}");
119 * params.setAaiNamedQueryUUID("${aaiNamedQueryUUID}");
120 * params.setAaiPatternMatch(${aaiPatternMatch});
121 * params.setNotificationTopic("${notificationTopic}");
122 * params.setAppcTopic("${appcTopic}");
124 * // This stays in memory as long as the rule is alive and running
131 * This rule responds to DCAE Events
134 rule "${policyName}.EVENT"
136 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
137 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
138 not ( EventManager( closedLoopControlName == $event.closedLoopControlName ))
140 System.out.println("rule EVENT is triggered.");
143 // Check the requestID in the event to make sure it is not null before we create the EventManager.
144 // The EventManager will do extra syntax checking as well check if the closed loop is disabled/
146 if ($event.requestID == null) {
147 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
148 notification.notification = ControlLoopNotificationType.REJECTED;
149 notification.from = "policy";
150 notification.message = "Missing requestID from DCAE event";
151 notification.policyName = drools.getRule().getName();
152 notification.policyScope = "${policyScope}";
153 notification.policyVersion = "${policyVersion}";
155 // Let interested parties know
158 System.out.println(Serialization.gsonPretty.toJson(notification));
159 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
160 } catch (Exception e) {
162 System.out.println("Can't deliver notification: " + notification);
165 // Retract it from memory
168 System.out.println("Event with requestID=null has been retracted.");
171 // Create an EventManager
173 EventManager manager = new EventManager($params.getClosedLoopControlName(), $event.requestID, $event.target);
175 // Determine if EventManager can actively process the event (i.e. syntax)
177 VirtualControlLoopNotification notification = manager.activate($event);
178 notification.from = "policy";
179 notification.policyName = drools.getRule().getName();
180 notification.policyScope = "${policyScope}";
181 notification.policyVersion = "${policyVersion}";
183 // Are we actively pursuing this event?
185 if (notification.notification == ControlLoopNotificationType.ACTIVE) {
187 // Insert Event Manager into memory, this will now kick off processing.
191 // Let interested parties know
194 System.out.println(Serialization.gsonPretty.toJson(notification));
195 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
196 } catch (Exception e) {
198 System.out.println("Can't deliver notification: " + notification);
202 // Let interested parties know
205 System.out.println(Serialization.gsonPretty.toJson(notification));
206 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
207 } catch (Exception e) {
209 System.out.println("Can't deliver notification: " + notification);
212 // Retract it from memory
217 // Now that the manager is inserted into Drools working memory, we'll wait for
218 // another rule to fire in order to continue processing. This way we can also
219 // then screen for additional ONSET and ABATED events for this same RequestIDs
220 // and for different RequestIDs but with the same closedLoopControlName and target.
224 } catch (Exception e) {
226 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
227 notification.notification = ControlLoopNotificationType.REJECTED;
228 notification.message = "Exception occurred " + e.getMessage();
229 notification.policyName = drools.getRule().getName();
230 notification.policyScope = "${policyScope}";
231 notification.policyVersion = "${policyVersion}";
236 System.out.println(Serialization.gsonPretty.toJson(notification));
237 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
238 } catch (Exception e1) {
239 System.out.println("Can't deliver notification: " + notification);
240 e1.printStackTrace();
251 * This rule happens when we got a valid ONSET, closed loop is enabled and an Event Manager
252 * is created. We can start the operations for this closed loop.
255 rule "${policyName}.EVENT.MANAGER"
257 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
258 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
259 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName, controlLoopResult == null)
261 System.out.println("rule EVENT.MANAGER is triggered.");
263 // Check which event this is.
265 EventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event);
267 // We only want the initial ONSET event in memory,
268 // all the other events need to be retracted to support
269 // cleanup and avoid the other rules being fired for this event.
271 if (eventStatus != EventManager.NEW_EVENT_STATUS.FIRST_ONSET) {
272 System.out.println("Retracting "+eventStatus+" Event.");
277 // Now the event in memory is first onset event
281 // Pull the known AAI field from the Event
283 // generic-vnf is needed for vFirewall case
284 // vserver-name is needed for vLoadBalancer case
286 String genericVNF = $event.AAI.get("generic-vnf.vnf-id");
287 String vserver = $event.AAI.get("vserver.vserver-name");
289 // Check if we are implementing a simple pattern match.
291 if ($params.getAaiPatternMatch() == 1) {
295 //Basic naming characteristics:
296 //VF Name (9 char)+VM name (13 char total)+VFC (19 char total)
298 //VF Name (9 characters): cscf0001v
299 //VM Name(13 characters): cscf0001vm001
300 //VFC name(19 characters): cscf0001vm001cfg001
302 // zdfw1fwl01fwl02 or zdfw1fwl01fwl01
304 // zdfw1fwl01pgn02 or zdfw1fwl01pgn01
306 int index = genericVNF.lastIndexOf("fwl");
308 System.err.println("The generic-vnf.vnf-id from DCAE Event is not valid.");
310 genericVNF = genericVNF.substring(0, index) + "pgn" + genericVNF.substring(index+"fwl".length());
313 // Construct an APPC request
315 ControlLoopOperation operation = new ControlLoopOperation();
316 operation.actor = $params.getActor();
317 operation.operation = "ModifyConfig";
318 operation.target = $event.target;
320 // Create operationWrapper
322 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
324 // insert operationWrapper into memory
326 insert(operationWrapper);
328 Request request = new Request();
329 request.CommonHeader = new CommonHeader();
330 request.CommonHeader.RequestID = $event.requestID;
331 request.Action = operation.operation;
332 request.Payload = new HashMap<String, Object>();
334 // Fill in the payload
336 request.Payload.put("generic-vnf.vnf-id", genericVNF);
338 PGRequest pgRequest = new PGRequest();
339 pgRequest.pgStreams = new PGStreams();
342 for(int i = 0; i < 5; i++){
343 pgStream = new PGStream();
344 pgStream.streamId = "fw_udp"+(i+1);
345 pgStream.isEnabled = "true";
346 pgRequest.pgStreams.pgStream.add(pgStream);
348 request.Payload.put("pg-streams", pgRequest.pgStreams);
350 if (request != null) {
352 // Insert request into memory
356 // Tell interested parties we are performing this Operation
358 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
359 notification.notification = ControlLoopNotificationType.OPERATION;
360 // message and history ??
361 notification.from = "policy";
362 notification.policyName = drools.getRule().getName();
363 notification.policyScope = "${policyScope}";
364 notification.policyVersion = "${policyVersion}";
366 System.out.println(Serialization.gsonPretty.toJson(notification));
367 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
368 } catch (Exception e) {
369 System.out.println("Can't deliver notification: " + notification);
373 // Now send the operation request
375 if (request instanceof Request) {
377 System.out.println("APPC request sent:");
378 System.out.println(Serialization.gsonPretty.toJson(request));
379 PolicyEngine.manager.deliver($params.getAppcTopic(), request);
380 } catch (Exception e) {
382 System.out.println("Can't deliver request: " + request);
387 // what happens if it is null
393 // create AAI named-query request with UUID started with "F199"
395 AAINQF199Request aainqf199request = new AAINQF199Request();
396 AAINQF199QueryParameters aainqf199queryparam = new AAINQF199QueryParameters();
397 AAINQF199NamedQuery aainqf199namedquery = new AAINQF199NamedQuery();
398 AAINQF199InstanceFilters aainqf199instancefilter = new AAINQF199InstanceFilters();
402 aainqf199namedquery.namedQueryUUID = UUID.fromString($params.getAaiNamedQueryUUID());
403 aainqf199queryparam.namedQuery = aainqf199namedquery;
404 aainqf199request.queryParameters = aainqf199queryparam;
408 Map aainqf199instancefiltermap = new HashMap();
409 Map aainqf199instancefiltermapitem = new HashMap();
410 aainqf199instancefiltermapitem.put("vserver-name", vserver);
411 aainqf199instancefiltermap.put("vserver", aainqf199instancefiltermapitem);
412 aainqf199instancefilter.instanceFilter.add(aainqf199instancefiltermap);
413 aainqf199request.instanceFilters = aainqf199instancefilter;
415 // print aainqf199request for debug
417 System.out.println("AAI Request sent:");
418 System.out.println(Serialization.gsonPretty.toJson(aainqf199request));
420 // Create AAINQF199RequestWrapper
422 AAINQF199RequestWrapper aainqf199RequestWrapper = new AAINQF199RequestWrapper($event.requestID, aainqf199request);
424 // insert aainqf199request into memory
426 insert(aainqf199RequestWrapper);
429 } catch (Exception e) {
436 * This rule happens when we got a valid ONSET, closed loop is enabled, an Event Manager
437 * is created, AAI Manager and AAI Request are ready in memory. We can start sending query to AAI and then wait for response.
440 rule "${policyName}.EVENT.MANAGER.AAINQF199REQUEST"
442 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
443 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
444 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
445 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
447 System.out.println("rule EVENT.MANAGER.AAINQF199REQUEST is triggered.");
451 AAINQF199Response aainqf199response = AAINQF199Manager.postQuery($params.getAaiURL(), $params.getAaiUsername(), $params.getAaiPassword(),
452 $aainqf199RequestWrapper.aainqf199request, $event.requestID);
454 // Check AAI response
456 if (aainqf199response == null) {
457 System.err.println("Failed to get AAI response");
459 // Fail and retract everything
463 retract($aainqf199RequestWrapper);
466 // Create AAINQF199ResponseWrapper
468 AAINQF199ResponseWrapper aainqf199ResponseWrapper = new AAINQF199ResponseWrapper($event.requestID, aainqf199response);
470 // insert aainqf199ResponseWrapper to memeory
472 insert(aainqf199ResponseWrapper);
478 * This rule happens when we got a valid AAI response. We can start sending request to APPC or MSO now.
481 rule "${policyName}.EVENT.MANAGER.AAINQF199RESPONSE"
483 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
484 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
485 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
486 $aainqf199RequestWrapper : AAINQF199RequestWrapper(requestID == $event.requestID)
487 $aainqf199ResponseWrapper : AAINQF199ResponseWrapper(requestID == $event.requestID)
489 System.out.println("rule EVENT.MANAGER.AAINQF199RESPONSE is triggered.");
491 // Extract related fields out of AAINQF199RESPONSE
493 String vnfItemVnfId, vnfItemVnfType, vnfItemPersonaModelId, vnfItemPersonaModelVersion, vnfItemModelName,
494 vnfItemModelVersion, vnfItemModelNameVersionId, serviceItemServiceInstanceId, serviceItemPersonaModelId,
495 serviceItemModelName, serviceItemModelType, serviceItemModelVersion, serviceItemModelNameVersionId,
496 vfModuleItemVfModuleName, vfModuleItemPersonaModelId, vfModuleItemPersonaModelVersion, vfModuleItemModelName,
497 vfModuleItemModelNameVersionId, tenantItemTenantId, cloudRegionItemCloudRegionId;
502 vnfItemVnfId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfID;
503 vnfItemVnfType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.vnfType;
504 vnfItemVnfType = vnfItemVnfType.substring(vnfItemVnfType.lastIndexOf("/")+1);
505 vnfItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelId;
506 vnfItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).genericVNF.personaModelVersion;
507 vnfItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
508 vnfItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(2).propertyValue;
509 vnfItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
513 serviceItemServiceInstanceId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.serviceInstanceID;
514 serviceItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelId;
515 serviceItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(0).propertyValue;
516 serviceItemModelType = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(1).propertyValue;
517 serviceItemModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).serviceInstance.personaModelVersion;
518 serviceItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).extraProperties.extraProperty.get(4).propertyValue;
520 // This comes from the base module
522 vfModuleItemVfModuleName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).vfModule.vfModuleName;
523 vfModuleItemVfModuleName = vfModuleItemVfModuleName.replace("Vfmodule", "vDNS");
525 // vfModuleItem - NOT the base module
527 vfModuleItemPersonaModelId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(2).vfModule.personaModelId;
528 vfModuleItemPersonaModelVersion = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(2).vfModule.personaModelVersion;
529 vfModuleItemModelName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(2).extraProperties.extraProperty.get(0).propertyValue;
530 vfModuleItemModelNameVersionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(2).extraProperties.extraProperty.get(4).propertyValue;
534 tenantItemTenantId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).tenant.tenantId;
538 cloudRegionItemCloudRegionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).items.inventoryResponseItems.get(0).cloudRegion.cloudRegionId;
540 } catch (Exception e) {
542 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
543 notification.notification = ControlLoopNotificationType.REJECTED;
544 notification.message = "Exception occurred " + e.getMessage();
545 notification.policyName = drools.getRule().getName();
546 notification.policyScope = "${policyScope}";
547 notification.policyVersion = "${policyVersion}";
550 System.out.println(Serialization.gsonPretty.toJson(notification));
551 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
552 } catch (Exception e1) {
553 System.out.println("Can't deliver notification: " + notification);
554 e1.printStackTrace();
557 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
558 notification.message = "Invalid named-query response from AAI";
561 System.out.println(Serialization.gsonPretty.toJson(notification));
562 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
563 } catch (Exception e1) {
564 System.out.println("Can't deliver notification: " + notification);
565 e1.printStackTrace();
568 // Retract everything
570 retract($aainqf199RequestWrapper);
571 retract($aainqf199ResponseWrapper);
577 // Extracted fields should not be null
579 if ((vnfItemVnfId == null) || (vnfItemVnfType == null) ||
580 (vnfItemPersonaModelId == null) || (vnfItemModelName == null) ||
581 (vnfItemModelVersion == null) || (vnfItemModelNameVersionId == null) ||
582 (serviceItemServiceInstanceId == null) || (serviceItemModelName == null) ||
583 (serviceItemModelType == null) || (serviceItemModelVersion == null) ||
584 (serviceItemModelNameVersionId == null) || (vfModuleItemVfModuleName == null) ||
585 (vfModuleItemPersonaModelId == null) || (vfModuleItemPersonaModelVersion == null) ||
586 (vfModuleItemModelName == null) || (vfModuleItemModelNameVersionId == null) ||
587 (tenantItemTenantId == null) || (cloudRegionItemCloudRegionId == null)) {
589 System.err.println("some fields are missing from AAI response.");
591 // Fail and retract everything
593 retract($aainqf199RequestWrapper);
594 retract($aainqf199ResponseWrapper);
600 // We don't need them any more
602 retract($aainqf199ResponseWrapper);
603 retract($aainqf199RequestWrapper);
605 // check the actor of this closed loop
607 switch ($params.getActor()) {
611 // Construct an APPC request
613 ControlLoopOperation operation = new ControlLoopOperation();
614 operation.actor = $params.getActor();
615 operation.operation = "ModifyConfig";
616 operation.target = $event.target;
618 // Create operationWrapper
620 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
622 // insert operationWrapper into memory
624 insert(operationWrapper);
626 Request request = new Request();
627 request.CommonHeader = new CommonHeader();
628 request.CommonHeader.RequestID = $event.requestID;
629 request.Action = operation.operation;
630 request.Payload = new HashMap<String, Object>();
632 // Fill in the payload
633 // Hardcode genericVNF for now since AAI has not been ready for vFirewall demo case
635 String genericVNF = "zdfw1fwl01pgn02";
636 request.Payload.put("generic-vnf.vnf-id", genericVNF);
638 PGRequest pgRequest = new PGRequest();
639 pgRequest.pgStreams = new PGStreams();
642 for(int i = 0; i < 5; i++){
643 pgStream = new PGStream();
644 pgStream.streamId = "fw_udp"+(i+1);
645 pgStream.isEnabled = "true";
646 pgRequest.pgStreams.pgStream.add(pgStream);
648 request.Payload.put("pg-streams", pgRequest.pgStreams);
650 if (request != null) {
652 // Insert request into memory
656 // Tell interested parties we are performing this Operation
658 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
659 notification.notification = ControlLoopNotificationType.OPERATION;
660 // message and history ??
661 notification.from = "policy";
662 notification.policyName = drools.getRule().getName();
663 notification.policyScope = "${policyScope}";
664 notification.policyVersion = "${policyVersion}";
666 System.out.println(Serialization.gsonPretty.toJson(notification));
667 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
668 } catch (Exception e) {
669 System.out.println("Can't deliver notification: " + notification);
673 // Now send the operation request
675 if (request instanceof Request) {
677 System.out.println("APPC request sent:");
678 System.out.println(Serialization.gsonPretty.toJson(request));
679 PolicyEngine.manager.deliver($params.getAppcTopic(), request);
680 } catch (Exception e) {
682 System.out.println("Can't deliver request: " + request);
687 // what happens if it is null
695 // Construct an operation
697 ControlLoopOperation operation = new ControlLoopOperation();
698 operation.actor = $params.getActor();
699 operation.operation = "createModuleInstance";
700 operation.target = $event.target;
702 // Create operationWrapper
704 ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation);
706 // Construct an MSO request
708 MSORequest request = new MSORequest();
709 request.requestDetails = new MSORequestDetails();
710 request.requestDetails.modelInfo = new MSOModelInfo();
711 request.requestDetails.cloudConfiguration = new MSOCloudConfiguration();
712 request.requestDetails.requestInfo = new MSORequestInfo();
713 request.requestDetails.requestParameters = new MSORequestParameters();
714 request.requestDetails.requestParameters.userParams = null;
716 // cloudConfiguration
718 request.requestDetails.cloudConfiguration.lcpCloudRegionId = cloudRegionItemCloudRegionId;
719 request.requestDetails.cloudConfiguration.tenantId = tenantItemTenantId;
723 request.requestDetails.modelInfo.modelType = "vfModule";
724 request.requestDetails.modelInfo.modelInvariantId = vfModuleItemPersonaModelId;
725 request.requestDetails.modelInfo.modelNameVersionId = vfModuleItemModelNameVersionId;
726 request.requestDetails.modelInfo.modelName = vfModuleItemModelName;
727 request.requestDetails.modelInfo.modelVersion = vfModuleItemPersonaModelVersion;
731 request.requestDetails.requestInfo.instanceName = vfModuleItemVfModuleName;
732 request.requestDetails.requestInfo.source = "POLICY";
733 request.requestDetails.requestInfo.suppressRollback = false;
735 // relatedInstanceList
737 MSORelatedInstanceListElement relatedInstanceListElement1 = new MSORelatedInstanceListElement();
738 MSORelatedInstanceListElement relatedInstanceListElement2 = new MSORelatedInstanceListElement();
739 relatedInstanceListElement1.relatedInstance = new MSORelatedInstance();
740 relatedInstanceListElement2.relatedInstance = new MSORelatedInstance();
742 relatedInstanceListElement1.relatedInstance.instanceId = serviceItemServiceInstanceId;
743 relatedInstanceListElement1.relatedInstance.modelInfo = new MSOModelInfo();
744 relatedInstanceListElement1.relatedInstance.modelInfo.modelType = "service";
745 relatedInstanceListElement1.relatedInstance.modelInfo.modelInvariantId = serviceItemPersonaModelId;
746 relatedInstanceListElement1.relatedInstance.modelInfo.modelNameVersionId = serviceItemModelNameVersionId;
747 relatedInstanceListElement1.relatedInstance.modelInfo.modelName = serviceItemModelName;
748 relatedInstanceListElement1.relatedInstance.modelInfo.modelVersion = serviceItemModelVersion;
750 relatedInstanceListElement2.relatedInstance.instanceId = vnfItemVnfId;
751 relatedInstanceListElement2.relatedInstance.modelInfo = new MSOModelInfo();
752 relatedInstanceListElement2.relatedInstance.modelInfo.modelType = "vnf";
753 relatedInstanceListElement2.relatedInstance.modelInfo.modelInvariantId = vnfItemPersonaModelId;
754 relatedInstanceListElement2.relatedInstance.modelInfo.modelNameVersionId = vnfItemModelNameVersionId;
755 relatedInstanceListElement2.relatedInstance.modelInfo.modelName = vnfItemModelName;
756 relatedInstanceListElement2.relatedInstance.modelInfo.modelVersion = vnfItemModelVersion;
757 relatedInstanceListElement2.relatedInstance.modelInfo.modelCustomizationName = vnfItemVnfType;
759 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement1);
760 request.requestDetails.relatedInstanceList.add(relatedInstanceListElement2);
762 // print MSO request for debug
764 System.out.println("MSO request sent:");
765 System.out.println(Serialization.gsonPretty.toJson(request));
769 if (request != null) {
771 // Tell interested parties we are performing this Operation
773 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
774 notification.notification = ControlLoopNotificationType.OPERATION;
775 notification.from = "policy";
776 notification.policyName = drools.getRule().getName();
777 notification.policyScope = "${policyScope}";
778 notification.policyVersion = "${policyVersion}";
780 System.out.println(Serialization.gsonPretty.toJson(notification));
781 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
782 } catch (Exception e) {
783 System.out.println("Can't deliver notification: " + notification);
787 // Concatenate serviceItemServiceInstanceId and vnfItemVnfId to msoURL
789 String MSOUrl = $params.getMsoURL() + "/serviceInstances/v2/" + serviceItemServiceInstanceId + "/vnfs/" + vnfItemVnfId + "/vfModules";
793 MSOResponse response = MSOManager.createModuleInstance(MSOUrl, $params.getMsoURL(), $params.getMsoUsername(), $params.getMsoPassword(), request);
795 if (response != null) {
799 request.requestId = $event.requestID.toString();
800 response.request.requestId = $event.requestID.toString();
804 insert(operationWrapper);
809 // MSO request not even accepted
811 notification.message = operationWrapper.operation.toMessage();
812 operationWrapper.operation.message = operationWrapper.operation.toMessage();
813 operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
814 $manager.setControlLoopResult("FAILURE_EXCEPTION");
815 notification.history.add(operationWrapper.operation);
816 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
818 // Let interested parties know
821 System.out.println(Serialization.gsonPretty.toJson(notification));
822 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
823 } catch (Exception e) {
824 System.out.println("Can't deliver notification: " + notification);
827 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
829 System.out.println(Serialization.gsonPretty.toJson(notification));
830 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
831 } catch (Exception e) {
832 System.out.println("Can't deliver notification: " + notification);
836 // Retract everything
842 System.err.println("constructed MSO request is invalid.");
851 * This rule responds to APPC Response Events
854 rule "${policyName}.APPC.RESPONSE"
856 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
857 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
858 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
859 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
860 $request : Request( getCommonHeader().RequestID == $event.requestID )
861 $response : Response( getCommonHeader().RequestID == $event.requestID )
863 System.out.println("rule APPC.RESPONSE is triggered.");
864 if ($response.Status == null) {
865 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
866 $manager.setControlLoopResult("FAILURE_EXCEPTION");
869 // Get the Response Code
871 ResponseCode code = ResponseCode.toResponseCode($response.Status.Code);
873 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
874 $manager.setControlLoopResult("FAILURE_EXCEPTION");
877 // Construct notification
879 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
880 notification.from = "policy";
881 notification.policyName = drools.getRule().getName();
882 notification.policyScope = "${policyScope}";
883 notification.policyVersion = "${policyVersion}";
884 notification.message = $operationWrapper.operation.toMessage();
885 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
887 // Ok, let's figure out what APP-C's response is
891 $operationWrapper.operation.outcome = "PROCESSING";
895 $operationWrapper.operation.outcome = "FAILURE_EXCEPTION";
896 $manager.setControlLoopResult("FAILURE_EXCEPTION");
899 $operationWrapper.operation.outcome = "SUCCESS";
900 $manager.setControlLoopResult("SUCCESS");
903 $operationWrapper.operation.outcome = "FAILURE";
904 $manager.setControlLoopResult("FAILURE");
907 if ($operationWrapper.operation.outcome.equals("SUCCESS")) {
908 notification.history.add($operationWrapper.operation);
909 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
911 // Let interested parties know
914 System.out.println(Serialization.gsonPretty.toJson(notification));
915 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
916 } catch (Exception e) {
917 System.out.println("Can't deliver notification: " + notification);
920 notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
922 System.out.println(Serialization.gsonPretty.toJson(notification));
923 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
924 } catch (Exception e) {
925 System.out.println("Can't deliver notification: " + notification);
930 // We are going to retract these objects from memory
932 System.out.println("Retracting everything");
933 retract($operationWrapper);
938 } else if ($operationWrapper.operation.outcome.equals("PROCESSING")) {
941 notification.history.add($operationWrapper.operation);
942 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
944 // Let interested parties know
947 System.out.println(Serialization.gsonPretty.toJson(notification));
948 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
949 } catch (Exception e) {
950 System.out.println("Can't deliver notification: " + notification);
953 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
955 // Let interested parties know
958 System.out.println(Serialization.gsonPretty.toJson(notification));
959 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
960 } catch (Exception e) {
961 System.out.println("Can't deliver notification: " + notification);
965 // We are going to retract these objects from memory
967 System.out.println("Retracting everything");
968 retract($operationWrapper);
979 * This rule is used to clean up APPC response
982 rule "${policyName}.APPC.RESPONSE.CLEANUP"
984 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
985 $response : Response($id : getCommonHeader().RequestID )
986 not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) )
988 System.out.println("rule APPC.RESPONSE.CLEANUP is triggered.");
994 * This rule responds to MSO Response Events
997 rule "${policyName}.MSO.RESPONSE"
999 $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" )
1000 $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET )
1001 $manager : EventManager( closedLoopControlName == $event.closedLoopControlName )
1002 $operationWrapper : ControlLoopOperationWrapper( requestID == $event.requestID )
1003 $request : MSORequest( requestId == $event.requestID.toString() )
1004 $response : MSOResponse( request.requestId == $event.requestID.toString() )
1006 System.out.println("rule MSO.RESPONSE is triggered.");
1008 // Construct notification
1010 VirtualControlLoopNotification notification = new VirtualControlLoopNotification($event);
1011 notification.from = "policy";
1012 notification.policyName = drools.getRule().getName();
1013 notification.policyScope = "${policyScope}";
1014 notification.policyVersion = "${policyVersion}";
1015 notification.message = $operationWrapper.operation.toMessage();
1016 $operationWrapper.operation.message = $operationWrapper.operation.toMessage();
1018 // The operation can either be succeeded or failed
1020 if($response.request.requestStatus.requestState.equals("Completed")) {
1021 $operationWrapper.operation.outcome = "SUCCESS";
1022 $manager.setControlLoopResult("SUCCESS");
1023 notification.history.add($operationWrapper.operation);
1024 notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS;
1026 // Let interested parties know
1029 System.out.println(Serialization.gsonPretty.toJson(notification));
1030 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1031 } catch (Exception e) {
1032 System.out.println("Can't deliver notification: " + notification);
1033 e.printStackTrace();
1035 notification.notification = ControlLoopNotificationType.FINAL_SUCCESS;
1037 // Let interested parties know
1040 System.out.println(Serialization.gsonPretty.toJson(notification));
1041 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1042 } catch (Exception e) {
1043 System.out.println("Can't deliver notification: " + notification);
1044 e.printStackTrace();
1047 // We are going to retract these objects from memory
1049 System.out.println("Retracting everything");
1050 retract($operationWrapper);
1056 $operationWrapper.operation.outcome = "FAILURE";
1057 $manager.setControlLoopResult("FAILURE");
1058 notification.history.add($operationWrapper.operation);
1059 notification.notification = ControlLoopNotificationType.OPERATION_FAILURE;
1061 // Let interested parties know
1064 System.out.println(Serialization.gsonPretty.toJson(notification));
1065 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1066 } catch (Exception e) {
1067 System.out.println("Can't deliver notification: " + notification);
1068 e.printStackTrace();
1070 notification.notification = ControlLoopNotificationType.FINAL_FAILURE;
1072 // Let interested parties know
1075 System.out.println(Serialization.gsonPretty.toJson(notification));
1076 PolicyEngine.manager.deliver($params.getNotificationTopic(), notification);
1077 } catch (Exception e) {
1078 System.out.println("Can't deliver notification: " + notification);
1079 e.printStackTrace();
1082 // We are going to retract these objects from memory
1084 System.out.println("Retracting everything");
1085 retract($operationWrapper);