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