2 * ============LICENSE_START=======================================================
3 * controlloop operation manager
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
7 * Modifications Copyright (C) 2019 Tech Mahindra
8 * Modifications Copyright (C) 2019 Bell Canada.
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.controlloop.eventmanager;
26 import java.io.Serializable;
27 import java.sql.Timestamp;
28 import java.time.Instant;
29 import java.util.AbstractMap;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.List;
34 import java.util.NoSuchElementException;
35 import java.util.Optional;
36 import java.util.Properties;
37 import javax.persistence.EntityManager;
38 import javax.persistence.Persistence;
40 import org.apache.commons.lang3.tuple.Pair;
41 import org.eclipse.persistence.config.PersistenceUnitProperties;
42 import org.onap.aai.domain.yang.GenericVnf;
43 import org.onap.aai.domain.yang.ServiceInstance;
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput;
45 import org.onap.policy.aai.AaiCqResponse;
46 import org.onap.policy.aai.util.AaiException;
47 import org.onap.policy.appc.Response;
48 import org.onap.policy.appc.ResponseCode;
49 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
50 import org.onap.policy.cds.CdsResponse;
51 import org.onap.policy.controlloop.ControlLoopEvent;
52 import org.onap.policy.controlloop.ControlLoopException;
53 import org.onap.policy.controlloop.ControlLoopOperation;
54 import org.onap.policy.controlloop.ControlLoopResponse;
55 import org.onap.policy.controlloop.VirtualControlLoopEvent;
56 import org.onap.policy.controlloop.actor.appc.AppcActorServiceProvider;
57 import org.onap.policy.controlloop.actor.appclcm.AppcLcmActorServiceProvider;
58 import org.onap.policy.controlloop.actor.cds.CdsActorServiceProvider;
59 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
60 import org.onap.policy.controlloop.actor.sdnc.SdncActorServiceProvider;
61 import org.onap.policy.controlloop.actor.sdnr.SdnrActorServiceProvider;
62 import org.onap.policy.controlloop.actor.so.SoActorServiceProvider;
63 import org.onap.policy.controlloop.actor.vfc.VfcActorServiceProvider;
64 import org.onap.policy.controlloop.policy.Policy;
65 import org.onap.policy.controlloop.policy.PolicyResult;
66 import org.onap.policy.controlloop.policy.TargetType;
67 import org.onap.policy.database.operationshistory.Dbao;
68 import org.onap.policy.drools.system.PolicyEngineConstants;
69 import org.onap.policy.guard.Util;
70 import org.onap.policy.sdnc.SdncResponse;
71 import org.onap.policy.sdnr.PciResponseWrapper;
72 import org.onap.policy.so.SoResponseWrapper;
73 import org.onap.policy.vfc.VfcResponse;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 public class ControlLoopOperationManager implements Serializable {
78 private static final String SUCCESS_MSG = " Success";
79 private static final String FAILED_MSG = " Failed";
80 private static final long serialVersionUID = -3773199283624595410L;
81 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManager.class);
83 private static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
84 private static final String GENERIC_VNF_VNF_NAME = "generic-vnf.vnf-name";
85 private static final String GENERIC_VNF_VNF_ID = "generic-vnf.vnf-id";
87 private static final String AAI_SERVICE_INSTANCE_ID_KEY = "service-instance.service-instance-id";
88 private static final String PNF_NAME = "pnf.pnf-name";
91 // These properties are not changeable, but accessible
92 // for Drools Rule statements.
94 public final ControlLoopEvent onset;
95 public final Policy policy;
98 // Properties used to track the Operation
100 private int attempts = 0;
101 private Operation currentOperation = null;
102 private LinkedList<Operation> operationHistory = new LinkedList<>();
103 private PolicyResult policyResult = null;
104 private ControlLoopEventManager eventManager = null;
105 private String targetEntity;
106 private String guardApprovalStatus = "NONE";// "NONE", "PERMIT", "DENY"
107 private AaiCqResponse aaiCqResponse;
108 private transient Object operationRequest;
111 * Construct an instance.
113 * @param onset the onset event
114 * @param policy the policy
115 * @param em the event manager
116 * @throws ControlLoopException if an error occurs
118 public ControlLoopOperationManager(ControlLoopEvent onset, Policy policy,
119 ControlLoopEventManager em) throws ControlLoopException {
122 this.policy = policy;
123 this.guardApprovalStatus = "NONE";
124 this.eventManager = em;
127 if (TargetType.VNF.equals(policy.getTarget().getType())
128 || TargetType.VFMODULE.equals(policy.getTarget().getType())) {
130 this.eventManager.getCqResponse((VirtualControlLoopEvent) onset);
133 this.targetEntity = getTarget(policy);
137 } catch (AaiException e) {
138 throw new ControlLoopException(e.getMessage(), e);
142 private void initActor(Policy policy) throws AaiException, ControlLoopException {
144 // Let's make a sanity check
146 switch (policy.getActor()) {
161 throw new ControlLoopException(
162 "ControlLoopEventManager: policy has an unknown actor.");
166 private void initAppc(Policy policy) throws AaiException {
167 if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
169 * The target vnf-id may not be the same as the source vnf-id specified in the yaml, the
171 * vnf-id is retrieved by a named query to A&AI.
173 GenericVnf genvnf = this.aaiCqResponse
174 .getGenericVnfByModelInvariantId(policy.getTarget().getResourceID());
175 if (genvnf == null) {
176 logger.info("Target entity could not be found");
177 throw new AaiException("Target vnf-id could not be found");
179 this.targetEntity = genvnf.getVnfId();
184 public ControlLoopEventManager getEventManager() {
188 public void setEventManager(ControlLoopEventManager eventManager) {
189 this.eventManager = eventManager;
192 public String getTargetEntity() {
193 return this.targetEntity;
197 public String toString() {
198 return "ControlLoopOperationManager [onset="
199 + (onset != null ? onset.getRequestId() : "null") + ", policy="
200 + (policy != null ? policy.getId() : "null") + ", attempts=" + attempts
201 + ", policyResult=" + policyResult + ", currentOperation=" + currentOperation
202 + ", operationHistory=" + operationHistory + "]";
206 // Internal class used for tracking
208 private class Operation implements Serializable {
209 private static final long serialVersionUID = 1L;
211 private ControlLoopOperation clOperation = new ControlLoopOperation();
212 private PolicyResult policyResult = null;
213 private int attempt = 0;
216 public String toString() {
217 return "Operation [attempt=" + attempt + ", policyResult=" + policyResult
218 + ", operation=" + clOperation + "]";
222 public Object getOperationRequest() {
223 return operationRequest;
226 public String getGuardApprovalStatus() {
227 return guardApprovalStatus;
230 public void setGuardApprovalStatus(String guardApprovalStatus) {
231 this.guardApprovalStatus = guardApprovalStatus;
235 * Get the target for a policy.
237 * @param policy the policy
239 * @throws ControlLoopException if an error occurs
241 public String getTarget(Policy policy) throws ControlLoopException {
242 if (policy.getTarget() == null) {
243 throw new ControlLoopException("The target is null");
246 if (policy.getTarget().getType() == null) {
247 throw new ControlLoopException("The target type is null");
250 switch (policy.getTarget().getType()) {
252 return getPnfTarget();
255 return getVfModuleTarget();
257 return getVfModuleTarget();
259 throw new ControlLoopException("The target type is not supported");
263 private String getVfModuleTarget() throws ControlLoopException {
264 VirtualControlLoopEvent virtualOnsetEvent = (VirtualControlLoopEvent) this.onset;
265 if (this.onset.getTarget().equalsIgnoreCase(VSERVER_VSERVER_NAME)) {
266 return virtualOnsetEvent.getAai().get(VSERVER_VSERVER_NAME);
267 } else if (this.onset.getTarget().equalsIgnoreCase(GENERIC_VNF_VNF_ID)) {
268 return virtualOnsetEvent.getAai().get(GENERIC_VNF_VNF_ID);
269 } else if (this.onset.getTarget().equalsIgnoreCase(GENERIC_VNF_VNF_NAME)) {
271 * If the onset is enriched with the vnf-id, we don't need an A&AI response
273 if (virtualOnsetEvent.getAai().containsKey(GENERIC_VNF_VNF_ID)) {
274 return virtualOnsetEvent.getAai().get(GENERIC_VNF_VNF_ID);
278 * If the vnf-name was retrieved from the onset then the vnf-id must be obtained from
280 * manager's A&AI GET query
283 String vnfId = this.aaiCqResponse.getDefaultGenericVnf().getVnfId();
285 throw new AaiException("No vnf-id found");
289 } catch (AaiException e) {
290 throw new ControlLoopException(e.getMessage(), e);
293 throw new ControlLoopException("Target does not match target type");
296 private String getPnfTarget() throws ControlLoopException {
297 VirtualControlLoopEvent virtualOnsetEvent = (VirtualControlLoopEvent) this.onset;
298 if (!PNF_NAME.equalsIgnoreCase(onset.getTarget())) {
299 throw new ControlLoopException(
300 "Target in the onset event is either null or does not match target key expected in AAI section.");
302 return virtualOnsetEvent.getAai().get(PNF_NAME);
306 * Start an operation.
308 * @param onset the onset event
309 * @return the operation request
310 * @throws ControlLoopException if an error occurs
312 public Object startOperation(/* VirtualControlLoopEvent */ControlLoopEvent onset)
313 throws ControlLoopException {
314 verifyOperatonCanRun();
319 this.policyResult = null;
320 Operation operation = new Operation();
321 operation.attempt = ++this.attempts;
322 operation.clOperation.setActor(this.policy.getActor());
323 operation.clOperation.setOperation(this.policy.getRecipe());
324 operation.clOperation.setTarget(this.policy.getTarget().toString());
325 operation.clOperation.setSubRequestId(Integer.toString(operation.attempt));
327 // Now determine which actor we need to construct a request for
330 switch (policy.getActor()) {
332 return startAppcOperation(onset, operation);
334 return startSoOperation(onset, operation);
336 return startVfcOperation(onset, operation);
338 return startSdnrOperation(onset, operation);
340 return startSdncOperation(onset, operation);
342 return startCdsOperation(onset, operation);
344 throw new ControlLoopException(
345 "invalid actor " + policy.getActor() + " on policy");
348 } catch (AaiException e) {
349 throw new ControlLoopException(e.getMessage(), e);
353 private Object startAppcOperation(ControlLoopEvent onset, Operation operation) {
355 * If the recipe is ModifyConfig, a legacy APPC request is constructed. Otherwise an
359 this.currentOperation = operation;
360 if ("ModifyConfig".equalsIgnoreCase(policy.getRecipe())) {
361 this.operationRequest =
362 AppcActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
363 operation.clOperation, this.policy, this.targetEntity);
365 this.operationRequest =
366 AppcLcmActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset,
367 operation.clOperation, this.policy, this.targetEntity);
370 // Save the operation
373 return operationRequest;
376 private Object startSoOperation(ControlLoopEvent onset, Operation operation) {
377 SoActorServiceProvider soActorSp = new SoActorServiceProvider();
378 this.operationRequest = soActorSp.constructRequestCq((VirtualControlLoopEvent) onset,
379 operation.clOperation, this.policy, this.aaiCqResponse);
381 // Save the operation
382 this.currentOperation = operation;
384 if (this.operationRequest == null) {
385 this.policyResult = PolicyResult.FAILURE;
388 return operationRequest;
391 private Object startVfcOperation(ControlLoopEvent onset, Operation operation) {
392 this.operationRequest =
393 VfcActorServiceProvider.constructRequestCq((VirtualControlLoopEvent) onset,
394 operation.clOperation, this.policy, this.aaiCqResponse);
395 this.currentOperation = operation;
396 if (this.operationRequest == null) {
397 this.policyResult = PolicyResult.FAILURE;
399 return operationRequest;
402 private Object startSdnrOperation(ControlLoopEvent onset, Operation operation) {
404 * If the recipe is ModifyConfig or ModifyConfigANR, a SDNR request is constructed.
406 this.currentOperation = operation;
407 this.operationRequest = SdnrActorServiceProvider
408 .constructRequest((VirtualControlLoopEvent) onset, operation.clOperation, this.policy);
410 // Save the operation
412 if (this.operationRequest == null) {
413 this.policyResult = PolicyResult.FAILURE;
416 return operationRequest;
419 private Object startSdncOperation(ControlLoopEvent onset, Operation operation) {
420 SdncActorServiceProvider provider = new SdncActorServiceProvider();
421 this.operationRequest = provider.constructRequest((VirtualControlLoopEvent) onset,
422 operation.clOperation, this.policy);
423 this.currentOperation = operation;
424 if (this.operationRequest == null) {
425 this.policyResult = PolicyResult.FAILURE;
427 return operationRequest;
430 private Object startCdsOperation(ControlLoopEvent onset, Operation operation)
431 throws AaiException {
433 CdsActorServiceProvider provider = new CdsActorServiceProvider();
434 Optional<ExecutionServiceInput> optionalRequest =
435 provider.constructRequest((VirtualControlLoopEvent) onset, operation.clOperation,
436 this.policy, this.buildAaiParams());
438 this.currentOperation = operation;
439 if (optionalRequest.isPresent()) {
440 this.operationRequest = optionalRequest.get();
442 this.operationRequest = null;
443 this.policyResult = PolicyResult.FAILURE;
446 return this.operationRequest;
450 * Build AAI parameters for CDS operation.
452 * @return a map containing vnf id key and value for the vnf to apply the action to.
453 * @throws AaiException if the vnf can not be found.
455 private Map<String, String> buildAaiParams() throws AaiException {
457 Map<String, String> result = new HashMap<>();
459 if (TargetType.VNF.equals(policy.getTarget().getType())
460 || TargetType.VFMODULE.equals(policy.getTarget().getType())) {
462 ServiceInstance serviceInstance = this.aaiCqResponse.getServiceInstance();
463 if (serviceInstance == null) {
464 logger.info("Target entity service instance could not be found");
465 throw new AaiException("Target service instance could not be found");
468 GenericVnf genericVnf = this.aaiCqResponse
469 .getGenericVnfByModelInvariantId(policy.getTarget().getResourceID());
470 if (genericVnf == null) {
471 logger.info("Target entity generic vnf could not be found");
472 throw new AaiException("Target generic vnf could not be found");
475 result.put(AAI_SERVICE_INSTANCE_ID_KEY, serviceInstance.getServiceInstanceId());
476 result.put(GENERIC_VNF_VNF_ID, genericVnf.getVnfId());
478 } else if (TargetType.PNF.equals(policy.getTarget().getType())) {
479 result = this.eventManager.getPnf((VirtualControlLoopEvent) onset);
489 * @param response the response
490 * @return a PolicyResult
492 public PolicyResult onResponse(Object response) {
494 // Which response is it?
496 if (response instanceof Response) {
498 // Cast APPC response and handle it
500 return onResponse((Response) response);
501 } else if (response instanceof AppcLcmDmaapWrapper) {
503 // Cast LCM response and handle it
505 return onResponse((AppcLcmDmaapWrapper) response);
506 } else if (response instanceof PciResponseWrapper) {
508 // Cast SDNR response and handle it
510 return onResponse((PciResponseWrapper) response);
511 } else if (response instanceof SoResponseWrapper) {
513 // Cast SO response and handle it
515 return onResponse((SoResponseWrapper) response);
516 } else if (response instanceof VfcResponse) {
518 // Cast VFC response and handle it
520 return onResponse((VfcResponse) response);
521 } else if (response instanceof SdncResponse) {
523 // Cast SDNC response and handle it
525 return onResponse((SdncResponse) response);
526 } else if (response instanceof CdsResponse) {
528 // Cast CDS response and handle it
530 return onResponse((CdsResponse) response);
537 * This method handles operation responses from APPC.
539 * @param appcResponse the APPC response
540 * @return The result of the response handling
542 private PolicyResult onResponse(Response appcResponse) {
544 // Determine which subrequestID (ie. attempt)
546 Integer operationAttempt = getSubRequestId(appcResponse);
547 if (operationAttempt == null) {
548 this.completeOperation(operationAttempt,
549 "Policy was unable to parse APP-C SubRequestID (it was null).",
550 PolicyResult.FAILURE_EXCEPTION);
551 return PolicyResult.FAILURE_EXCEPTION;
554 // Sanity check the response message
556 if (appcResponse.getStatus() == null) {
558 // We cannot tell what happened if this doesn't exist
560 this.completeOperation(operationAttempt,
561 "Policy was unable to parse APP-C response status field (it was null).",
562 PolicyResult.FAILURE_EXCEPTION);
563 return PolicyResult.FAILURE_EXCEPTION;
566 // Get the Response Code
568 ResponseCode code = ResponseCode.toResponseCode(appcResponse.getStatus().getCode());
571 // We are unaware of this code
573 this.completeOperation(operationAttempt,
574 "Policy was unable to parse APP-C response status code field.",
575 PolicyResult.FAILURE_EXCEPTION);
576 return PolicyResult.FAILURE_EXCEPTION;
579 return onResponse(appcResponse, operationAttempt, code);
582 private PolicyResult onResponse(Response appcResponse, Integer operationAttempt,
585 // Ok, let's figure out what APP-C's response is
590 // This is good, they got our original message and
593 // Is there any need to track this?
599 // We'll consider these two codes as exceptions
601 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
602 PolicyResult.FAILURE_EXCEPTION);
603 return getTimeoutResult(PolicyResult.FAILURE_EXCEPTION);
608 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
609 PolicyResult.SUCCESS);
610 return getTimeoutResult(PolicyResult.SUCCESS);
615 this.completeOperation(operationAttempt, appcResponse.getStatus().getDescription(),
616 PolicyResult.FAILURE);
617 return getTimeoutResult(PolicyResult.FAILURE);
624 * This method handles operation responses from LCM.
626 * @param dmaapResponse the LCM response
627 * @return The result of the response handling
629 private PolicyResult onResponse(AppcLcmDmaapWrapper dmaapResponse) {
631 * Parse out the operation attempt using the subrequestid
633 Integer operationAttempt = AppcLcmActorServiceProvider.parseOperationAttempt(
634 dmaapResponse.getBody().getOutput().getCommonHeader().getSubRequestId());
635 if (operationAttempt == null) {
636 this.completeOperation(operationAttempt,
637 "Policy was unable to parse APP-C SubRequestID (it was null).",
638 PolicyResult.FAILURE_EXCEPTION);
639 return PolicyResult.FAILURE_EXCEPTION;
643 * Process the APPCLCM response to see what PolicyResult should be returned
645 AbstractMap.SimpleEntry<PolicyResult, String> result =
646 AppcLcmActorServiceProvider.processResponse(dmaapResponse);
648 if (result.getKey() != null) {
649 this.completeOperation(operationAttempt, result.getValue(), result.getKey());
650 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
653 return result.getKey();
659 * This method handles operation responses from SDNR.
661 * @param dmaapResponse the SDNR response
662 * @return the result of the response handling
664 private PolicyResult onResponse(PciResponseWrapper dmaapResponse) {
666 * Parse out the operation attempt using the subrequestid
668 Integer operationAttempt = SdnrActorServiceProvider
669 .parseOperationAttempt(dmaapResponse.getBody().getCommonHeader().getSubRequestId());
670 if (operationAttempt == null) {
671 this.completeOperation(operationAttempt,
672 "Policy was unable to parse SDNR SubRequestID.", PolicyResult.FAILURE_EXCEPTION);
673 return PolicyResult.FAILURE_EXCEPTION;
677 * Process the SDNR response to see what PolicyResult should be returned
679 Pair<PolicyResult, String> result =
680 SdnrActorServiceProvider.processResponse(dmaapResponse);
682 if (result.getLeft() != null) {
683 this.completeOperation(operationAttempt, result.getRight(), result.getLeft());
684 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
687 return result.getLeft();
693 * This method handles operation responses from SO.
695 * @param msoResponse the SO response
696 * @return The result of the response handling
698 private PolicyResult onResponse(SoResponseWrapper msoResponse) {
699 switch (msoResponse.getSoResponse().getHttpResponseCode()) {
703 // Consider it as success
705 this.completeOperation(this.attempts,
706 msoResponse.getSoResponse().getHttpResponseCode() + SUCCESS_MSG,
707 PolicyResult.SUCCESS);
708 return getTimeoutResult(PolicyResult.SUCCESS);
711 // Consider it as failure
713 this.completeOperation(this.attempts,
714 msoResponse.getSoResponse().getHttpResponseCode() + FAILED_MSG,
715 PolicyResult.FAILURE);
716 return getTimeoutResult(PolicyResult.FAILURE);
721 * This method handles operation responses from VFC.
723 * @param vfcResponse the VFC response
724 * @return The result of the response handling
726 private PolicyResult onResponse(VfcResponse vfcResponse) {
727 if ("finished".equalsIgnoreCase(vfcResponse.getResponseDescriptor().getStatus())) {
729 // Consider it as success
731 this.completeOperation(this.attempts, SUCCESS_MSG, PolicyResult.SUCCESS);
732 return getTimeoutResult(PolicyResult.SUCCESS);
735 // Consider it as failure
737 this.completeOperation(this.attempts, FAILED_MSG, PolicyResult.FAILURE);
738 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
741 // increment operation attempts for retries
743 return PolicyResult.FAILURE;
748 * This method handles operation responses from SDNC.
750 * @param sdncResponse the VFC response
751 * @return The result of the response handling
753 private PolicyResult onResponse(SdncResponse sdncResponse) {
754 if ("200".equals(sdncResponse.getResponseOutput().getResponseCode())) {
756 // Consider it as success
758 this.completeOperation(this.attempts, SUCCESS_MSG, PolicyResult.SUCCESS);
759 return getTimeoutResult(PolicyResult.SUCCESS);
762 // Consider it as failure
764 this.completeOperation(this.attempts, FAILED_MSG, PolicyResult.FAILURE);
765 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
768 // increment operation attempts for retries
770 return PolicyResult.FAILURE;
775 * This method handles operation responses from CDS.
777 * @param response the CDS response
778 * @return The result of the response handling
780 private PolicyResult onResponse(CdsResponse response) {
781 if (response != null && CdsActorConstants.SUCCESS.equals(response.getStatus())) {
783 // Consider it as success
785 this.completeOperation(this.attempts, SUCCESS_MSG, PolicyResult.SUCCESS);
786 return getTimeoutResult(PolicyResult.SUCCESS);
789 // Consider it as failure
791 this.completeOperation(this.attempts, FAILED_MSG, PolicyResult.FAILURE);
792 if (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult)) {
795 // increment operation attempts for retries
797 return PolicyResult.FAILURE;
801 private PolicyResult getTimeoutResult(PolicyResult result) {
802 return (PolicyResult.FAILURE_TIMEOUT.equals(this.policyResult) ? null : result);
805 private Integer getSubRequestId(Response appcResponse) {
807 return Integer.valueOf(appcResponse.getCommonHeader().getSubRequestId());
808 } catch (NumberFormatException e) {
810 // We cannot tell what happened if this doesn't exist
817 * Get the operation timeout.
819 * @return the timeout
821 public Integer getOperationTimeout() {
825 if (this.policy == null) {
826 logger.debug("getOperationTimeout returning 0");
829 logger.debug("getOperationTimeout returning {}", this.policy.getTimeout());
830 return this.policy.getTimeout();
834 * Get the operation timeout as a String.
836 * @param defaultTimeout the default timeout
837 * @return the timeout as a String
839 public String getOperationTimeoutString(int defaultTimeout) {
840 Integer to = this.getOperationTimeout();
841 if (to == null || to == 0) {
842 return Integer.toString(defaultTimeout) + "s";
844 return to.toString() + "s";
847 public PolicyResult getOperationResult() {
848 return this.policyResult;
852 * Get the operation as a message.
854 * @return the operation as a message
856 public String getOperationMessage() {
857 if (this.currentOperation != null && this.currentOperation.clOperation != null) {
858 return this.currentOperation.clOperation.toMessage();
861 if (!this.operationHistory.isEmpty()) {
862 return this.operationHistory.getLast().clOperation.toMessage();
868 * Get the operation as a message including the guard result.
870 * @param guardResult the guard result
871 * @return the operation as a message including the guard result
873 public String getOperationMessage(String guardResult) {
874 if (this.currentOperation != null && this.currentOperation.clOperation != null) {
875 return this.currentOperation.clOperation.toMessage() + ", Guard result: " + guardResult;
878 if (!this.operationHistory.isEmpty()) {
879 return this.operationHistory.getLast().clOperation.toMessage() + ", Guard result: "
886 * Get the operation history.
888 * @return the operation history
890 public String getOperationHistory() {
891 if (this.currentOperation != null && this.currentOperation.clOperation != null) {
892 return this.currentOperation.clOperation.toHistory();
895 if (!this.operationHistory.isEmpty()) {
896 return this.operationHistory.getLast().clOperation.toHistory();
904 * @return the list of control loop operations
906 public List<ControlLoopOperation> getHistory() {
907 LinkedList<ControlLoopOperation> history = new LinkedList<>();
908 for (Operation op : this.operationHistory) {
909 history.add(new ControlLoopOperation(op.clOperation));
916 * Set the operation has timed out.
918 public void setOperationHasTimedOut() {
922 this.completeOperation(this.attempts, "Operation timed out", PolicyResult.FAILURE_TIMEOUT);
926 * Set the operation has been denied by guard.
928 public void setOperationHasGuardDeny() {
932 this.completeOperation(this.attempts, "Operation denied by Guard",
933 PolicyResult.FAILURE_GUARD);
936 public void setOperationHasException(String message) {
937 this.completeOperation(this.attempts, message, PolicyResult.FAILURE_EXCEPTION);
941 * Is the operation complete.
943 * @return <code>true</code> if the operation is complete, <code>false</code> otherwise
945 public boolean isOperationComplete() {
947 // Is there currently a result?
949 if (this.policyResult == null) {
951 // either we are in process or we
957 // We have some result, check if the operation failed
959 if (this.policyResult.equals(PolicyResult.FAILURE)) {
961 // Check if there were no retries specified
963 if (getMaxRetries() < 1) {
965 // The result is the failure
972 if (this.attempts > getMaxRetries()) {
974 // No more attempts allowed, reset
975 // that our actual result is failure due to retries
977 this.policyResult = PolicyResult.FAILURE_RETRIES;
981 // There are more attempts available to try the
988 // Other results mean we are done
993 public boolean isOperationRunning() {
994 return (this.currentOperation != null);
998 * This method verifies that the operation manager may run an operation.
1000 * @return True if the operation can run, false otherwise
1001 * @throws ControlLoopException if the operation cannot run
1003 private void verifyOperatonCanRun() throws ControlLoopException {
1005 // They shouldn't call us if we currently running something
1007 if (this.currentOperation != null) {
1009 // what do we do if we are already running an operation?
1011 throw new ControlLoopException(
1012 "current operation is not null (an operation is already running)");
1015 // Check if we have maxed out on retries
1017 if (getMaxRetries() < 1) {
1019 // No retries are allowed, so check have we even made
1020 // one attempt to execute the operation?
1022 if (this.attempts >= 1) {
1024 // We have, let's ensure our PolicyResult is set
1026 if (this.policyResult == null) {
1027 this.policyResult = PolicyResult.FAILURE_RETRIES;
1032 throw new ControlLoopException(
1033 "current operation failed and retries are not allowed");
1037 // Have we maxed out on retries?
1039 if (this.attempts > getMaxRetries()) {
1040 if (this.policyResult == null) {
1041 this.policyResult = PolicyResult.FAILURE_RETRIES;
1043 throw new ControlLoopException(
1044 "current oepration has failed after " + this.attempts + " retries");
1050 * Gets the maximum number of retries.
1052 * @return the maximum number of retries, or {@code 0}, if not specified
1054 public int getMaxRetries() {
1055 return (policy.getRetry() != null ? policy.getRetry() : 0);
1058 private void storeOperationInDataBase() {
1059 // Only store in DB if enabled
1060 boolean guardEnabled = "false".equalsIgnoreCase(
1061 PolicyEngineConstants.getManager().getEnvironmentProperty("guard.disabled"));
1062 if (!guardEnabled) {
1067 Properties props = new Properties();
1068 if (PolicyEngineConstants.getManager().getEnvironmentProperty(Util.ONAP_KEY_URL) != null
1069 && PolicyEngineConstants.getManager().getEnvironmentProperty(Util.ONAP_KEY_USER) != null
1070 && PolicyEngineConstants.getManager()
1071 .getEnvironmentProperty(Util.ONAP_KEY_PASS) != null) {
1072 props.put(Util.ECLIPSE_LINK_KEY_URL,
1073 PolicyEngineConstants.getManager().getEnvironmentProperty(Util.ONAP_KEY_URL));
1074 props.put(Util.ECLIPSE_LINK_KEY_USER,
1075 PolicyEngineConstants.getManager().getEnvironmentProperty(Util.ONAP_KEY_USER));
1076 props.put(Util.ECLIPSE_LINK_KEY_PASS,
1077 PolicyEngineConstants.getManager().getEnvironmentProperty(Util.ONAP_KEY_PASS));
1078 props.put(PersistenceUnitProperties.CLASSLOADER,
1079 ControlLoopOperationManager.class.getClassLoader());
1082 String opsHistPu = System.getProperty("OperationsHistoryPU");
1083 if (!"OperationsHistoryPUTest".equals(opsHistPu)) {
1084 opsHistPu = "OperationsHistoryPU";
1090 em = Persistence.createEntityManagerFactory(opsHistPu, props).createEntityManager();
1091 } catch (Exception e) {
1092 logger.error("storeOperationInDataBase threw: ", e);
1096 Dbao newEntry = new Dbao();
1098 newEntry.setClosedLoopName(this.onset.getClosedLoopControlName());
1099 newEntry.setRequestId(this.onset.getRequestId().toString());
1100 newEntry.setActor(this.currentOperation.clOperation.getActor());
1101 newEntry.setOperation(this.currentOperation.clOperation.getOperation());
1102 newEntry.setTarget(this.targetEntity);
1103 newEntry.setStarttime(Timestamp.from(this.currentOperation.clOperation.getStart()));
1104 newEntry.setSubrequestId(this.currentOperation.clOperation.getSubRequestId());
1106 .setEndtime(new Timestamp(this.currentOperation.clOperation.getEnd().toEpochMilli()));
1107 newEntry.setMessage(this.currentOperation.clOperation.getMessage());
1108 newEntry.setOutcome(this.currentOperation.clOperation.getOutcome());
1110 em.getTransaction().begin();
1111 em.persist(newEntry);
1112 em.getTransaction().commit();
1117 private void completeOperation(Integer attempt, String message, PolicyResult result) {
1118 if (attempt == null) {
1119 logger.debug("attempt cannot be null (i.e. subRequestID)");
1122 if (this.currentOperation != null) {
1123 if (this.currentOperation.attempt == attempt.intValue()) {
1124 this.currentOperation.clOperation.setEnd(Instant.now());
1125 this.currentOperation.clOperation.setMessage(message);
1126 this.currentOperation.clOperation.setOutcome(result.toString());
1127 this.currentOperation.policyResult = result;
1129 // Save it in history
1131 this.operationHistory.add(this.currentOperation);
1132 this.storeOperationInDataBase();
1134 // Set our last result
1136 this.policyResult = result;
1138 // Clear the current operation field
1140 this.currentOperation = null;
1143 logger.debug("not current");
1145 for (Operation op : this.operationHistory) {
1146 if (op.attempt == attempt.intValue()) {
1147 op.clOperation.setEnd(Instant.now());
1148 op.clOperation.setMessage(message);
1149 op.clOperation.setOutcome(result.toString());
1150 op.policyResult = result;
1154 logger.debug("Could not find associated operation");
1158 * Commit the abatement to the history database.
1160 * @param message the abatement message
1161 * @param outcome the abatement outcome
1163 public void commitAbatement(String message, String outcome) {
1164 logger.info("commitAbatement: {}. {}", message, outcome);
1166 if (this.currentOperation == null) {
1168 this.currentOperation = this.operationHistory.getLast();
1169 } catch (NoSuchElementException e) {
1170 logger.error("{}: commitAbatement threw an exception ", this, e);
1174 this.currentOperation.clOperation.setEnd(Instant.now());
1175 this.currentOperation.clOperation.setMessage(message);
1176 this.currentOperation.clOperation.setOutcome(outcome);
1178 // Store commit in DB
1180 this.storeOperationInDataBase();
1182 // Clear the current operation field
1184 this.currentOperation = null;
1188 * Construct a ControlLoopResponse object from actor response and input event.
1190 * @param response the response from actor
1191 * @param event the input event
1193 * @return a ControlLoopResponse
1195 public ControlLoopResponse getControlLoopResponse(Object response,
1196 VirtualControlLoopEvent event) {
1197 if (response instanceof PciResponseWrapper) {
1199 // Cast SDNR response and handle it
1201 return SdnrActorServiceProvider.getControlLoopResponse((PciResponseWrapper) response,