006899efe8507287f612ba37f8b419862a03ce47
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * controlloop operation manager
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
7  * Modifications Copyright (C) 2019 Tech Mahindra
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.controlloop.eventmanager;
24
25 import java.io.Serializable;
26 import java.sql.Timestamp;
27 import java.time.Instant;
28 import java.util.AbstractMap;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.NoSuchElementException;
32 import java.util.Properties;
33 import javax.persistence.EntityManager;
34 import javax.persistence.Persistence;
35 import org.eclipse.persistence.config.PersistenceUnitProperties;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.policy.aai.util.AaiException;
38 import org.onap.policy.appc.Response;
39 import org.onap.policy.appc.ResponseCode;
40 import org.onap.policy.appclcm.LcmResponseWrapper;
41 import org.onap.policy.controlloop.ControlLoopEvent;
42 import org.onap.policy.controlloop.ControlLoopException;
43 import org.onap.policy.controlloop.ControlLoopOperation;
44 import org.onap.policy.controlloop.ControlLoopResponse;
45 import org.onap.policy.controlloop.VirtualControlLoopEvent;
46 import org.onap.policy.controlloop.actor.appc.AppcActorServiceProvider;
47 import org.onap.policy.controlloop.actor.appclcm.AppcLcmActorServiceProvider;
48 import org.onap.policy.controlloop.actor.sdnc.SdncActorServiceProvider;
49 import org.onap.policy.controlloop.actor.sdnr.SdnrActorServiceProvider;
50 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
51 import org.onap.policy.controlloop.actor.vfc.VfcActorServiceProvider;
52 import org.onap.policy.controlloop.policy.Policy;
53 import org.onap.policy.controlloop.policy.PolicyResult;
54 import org.onap.policy.database.operationshistory.Dbao;
55 import org.onap.policy.drools.system.PolicyEngine;
56 import org.onap.policy.guard.Util;
57 import org.onap.policy.sdnc.SdncResponse;
58 import org.onap.policy.sdnr.PciResponseWrapper;
59 import org.onap.policy.so.SoResponseWrapper;
60 import org.onap.policy.vfc.VfcResponse;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 public class ControlLoopOperationManager implements Serializable {
65     private static final String SUCCESS_MSG = " Success";
66     private static final String FAILED_MSG = " Failed";
67     private static final String AAI_CUSTOM_QUERY = "aai.customQuery";
68     private static final long serialVersionUID = -3773199283624595410L;
69     private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManager.class);
70
71     private static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
72     private static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
73     private static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
74
75     //
76     // These properties are not changeable, but accessible
77     // for Drools Rule statements.
78     //
79     public final ControlLoopEvent onset;
80     public final Policy policy;
81
82     //
83     // Properties used to track the Operation
84     //
85     private int attempts = 0;
86     private Operation currentOperation = null;
87     private LinkedList<Operation> operationHistory = new LinkedList<>();
88     private PolicyResult policyResult = null;
89     private ControlLoopEventManager eventManager = null;
90     private String targetEntity;
91     private String guardApprovalStatus = "NONE";// "NONE", "PERMIT", "DENY"
92     private transient Object operationRequest;
93
94     /**
95      * Construct an instance.
96      *
97      * @param onset the onset event
98      * @param policy the policy
99      * @param em the event manager
100      * @throws ControlLoopException if an error occurs
101      * @throws AaiException if an error occurs retrieving information from A&AI
102      */
103     public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy, ControlLoopEventManager em)
104             throws ControlLoopException, AaiException {
105         this.onset = onset;
106         this.policy = policy;
107         this.guardApprovalStatus = "NONE";
108         this.eventManager = em;
109         this.targetEntity = getTarget(policy);
110
111         //
112         // Let's make a sanity check
113         //
114         switch (policy.getActor()) {
115             case "APPC":
116                 initAppc(onset, policy);
117                 break;
118             case "SO":
119                 break;
120             case "SDNR":
121                 break;
122             case "VFC":
123                 break;
124             case "SDNC":
125                 break;
126             default:
127                 throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor.");
128         }
129     }
130
131
132     private void initAppc(ControlLoopEvent onset, Policy policy) throws AaiException {
133         if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
134             /*
135              * The target vnf-id may not be the same as the source vnf-id specified in the yaml, the target
136              * vnf-id is retrieved by a named query to A&AI.
137              */
138             if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
139                 GenericVnf genvnf = this.eventManager.getCqResponse((VirtualControlLoopEvent) onset)
140                         .getGenericVnfByModelInvariantId(policy.getTarget().getResourceID());
141                 if (genvnf == null) {
142                     logger.info("Target entity could not be found");
143                     throw new AaiException("Target vnf-id could not be found");
144                 }
145                 this.targetEntity = genvnf.getVnfId();
146
147             } else {
148                 this.targetEntity =
149                         AppcLcmActorServiceProvider.vnfNamedQuery(policy.getTarget().getResourceID(),
150                                 this.targetEntity, PolicyEngine.manager.getEnvironmentProperty("aai.url"),
151                                 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
152                                 PolicyEngine.manager.getEnvironmentProperty("aai.password"));
153             }
154         }
155     }
156
157
158     public ControlLoopEventManager getEventManager() {
159         return eventManager;
160     }
161
162     public void setEventManager(ControlLoopEventManager eventManager) {
163         this.eventManager = eventManager;
164     }
165
166     public String getTargetEntity() {
167         return this.targetEntity;
168     }
169
170     @Override
171     public String toString() {
172         return "ControlLoopOperationManager [onset=" + (onset != null ? onset.getRequestId() : "null") + ", policy="
173                 + (policy != null ? policy.getId() : "null") + ", attempts=" + attempts + ", policyResult="
174                 + policyResult + ", currentOperation=" + currentOperation + ", operationHistory=" + operationHistory
175                 + "]";
176     }
177
178     //
179     // Internal class used for tracking
180     //
181     private class Operation implements Serializable {
182         private static final long serialVersionUID = 1L;
183
184         private ControlLoopOperation clOperation = new ControlLoopOperation();
185         private PolicyResult policyResult = null;
186         private int attempt = 0;
187
188         @Override
189         public String toString() {
190             return "Operation [attempt=" + attempt + ", policyResult=" + policyResult + ", operation=" + clOperation
191                     + "]";
192         }
193     }
194
195     public Object getOperationRequest() {
196         return operationRequest;
197     }
198
199     public String getGuardApprovalStatus() {
200         return guardApprovalStatus;
201     }
202
203     public void setGuardApprovalStatus(String guardApprovalStatus) {
204         this.guardApprovalStatus = guardApprovalStatus;
205     }
206
207     /**
208      * Get the target for a policy.
209      *
210      * @param policy the policy
211      * @return the target
212      * @throws ControlLoopException if an error occurs
213      * @throws AaiException if an error occurs retrieving information from A&AI
214      */
215     public String getTarget(Policy policy) throws ControlLoopException, AaiException {
216         if (policy.getTarget() == null) {
217             throw new ControlLoopException("The target is null");
218         }
219
220         if (policy.getTarget().getType() == null) {
221             throw new ControlLoopException("The target type is null");
222         }
223
224         switch (policy.getTarget().getType()) {
225             case PNF:
226                 throw new ControlLoopException("PNF target is not supported");
227             case VM:
228             case VNF:
229                 return getVfModuleTarget();
230             case VFMODULE:
231                 return getVfModuleTarget();
232             default:
233                 throw new ControlLoopException("The target type is not supported");
234         }
235     }
236
237
238     private String getVfModuleTarget() throws AaiException, ControlLoopException {
239         VirtualControlLoopEvent virtualOnsetEvent = (VirtualControlLoopEvent) this.onset;
240         if (this.onset.getTarget().equalsIgnoreCase(VSERVER_VSERVER_NAME)) {
241             return virtualOnsetEvent.getAai().get(VSERVER_VSERVER_NAME);
242         } else if (this.onset.getTarget().equalsIgnoreCase(GENERIC_VNF_VNF_ID)) {
243             return virtualOnsetEvent.getAai().get(GENERIC_VNF_VNF_ID);
244         } else if (this.onset.getTarget().equalsIgnoreCase(GENERIC_VNF_VNF_NAME)) {
245             /*
246              * If the onset is enriched with the vnf-id, we don't need an A&AI response
247              */
248             if (virtualOnsetEvent.getAai().containsKey(GENERIC_VNF_VNF_ID)) {
249                 return virtualOnsetEvent.getAai().get(GENERIC_VNF_VNF_ID);
250             }
251
252             /*
253              * If the vnf-name was retrieved from the onset then the vnf-id must be obtained from the event
254              * manager's A&AI GET query
255              */
256             String vnfId;
257             if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
258                 vnfId = this.eventManager.getCqResponse((VirtualControlLoopEvent) onset).getDefaultGenericVnf()
259                         .getVnfId();
260             } else {
261                 vnfId = this.eventManager.getVnfResponse().getVnfId();
262             }
263             if (vnfId == null) {
264                 throw new AaiException("No vnf-id found");
265             }
266             return vnfId;
267         }
268         throw new ControlLoopException("Target does not match target type");
269     }
270
271     /**
272      * Start an operation.
273      *
274      * @param onset the onset event
275      * @return the operation request
276      * @throws ControlLoopException if an error occurs
277      * @throws AaiException if error occurs
278      */
279     public Object startOperation(/* VirtualControlLoopEvent */ControlLoopEvent onset)
280             throws ControlLoopException, AaiException {
281         verifyOperatonCanRun();
282
283         //
284         // Setup
285         //
286         this.policyResult = null;
287         Operation operation = new Operation();
288         operation.attempt = ++this.attempts;
289         operation.clOperation.setActor(this.policy.getActor());
290         operation.clOperation.setOperation(this.policy.getRecipe());
291         operation.clOperation.setTarget(this.policy.getTarget().toString());
292         operation.clOperation.setSubRequestId(Integer.toString(operation.attempt));
293         //
294         // Now determine which actor we need to construct a request for
295         //
296         switch (policy.getActor()) {
297             case "APPC":
298                 return startAppcOperation(onset, operation);
299             case "SO":
300                 return startSoOperation(onset, operation);
301             case "VFC":
302                 return startVfcOperation(onset, operation);
303             case "SDNR":
304                 return startSdnrOperation(onset, operation);
305             case "SDNC":
306                 return startSdncOperation(onset, operation);
307             default:
308                 throw new ControlLoopException("invalid actor " + policy.getActor() + " on policy");
309         }
310     }
311
312
313     private Object startAppcOperation(ControlLoopEvent onset, Operation operation) {
314         /*
315          * If the recipe is ModifyConfig, a legacy APPC request is constructed. Otherwise an LCMRequest is
316          * constructed.
317          */
318         this.currentOperation = operation;
319         if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
320             this.operationRequest = AppcActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
321                     operation.clOperation, this.policy, this.targetEntity);
322         } else {
323             this.operationRequest = AppcLcmActorServiceProvider.constructRequest(
324                     (VirtualControlLoopEvent) onset, operation.clOperation, this.policy, this.targetEntity);
325         }
326         //
327         // Save the operation
328         //
329
330         return operationRequest;
331     }
332
333
334     private Object startSoOperation(ControlLoopEvent onset, Operation operation) throws AaiException {
335         SoActorServiceProvider soActorSp = new SoActorServiceProvider();
336         if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
337             this.operationRequest =
338                     soActorSp.constructRequestCq((VirtualControlLoopEvent) onset, operation.clOperation,
339                             this.policy, eventManager.getCqResponse((VirtualControlLoopEvent) onset));
340         } else {
341             this.operationRequest = soActorSp.constructRequest((VirtualControlLoopEvent) onset,
342                     operation.clOperation, this.policy, eventManager.getNqVserverFromAai());
343         }
344
345         // Save the operation
346         this.currentOperation = operation;
347
348         if (this.operationRequest == null) {
349             this.policyResult = PolicyResult.FAILURE;
350         }
351
352         return operationRequest;
353     }
354
355
356     private Object startVfcOperation(ControlLoopEvent onset, Operation operation) throws AaiException {
357         if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
358             this.operationRequest = VfcActorServiceProvider.constructRequestCq((VirtualControlLoopEvent) onset,
359                     operation.clOperation, this.policy,
360                     eventManager.getCqResponse((VirtualControlLoopEvent) onset));
361         } else {
362             this.operationRequest = VfcActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
363                     operation.clOperation, this.policy, this.eventManager.getVnfResponse(),
364                     PolicyEngine.manager.getEnvironmentProperty("vfc.url"),
365                     PolicyEngine.manager.getEnvironmentProperty("vfc.username"),
366                     PolicyEngine.manager.getEnvironmentProperty("vfc.password"));
367         }
368         this.currentOperation = operation;
369         if (this.operationRequest == null) {
370             this.policyResult = PolicyResult.FAILURE;
371         }
372         return operationRequest;
373     }
374
375
376     private Object startSdnrOperation(ControlLoopEvent onset, Operation operation) {
377         /*
378          * If the recipe is ModifyConfig or ModifyConfigANR, a SDNR request is constructed.
379          */
380         this.currentOperation = operation;
381         this.operationRequest = SdnrActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
382                 operation.clOperation, this.policy);
383         //
384         // Save the operation
385         //
386         if (this.operationRequest == null) {
387             this.policyResult = PolicyResult.FAILURE;
388         }
389
390         return operationRequest;
391     }
392
393
394     private Object startSdncOperation(ControlLoopEvent onset, Operation operation) {
395         SdncActorServiceProvider provider = new SdncActorServiceProvider();
396         this.operationRequest =
397                 provider.constructRequest((VirtualControlLoopEvent) onset, operation.clOperation, this.policy);
398         this.currentOperation = operation;
399         if (this.operationRequest == null) {
400             this.policyResult = PolicyResult.FAILURE;
401         }
402         return operationRequest;
403     }
404
405     /**
406      * Handle a response.
407      *
408      * @param response the response
409      * @return a PolicyResult
410      */
411     public PolicyResult onResponse(Object response) {
412         //
413         // Which response is it?
414         //
415         if (response instanceof Response) {
416             //
417             // Cast APPC response and handle it
418             //
419             return onResponse((Response) response);
420         } else if (response instanceof LcmResponseWrapper) {
421             //
422             // Cast LCM response and handle it
423             //
424             return onResponse((LcmResponseWrapper) response);
425         } else if (response instanceof PciResponseWrapper) {
426             //
427             // Cast SDNR response and handle it
428             //
429             return onResponse((PciResponseWrapper) response);
430         } else if (response instanceof SoResponseWrapper) {
431             //
432             // Cast SO response and handle it
433             //
434             return onResponse((SoResponseWrapper) response);
435         } else if (response instanceof VfcResponse) {
436             //
437             // Cast VFC response and handle it
438             //
439             return onResponse((VfcResponse) response);
440         } else if (response instanceof SdncResponse) {
441             //
442             // Cast SDNC response and handle it
443             //
444             return onResponse((SdncResponse) response);
445         } else {
446             return null;
447         }
448     }
449
450     /**
451      * This method handles operation responses from APPC.
452      *
453      * @param appcResponse the APPC response
454      * @return The result of the response handling
455      */
456     private PolicyResult onResponse(Response appcResponse) {
457         //
458         // Determine which subrequestID (ie. attempt)
459         //
460         Integer operationAttempt = getSubRequestId(appcResponse);
461         if (operationAttempt == null) {
462             this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).",
463                             PolicyResult.FAILURE_EXCEPTION);
464             return PolicyResult.FAILURE_EXCEPTION;
465         }
466         //
467         // Sanity check the response message
468         //
469         if (appcResponse.getStatus() == null) {
470             //
471             // We cannot tell what happened if this doesn't exist
472             //
473             this.completeOperation(operationAttempt,
474                     "Policy was unable to parse APP-C response status field (it was null).",
475                     PolicyResult.FAILURE_EXCEPTION);
476             return PolicyResult.FAILURE_EXCEPTION;
477         }
478         //
479         // Get the Response Code
480         //
481         ResponseCode code = ResponseCode.toResponseCode(appcResponse.getStatus().getCode());
482         if (code == null) {
483             //
484             // We are unaware of this code
485             //
486             this.completeOperation(operationAttempt, "Policy was unable to parse APP-C response status code field.",
487                     PolicyResult.FAILURE_EXCEPTION);
488             return PolicyResult.FAILURE_EXCEPTION;
489         }
490
491         return onResponse(appcResponse, operationAttempt, code);
492     }
493
494
495     private PolicyResult onResponse(Response appcResponse, Integer operationAttempt, ResponseCode code) {
496         //
497         // Ok, let's figure out what APP-C's response is
498         //
499         switch (code) {
500             case ACCEPT:
501                 //
502                 // This is good, they got our original message and
503                 // acknowledged it.
504                 //
505                 // Is there any need to track this?
506                 //
507                 return null;
508             case ERROR:
509             case REJECT:
510                 //
511                 // We'll consider these two codes as exceptions
512                 //
513                 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
514                         PolicyResult.FAILURE_EXCEPTION);
515                 return getTimeoutResult(PolicyResult.FAILURE_EXCEPTION);
516             case SUCCESS:
517                 //
518                 //
519                 //
520                 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
521                         PolicyResult.SUCCESS);
522                 return getTimeoutResult(PolicyResult.SUCCESS);
523             case FAILURE:
524                 //
525                 //
526                 //
527                 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
528                         PolicyResult.FAILURE);
529                 return getTimeoutResult(PolicyResult.FAILURE);
530             default:
531                 return null;
532         }
533     }
534
535     /**
536      * This method handles operation responses from LCM.
537      *
538      * @param dmaapResponse the LCM response
539      * @return The result of the response handling
540      */
541     private PolicyResult onResponse(LcmResponseWrapper dmaapResponse) {
542         /*
543          * Parse out the operation attempt using the subrequestid
544          */
545         Integer operationAttempt = AppcLcmActorServiceProvider
546                 .parseOperationAttempt(dmaapResponse.getBody().getCommonHeader().getSubRequestId());
547         if (operationAttempt == null) {
548             this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).",
549                     PolicyResult.FAILURE_EXCEPTION);
550             return PolicyResult.FAILURE_EXCEPTION;
551         }
552
553         /*
554          * Process the APPCLCM response to see what PolicyResult should be returned
555          */
556         AbstractMap.SimpleEntry<PolicyResult, String> result =
557                 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
558
559         if (result.getKey() != null) {
560             this.completeOperation(operationAttempt, result.getValue(), result.getKey());
561             if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
562                 return null;
563             }
564             return result.getKey();
565         }
566         return null;
567     }
568
569     /**
570      * This method handles operation responses from SDNR.
571      *
572      * @param dmaapResponse the SDNR response
573      * @return the result of the response handling
574      */
575     private PolicyResult onResponse(PciResponseWrapper dmaapResponse) {
576         /*
577          * Parse out the operation attempt using the subrequestid
578          */
579         Integer operationAttempt = SdnrActorServiceProvider
580                 .parseOperationAttempt(dmaapResponse.getBody().getCommonHeader().getSubRequestId());
581         if (operationAttempt == null) {
582             this.completeOperation(operationAttempt, "Policy was unable to parse SDNR SubRequestID.",
583                     PolicyResult.FAILURE_EXCEPTION);
584             return PolicyResult.FAILURE_EXCEPTION;
585         }
586
587         /*
588          * Process the SDNR response to see what PolicyResult should be returned
589          */
590         SdnrActorServiceProvider.Pair<PolicyResult, String> result =
591                 SdnrActorServiceProvider.processResponse(dmaapResponse);
592
593         if (result.getResult() != null) {
594             this.completeOperation(operationAttempt, result.getMessage(), result.getResult());
595             if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
596                 return null;
597             }
598             return result.getResult();
599         }
600         return null;
601     }
602
603     /**
604      * This method handles operation responses from SO.
605      *
606      * @param msoResponse the SO response
607      * @return The result of the response handling
608      */
609     private PolicyResult onResponse(SoResponseWrapper msoResponse) {
610         switch (msoResponse.getSoResponse().getHttpResponseCode()) {
611             case 200:
612             case 202:
613                 //
614                 // Consider it as success
615                 //
616                 this.completeOperation(this.attempts, msoResponse.getSoResponse().getHttpResponseCode() + SUCCESS_MSG,
617                         PolicyResult.SUCCESS);
618                 return getTimeoutResult(PolicyResult.SUCCESS);
619             default:
620                 //
621                 // Consider it as failure
622                 //
623                 this.completeOperation(this.attempts, msoResponse.getSoResponse().getHttpResponseCode() + FAILED_MSG,
624                         PolicyResult.FAILURE);
625                 return getTimeoutResult(PolicyResult.FAILURE);
626         }
627     }
628
629     /**
630      * This method handles operation responses from VFC.
631      *
632      * @param vfcResponse the VFC response
633      * @return The result of the response handling
634      */
635     private PolicyResult onResponse(VfcResponse vfcResponse) {
636         if ("finished".equalsIgnoreCase(vfcResponse.getResponseDescriptor().getStatus())) {
637             //
638             // Consider it as success
639             //
640             this.completeOperation(this.attempts, SUCCESS_MSG, PolicyResult.SUCCESS);
641             return getTimeoutResult(PolicyResult.SUCCESS);
642         } else {
643             //
644             // Consider it as failure
645             //
646             this.completeOperation(this.attempts, FAILED_MSG, PolicyResult.FAILURE);
647             if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
648                 return null;
649             }
650             // increment operation attempts for retries
651             this.attempts += 1;
652             return PolicyResult.FAILURE;
653         }
654     }
655
656     /**
657      * This method handles operation responses from SDNC.
658      *
659      * @param sdncResponse the VFC response
660      * @return The result of the response handling
661      */
662     private PolicyResult onResponse(SdncResponse sdncResponse) {
663         if ("200".equals(sdncResponse.getResponseOutput().getResponseCode())) {
664             //
665             // Consider it as success
666             //
667             this.completeOperation(this.attempts, SUCCESS_MSG, PolicyResult.SUCCESS);
668             return getTimeoutResult(PolicyResult.SUCCESS);
669         } else {
670             //
671             // Consider it as failure
672             //
673             this.completeOperation(this.attempts, FAILED_MSG, PolicyResult.FAILURE);
674             if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
675                 return null;
676             }
677             // increment operation attempts for retries
678             this.attempts += 1;
679             return PolicyResult.FAILURE;
680         }
681     }
682
683     private PolicyResult getTimeoutResult(PolicyResult result) {
684         return (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult) ? null : result);
685     }
686
687
688     private Integer getSubRequestId(Response appcResponse) {
689         try {
690             return Integer.valueOf(appcResponse.getCommonHeader().getSubRequestId());
691         } catch (NumberFormatException e) {
692             //
693             // We cannot tell what happened if this doesn't exist
694             //
695             return null;
696         }
697     }
698
699     /**
700      * Get the operation timeout.
701      *
702      * @return the timeout
703      */
704     public Integer getOperationTimeout() {
705         //
706         // Sanity check
707         //
708         if (this.policy == null) {
709             logger.debug("getOperationTimeout returning 0");
710             return 0;
711         }
712         logger.debug("getOperationTimeout returning {}", this.policy.getTimeout());
713         return this.policy.getTimeout();
714     }
715
716     /**
717      * Get the operation timeout as a String.
718      *
719      * @param defaultTimeout the default timeout
720      * @return the timeout as a String
721      */
722     public String getOperationTimeoutString(int defaultTimeout) {
723         Integer to = this.getOperationTimeout();
724         if (to == null || to == 0) {
725             return Integer.toString(defaultTimeout) + "s";
726         }
727         return to.toString() + "s";
728     }
729
730     public PolicyResult getOperationResult() {
731         return this.policyResult;
732     }
733
734     /**
735      * Get the operation as a message.
736      *
737      * @return the operation as a message
738      */
739     public String getOperationMessage() {
740         if (this.currentOperation != null && this.currentOperation.clOperation != null) {
741             return this.currentOperation.clOperation.toMessage();
742         }
743
744         if (!this.operationHistory.isEmpty()) {
745             return this.operationHistory.getLast().clOperation.toMessage();
746         }
747         return null;
748     }
749
750     /**
751      * Get the operation as a message including the guard result.
752      *
753      * @param guardResult the guard result
754      * @return the operation as a message including the guard result
755      */
756     public String getOperationMessage(String guardResult) {
757         if (this.currentOperation != null && this.currentOperation.clOperation != null) {
758             return this.currentOperation.clOperation.toMessage() + ", Guard result: " + guardResult;
759         }
760
761         if (!this.operationHistory.isEmpty()) {
762             return this.operationHistory.getLast().clOperation.toMessage() + ", Guard result: " + guardResult;
763         }
764         return null;
765     }
766
767     /**
768      * Get the operation history.
769      *
770      * @return the operation history
771      */
772     public String getOperationHistory() {
773         if (this.currentOperation != null && this.currentOperation.clOperation != null) {
774             return this.currentOperation.clOperation.toHistory();
775         }
776
777         if (!this.operationHistory.isEmpty()) {
778             return this.operationHistory.getLast().clOperation.toHistory();
779         }
780         return null;
781     }
782
783     /**
784      * Get the history.
785      *
786      * @return the list of control loop operations
787      */
788     public List<ControlLoopOperation> getHistory() {
789         LinkedList<ControlLoopOperation> history = new LinkedList<>();
790         for (Operation op : this.operationHistory) {
791             history.add(new ControlLoopOperation(op.clOperation));
792
793         }
794         return history;
795     }
796
797     /**
798      * Set the operation has timed out.
799      */
800     public void setOperationHasTimedOut() {
801         //
802         //
803         //
804         this.completeOperation(this.attempts, "Operation timed out", PolicyResult.FAILURE_TIMEOUT);
805     }
806
807     /**
808      * Set the operation has been denied by guard.
809      */
810     public void setOperationHasGuardDeny() {
811         //
812         //
813         //
814         this.completeOperation(this.attempts, "Operation denied by Guard", PolicyResult.FAILURE_GUARD);
815     }
816
817     public void setOperationHasException(String message) {
818         this.completeOperation(this.attempts, message, PolicyResult.FAILURE_EXCEPTION);
819     }
820
821     /**
822      * Is the operation complete.
823      *
824      * @return <code>true</code> if the operation is complete, <code>false</code> otherwise
825      */
826     public boolean isOperationComplete() {
827         //
828         // Is there currently a result?
829         //
830         if (this.policyResult == null) {
831             //
832             // either we are in process or we
833             // haven't started
834             //
835             return false;
836         }
837         //
838         // We have some result, check if the operation failed
839         //
840         if (this.policyResult.equals(PolicyResult.FAILURE)) {
841             //
842             // Check if there were no retries specified
843             //
844             if (policy.getRetry() == null || policy.getRetry() == 0) {
845                 //
846                 // The result is the failure
847                 //
848                 return true;
849             }
850             //
851             // Check retries
852             //
853             if (this.isRetriesMaxedOut()) {
854                 //
855                 // No more attempts allowed, reset
856                 // that our actual result is failure due to retries
857                 //
858                 this.policyResult = PolicyResult.FAILURE_RETRIES;
859                 return true;
860             } else {
861                 //
862                 // There are more attempts available to try the
863                 // policy recipe.
864                 //
865                 return false;
866             }
867         }
868         //
869         // Other results mean we are done
870         //
871         return true;
872     }
873
874     public boolean isOperationRunning() {
875         return (this.currentOperation != null);
876     }
877
878     /**
879      * This method verifies that the operation manager may run an operation.
880      *
881      * @return True if the operation can run, false otherwise
882      * @throws ControlLoopException if the operation cannot run
883      */
884     private void verifyOperatonCanRun() throws ControlLoopException {
885         //
886         // They shouldn't call us if we currently running something
887         //
888         if (this.currentOperation != null) {
889             //
890             // what do we do if we are already running an operation?
891             //
892             throw new ControlLoopException("current operation is not null (an operation is already running)");
893         }
894         //
895         // Check if we have maxed out on retries
896         //
897         if (this.policy.getRetry() == null || this.policy.getRetry() < 1) {
898             //
899             // No retries are allowed, so check have we even made
900             // one attempt to execute the operation?
901             //
902             if (this.attempts >= 1) {
903                 //
904                 // We have, let's ensure our PolicyResult is set
905                 //
906                 if (this.policyResult == null) {
907                     this.policyResult = PolicyResult.FAILURE_RETRIES;
908                 }
909                 //
910                 //
911                 //
912                 throw new ControlLoopException("current operation failed and retries are not allowed");
913             }
914         } else {
915             //
916             // Have we maxed out on retries?
917             //
918             if (this.attempts > this.policy.getRetry()) {
919                 if (this.policyResult == null) {
920                     this.policyResult = PolicyResult.FAILURE_RETRIES;
921                 }
922                 throw new ControlLoopException("current oepration has failed after " + this.attempts + " retries");
923             }
924         }
925     }
926
927     private boolean isRetriesMaxedOut() {
928         if (policy.getRetry() == null || policy.getRetry() == 0) {
929             //
930             // There were NO retries specified, so declare
931             // this as completed.
932             //
933             return (this.attempts > 0);
934         }
935         return (this.attempts > policy.getRetry());
936     }
937
938     private void storeOperationInDataBase() {
939         // Only store in DB if enabled
940         boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
941         if (!guardEnabled) {
942             return;
943         }
944
945
946         // DB Properties
947         Properties props = new Properties();
948         if (PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL) != null
949                 && PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER) != null
950                 && PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS) != null) {
951             props.put(Util.ECLIPSE_LINK_KEY_URL, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL));
952             props.put(Util.ECLIPSE_LINK_KEY_USER, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER));
953             props.put(Util.ECLIPSE_LINK_KEY_PASS, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS));
954             props.put(PersistenceUnitProperties.CLASSLOADER, ControlLoopOperationManager.class.getClassLoader());
955         }
956
957
958         String opsHistPu = System.getProperty("OperationsHistoryPU");
959         if (!"OperationsHistoryPUTest".equals(opsHistPu)) {
960             opsHistPu = "OperationsHistoryPU";
961         } else {
962             props.clear();
963         }
964         EntityManager em;
965         try {
966             em = Persistence.createEntityManagerFactory(opsHistPu, props).createEntityManager();
967         } catch (Exception e) {
968             logger.error("storeOperationInDataBase threw: ", e);
969             return;
970         }
971
972         Dbao newEntry = new Dbao();
973
974         newEntry.setClosedLoopName(this.onset.getClosedLoopControlName());
975         newEntry.setRequestId(this.onset.getRequestId().toString());
976         newEntry.setActor(this.currentOperation.clOperation.getActor());
977         newEntry.setOperation(this.currentOperation.clOperation.getOperation());
978         newEntry.setTarget(this.targetEntity);
979         newEntry.setStarttime(Timestamp.from(this.currentOperation.clOperation.getStart()));
980         newEntry.setSubrequestId(this.currentOperation.clOperation.getSubRequestId());
981         newEntry.setEndtime(new Timestamp(this.currentOperation.clOperation.getEnd().toEpochMilli()));
982         newEntry.setMessage(this.currentOperation.clOperation.getMessage());
983         newEntry.setOutcome(this.currentOperation.clOperation.getOutcome());
984
985         em.getTransaction().begin();
986         em.persist(newEntry);
987         em.getTransaction().commit();
988
989         em.close();
990     }
991
992     private void completeOperation(Integer attempt, String message, PolicyResult result) {
993         if (attempt == null) {
994             logger.debug("attempt cannot be null (i.e. subRequestID)");
995             return;
996         }
997         if (this.currentOperation != null) {
998             if (this.currentOperation.attempt == attempt.intValue()) {
999                 this.currentOperation.clOperation.setEnd(Instant.now());
1000                 this.currentOperation.clOperation.setMessage(message);
1001                 this.currentOperation.clOperation.setOutcome(result.toString());
1002                 this.currentOperation.policyResult = result;
1003                 //
1004                 // Save it in history
1005                 //
1006                 this.operationHistory.add(this.currentOperation);
1007                 this.storeOperationInDataBase();
1008                 //
1009                 // Set our last result
1010                 //
1011                 this.policyResult = result;
1012                 //
1013                 // Clear the current operation field
1014                 //
1015                 this.currentOperation = null;
1016                 return;
1017             }
1018             logger.debug("not current");
1019         }
1020         for (Operation op : this.operationHistory) {
1021             if (op.attempt == attempt.intValue()) {
1022                 op.clOperation.setEnd(Instant.now());
1023                 op.clOperation.setMessage(message);
1024                 op.clOperation.setOutcome(result.toString());
1025                 op.policyResult = result;
1026                 return;
1027             }
1028         }
1029         logger.debug("Could not find associated operation");
1030     }
1031
1032
1033     /**
1034      * Commit the abatement to the history database.
1035      *
1036      * @param message the abatement message
1037      * @param outcome the abatement outcome
1038      */
1039     public void commitAbatement(String message, String outcome) {
1040         logger.info("commitAbatement: {}. {}", message, outcome);
1041
1042         if (this.currentOperation == null) {
1043             try {
1044                 this.currentOperation = this.operationHistory.getLast();
1045             } catch (NoSuchElementException e) {
1046                 logger.error("{}: commitAbatement threw an exception ", this, e);
1047                 return;
1048             }
1049         }
1050         this.currentOperation.clOperation.setEnd(Instant.now());
1051         this.currentOperation.clOperation.setMessage(message);
1052         this.currentOperation.clOperation.setOutcome(outcome);
1053         //
1054         // Store commit in DB
1055         //
1056         this.storeOperationInDataBase();
1057         //
1058         // Clear the current operation field
1059         //
1060         this.currentOperation = null;
1061     }
1062
1063     /**
1064      * Construct a ControlLoopResponse object from actor response and input event.
1065      *
1066      * @param response the response from actor
1067      * @param event the input event
1068      *
1069      * @return a ControlLoopResponse
1070      */
1071     public ControlLoopResponse getControlLoopResponse(Object response, VirtualControlLoopEvent event) {
1072         if (response instanceof PciResponseWrapper) {
1073             //
1074             // Cast SDNR response and handle it
1075             //
1076             return SdnrActorServiceProvider.getControlLoopResponse((PciResponseWrapper) response, event);
1077         } else {
1078             return null;
1079         }
1080     }
1081
1082 }