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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import java.io.Serializable;
24 import java.sql.Timestamp;
25 import java.time.Instant;
26 import java.util.LinkedList;
28 import javax.persistence.EntityManager;
29 import javax.persistence.Persistence;
31 import org.onap.policy.appc.Response;
32 import org.onap.policy.appc.ResponseCode;
33 import org.onap.policy.controlloop.ControlLoopEvent;
34 import org.onap.policy.controlloop.ControlLoopOperation;
35 import org.onap.policy.controlloop.VirtualControlLoopEvent;
36 import org.onap.policy.controlloop.ControlLoopException;
37 import org.onap.policy.controlloop.policy.Policy;
38 import org.onap.policy.controlloop.policy.PolicyResult;
39 import org.onap.policy.controlloop.actor.appc.APPCActorServiceProvider;
40 import org.onap.policy.controlloop.actor.vfc.VFCActorServiceProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 public class ControlLoopOperationManager implements Serializable {
50 private static final long serialVersionUID = -3773199283624595410L;
51 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManager.class);
54 public String toString() {
55 return "ControlLoopOperationManager [onset=" + (onset != null ? onset.requestID : "null") + ", policy="
56 + (policy != null ? policy.getId() : "null") + ", attempts=" + attempts
57 + ", policyResult=" + policyResult
58 + ", currentOperation=" + currentOperation + ", operationHistory=" + operationHistory
63 // These properties are not changeable, but accessible
64 // for Drools Rule statements.
66 //public final ATTControlLoopEvent onset;
67 public final ControlLoopEvent onset;
68 public final Policy policy;
71 // Properties used to track the Operation
73 private int attempts = 0;
74 private Operation currentOperation = null;
75 private LinkedList<Operation> operationHistory = new LinkedList<Operation>();
76 private PolicyResult policyResult = null;
77 private ControlLoopEventManager eventManager = null;
79 public ControlLoopEventManager getEventManager() {
83 public void setEventManager(ControlLoopEventManager eventManager) {
84 this.eventManager = eventManager;
89 // Internal class used for tracking
91 private class Operation {
92 public ControlLoopOperation operation = new ControlLoopOperation();
93 public PolicyResult policyResult = null;
94 public int attempt = 0;
97 public String toString() {
98 return "Operation [attempt=" + attempt + ", policyResult=" + policyResult + ", operation=" + operation
103 private String guardApprovalStatus = "NONE";//"NONE", "PERMIT", "DENY"
104 private Object operationRequest;
106 public Object getOperationRequest() {
107 return operationRequest;
110 public String getGuardApprovalStatus() {
111 return guardApprovalStatus;
113 public void setGuardApprovalStatus(String guardApprovalStatus) {
114 this.guardApprovalStatus = guardApprovalStatus;
118 public ControlLoopOperationManager(/*ATTControlLoopEvent*/ControlLoopEvent onset, Policy policy, ControlLoopEventManager em) throws ControlLoopException {
120 this.policy = policy;
121 this.guardApprovalStatus = "NONE";
122 this.eventManager = em;
125 // Let's make a sanity check
127 switch (policy.getActor()) {
139 throw new ControlLoopException("ControlLoopEventManager: policy has an unknown actor.");
143 public Object startOperation(/*VirtualControlLoopEvent*/ControlLoopEvent onset) {
145 // They shouldn't call us if we currently running something
147 if (this.currentOperation != null) {
149 // what do we do if we are already running an operation?
154 // Check if we have maxed out on retries
156 if (this.policy.getRetry() == null || this.policy.getRetry() < 1) {
158 // No retries are allowed, so check have we even made
159 // one attempt to execute the operation?
161 if (this.attempts >= 1) {
163 // We have, let's ensure our PolicyResult is set
165 if (this.policyResult == null) {
166 this.policyResult = PolicyResult.FAILURE_RETRIES;
175 // Have we maxed out on retries?
177 if (this.attempts > this.policy.getRetry()) {
178 if (this.policyResult == null) {
179 this.policyResult = PolicyResult.FAILURE_RETRIES;
187 this.policyResult = null;
188 Operation operation = new Operation();
189 operation.attempt = ++this.attempts;
190 operation.operation.actor = this.policy.getActor().toString();
191 operation.operation.operation = this.policy.getRecipe();
192 operation.operation.target = this.policy.getTarget().toString();
193 operation.operation.subRequestId = Integer.toString(operation.attempt);
195 // Now determine which actor we need to construct a request for
197 switch (policy.getActor()) {
199 //Request request = APPCActorServiceProvider.constructRequest(onset, operation.operation, this.policy);
200 this.operationRequest = APPCActorServiceProvider.constructRequest((VirtualControlLoopEvent)onset, operation.operation, this.policy);
202 // Save the operation
204 this.currentOperation = operation;
205 //System.out.print("************* BEFORE STORING.....");
206 //this.storeOperationInDataBase("startOperation");
207 //System.out.print("************* AFTER STORING.....");
209 return operationRequest;
212 // We are not supporting MSO interface at the moment
214 System.out.println("We are not supporting MSO actor in the latest release.");
217 this.operationRequest = VFCActorServiceProvider.constructRequest((VirtualControlLoopEvent) onset, operation.operation, this.policy);
218 this.currentOperation = operation;
219 return operationRequest;
225 public PolicyResult onResponse(Object response) {
227 // Which response is it?
229 if (response instanceof Response) {
233 Response appcResponse = (Response) response;
235 // Determine which subrequestID (ie. attempt)
237 Integer operationAttempt = null;
239 operationAttempt = Integer.parseInt(appcResponse.CommonHeader.SubRequestID);
240 } catch (NumberFormatException e) {
242 // We cannot tell what happened if this doesn't exist
244 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C SubRequestID (it was null).", PolicyResult.FAILURE_EXCEPTION);
245 return PolicyResult.FAILURE_EXCEPTION;
248 // Sanity check the response message
250 if (appcResponse.Status == null) {
252 // We cannot tell what happened if this doesn't exist
254 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C response status field (it was null).", PolicyResult.FAILURE_EXCEPTION);
255 return PolicyResult.FAILURE_EXCEPTION;
258 // Get the Response Code
260 ResponseCode code = ResponseCode.toResponseCode(appcResponse.Status.Code);
263 // We are unaware of this code
265 this.completeOperation(operationAttempt, "Policy was unable to parse APP-C response status code field.", PolicyResult.FAILURE_EXCEPTION);
266 return PolicyResult.FAILURE_EXCEPTION;
269 // Ok, let's figure out what APP-C's response is
274 // This is good, they got our original message and
277 // Is there any need to track this?
283 // We'll consider these two codes as exceptions
285 this.completeOperation(operationAttempt, appcResponse.getStatus().Description, PolicyResult.FAILURE_EXCEPTION);
286 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
289 return PolicyResult.FAILURE_EXCEPTION;
294 this.completeOperation(operationAttempt, appcResponse.getStatus().Description, PolicyResult.SUCCESS);
295 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
298 return PolicyResult.SUCCESS;
303 this.completeOperation(operationAttempt, appcResponse.getStatus().Description, PolicyResult.FAILURE);
304 if (this.policyResult != null && this.policyResult.equals(PolicyResult.FAILURE_TIMEOUT)) {
307 return PolicyResult.FAILURE;
313 public Integer getOperationTimeout() {
317 if (this.policy == null) {
318 System.out.println("getOperationTimeout returning 0");
321 System.out.println("getOperationTimeout returning " + this.policy.getTimeout());
322 return this.policy.getTimeout();
325 public String getOperationTimeoutString(int defaultTimeout) {
326 Integer to = this.getOperationTimeout();
327 if (to == null || to == 0) {
328 return Integer.toString(defaultTimeout) + "s";
330 return to.toString() + "s";
333 public PolicyResult getOperationResult() {
334 return this.policyResult;
337 public String getOperationMessage() {
338 if (this.currentOperation != null && this.currentOperation.operation != null) {
339 return this.currentOperation.operation.toMessage();
341 if (this.operationHistory != null && this.operationHistory.size() > 0) {
342 return this.operationHistory.getLast().operation.toMessage();
347 public String getOperationMessage(String guardResult) {
348 if (this.currentOperation != null && this.currentOperation.operation != null) {
349 return this.currentOperation.operation.toMessage()+ ", Guard result: " + guardResult;
351 if (this.operationHistory != null && this.operationHistory.size() > 0) {
352 return this.operationHistory.getLast().operation.toMessage() + ", Guard result: " + guardResult;
357 public String getOperationHistory() {
358 if (this.currentOperation != null && this.currentOperation.operation != null) {
359 return this.currentOperation.operation.toHistory();
361 if (this.operationHistory != null && this.operationHistory.size() > 0) {
362 return this.operationHistory.getLast().operation.toHistory();
367 public LinkedList<ControlLoopOperation> getHistory() {
368 LinkedList<ControlLoopOperation> history = new LinkedList<ControlLoopOperation>();
369 for (Operation op : this.operationHistory) {
370 history.add(new ControlLoopOperation(op.operation));
376 public void setOperationHasTimedOut() {
380 this.completeOperation(this.attempts, "Operation timed out", PolicyResult.FAILURE_TIMEOUT);
383 public void setOperationHasGuardDeny() {
387 this.completeOperation(this.attempts, "Operation denied by Guard", PolicyResult.FAILURE_GUARD);
390 public boolean isOperationComplete() {
392 // Is there currently a result?
394 if (this.policyResult == null) {
396 // either we are in process or we
402 // We have some result, check if the operation failed
404 if (this.policyResult.equals(PolicyResult.FAILURE)) {
406 // Check if there were no retries specified
408 if (policy.getRetry() == null || policy.getRetry() == 0) {
410 // The result is the failure
417 if (this.isRetriesMaxedOut()) {
419 // No more attempts allowed, reset
420 // that our actual result is failure due to retries
422 this.policyResult = PolicyResult.FAILURE_RETRIES;
426 // There are more attempts available to try the
433 // Other results mean we are done
438 public boolean isOperationRunning() {
439 return (this.currentOperation != null);
442 private boolean isRetriesMaxedOut() {
443 if (policy.getRetry() == null || policy.getRetry() == 0) {
445 // There were NO retries specified, so declare
446 // this as completed.
448 return (this.attempts > 0);
450 return (this.attempts > policy.getRetry());
453 private void storeOperationInDataBase(){
457 em = Persistence.createEntityManagerFactory("OperationsHistoryPU").createEntityManager();//emf.createEntityManager();
459 logger.error("storeOperationInDataBase threw: ", e);
463 OperationsHistoryDbEntry newEntry = new OperationsHistoryDbEntry();
465 newEntry.closedLoopName = this.onset.closedLoopControlName;
466 newEntry.requestId = this.onset.requestID.toString();
467 newEntry.actor = this.currentOperation.operation.actor;
468 newEntry.operation = this.currentOperation.operation.operation;
469 newEntry.target = this.eventManager.getTargetInstance(this.policy);
470 newEntry.starttime = Timestamp.from(this.currentOperation.operation.start);
471 newEntry.subrequestId = this.currentOperation.operation.subRequestId;
472 newEntry.endtime = new Timestamp(this.currentOperation.operation.end.toEpochMilli());
473 newEntry.message = this.currentOperation.operation.message;
474 newEntry.outcome = this.currentOperation.operation.outcome;
476 em.getTransaction().begin();
477 em.persist(newEntry);
478 em.getTransaction().commit();
486 private void completeOperation(Integer attempt, String message, PolicyResult result) {
487 if (attempt == null) {
488 System.out.println("attempt cannot be null (i.e. subRequestID)");
491 if (this.currentOperation != null) {
492 if (this.currentOperation.attempt == attempt.intValue()) {
493 this.currentOperation.operation.end = Instant.now();
494 this.currentOperation.operation.message = message;
495 this.currentOperation.operation.outcome = result.toString();
496 this.currentOperation.policyResult = result;
498 // Save it in history
500 this.operationHistory.add(this.currentOperation);
501 this.storeOperationInDataBase();
503 // Set our last result
505 this.policyResult = result;
507 // Clear the current operation field
509 this.currentOperation = null;
512 System.out.println("not current");
514 for (Operation op : this.operationHistory) {
515 if (op.attempt == attempt.intValue()) {
516 op.operation.end = Instant.now();
517 op.operation.message = message;
518 op.operation.outcome = result.toString();
519 op.policyResult = result;
523 System.out.println("Could not find associated operation");