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