2 * ============LICENSE_START=======================================================
3 * controlloop operation manager
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
7 * Modifications Copyright (C) 2019 Tech Mahindra
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.controlloop.eventmanager;
25 import java.io.Serializable;
26 import java.sql.Timestamp;
27 import java.time.Instant;
28 import java.util.AbstractMap;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.NoSuchElementException;
32 import java.util.Properties;
33 import javax.persistence.EntityManager;
34 import javax.persistence.Persistence;
35 import org.eclipse.persistence.config.PersistenceUnitProperties;
36 import org.onap.aai.domain.yang.GenericVnf;
37 import org.onap.policy.aai.util.AaiException;
38 import org.onap.policy.appc.Response;
39 import org.onap.policy.appc.ResponseCode;
40 import org.onap.policy.appclcm.LcmResponseWrapper;
41 import org.onap.policy.controlloop.ControlLoopEvent;
42 import org.onap.policy.controlloop.ControlLoopException;
43 import org.onap.policy.controlloop.ControlLoopOperation;
44 import org.onap.policy.controlloop.ControlLoopResponse;
45 import org.onap.policy.controlloop.VirtualControlLoopEvent;
46 import org.onap.policy.controlloop.actor.appc.AppcActorServiceProvider;
47 import org.onap.policy.controlloop.actor.appclcm.AppcLcmActorServiceProvider;
48 import org.onap.policy.controlloop.actor.sdnc.SdncActorServiceProvider;
49 import org.onap.policy.controlloop.actor.sdnr.SdnrActorServiceProvider;
50 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
51 import org.onap.policy.controlloop.actor.vfc.VfcActorServiceProvider;
52 import org.onap.policy.controlloop.policy.Policy;
53 import org.onap.policy.controlloop.policy.PolicyResult;
54 import org.onap.policy.database.operationshistory.Dbao;
55 import org.onap.policy.drools.system.PolicyEngine;
56 import org.onap.policy.guard.Util;
57 import org.onap.policy.sdnc.SdncResponse;
58 import org.onap.policy.sdnr.PciResponseWrapper;
59 import org.onap.policy.so.SoResponseWrapper;
60 import org.onap.policy.vfc.VfcResponse;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
64 public class ControlLoopOperationManager implements Serializable {
65 private static final String SUCCESS_MSG = " Success";
66 private static final String FAILED_MSG = " Failed";
67 private static final String AAI_CUSTOM_QUERY = "aai.customQuery";
68 private static final long serialVersionUID = -3773199283624595410L;
69 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManager.class);
71 private static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
72 private static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
73 private static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
76 // These properties are not changeable, but accessible
77 // for Drools Rule statements.
79 public final ControlLoopEvent onset;
80 public final Policy policy;
83 // Properties used to track the Operation
85 private int attempts = 0;
86 private Operation currentOperation = null;
87 private LinkedList<Operation> operationHistory = new LinkedList<>();
88 private PolicyResult policyResult = null;
89 private ControlLoopEventManager eventManager = null;
90 private String targetEntity;
91 private String guardApprovalStatus = "NONE";// "NONE", "PERMIT", "DENY"
92 private transient Object operationRequest;
95 * Construct an instance.
97 * @param onset the onset event
98 * @param policy the policy
99 * @param em the event manager
100 * @throws ControlLoopException if an error occurs
101 * @throws AaiException if an error occurs retrieving information from A&AI
103 public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy, ControlLoopEventManager em)
104 throws ControlLoopException, AaiException {
106 this.policy = policy;
107 this.guardApprovalStatus = "NONE";
108 this.eventManager = em;
109 this.targetEntity = getTarget(policy);
112 // Let's make a sanity check
114 switch (policy.getActor()) {
116 initAppc(onset, policy);
127 throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor.");
132 private void initAppc(ControlLoopEvent onset, Policy policy) throws AaiException {
133 if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
135 * The target vnf-id may not be the same as the source vnf-id specified in the yaml, the target
136 * vnf-id is retrieved by a named query to A&AI.
138 if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
139 GenericVnf genvnf = this.eventManager.getCqResponse((VirtualControlLoopEvent) onset)
140 .getGenericVnfByModelInvariantId(policy.getTarget().getResourceID());
141 if (genvnf == null) {
142 logger.info("Target entity could not be found");
143 throw new AaiException("Target vnf-id could not be found");
145 this.targetEntity = genvnf.getVnfId();
149 AppcLcmActorServiceProvider.vnfNamedQuery(policy.getTarget().getResourceID(),
150 this.targetEntity, PolicyEngine.manager.getEnvironmentProperty("aai.url"),
151 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
152 PolicyEngine.manager.getEnvironmentProperty("aai.password"));
158 public ControlLoopEventManager getEventManager() {
162 public void setEventManager(ControlLoopEventManager eventManager) {
163 this.eventManager = eventManager;
166 public String getTargetEntity() {
167 return this.targetEntity;
171 public String toString() {
172 return "ControlLoopOperationManager [onset=" + (onset != null ? onset.getRequestId() : "null") + ", policy="
173 + (policy != null ? policy.getId() : "null") + ", attempts=" + attempts + ", policyResult="
174 + policyResult + ", currentOperation=" + currentOperation + ", operationHistory=" + operationHistory
179 // Internal class used for tracking
181 private class Operation implements Serializable {
182 private static final long serialVersionUID = 1L;
184 private ControlLoopOperation clOperation = new ControlLoopOperation();
185 private PolicyResult policyResult = null;
186 private int attempt = 0;
189 public String toString() {
190 return "Operation [attempt=" + attempt + ", policyResult=" + policyResult + ", operation=" + clOperation
195 public Object getOperationRequest() {
196 return operationRequest;
199 public String getGuardApprovalStatus() {
200 return guardApprovalStatus;
203 public void setGuardApprovalStatus(String guardApprovalStatus) {
204 this.guardApprovalStatus = guardApprovalStatus;
208 * Get the target for a policy.
210 * @param policy the policy
212 * @throws ControlLoopException if an error occurs
213 * @throws AaiException if an error occurs retrieving information from A&AI
215 public String getTarget(Policy policy) throws ControlLoopException, AaiException {
216 if (policy.getTarget() == null) {
217 throw new ControlLoopException("The target is null");
220 if (policy.getTarget().getType() == null) {
221 throw new ControlLoopException("The target type is null");
224 switch (policy.getTarget().getType()) {
226 throw new ControlLoopException("PNF target is not supported");
229 return getVfModuleTarget();
231 return getVfModuleTarget();
233 throw new ControlLoopException("The target type is not supported");
238 private String getVfModuleTarget() throws AaiException, ControlLoopException {
239 VirtualControlLoopEvent virtualOnsetEvent = (VirtualControlLoopEvent) this.onset;
240 if (this.onset.getTarget().equalsIgnoreCase(VSERVER_VSERVER_NAME)) {
241 return virtualOnsetEvent.getAai().get(VSERVER_VSERVER_NAME);
242 } else if (this.onset.getTarget().equalsIgnoreCase(GENERIC_VNF_VNF_ID)) {
243 return virtualOnsetEvent.getAai().get(GENERIC_VNF_VNF_ID);
244 } else if (this.onset.getTarget().equalsIgnoreCase(GENERIC_VNF_VNF_NAME)) {
246 * If the onset is enriched with the vnf-id, we don't need an A&AI response
248 if (virtualOnsetEvent.getAai().containsKey(GENERIC_VNF_VNF_ID)) {
249 return virtualOnsetEvent.getAai().get(GENERIC_VNF_VNF_ID);
253 * If the vnf-name was retrieved from the onset then the vnf-id must be obtained from the event
254 * manager's A&AI GET query
257 if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
258 vnfId = this.eventManager.getCqResponse((VirtualControlLoopEvent) onset).getDefaultGenericVnf()
261 vnfId = this.eventManager.getVnfResponse().getVnfId();
264 throw new AaiException("No vnf-id found");
268 throw new ControlLoopException("Target does not match target type");
272 * Start an operation.
274 * @param onset the onset event
275 * @return the operation request
276 * @throws ControlLoopException if an error occurs
277 * @throws AaiException if error occurs
279 public Object startOperation(/* VirtualControlLoopEvent */ControlLoopEvent onset)
280 throws ControlLoopException, AaiException {
281 verifyOperatonCanRun();
286 this.policyResult = null;
287 Operation operation = new Operation();
288 operation.attempt = ++this.attempts;
289 operation.clOperation.setActor(this.policy.getActor());
290 operation.clOperation.setOperation(this.policy.getRecipe());
291 operation.clOperation.setTarget(this.policy.getTarget().toString());
292 operation.clOperation.setSubRequestId(Integer.toString(operation.attempt));
294 // Now determine which actor we need to construct a request for
296 switch (policy.getActor()) {
298 return startAppcOperation(onset, operation);
300 return startSoOperation(onset, operation);
302 return startVfcOperation(onset, operation);
304 return startSdnrOperation(onset, operation);
306 return startSdncOperation(onset, operation);
308 throw new ControlLoopException("invalid actor " + policy.getActor() + " on policy");
313 private Object startAppcOperation(ControlLoopEvent onset, Operation operation) {
315 * If the recipe is ModifyConfig, a legacy APPC request is constructed. Otherwise an LCMRequest is
318 this.currentOperation = operation;
319 if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
320 this.operationRequest = AppcActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
321 operation.clOperation, this.policy, this.targetEntity);
323 this.operationRequest = AppcLcmActorServiceProvider.constructRequest(
324 (VirtualControlLoopEvent) onset, operation.clOperation, this.policy, this.targetEntity);
327 // Save the operation
330 return operationRequest;
334 private Object startSoOperation(ControlLoopEvent onset, Operation operation) throws AaiException {
335 SoActorServiceProvider soActorSp = new SoActorServiceProvider();
336 if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
337 this.operationRequest =
338 soActorSp.constructRequestCq((VirtualControlLoopEvent) onset, operation.clOperation,
339 this.policy, eventManager.getCqResponse((VirtualControlLoopEvent) onset));
341 this.operationRequest = soActorSp.constructRequest((VirtualControlLoopEvent) onset,
342 operation.clOperation, this.policy, eventManager.getNqVserverFromAai());
345 // Save the operation
346 this.currentOperation = operation;
348 if (this.operationRequest == null) {
349 this.policyResult = PolicyResult.FAILURE;
352 return operationRequest;
356 private Object startVfcOperation(ControlLoopEvent onset, Operation operation) throws AaiException {
357 if (Boolean.valueOf(PolicyEngine.manager.getEnvironmentProperty(AAI_CUSTOM_QUERY))) {
358 this.operationRequest = VfcActorServiceProvider.constructRequestCq((VirtualControlLoopEvent) onset,
359 operation.clOperation, this.policy,
360 eventManager.getCqResponse((VirtualControlLoopEvent) onset));
362 this.operationRequest = VfcActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
363 operation.clOperation, this.policy, this.eventManager.getVnfResponse(),
364 PolicyEngine.manager.getEnvironmentProperty("vfc.url"),
365 PolicyEngine.manager.getEnvironmentProperty("vfc.username"),
366 PolicyEngine.manager.getEnvironmentProperty("vfc.password"));
368 this.currentOperation = operation;
369 if (this.operationRequest == null) {
370 this.policyResult = PolicyResult.FAILURE;
372 return operationRequest;
376 private Object startSdnrOperation(ControlLoopEvent onset, Operation operation) {
378 * If the recipe is ModifyConfig or ModifyConfigANR, a SDNR request is constructed.
380 this.currentOperation = operation;
381 this.operationRequest = SdnrActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
382 operation.clOperation, this.policy);
384 // Save the operation
386 if (this.operationRequest == null) {
387 this.policyResult = PolicyResult.FAILURE;
390 return operationRequest;
394 private Object startSdncOperation(ControlLoopEvent onset, Operation operation) {
395 SdncActorServiceProvider provider = new SdncActorServiceProvider();
396 this.operationRequest =
397 provider.constructRequest((VirtualControlLoopEvent) onset, operation.clOperation, this.policy);
398 this.currentOperation = operation;
399 if (this.operationRequest == null) {
400 this.policyResult = PolicyResult.FAILURE;
402 return operationRequest;
408 * @param response the response
409 * @return a PolicyResult
411 public PolicyResult onResponse(Object response) {
413 // Which response is it?
415 if (response instanceof Response) {
417 // Cast APPC response and handle it
419 return onResponse((Response) response);
420 } else if (response instanceof LcmResponseWrapper) {
422 // Cast LCM response and handle it
424 return onResponse((LcmResponseWrapper) response);
425 } else if (response instanceof PciResponseWrapper) {
427 // Cast SDNR response and handle it
429 return onResponse((PciResponseWrapper) response);
430 } else if (response instanceof SoResponseWrapper) {
432 // Cast SO response and handle it
434 return onResponse((SoResponseWrapper) response);
435 } else if (response instanceof VfcResponse) {
437 // Cast VFC response and handle it
439 return onResponse((VfcResponse) response);
440 } else if (response instanceof SdncResponse) {
442 // Cast SDNC response and handle it
444 return onResponse((SdncResponse) response);
451 * This method handles operation responses from APPC.
453 * @param appcResponse the APPC response
454 * @return The result of the response handling
456 private PolicyResult onResponse(Response appcResponse) {
458 // Determine which subrequestID (ie. attempt)
460 Integer operationAttempt = getSubRequestId(appcResponse);
461 if (operationAttempt == null) {
462 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).",
463 PolicyResult.FAILURE_EXCEPTION);
464 return PolicyResult.FAILURE_EXCEPTION;
467 // Sanity check the response message
469 if (appcResponse.getStatus() == null) {
471 // We cannot tell what happened if this doesn't exist
473 this.completeOperation(operationAttempt,
474 "Policy was unable to parse APP-C response status field (it was null).",
475 PolicyResult.FAILURE_EXCEPTION);
476 return PolicyResult.FAILURE_EXCEPTION;
479 // Get the Response Code
481 ResponseCode code = ResponseCode.toResponseCode(appcResponse.getStatus().getCode());
484 // We are unaware of this code
486 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C response status code field.",
487 PolicyResult.FAILURE_EXCEPTION);
488 return PolicyResult.FAILURE_EXCEPTION;
491 return onResponse(appcResponse, operationAttempt, code);
495 private PolicyResult onResponse(Response appcResponse, Integer operationAttempt, ResponseCode code) {
497 // Ok, let's figure out what APP-C's response is
502 // This is good, they got our original message and
505 // Is there any need to track this?
511 // We'll consider these two codes as exceptions
513 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
514 PolicyResult.FAILURE_EXCEPTION);
515 return getTimeoutResult(PolicyResult.FAILURE_EXCEPTION);
520 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
521 PolicyResult.SUCCESS);
522 return getTimeoutResult(PolicyResult.SUCCESS);
527 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
528 PolicyResult.FAILURE);
529 return getTimeoutResult(PolicyResult.FAILURE);
536 * This method handles operation responses from LCM.
538 * @param dmaapResponse the LCM response
539 * @return The result of the response handling
541 private PolicyResult onResponse(LcmResponseWrapper dmaapResponse) {
543 * Parse out the operation attempt using the subrequestid
545 Integer operationAttempt = AppcLcmActorServiceProvider
546 .parseOperationAttempt(dmaapResponse.getBody().getCommonHeader().getSubRequestId());
547 if (operationAttempt == null) {
548 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).",
549 PolicyResult.FAILURE_EXCEPTION);
550 return PolicyResult.FAILURE_EXCEPTION;
554 * Process the APPCLCM response to see what PolicyResult should be returned
556 AbstractMap.SimpleEntry<PolicyResult, String> result =
557 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
559 if (result.getKey() != null) {
560 this.completeOperation(operationAttempt, result.getValue(), result.getKey());
561 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
564 return result.getKey();
570 * This method handles operation responses from SDNR.
572 * @param dmaapResponse the SDNR response
573 * @return the result of the response handling
575 private PolicyResult onResponse(PciResponseWrapper dmaapResponse) {
577 * Parse out the operation attempt using the subrequestid
579 Integer operationAttempt = SdnrActorServiceProvider
580 .parseOperationAttempt(dmaapResponse.getBody().getCommonHeader().getSubRequestId());
581 if (operationAttempt == null) {
582 this.completeOperation(operationAttempt, "Policy was unable to parse SDNR SubRequestID.",
583 PolicyResult.FAILURE_EXCEPTION);
584 return PolicyResult.FAILURE_EXCEPTION;
588 * Process the SDNR response to see what PolicyResult should be returned
590 SdnrActorServiceProvider.Pair<PolicyResult, String> result =
591 SdnrActorServiceProvider.processResponse(dmaapResponse);
593 if (result.getResult() != null) {
594 this.completeOperation(operationAttempt, result.getMessage(), result.getResult());
595 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
598 return result.getResult();
604 * This method handles operation responses from SO.
606 * @param msoResponse the SO response
607 * @return The result of the response handling
609 private PolicyResult onResponse(SoResponseWrapper msoResponse) {
610 switch (msoResponse.getSoResponse().getHttpResponseCode()) {
614 // Consider it as success
616 this.completeOperation(this.attempts, msoResponse.getSoResponse().getHttpResponseCode() + SUCCESS_MSG,
617 PolicyResult.SUCCESS);
618 return getTimeoutResult(PolicyResult.SUCCESS);
621 // Consider it as failure
623 this.completeOperation(this.attempts, msoResponse.getSoResponse().getHttpResponseCode() + FAILED_MSG,
624 PolicyResult.FAILURE);
625 return getTimeoutResult(PolicyResult.FAILURE);
630 * This method handles operation responses from VFC.
632 * @param vfcResponse the VFC response
633 * @return The result of the response handling
635 private PolicyResult onResponse(VfcResponse vfcResponse) {
636 if ("finished".equalsIgnoreCase(vfcResponse.getResponseDescriptor().getStatus())) {
638 // Consider it as success
640 this.completeOperation(this.attempts, SUCCESS_MSG, PolicyResult.SUCCESS);
641 return getTimeoutResult(PolicyResult.SUCCESS);
644 // Consider it as failure
646 this.completeOperation(this.attempts, FAILED_MSG, PolicyResult.FAILURE);
647 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
650 // increment operation attempts for retries
652 return PolicyResult.FAILURE;
657 * This method handles operation responses from SDNC.
659 * @param sdncResponse the VFC response
660 * @return The result of the response handling
662 private PolicyResult onResponse(SdncResponse sdncResponse) {
663 if ("200".equals(sdncResponse.getResponseOutput().getResponseCode())) {
665 // Consider it as success
667 this.completeOperation(this.attempts, SUCCESS_MSG, PolicyResult.SUCCESS);
668 return getTimeoutResult(PolicyResult.SUCCESS);
671 // Consider it as failure
673 this.completeOperation(this.attempts, FAILED_MSG, PolicyResult.FAILURE);
674 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
677 // increment operation attempts for retries
679 return PolicyResult.FAILURE;
683 private PolicyResult getTimeoutResult(PolicyResult result) {
684 return (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult) ? null : result);
688 private Integer getSubRequestId(Response appcResponse) {
690 return Integer.valueOf(appcResponse.getCommonHeader().getSubRequestId());
691 } catch (NumberFormatException e) {
693 // We cannot tell what happened if this doesn't exist
700 * Get the operation timeout.
702 * @return the timeout
704 public Integer getOperationTimeout() {
708 if (this.policy == null) {
709 logger.debug("getOperationTimeout returning 0");
712 logger.debug("getOperationTimeout returning {}", this.policy.getTimeout());
713 return this.policy.getTimeout();
717 * Get the operation timeout as a String.
719 * @param defaultTimeout the default timeout
720 * @return the timeout as a String
722 public String getOperationTimeoutString(int defaultTimeout) {
723 Integer to = this.getOperationTimeout();
724 if (to == null || to == 0) {
725 return Integer.toString(defaultTimeout) + "s";
727 return to.toString() + "s";
730 public PolicyResult getOperationResult() {
731 return this.policyResult;
735 * Get the operation as a message.
737 * @return the operation as a message
739 public String getOperationMessage() {
740 if (this.currentOperation != null && this.currentOperation.clOperation != null) {
741 return this.currentOperation.clOperation.toMessage();
744 if (!this.operationHistory.isEmpty()) {
745 return this.operationHistory.getLast().clOperation.toMessage();
751 * Get the operation as a message including the guard result.
753 * @param guardResult the guard result
754 * @return the operation as a message including the guard result
756 public String getOperationMessage(String guardResult) {
757 if (this.currentOperation != null && this.currentOperation.clOperation != null) {
758 return this.currentOperation.clOperation.toMessage() + ", Guard result: " + guardResult;
761 if (!this.operationHistory.isEmpty()) {
762 return this.operationHistory.getLast().clOperation.toMessage() + ", Guard result: " + guardResult;
768 * Get the operation history.
770 * @return the operation history
772 public String getOperationHistory() {
773 if (this.currentOperation != null && this.currentOperation.clOperation != null) {
774 return this.currentOperation.clOperation.toHistory();
777 if (!this.operationHistory.isEmpty()) {
778 return this.operationHistory.getLast().clOperation.toHistory();
786 * @return the list of control loop operations
788 public List<ControlLoopOperation> getHistory() {
789 LinkedList<ControlLoopOperation> history = new LinkedList<>();
790 for (Operation op : this.operationHistory) {
791 history.add(new ControlLoopOperation(op.clOperation));
798 * Set the operation has timed out.
800 public void setOperationHasTimedOut() {
804 this.completeOperation(this.attempts, "Operation timed out", PolicyResult.FAILURE_TIMEOUT);
808 * Set the operation has been denied by guard.
810 public void setOperationHasGuardDeny() {
814 this.completeOperation(this.attempts, "Operation denied by Guard", PolicyResult.FAILURE_GUARD);
817 public void setOperationHasException(String message) {
818 this.completeOperation(this.attempts, message, PolicyResult.FAILURE_EXCEPTION);
822 * Is the operation complete.
824 * @return <code>true</code> if the operation is complete, <code>false</code> otherwise
826 public boolean isOperationComplete() {
828 // Is there currently a result?
830 if (this.policyResult == null) {
832 // either we are in process or we
838 // We have some result, check if the operation failed
840 if (this.policyResult.equals(PolicyResult.FAILURE)) {
842 // Check if there were no retries specified
844 if (policy.getRetry() == null || policy.getRetry() == 0) {
846 // The result is the failure
853 if (this.isRetriesMaxedOut()) {
855 // No more attempts allowed, reset
856 // that our actual result is failure due to retries
858 this.policyResult = PolicyResult.FAILURE_RETRIES;
862 // There are more attempts available to try the
869 // Other results mean we are done
874 public boolean isOperationRunning() {
875 return (this.currentOperation != null);
879 * This method verifies that the operation manager may run an operation.
881 * @return True if the operation can run, false otherwise
882 * @throws ControlLoopException if the operation cannot run
884 private void verifyOperatonCanRun() throws ControlLoopException {
886 // They shouldn't call us if we currently running something
888 if (this.currentOperation != null) {
890 // what do we do if we are already running an operation?
892 throw new ControlLoopException("current operation is not null (an operation is already running)");
895 // Check if we have maxed out on retries
897 if (this.policy.getRetry() == null || this.policy.getRetry() < 1) {
899 // No retries are allowed, so check have we even made
900 // one attempt to execute the operation?
902 if (this.attempts >= 1) {
904 // We have, let's ensure our PolicyResult is set
906 if (this.policyResult == null) {
907 this.policyResult = PolicyResult.FAILURE_RETRIES;
912 throw new ControlLoopException("current operation failed and retries are not allowed");
916 // Have we maxed out on retries?
918 if (this.attempts > this.policy.getRetry()) {
919 if (this.policyResult == null) {
920 this.policyResult = PolicyResult.FAILURE_RETRIES;
922 throw new ControlLoopException("current oepration has failed after " + this.attempts + " retries");
927 private boolean isRetriesMaxedOut() {
928 if (policy.getRetry() == null || policy.getRetry() == 0) {
930 // There were NO retries specified, so declare
931 // this as completed.
933 return (this.attempts > 0);
935 return (this.attempts > policy.getRetry());
938 private void storeOperationInDataBase() {
939 // Only store in DB if enabled
940 boolean guardEnabled = "false".equalsIgnoreCase(PolicyEngine.manager.getEnvironmentProperty("guard.disabled"));
947 Properties props = new Properties();
948 if (PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL) != null
949 && PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER) != null
950 && PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS) != null) {
951 props.put(Util.ECLIPSE_LINK_KEY_URL, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_URL));
952 props.put(Util.ECLIPSE_LINK_KEY_USER, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_USER));
953 props.put(Util.ECLIPSE_LINK_KEY_PASS, PolicyEngine.manager.getEnvironmentProperty(Util.ONAP_KEY_PASS));
954 props.put(PersistenceUnitProperties.CLASSLOADER, ControlLoopOperationManager.class.getClassLoader());
958 String opsHistPu = System.getProperty("OperationsHistoryPU");
959 if (!"OperationsHistoryPUTest".equals(opsHistPu)) {
960 opsHistPu = "OperationsHistoryPU";
966 em = Persistence.createEntityManagerFactory(opsHistPu, props).createEntityManager();
967 } catch (Exception e) {
968 logger.error("storeOperationInDataBase threw: ", e);
972 Dbao newEntry = new Dbao();
974 newEntry.setClosedLoopName(this.onset.getClosedLoopControlName());
975 newEntry.setRequestId(this.onset.getRequestId().toString());
976 newEntry.setActor(this.currentOperation.clOperation.getActor());
977 newEntry.setOperation(this.currentOperation.clOperation.getOperation());
978 newEntry.setTarget(this.targetEntity);
979 newEntry.setStarttime(Timestamp.from(this.currentOperation.clOperation.getStart()));
980 newEntry.setSubrequestId(this.currentOperation.clOperation.getSubRequestId());
981 newEntry.setEndtime(new Timestamp(this.currentOperation.clOperation.getEnd().toEpochMilli()));
982 newEntry.setMessage(this.currentOperation.clOperation.getMessage());
983 newEntry.setOutcome(this.currentOperation.clOperation.getOutcome());
985 em.getTransaction().begin();
986 em.persist(newEntry);
987 em.getTransaction().commit();
992 private void completeOperation(Integer attempt, String message, PolicyResult result) {
993 if (attempt == null) {
994 logger.debug("attempt cannot be null (i.e. subRequestID)");
997 if (this.currentOperation != null) {
998 if (this.currentOperation.attempt == attempt.intValue()) {
999 this.currentOperation.clOperation.setEnd(Instant.now());
1000 this.currentOperation.clOperation.setMessage(message);
1001 this.currentOperation.clOperation.setOutcome(result.toString());
1002 this.currentOperation.policyResult = result;
1004 // Save it in history
1006 this.operationHistory.add(this.currentOperation);
1007 this.storeOperationInDataBase();
1009 // Set our last result
1011 this.policyResult = result;
1013 // Clear the current operation field
1015 this.currentOperation = null;
1018 logger.debug("not current");
1020 for (Operation op : this.operationHistory) {
1021 if (op.attempt == attempt.intValue()) {
1022 op.clOperation.setEnd(Instant.now());
1023 op.clOperation.setMessage(message);
1024 op.clOperation.setOutcome(result.toString());
1025 op.policyResult = result;
1029 logger.debug("Could not find associated operation");
1034 * Commit the abatement to the history database.
1036 * @param message the abatement message
1037 * @param outcome the abatement outcome
1039 public void commitAbatement(String message, String outcome) {
1040 logger.info("commitAbatement: {}. {}", message, outcome);
1042 if (this.currentOperation == null) {
1044 this.currentOperation = this.operationHistory.getLast();
1045 } catch (NoSuchElementException e) {
1046 logger.error("{}: commitAbatement threw an exception ", this, e);
1050 this.currentOperation.clOperation.setEnd(Instant.now());
1051 this.currentOperation.clOperation.setMessage(message);
1052 this.currentOperation.clOperation.setOutcome(outcome);
1054 // Store commit in DB
1056 this.storeOperationInDataBase();
1058 // Clear the current operation field
1060 this.currentOperation = null;
1064 * Construct a ControlLoopResponse object from actor response and input event.
1066 * @param response the response from actor
1067 * @param event the input event
1069 * @return a ControlLoopResponse
1071 public ControlLoopResponse getControlLoopResponse(Object response, VirtualControlLoopEvent event) {
1072 if (response instanceof PciResponseWrapper) {
1074 // Cast SDNR response and handle it
1076 return SdnrActorServiceProvider.getControlLoopResponse((PciResponseWrapper) response, event);