Send final failure on null vfc request
[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 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.Properties;
29
30 import javax.persistence.EntityManager;
31 import javax.persistence.Persistence;
32
33 import org.onap.policy.aai.util.AAIException;
34 import org.onap.policy.appc.Response;
35 import org.onap.policy.appc.ResponseCode;
36 import org.onap.policy.appclcm.LCMResponseWrapper;
37 import org.onap.policy.controlloop.ControlLoopEvent;
38 import org.onap.policy.controlloop.ControlLoopException;
39 import org.onap.policy.controlloop.ControlLoopOperation;
40 import org.onap.policy.controlloop.VirtualControlLoopEvent;
41 import org.onap.policy.controlloop.actor.appc.APPCActorServiceProvider;
42 import org.onap.policy.controlloop.actor.appclcm.AppcLcmActorServiceProvider;
43 import org.onap.policy.controlloop.actor.so.SOActorServiceProvider;
44 import org.onap.policy.controlloop.actor.vfc.VFCActorServiceProvider;
45 import org.onap.policy.controlloop.policy.Policy;
46 import org.onap.policy.controlloop.policy.PolicyResult;
47 import org.onap.policy.drools.system.PolicyEngine;
48 import org.onap.policy.guard.Util;
49 import org.onap.policy.so.SOResponse;
50 import org.onap.policy.vfc.VFCResponse;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class ControlLoopOperationManager implements Serializable {
55
56         /**
57          *
58          */
59         private static final long serialVersionUID = -3773199283624595410L;
60         private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManager.class);
61
62         @Override
63         public String toString() {
64                 return "ControlLoopOperationManager [onset=" + (onset != null ? onset.requestID : "null") + ", policy="
65                                 + (policy != null ? policy.getId() : "null") + ", attempts=" + attempts
66                                 + ", policyResult=" + policyResult
67                                 + ", currentOperation=" + currentOperation + ", operationHistory=" + operationHistory
68                                 + "]";
69         }
70
71         //
72         // These properties are not changeable, but accessible
73         // for Drools Rule statements.
74         //
75         //public final ATTControlLoopEvent onset;
76         public final ControlLoopEvent onset;
77         public final transient Policy policy;
78
79         //
80         // Properties used to track the Operation
81         //
82         private int attempts = 0;
83         private transient Operation currentOperation = null;
84         private LinkedList<Operation> operationHistory = new LinkedList<Operation>();
85         private PolicyResult policyResult = null;
86         private ControlLoopEventManager eventManager = null;
87         private String targetEntity;
88
89         public ControlLoopEventManager getEventManager() {
90                 return eventManager;
91         }
92
93         public void setEventManager(ControlLoopEventManager eventManager) {
94                 this.eventManager = eventManager;
95         }
96
97         public String getTargetEntity() {
98             return this.targetEntity;
99         }
100
101         //
102         // Internal class used for tracking
103         //
104         private class Operation {
105                 public ControlLoopOperation operation = new ControlLoopOperation();
106                 public PolicyResult policyResult = null;
107                 public int attempt = 0;
108
109                 @Override
110                 public String toString() {
111                         return "Operation [attempt=" + attempt + ", policyResult=" + policyResult + ", operation=" + operation
112                                         + "]";
113                 }
114         }
115
116         private String guardApprovalStatus = "NONE";//"NONE", "PERMIT", "DENY"
117         private transient Object operationRequest;
118
119         public Object getOperationRequest() {
120                 return operationRequest;
121         }
122
123         public String getGuardApprovalStatus() {
124                 return guardApprovalStatus;
125         }
126         public void setGuardApprovalStatus(String guardApprovalStatus) {
127                 this.guardApprovalStatus = guardApprovalStatus;
128         }
129
130         public String getTarget(Policy policy) throws ControlLoopException, AAIException {
131         if (policy.getTarget() != null) {
132             if (policy.getTarget().getType() != null) {
133                 switch(policy.getTarget().getType()) {
134                 case PNF:
135                     break;
136                 case VM:
137                 case VNF:
138                     VirtualControlLoopEvent virtualOnset = (VirtualControlLoopEvent) this.onset;
139                     if (this.onset.target.equalsIgnoreCase("vserver.vserver-name")) {
140                         return virtualOnset.AAI.get("vserver.vserver-name");
141                     }
142                     else if (this.onset.target.equalsIgnoreCase("generic-vnf.vnf-id")) {
143                         return virtualOnset.AAI.get("generic-vnf.vnf-id");
144                     }
145                     else if (this.onset.target.equalsIgnoreCase("generic-vnf.vnf-name")) {
146                         /*
147                          * If the vnf-name was retrieved from the onset then the vnf-id
148                          * must be obtained from the event manager's A&AI GET query
149                          */
150                         String vnfId = this.eventManager.getVnfResponse().vnfID;
151                         if (vnfId == null) {
152                             throw new AAIException("No vnf-id found");
153                         }
154                         return vnfId;
155                     }
156                     break;
157                 default:
158                     throw new ControlLoopException("The target type is not supported");
159                 }
160             }
161             else {
162                 throw new ControlLoopException("The target type is null");
163             }
164         }
165         else {
166             throw new ControlLoopException("The target is null");
167         }
168         throw new ControlLoopException("Target does not match target type");
169     }
170         
171         public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy, ControlLoopEventManager em) throws ControlLoopException, AAIException {
172                 this.onset = onset;
173                 this.policy = policy;
174                 this.guardApprovalStatus = "NONE";
175                 this.eventManager = em;
176                 this.targetEntity = getTarget(policy);
177                 
178                 //
179                 // Let's make a sanity check
180                 //
181                 switch (policy.getActor()) {
182                 case "APPC":
183                     if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
184                         /*
185                  * The target vnf-id may not be the same as the source vnf-id
186                  * specified in the yaml, the target vnf-id is retrieved by
187                  * a named query to A&AI.
188                  */
189                         String targetVnf = AppcLcmActorServiceProvider.vnfNamedQuery(
190                                     policy.getTarget().getResourceID(), this.targetEntity);
191                         this.targetEntity = targetVnf;
192                     }
193                         break;
194                 case "SO":
195                     break;
196                 case "VFC":
197                         break;
198                 default:
199                         throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor.");
200                 }
201         }
202
203         public Object startOperation(/*VirtualControlLoopEvent*/ControlLoopEvent onset) throws AAIException {
204                 //
205                 // They shouldn't call us if we currently running something
206                 //
207                 if (this.currentOperation != null) {
208                         //
209                         // what do we do if we are already running an operation?
210                         //
211                         return null;
212                 }
213                 //
214                 // Check if we have maxed out on retries
215                 //
216                 if (this.policy.getRetry() == null || this.policy.getRetry() < 1) {
217                         //
218                         // No retries are allowed, so check have we even made
219                         // one attempt to execute the operation?
220                         //
221                         if (this.attempts >= 1) {
222                                 //
223                                 // We have, let's ensure our PolicyResult is set
224                                 //
225                                 if (this.policyResult == null) {
226                                         this.policyResult = PolicyResult.FAILURE_RETRIES;
227                                 }
228                                 //
229                                 //
230                                 //
231                                 return null;
232                         }
233                 } else {
234                         //
235                         // Have we maxed out on retries?
236                         //
237                         if (this.attempts > this.policy.getRetry()) {
238                                 if (this.policyResult == null) {
239                                         this.policyResult = PolicyResult.FAILURE_RETRIES;
240                                 }
241                                 return null;
242                         }
243                 }
244                 //
245                 // Setup
246                 //
247                 this.policyResult = null;
248                 Operation operation = new Operation();
249                 operation.attempt = ++this.attempts;
250                 operation.operation.actor = this.policy.getActor();
251                 operation.operation.operation = this.policy.getRecipe();
252                 operation.operation.target = this.policy.getTarget().toString();
253                 operation.operation.subRequestId = Integer.toString(operation.attempt);
254                 //
255                 // Now determine which actor we need to construct a request for
256                 //
257                 switch (policy.getActor()) {
258                 case "APPC":
259                     /*
260                      * If the recipe is ModifyConfig, a legacy APPC
261                      * request is constructed. Otherwise an LCMRequest
262                      * is constructed.
263                      */
264                         this.currentOperation = operation;
265                     if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
266
267                     this.operationRequest = APPCActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, 
268                                             operation.operation, this.policy, this.targetEntity);
269                     }
270                     else {
271                         this.operationRequest = AppcLcmActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, 
272                                                 operation.operation, this.policy, this.targetEntity);
273                     }
274                         //
275                         // Save the operation
276                         //
277                         
278                         return operationRequest;
279                 case "SO":
280                         SOActorServiceProvider SOAsp = new SOActorServiceProvider();
281                         this.operationRequest = SOAsp.constructRequest((VirtualControlLoopEvent)onset, operation.operation, this.policy);
282
283                         // Save the operation
284                         this.currentOperation = operation;
285
286                         if (this.operationRequest == null) {
287                                 this.policyResult = PolicyResult.FAILURE;
288                         }
289
290                         return operationRequest;
291                 case "VFC":
292             this.operationRequest = VFCActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, operation.operation, this.policy, this.eventManager.getVnfResponse());
293             this.currentOperation = operation;
294             if (this.operationRequest == null) {
295                 this.policyResult = PolicyResult.FAILURE;
296             }
297             return operationRequest;
298
299                 }
300                 return null;
301         }
302
303         public PolicyResult     onResponse(Object response) {
304                 //
305                 // Which response is it?
306                 //
307                 if (response instanceof Response) {
308                         //
309                         // Cast it
310                         //
311                         Response appcResponse = (Response) response;
312                         //
313                         // Determine which subrequestID (ie. attempt)
314                         //
315                         Integer operationAttempt = null;
316                         try {
317                                 operationAttempt = Integer.parseInt(appcResponse.CommonHeader.SubRequestID);
318                         } catch (NumberFormatException e) {
319                                 //
320                                 // We cannot tell what happened if this doesn't exist
321                                 //
322                                 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).", PolicyResult.FAILURE_EXCEPTION);
323                                 return PolicyResult.FAILURE_EXCEPTION;
324                         }
325                         //
326                         // Sanity check the response message
327                         //
328                         if (appcResponse.Status == null) {
329                                 //
330                                 // We cannot tell what happened if this doesn't exist
331                                 //
332                                 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C response status field (it was null).", PolicyResult.FAILURE_EXCEPTION);
333                                 return PolicyResult.FAILURE_EXCEPTION;
334                         }
335                         //
336                         // Get the Response Code
337                         //
338                         ResponseCode code = ResponseCode.toResponseCode(appcResponse.Status.Code);
339                         if (code == null) {
340                                 //
341                                 // We are unaware of this code
342                                 //
343                                 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C response status code field.", PolicyResult.FAILURE_EXCEPTION);
344                                 return PolicyResult.FAILURE_EXCEPTION;
345                         }
346                         //
347                         // Ok, let's figure out what APP-C's response is
348                         //
349                         switch (code) {
350                         case ACCEPT:
351                                 //
352                                 // This is good, they got our original message and
353                                 // acknowledged it.
354                                 //
355                                 // Is there any need to track this?
356                                 //
357                                 return null;
358                         case ERROR:
359                         case REJECT:
360                                 //
361                                 // We'll consider these two codes as exceptions
362                                 //
363                                 this.completeOperation(operationAttempt, appcResponse.getStatus().Description, PolicyResult.FAILURE_EXCEPTION);
364                                 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
365                                         return null;
366                                 }
367                                 return PolicyResult.FAILURE_EXCEPTION;
368                         case SUCCESS:
369                                 //
370                                 //
371                                 //
372                                 this.completeOperation(operationAttempt, appcResponse.getStatus().Description, PolicyResult.SUCCESS);
373                                 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
374                                         return null;
375                                 }
376                                 return PolicyResult.SUCCESS;
377                         case FAILURE:
378                                 //
379                                 //
380                                 //
381                                 this.completeOperation(operationAttempt, appcResponse.getStatus().Description, PolicyResult.FAILURE);
382                                 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
383                                         return null;
384                                 }
385                                 return PolicyResult.FAILURE;
386                         }
387                 }
388                 else if (response instanceof LCMResponseWrapper) {
389
390                     LCMResponseWrapper dmaapResponse = (LCMResponseWrapper) response;
391
392                     /*
393                      * Parse out the operation attempt using the subrequestid
394                      */
395                     Integer operationAttempt = AppcLcmActorServiceProvider.parseOperationAttempt(dmaapResponse.getBody().getCommonHeader().getSubRequestId());
396                     if (operationAttempt == null) {
397                         this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).", PolicyResult.FAILURE_EXCEPTION);
398                     }
399
400                     /*
401                      * Process the APPCLCM response to see what PolicyResult
402                      * should be returned
403                      */
404                     AbstractMap.SimpleEntry<PolicyResult, String> result = AppcLcmActorServiceProvider.processResponse(dmaapResponse);
405
406                     if (result.getKey() != null) {
407                     this.completeOperation(operationAttempt, result.getValue(), result.getKey());
408                     if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
409                     return null;
410                 }
411                     return result.getKey();
412                     }
413                     return null;
414                 } else if (response instanceof SOResponse) {
415                         SOResponse msoResponse = (SOResponse) response;
416
417                         switch (msoResponse.httpResponseCode) {
418                         case 200:
419                         case 202:
420                                 //
421                                 // Consider it as success
422                                 //
423                                 this.completeOperation(this.attempts, msoResponse.httpResponseCode + " Success", PolicyResult.SUCCESS);
424                                 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
425                                         return null;
426                                 }
427                                 return PolicyResult.SUCCESS;
428                         default:
429                                 //
430                                 // Consider it as failure
431                                 //
432                                 this.completeOperation(this.attempts, msoResponse.httpResponseCode + " Failed", PolicyResult.FAILURE);
433                                 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
434                                         return null;
435                                 }
436                                 return PolicyResult.FAILURE;
437                         }
438
439                 } else if (response instanceof VFCResponse) {
440                         VFCResponse vfcResponse = (VFCResponse) response;
441
442                         if (vfcResponse.responseDescriptor.getStatus().equalsIgnoreCase("finished")) {
443                                 //
444                                 // Consider it as success
445                                 //
446                                 this.completeOperation(this.attempts, " Success", PolicyResult.SUCCESS);
447                                 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
448                                         return null;
449                                 }
450                                 return PolicyResult.SUCCESS;
451                         } else {
452                                 //
453                                 // Consider it as failure
454                                 //
455                                 this.completeOperation(this.attempts, " Failed", PolicyResult.FAILURE);
456                                 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
457                                         return null;
458                                 }
459                                 // increment operation attempts for retries
460                                 this.attempts += 1;
461                                 return PolicyResult.FAILURE;
462                         }
463                 }
464                 return null;
465         }
466
467         public Integer  getOperationTimeout() {
468                 //
469                 // Sanity check
470                 //
471                 if (this.policy == null) {
472                         logger.debug("getOperationTimeout returning 0");
473                         return 0;
474                 }
475                 logger.debug("getOperationTimeout returning {}", this.policy.getTimeout());
476                 return this.policy.getTimeout();
477         }
478
479         public String   getOperationTimeoutString(int defaultTimeout) {
480                 Integer to = this.getOperationTimeout();
481                 if (to == null || to == 0) {
482                         return Integer.toString(defaultTimeout) + "s";
483                 }
484                 return to.toString() + "s";
485         }
486
487         public PolicyResult     getOperationResult() {
488                 return this.policyResult;
489         }
490
491         public String   getOperationMessage() {
492                 if (this.currentOperation != null && this.currentOperation.operation != null) {
493                         return this.currentOperation.operation.toMessage();
494                 }
495
496                 if (!this.operationHistory.isEmpty()) {
497                         return this.operationHistory.getLast().operation.toMessage();
498                 }
499                 return null;
500         }
501
502         public String   getOperationMessage(String guardResult) {
503                 if (this.currentOperation != null && this.currentOperation.operation != null) {
504                         return this.currentOperation.operation.toMessage()+ ", Guard result: " + guardResult;
505                 }
506           
507                 if (!this.operationHistory.isEmpty()) {
508                         return this.operationHistory.getLast().operation.toMessage() + ", Guard result: " + guardResult;
509                 }
510                 return null;
511         }
512
513         public String   getOperationHistory() {
514                 if (this.currentOperation != null && this.currentOperation.operation != null) {
515                         return this.currentOperation.operation.toHistory();
516                 }
517           
518                 if (!this.operationHistory.isEmpty()) {
519                         return this.operationHistory.getLast().operation.toHistory();
520                 }
521                 return null;
522         }
523
524         public LinkedList<ControlLoopOperation> getHistory() {
525                 LinkedList<ControlLoopOperation> history = new LinkedList<ControlLoopOperation>();
526                 for (Operation op : this.operationHistory) {
527                         history.add(new ControlLoopOperation(op.operation));
528
529                 }
530                 return history;
531         }
532
533         public void             setOperationHasTimedOut() {
534                 //
535                 //
536                 //
537                 this.completeOperation(this.attempts, "Operation timed out", PolicyResult.FAILURE_TIMEOUT);
538         }
539
540         public void             setOperationHasGuardDeny() {
541                 //
542                 //
543                 //
544                 this.completeOperation(this.attempts, "Operation denied by Guard", PolicyResult.FAILURE_GUARD);
545         }
546         
547         public void setOperationHasException(String message) {
548                 this.completeOperation(this.attempts, message, PolicyResult.FAILURE_EXCEPTION);
549         }
550
551         public boolean  isOperationComplete() {
552                 //
553                 // Is there currently a result?
554                 //
555                 if (this.policyResult == null) {
556                         //
557                         // either we are in process or we
558                         // haven't started
559                         //
560                         return false;
561                 }
562                 //
563                 // We have some result, check if the operation failed
564                 //
565                 if (this.policyResult.equals(PolicyResult.FAILURE)) {
566                         //
567                         // Check if there were no retries specified
568                         //
569                         if (policy.getRetry() == null || policy.getRetry() == 0) {
570                                 //
571                                 // The result is the failure
572                                 //
573                                 return true;
574                         }
575                         //
576                         // Check retries
577                         //
578                         if (this.isRetriesMaxedOut()) {
579                                 //
580                                 // No more attempts allowed, reset
581                                 // that our actual result is failure due to retries
582                                 //
583                                 this.policyResult = PolicyResult.FAILURE_RETRIES;
584                                 return true;
585                         } else {
586                                 //
587                                 // There are more attempts available to try the
588                                 // policy recipe.
589                                 //
590                                 return false;
591                         }
592                 }
593                 //
594                 // Other results mean we are done
595                 //
596                 return true;
597         }
598
599         public boolean  isOperationRunning() {
600                 return (this.currentOperation != null);
601         }
602
603         private boolean isRetriesMaxedOut() {
604                 if (policy.getRetry() == null || policy.getRetry() == 0) {
605                         //
606                         // There were NO retries specified, so declare
607                         // this as completed.
608                         //
609                         return (this.attempts > 0);
610                 }
611                 return (this.attempts > policy.getRetry());
612         }
613
614         private void    storeOperationInDataBase(){
615                 // Only store in DB if enabled
616                 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
617                 if( !guardEnabled ){
618                         return;
619                 }
620
621
622                 // DB Properties
623                 Properties props = new Properties();
624                 if(PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL) != null &&
625                                 PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER) != null &&
626                                 PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS) != null){
627                         props.put(Util.ECLIPSE_LINK_KEY_URL, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL));
628                         props.put(Util.ECLIPSE_LINK_KEY_USER, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER));
629                         props.put(Util.ECLIPSE_LINK_KEY_PASS, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS));
630                 }
631                 
632                 
633                 String OpsHistPU = System.getProperty("OperationsHistoryPU");
634                 if(OpsHistPU == null || !OpsHistPU.equals("TestOperationsHistoryPU")){
635                         OpsHistPU = "OperationsHistoryPU";
636                 }
637                 else{
638                         props.clear();
639                 }
640                 EntityManager em;
641                 try{
642                         em = Persistence.createEntityManagerFactory(OpsHistPU, props).createEntityManager();
643                 }catch(Exception e){
644                         logger.error("storeOperationInDataBase threw: ", e);
645                         return;
646                 }
647
648                 OperationsHistoryDbEntry newEntry = new OperationsHistoryDbEntry();
649
650                 newEntry.closedLoopName = this.onset.closedLoopControlName;
651                 newEntry.requestId = this.onset.requestID.toString();
652                 newEntry.actor = this.currentOperation.operation.actor;
653                 newEntry.operation = this.currentOperation.operation.operation;
654                 newEntry.target = this.targetEntity;
655                 newEntry.starttime = Timestamp.from(this.currentOperation.operation.start);
656                 newEntry.subrequestId = this.currentOperation.operation.subRequestId;
657                 newEntry.endtime = new Timestamp(this.currentOperation.operation.end.toEpochMilli());
658                 newEntry.message = this.currentOperation.operation.message;
659                 newEntry.outcome = this.currentOperation.operation.outcome;
660
661                 em.getTransaction().begin();
662                 em.persist(newEntry);
663                 em.getTransaction().commit();
664
665                 em.close();
666
667         }
668
669
670
671         private void    completeOperation(Integer attempt, String message, PolicyResult result) {
672                 if (attempt == null) {
673                         logger.debug("attempt cannot be null (i.e. subRequestID)");
674                         return;
675                 }
676                 if (this.currentOperation != null) {
677                         if (this.currentOperation.attempt == attempt.intValue()) {
678                                 this.currentOperation.operation.end = Instant.now();
679                                 this.currentOperation.operation.message = message;
680                                 this.currentOperation.operation.outcome = result.toString();
681                                 this.currentOperation.policyResult = result;
682                                 //
683                                 // Save it in history
684                                 //
685                                 this.operationHistory.add(this.currentOperation);
686                                 this.storeOperationInDataBase();
687                                 //
688                                 // Set our last result
689                                 //
690                                 this.policyResult = result;
691                                 //
692                                 // Clear the current operation field
693                                 //
694                                 this.currentOperation = null;
695                                 return;
696                         }
697                         logger.debug("not current");
698                 }
699                 for (Operation op : this.operationHistory) {
700                         if (op.attempt == attempt.intValue()) {
701                                 op.operation.end = Instant.now();
702                                 op.operation.message = message;
703                                 op.operation.outcome = result.toString();
704                                 op.policyResult = result;
705                                 return;
706                         }
707                 }
708                 logger.debug("Could not find associated operation");
709
710         }
711
712 }