Guo Ruijing | 073cc18 | 2017-07-31 08:47:35 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * archetype-closed-loop-demo-rules |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | package org.onap.policy.controlloop; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.LinkedList; |
| 25 | import java.util.Map; |
| 26 | import java.util.HashMap; |
| 27 | import java.util.UUID; |
| 28 | |
| 29 | import org.onap.policy.controlloop.VirtualControlLoopEvent; |
| 30 | import org.onap.policy.controlloop.ControlLoopEventStatus; |
| 31 | import org.onap.policy.controlloop.VirtualControlLoopNotification; |
| 32 | import org.onap.policy.controlloop.ControlLoopNotificationType; |
| 33 | import org.onap.policy.controlloop.ControlLoopOperation; |
| 34 | import org.onap.policy.controlloop.ControlLoopOperationWrapper; |
| 35 | import org.onap.policy.template.demo.ControlLoopException; |
| 36 | |
| 37 | import org.onap.policy.aai.AAINQF199.AAINQF199CloudRegion; |
| 38 | import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperties; |
| 39 | import org.onap.policy.aai.AAINQF199.AAINQF199ExtraProperty; |
| 40 | import org.onap.policy.aai.AAINQF199.AAINQF199GenericVNF; |
| 41 | import org.onap.policy.aai.AAINQF199.AAINQF199InstanceFilters; |
| 42 | import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItem; |
| 43 | import org.onap.policy.aai.AAINQF199.AAINQF199InventoryResponseItems; |
| 44 | import org.onap.policy.aai.AAINQF199.AAINQF199Manager; |
| 45 | import org.onap.policy.aai.AAINQF199.AAINQF199NamedQuery; |
| 46 | import org.onap.policy.aai.AAINQF199.AAINQF199QueryParameters; |
| 47 | import org.onap.policy.aai.AAINQF199.AAINQF199Request; |
| 48 | import org.onap.policy.aai.AAINQF199.AAINQF199RequestWrapper; |
| 49 | import org.onap.policy.aai.AAINQF199.AAINQF199Response; |
| 50 | import org.onap.policy.aai.AAINQF199.AAINQF199ResponseWrapper; |
| 51 | import org.onap.policy.aai.AAINQF199.AAINQF199ServiceInstance; |
| 52 | import org.onap.policy.aai.AAINQF199.AAINQF199Tenant; |
| 53 | import org.onap.policy.aai.AAINQF199.AAINQF199VfModule; |
| 54 | import org.onap.policy.aai.AAINQF199.AAINQF199VServer; |
| 55 | import org.onap.policy.aai.util.Serialization; |
| 56 | |
| 57 | import org.onap.policy.appc.CommonHeader; |
| 58 | import org.onap.policy.appc.Request; |
| 59 | import org.onap.policy.appc.Response; |
| 60 | import org.onap.policy.appc.ResponseCode; |
| 61 | import org.onap.policy.appc.ResponseStatus; |
| 62 | import org.onap.policy.appc.ResponseValue; |
| 63 | |
| 64 | import org.onap.policy.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; |
| 68 | |
| 69 | import org.onap.policy.mso.MSOManager; |
| 70 | import org.onap.policy.mso.MSORequest; |
| 71 | import org.onap.policy.mso.MSORequestStatus; |
| 72 | import org.onap.policy.mso.MSORequestDetails; |
| 73 | import org.onap.policy.mso.MSOModelInfo; |
| 74 | import org.onap.policy.mso.MSOCloudConfiguration; |
| 75 | import org.onap.policy.mso.MSORequestInfo; |
| 76 | import org.onap.policy.mso.MSORequestParameters; |
| 77 | import org.onap.policy.mso.MSORelatedInstanceListElement; |
| 78 | import org.onap.policy.mso.MSORelatedInstance; |
| 79 | import org.onap.policy.mso.MSOResponse; |
| 80 | |
| 81 | import org.onap.policy.drools.system.PolicyEngine; |
| 82 | |
| 83 | // |
| 84 | // These parameters are required to build the runtime policy |
| 85 | // |
| 86 | declare Params |
| 87 | closedLoopControlName : String |
| 88 | actor : String |
| 89 | aaiURL : String |
| 90 | aaiUsername : String |
| 91 | aaiPassword : String |
| 92 | msoURL : String |
| 93 | msoUsername : String |
| 94 | msoPassword : String |
| 95 | aaiNamedQueryUUID : String |
| 96 | aaiPatternMatch : int |
| 97 | notificationTopic : String |
| 98 | appcTopic : String |
| 99 | end |
| 100 | |
| 101 | /* |
| 102 | * |
| 103 | * Called once and only once to insert the parameters into working memory for this Closed Loop policy. |
| 104 | * (Comment SETUP rule out for the first ONAP opensource release since policy BRMS_GW already puts a Params fact in there) |
| 105 | * |
| 106 | * |
| 107 | *rule "${policyName}.SETUP" |
| 108 | * when |
| 109 | * then |
| 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}"); |
| 124 | * // |
| 125 | * // This stays in memory as long as the rule is alive and running |
| 126 | * // |
| 127 | * insert(params); |
| 128 | *end |
| 129 | */ |
| 130 | /* |
| 131 | * |
| 132 | * This rule responds to DCAE Events |
| 133 | * |
| 134 | */ |
| 135 | rule "${policyName}.EVENT" |
| 136 | when |
| 137 | $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) |
| 138 | $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) |
| 139 | not ( EventManager( closedLoopControlName == $event.closedLoopControlName )) |
| 140 | then |
| 141 | System.out.println("rule EVENT is triggered."); |
| 142 | try { |
| 143 | // |
| 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/ |
| 146 | // |
| 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}"; |
| 155 | // |
| 156 | // Let interested parties know |
| 157 | // |
| 158 | try { |
| 159 | System.out.println(Serialization.gsonPretty.toJson(notification)); |
| 160 | PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); |
| 161 | } catch (Exception e) { |
| 162 | e.printStackTrace(); |
| 163 | System.out.println("Can't deliver notification: " + notification); |
| 164 | } |
| 165 | // |
| 166 | // Retract it from memory |
| 167 | // |
| 168 | retract($event); |
| 169 | System.out.println("Event with requestID=null has been retracted."); |
| 170 | } else { |
| 171 | // |
| 172 | // Create an EventManager |
| 173 | // |
| 174 | EventManager manager = new EventManager($params.getClosedLoopControlName(), $event.requestID, $event.target); |
| 175 | // |
| 176 | // Determine if EventManager can actively process the event (i.e. syntax) |
| 177 | // |
| 178 | VirtualControlLoopNotification notification = manager.activate($event); |
| 179 | notification.from = "policy"; |
| 180 | notification.policyName = drools.getRule().getName(); |
| 181 | notification.policyScope = "${policyScope}"; |
| 182 | notification.policyVersion = "${policyVersion}"; |
| 183 | // |
| 184 | // Are we actively pursuing this event? |
| 185 | // |
| 186 | if (notification.notification == ControlLoopNotificationType.ACTIVE) { |
| 187 | // |
| 188 | // Insert Event Manager into memory, this will now kick off processing. |
| 189 | // |
| 190 | insert(manager); |
| 191 | // |
| 192 | // Let interested parties know |
| 193 | // |
| 194 | try { |
| 195 | System.out.println(Serialization.gsonPretty.toJson(notification)); |
| 196 | PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); |
| 197 | } catch (Exception e) { |
| 198 | e.printStackTrace(); |
| 199 | System.out.println("Can't deliver notification: " + notification); |
| 200 | } |
| 201 | } else { |
| 202 | // |
| 203 | // Let interested parties know |
| 204 | // |
| 205 | try { |
| 206 | System.out.println(Serialization.gsonPretty.toJson(notification)); |
| 207 | PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); |
| 208 | } catch (Exception e) { |
| 209 | e.printStackTrace(); |
| 210 | System.out.println("Can't deliver notification: " + notification); |
| 211 | } |
| 212 | // |
| 213 | // Retract it from memory |
| 214 | // |
| 215 | retract($event); |
| 216 | } |
| 217 | // |
| 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. |
| 222 | // |
| 223 | } |
| 224 | // |
| 225 | } catch (Exception e) { |
| 226 | e.printStackTrace(); |
| 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}"; |
| 233 | // |
| 234 | // |
| 235 | // |
| 236 | try { |
| 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(); |
| 242 | } |
| 243 | // |
| 244 | // Retract the event |
| 245 | // |
| 246 | retract($event); |
| 247 | } |
| 248 | end |
| 249 | |
| 250 | /* |
| 251 | * |
| 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. |
| 254 | * |
| 255 | */ |
| 256 | rule "${policyName}.EVENT.MANAGER" |
| 257 | when |
| 258 | $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) |
| 259 | $event : VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), closedLoopEventStatus == ControlLoopEventStatus.ONSET ) |
| 260 | $manager : EventManager( closedLoopControlName == $event.closedLoopControlName, controlLoopResult == null) |
| 261 | then |
| 262 | System.out.println("rule EVENT.MANAGER is triggered."); |
| 263 | // |
| 264 | // Check which event this is. |
| 265 | // |
| 266 | EventManager.NEW_EVENT_STATUS eventStatus = $manager.onNewEvent($event); |
| 267 | // |
| 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. |
| 271 | // |
| 272 | if (eventStatus != EventManager.NEW_EVENT_STATUS.FIRST_ONSET) { |
| 273 | System.out.println("Retracting "+eventStatus+" Event."); |
| 274 | retract($event); |
| 275 | return; |
| 276 | } |
| 277 | // |
| 278 | // Now the event in memory is first onset event |
| 279 | // |
| 280 | try { |
| 281 | // |
| 282 | // Pull the known AAI field from the Event |
| 283 | // |
| 284 | // generic-vnf is needed for vFirewall case |
| 285 | // vserver-name is needed for vLoadBalancer case |
| 286 | // |
| 287 | String genericVNF = $event.AAI.get("generic-vnf.vnf-id"); |
| 288 | String vserver = $event.AAI.get("vserver.vserver-name"); |
| 289 | // |
| 290 | // Check if we are implementing a simple pattern match. |
| 291 | // |
| 292 | if ($params.getAaiPatternMatch() == 1) { |
| 293 | // |
| 294 | // Yes |
| 295 | // |
| 296 | //Basic naming characteristics: |
| 297 | //VF Name (9 char)+VM name (13 char total)+VFC (19 char total) |
| 298 | //Example: |
| 299 | //VF Name (9 characters): cscf0001v |
| 300 | //VM Name(13 characters): cscf0001vm001 |
| 301 | //VFC name(19 characters): cscf0001vm001cfg001 |
| 302 | // |
| 303 | // zdfw1fwl01fwl02 or zdfw1fwl01fwl01 |
| 304 | // replaced with |
| 305 | // zdfw1fwl01pgn02 or zdfw1fwl01pgn01 |
| 306 | // |
| 307 | int index = genericVNF.lastIndexOf("fwl"); |
| 308 | if (index == -1) { |
| 309 | System.err.println("The generic-vnf.vnf-id from DCAE Event is not valid."); |
| 310 | } else { |
| 311 | genericVNF = genericVNF.substring(0, index) + "pgn" + genericVNF.substring(index+"fwl".length()); |
| 312 | } |
| 313 | // |
| 314 | // Construct an APPC request |
| 315 | // |
| 316 | ControlLoopOperation operation = new ControlLoopOperation(); |
| 317 | operation.actor = $params.getActor(); |
| 318 | operation.operation = "ModifyConfig"; |
| 319 | operation.target = $event.target; |
| 320 | // |
| 321 | // Create operationWrapper |
| 322 | // |
| 323 | ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); |
| 324 | // |
| 325 | // insert operationWrapper into memory |
| 326 | // |
| 327 | insert(operationWrapper); |
| 328 | // |
| 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>(); |
| 334 | // |
| 335 | // Fill in the payload |
| 336 | // |
| 337 | request.Payload.put("generic-vnf.vnf-id", genericVNF); |
| 338 | // |
| 339 | PGRequest pgRequest = new PGRequest(); |
| 340 | pgRequest.pgStreams = new PGStreams(); |
| 341 | |
| 342 | PGStream pgStream; |
| 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); |
| 348 | } |
| 349 | request.Payload.put("pg-streams", pgRequest.pgStreams); |
| 350 | |
| 351 | if (request != null) { |
| 352 | // |
| 353 | // Insert request into memory |
| 354 | // |
| 355 | insert(request); |
| 356 | // |
| 357 | // Tell interested parties we are performing this Operation |
| 358 | // |
| 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}"; |
| 366 | try { |
| 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); |
| 371 | e.printStackTrace(); |
| 372 | } |
| 373 | // |
| 374 | // Now send the operation request |
| 375 | // |
| 376 | if (request instanceof Request) { |
| 377 | try { |
| 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) { |
| 382 | e.printStackTrace(); |
| 383 | System.out.println("Can't deliver request: " + request); |
| 384 | } |
| 385 | } |
| 386 | } else { |
| 387 | // |
| 388 | // what happens if it is null |
| 389 | // |
| 390 | } |
| 391 | // |
| 392 | } else { |
| 393 | // |
| 394 | // create AAI named-query request with UUID started with "F199" |
| 395 | // |
| 396 | AAINQF199Request aainqf199request = new AAINQF199Request(); |
| 397 | AAINQF199QueryParameters aainqf199queryparam = new AAINQF199QueryParameters(); |
| 398 | AAINQF199NamedQuery aainqf199namedquery = new AAINQF199NamedQuery(); |
| 399 | AAINQF199InstanceFilters aainqf199instancefilter = new AAINQF199InstanceFilters(); |
| 400 | // |
| 401 | // queryParameters |
| 402 | // |
| 403 | aainqf199namedquery.namedQueryUUID = UUID.fromString($params.getAaiNamedQueryUUID()); |
| 404 | aainqf199queryparam.namedQuery = aainqf199namedquery; |
| 405 | aainqf199request.queryParameters = aainqf199queryparam; |
| 406 | // |
| 407 | // instanceFilters |
| 408 | // |
| 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; |
| 415 | // |
| 416 | // print aainqf199request for debug |
| 417 | // |
| 418 | System.out.println("AAI Request sent:"); |
| 419 | System.out.println(Serialization.gsonPretty.toJson(aainqf199request)); |
| 420 | // |
| 421 | // Create AAINQF199RequestWrapper |
| 422 | // |
| 423 | AAINQF199RequestWrapper aainqf199RequestWrapper = new AAINQF199RequestWrapper($event.requestID, aainqf199request); |
| 424 | // |
| 425 | // insert aainqf199request into memory |
| 426 | // |
| 427 | insert(aainqf199RequestWrapper); |
| 428 | } |
| 429 | // |
| 430 | } catch (Exception e) { |
| 431 | e.printStackTrace(); |
| 432 | } |
| 433 | end |
| 434 | |
| 435 | /* |
| 436 | * |
| 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. |
| 439 | * |
| 440 | */ |
| 441 | rule "${policyName}.EVENT.MANAGER.AAINQF199REQUEST" |
| 442 | when |
| 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) |
| 447 | then |
| 448 | System.out.println("rule EVENT.MANAGER.AAINQF199REQUEST is triggered."); |
| 449 | // |
| 450 | // send the request |
| 451 | // |
| 452 | AAINQF199Response aainqf199response = AAINQF199Manager.postQuery($params.getAaiURL(), $params.getAaiUsername(), $params.getAaiPassword(), |
| 453 | $aainqf199RequestWrapper.aainqf199request, $event.requestID); |
| 454 | // |
| 455 | // Check AAI response |
| 456 | // |
| 457 | if (aainqf199response == null) { |
| 458 | System.err.println("Failed to get AAI response"); |
| 459 | // |
| 460 | // Fail and retract everything |
| 461 | // |
| 462 | retract($event); |
| 463 | retract($manager); |
| 464 | retract($aainqf199RequestWrapper); |
| 465 | } else { |
| 466 | // |
| 467 | // Create AAINQF199ResponseWrapper |
| 468 | // |
| 469 | AAINQF199ResponseWrapper aainqf199ResponseWrapper = new AAINQF199ResponseWrapper($event.requestID, aainqf199response); |
| 470 | // |
| 471 | // insert aainqf199ResponseWrapper to memeory |
| 472 | // |
| 473 | insert(aainqf199ResponseWrapper); |
| 474 | } |
| 475 | end |
| 476 | |
| 477 | /* |
| 478 | * |
| 479 | * This rule happens when we got a valid AAI response. We can start sending request to APPC or MSO now. |
| 480 | * |
| 481 | */ |
| 482 | rule "${policyName}.EVENT.MANAGER.AAINQF199RESPONSE" |
| 483 | when |
| 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) |
| 489 | then |
| 490 | System.out.println("rule EVENT.MANAGER.AAINQF199RESPONSE is triggered."); |
| 491 | // |
| 492 | // Extract related fields out of AAINQF199RESPONSE |
| 493 | // |
| 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; |
| 499 | try { |
| 500 | // |
| 501 | // vnfItem |
| 502 | // |
| 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; |
| 511 | // |
| 512 | // serviceItem |
| 513 | // |
| 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; |
| 520 | // |
| 521 | // Find the index for base vf module and non-base vf module |
| 522 | // |
| 523 | int baseIndex = -1; |
| 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); |
| 531 | } |
| 532 | // |
| 533 | if (baseIndex != -1 && nonBaseIndex != -1) { |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | // |
| 538 | // Report the error if either base vf module or non-base vf module is not found |
| 539 | // |
| 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); |
| 544 | retract($manager); |
| 545 | retract($event); |
| 546 | return; |
| 547 | } |
| 548 | // |
| 549 | // This comes from the base module |
| 550 | // |
| 551 | vfModuleItemVfModuleName = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(0).items.inventoryResponseItems.get(baseIndex).vfModule.vfModuleName; |
| 552 | vfModuleItemVfModuleName = vfModuleItemVfModuleName.replace("Vfmodule", "vDNS"); |
| 553 | // |
| 554 | // vfModuleItem - NOT the base module |
| 555 | // |
| 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; |
| 560 | // |
| 561 | // tenantItem |
| 562 | // |
| 563 | tenantItemTenantId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).tenant.tenantId; |
| 564 | // |
| 565 | // cloudRegionItem |
| 566 | // |
| 567 | cloudRegionItemCloudRegionId = $aainqf199ResponseWrapper.aainqf199response.inventoryResponseItems.get(0).items.inventoryResponseItems.get(1).items.inventoryResponseItems.get(0).cloudRegion.cloudRegionId; |
| 568 | // |
| 569 | } catch (Exception e) { |
| 570 | e.printStackTrace(); |
| 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}"; |
| 577 | // |
| 578 | try { |
| 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(); |
| 584 | } |
| 585 | // |
| 586 | notification.notification = ControlLoopNotificationType.FINAL_FAILURE; |
| 587 | notification.message = "Invalid named-query response from AAI"; |
| 588 | // |
| 589 | try { |
| 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(); |
| 595 | } |
| 596 | // |
| 597 | // Retract everything |
| 598 | // |
| 599 | retract($aainqf199RequestWrapper); |
| 600 | retract($aainqf199ResponseWrapper); |
| 601 | retract($manager); |
| 602 | retract($event); |
| 603 | return; |
| 604 | } |
| 605 | // |
| 606 | // Extracted fields should not be null |
| 607 | // |
| 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)) { |
| 617 | // |
| 618 | System.err.println("some fields are missing from AAI response."); |
| 619 | // |
| 620 | // Fail and retract everything |
| 621 | // |
| 622 | retract($aainqf199RequestWrapper); |
| 623 | retract($aainqf199ResponseWrapper); |
| 624 | retract($manager); |
| 625 | retract($event); |
| 626 | return; |
| 627 | } |
| 628 | // |
| 629 | // We don't need them any more |
| 630 | // |
| 631 | retract($aainqf199ResponseWrapper); |
| 632 | retract($aainqf199RequestWrapper); |
| 633 | // |
| 634 | // check the actor of this closed loop |
| 635 | // |
| 636 | switch ($params.getActor()) { |
| 637 | case "APPC": |
| 638 | { |
| 639 | // |
| 640 | // Construct an APPC request |
| 641 | // |
| 642 | ControlLoopOperation operation = new ControlLoopOperation(); |
| 643 | operation.actor = $params.getActor(); |
| 644 | operation.operation = "ModifyConfig"; |
| 645 | operation.target = $event.target; |
| 646 | // |
| 647 | // Create operationWrapper |
| 648 | // |
| 649 | ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); |
| 650 | // |
| 651 | // insert operationWrapper into memory |
| 652 | // |
| 653 | insert(operationWrapper); |
| 654 | // |
| 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>(); |
| 660 | // |
| 661 | // Fill in the payload |
| 662 | // Hardcode genericVNF for now since AAI has not been ready for vFirewall demo case |
| 663 | // |
| 664 | String genericVNF = "zdfw1fwl01pgn02"; |
| 665 | request.Payload.put("generic-vnf.vnf-id", genericVNF); |
| 666 | // |
| 667 | PGRequest pgRequest = new PGRequest(); |
| 668 | pgRequest.pgStreams = new PGStreams(); |
| 669 | |
| 670 | PGStream pgStream; |
| 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); |
| 676 | } |
| 677 | request.Payload.put("pg-streams", pgRequest.pgStreams); |
| 678 | |
| 679 | if (request != null) { |
| 680 | // |
| 681 | // Insert request into memory |
| 682 | // |
| 683 | insert(request); |
| 684 | // |
| 685 | // Tell interested parties we are performing this Operation |
| 686 | // |
| 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}"; |
| 694 | try { |
| 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); |
| 699 | e.printStackTrace(); |
| 700 | } |
| 701 | // |
| 702 | // Now send the operation request |
| 703 | // |
| 704 | if (request instanceof Request) { |
| 705 | try { |
| 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) { |
| 710 | e.printStackTrace(); |
| 711 | System.out.println("Can't deliver request: " + request); |
| 712 | } |
| 713 | } |
| 714 | } else { |
| 715 | // |
| 716 | // what happens if it is null |
| 717 | // |
| 718 | } |
| 719 | } |
| 720 | break; |
| 721 | case "MSO": |
| 722 | { |
| 723 | // |
| 724 | // Construct an operation |
| 725 | // |
| 726 | ControlLoopOperation operation = new ControlLoopOperation(); |
| 727 | operation.actor = $params.getActor(); |
| 728 | operation.operation = "createModuleInstance"; |
| 729 | operation.target = $event.target; |
| 730 | // |
| 731 | // Create operationWrapper |
| 732 | // |
| 733 | ControlLoopOperationWrapper operationWrapper = new ControlLoopOperationWrapper($event.requestID, operation); |
| 734 | // |
| 735 | // Construct an MSO request |
| 736 | // |
| 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; |
| 744 | // |
| 745 | // cloudConfiguration |
| 746 | // |
| 747 | request.requestDetails.cloudConfiguration.lcpCloudRegionId = cloudRegionItemCloudRegionId; |
| 748 | request.requestDetails.cloudConfiguration.tenantId = tenantItemTenantId; |
| 749 | // |
| 750 | // modelInfo |
| 751 | // |
| 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; |
| 757 | // |
| 758 | // requestInfo |
| 759 | // |
| 760 | request.requestDetails.requestInfo.instanceName = vfModuleItemVfModuleName; |
| 761 | request.requestDetails.requestInfo.source = "POLICY"; |
| 762 | request.requestDetails.requestInfo.suppressRollback = false; |
| 763 | // |
| 764 | // relatedInstanceList |
| 765 | // |
| 766 | MSORelatedInstanceListElement relatedInstanceListElement1 = new MSORelatedInstanceListElement(); |
| 767 | MSORelatedInstanceListElement relatedInstanceListElement2 = new MSORelatedInstanceListElement(); |
| 768 | relatedInstanceListElement1.relatedInstance = new MSORelatedInstance(); |
| 769 | relatedInstanceListElement2.relatedInstance = new MSORelatedInstance(); |
| 770 | // |
| 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; |
| 778 | // |
| 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; |
| 787 | // |
| 788 | request.requestDetails.relatedInstanceList.add(relatedInstanceListElement1); |
| 789 | request.requestDetails.relatedInstanceList.add(relatedInstanceListElement2); |
| 790 | // |
| 791 | // print MSO request for debug |
| 792 | // |
| 793 | System.out.println("MSO request sent:"); |
| 794 | System.out.println(Serialization.gsonPretty.toJson(request)); |
| 795 | // |
| 796 | // |
| 797 | // |
| 798 | if (request != null) { |
| 799 | // |
| 800 | // Tell interested parties we are performing this Operation |
| 801 | // |
| 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}"; |
| 808 | try { |
| 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); |
| 813 | e.printStackTrace(); |
| 814 | } |
| 815 | // |
| 816 | // Concatenate serviceItemServiceInstanceId and vnfItemVnfId to msoURL |
| 817 | // |
| 818 | String MSOUrl = $params.getMsoURL() + "/serviceInstances/v2/" + serviceItemServiceInstanceId + "/vnfs/" + vnfItemVnfId + "/vfModules"; |
| 819 | // |
| 820 | // Call MSO |
| 821 | // |
| 822 | MSOResponse response = MSOManager.createModuleInstance(MSOUrl, $params.getMsoURL(), $params.getMsoUsername(), $params.getMsoPassword(), request); |
| 823 | // |
| 824 | if (response != null) { |
| 825 | // |
| 826 | // Assign requestId |
| 827 | // |
| 828 | request.requestId = $event.requestID.toString(); |
| 829 | response.request.requestId = $event.requestID.toString(); |
| 830 | // |
| 831 | // Insert facts |
| 832 | // |
| 833 | insert(operationWrapper); |
| 834 | insert(request); |
| 835 | insert(response); |
| 836 | } else { |
| 837 | // |
| 838 | // MSO request not even accepted |
| 839 | // |
| 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; |
| 846 | // |
| 847 | // Let interested parties know |
| 848 | // |
| 849 | try { |
| 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); |
| 854 | e.printStackTrace(); |
| 855 | } |
| 856 | notification.notification = ControlLoopNotificationType.FINAL_FAILURE; |
| 857 | try { |
| 858 | System.out.println(Serialization.gsonPretty.toJson(notification)); |
| 859 | PolicyEngine.manager.deliver($params.getNotificationTopic(), notification); |
| 860 | } catch (Exception e) { |
| 861 | System.out.println("Can't deliver notification: " + notification); |
| 862 | e.printStackTrace(); |
| 863 | } |
| 864 | // |
| 865 | // Retract everything |
| 866 | // |
| 867 | retract($event); |
| 868 | retract($manager); |
| 869 | } |
| 870 | } else { |
| 871 | System.err.println("constructed MSO request is invalid."); |
| 872 | } |
| 873 | } |
| 874 | break; |
| 875 | } |
| 876 | end |
| 877 | |
| 878 | /* |
| 879 | * |
| 880 | * This rule responds to APPC Response Events |
| 881 | * |
| 882 | */ |
| 883 | rule "${policyName}.APPC.RESPONSE" |
| 884 | when |
| 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 ) |
| 891 | then |
| 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"); |
| 896 | } |
| 897 | // |
| 898 | // Get the Response Code |
| 899 | // |
| 900 | ResponseCode code = ResponseCode.toResponseCode($response.Status.Code); |
| 901 | if (code == null) { |
| 902 | $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; |
| 903 | $manager.setControlLoopResult("FAILURE_EXCEPTION"); |
| 904 | } |
| 905 | // |
| 906 | // Construct notification |
| 907 | // |
| 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(); |
| 915 | // |
| 916 | // Ok, let's figure out what APP-C's response is |
| 917 | // |
| 918 | switch (code) { |
| 919 | case ACCEPT: |
| 920 | $operationWrapper.operation.outcome = "PROCESSING"; |
| 921 | break; |
| 922 | case ERROR: |
| 923 | case REJECT: |
| 924 | $operationWrapper.operation.outcome = "FAILURE_EXCEPTION"; |
| 925 | $manager.setControlLoopResult("FAILURE_EXCEPTION"); |
| 926 | break; |
| 927 | case SUCCESS: |
| 928 | $operationWrapper.operation.outcome = "SUCCESS"; |
| 929 | $manager.setControlLoopResult("SUCCESS"); |
| 930 | break; |
| 931 | case FAILURE: |
| 932 | $operationWrapper.operation.outcome = "FAILURE"; |
| 933 | $manager.setControlLoopResult("FAILURE"); |
| 934 | break; |
| 935 | } |
| 936 | if ($operationWrapper.operation.outcome.equals("SUCCESS")) { |
| 937 | notification.history.add($operationWrapper.operation); |
| 938 | notification.notification = ControlLoopNotificationType.OPERATION_SUCCESS; |
| 939 | // |
| 940 | // Let interested parties know |
| 941 | // |
| 942 | try { |
| 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); |
| 947 | e.printStackTrace(); |
| 948 | } |
| 949 | notification.notification = ControlLoopNotificationType.FINAL_SUCCESS; |
| 950 | try { |
| 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); |
| 955 | e.printStackTrace(); |
| 956 | } |
| 957 | |
| 958 | // |
| 959 | // We are going to retract these objects from memory |
| 960 | // |
| 961 | System.out.println("Retracting everything"); |
| 962 | retract($operationWrapper); |
| 963 | retract($request); |
| 964 | retract($response); |
| 965 | retract($event); |
| 966 | retract($manager); |
| 967 | } else if ($operationWrapper.operation.outcome.equals("PROCESSING")) { |
| 968 | retract($response); |
| 969 | } else { |
| 970 | notification.history.add($operationWrapper.operation); |
| 971 | notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; |
| 972 | // |
| 973 | // Let interested parties know |
| 974 | // |
| 975 | try { |
| 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); |
| 980 | e.printStackTrace(); |
| 981 | } |
| 982 | notification.notification = ControlLoopNotificationType.FINAL_FAILURE; |
| 983 | // |
| 984 | // Let interested parties know |
| 985 | // |
| 986 | try { |
| 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); |
| 991 | e.printStackTrace(); |
| 992 | } |
| 993 | // |
| 994 | // We are going to retract these objects from memory |
| 995 | // |
| 996 | System.out.println("Retracting everything"); |
| 997 | retract($operationWrapper); |
| 998 | retract($request); |
| 999 | retract($response); |
| 1000 | retract($event); |
| 1001 | retract($manager); |
| 1002 | } |
| 1003 | |
| 1004 | end |
| 1005 | |
| 1006 | /* |
| 1007 | * |
| 1008 | * This rule is used to clean up APPC response |
| 1009 | * |
| 1010 | */ |
| 1011 | rule "${policyName}.APPC.RESPONSE.CLEANUP" |
| 1012 | when |
| 1013 | $params : Params( getClosedLoopControlName() == "${closedLoopControlName}" ) |
| 1014 | $response : Response($id : getCommonHeader().RequestID ) |
| 1015 | not ( VirtualControlLoopEvent( closedLoopControlName == $params.getClosedLoopControlName(), requestID == $id, closedLoopEventStatus == ControlLoopEventStatus.ONSET ) ) |
| 1016 | then |
| 1017 | System.out.println("rule APPC.RESPONSE.CLEANUP is triggered."); |
| 1018 | retract($response); |
| 1019 | end |
| 1020 | |
| 1021 | /* |
| 1022 | * |
| 1023 | * This rule responds to MSO Response Events |
| 1024 | * |
| 1025 | */ |
| 1026 | rule "${policyName}.MSO.RESPONSE" |
| 1027 | when |
| 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() ) |
| 1034 | then |
| 1035 | System.out.println("rule MSO.RESPONSE is triggered."); |
| 1036 | // |
| 1037 | // Construct notification |
| 1038 | // |
| 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(); |
| 1046 | // |
| 1047 | // The operation can either be succeeded or failed |
| 1048 | // |
| 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; |
| 1054 | // |
| 1055 | // Let interested parties know |
| 1056 | // |
| 1057 | try { |
| 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(); |
| 1063 | } |
| 1064 | notification.notification = ControlLoopNotificationType.FINAL_SUCCESS; |
| 1065 | // |
| 1066 | // Let interested parties know |
| 1067 | // |
| 1068 | try { |
| 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(); |
| 1074 | } |
| 1075 | // |
| 1076 | // We are going to retract these objects from memory |
| 1077 | // |
| 1078 | System.out.println("Retracting everything"); |
| 1079 | retract($operationWrapper); |
| 1080 | retract($request); |
| 1081 | retract($response); |
| 1082 | retract($event); |
| 1083 | retract($manager); |
| 1084 | } else { |
| 1085 | $operationWrapper.operation.outcome = "FAILURE"; |
| 1086 | $manager.setControlLoopResult("FAILURE"); |
| 1087 | notification.history.add($operationWrapper.operation); |
| 1088 | notification.notification = ControlLoopNotificationType.OPERATION_FAILURE; |
| 1089 | // |
| 1090 | // Let interested parties know |
| 1091 | // |
| 1092 | try { |
| 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(); |
| 1098 | } |
| 1099 | notification.notification = ControlLoopNotificationType.FINAL_FAILURE; |
| 1100 | // |
| 1101 | // Let interested parties know |
| 1102 | // |
| 1103 | try { |
| 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(); |
| 1109 | } |
| 1110 | // |
| 1111 | // We are going to retract these objects from memory |
| 1112 | // |
| 1113 | System.out.println("Retracting everything"); |
| 1114 | retract($operationWrapper); |
| 1115 | retract($request); |
| 1116 | retract($response); |
| 1117 | retract($event); |
| 1118 | retract($manager); |
| 1119 | } |
| 1120 | |
| 1121 | end |