Fix upcoming checkstyle issues
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / OperationOutcome.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.actorserviceprovider;
22
23 import java.time.Instant;
24 import lombok.Data;
25 import lombok.NoArgsConstructor;
26 import lombok.NonNull;
27 import org.onap.policy.controlloop.ControlLoopOperation;
28
29 /**
30  * Outcome from an operation. Objects of this type are passed from one stage to the next.
31  */
32 @Data
33 @NoArgsConstructor
34 public class OperationOutcome {
35     private String actor;
36     private String operation;
37     private String target;
38     private Instant start;
39     private Instant end;
40     private String subRequestId;
41     private OperationResult result = OperationResult.SUCCESS;
42     private String message;
43     private boolean finalOutcome;
44     private Object response;
45
46     /**
47      * Copy constructor.
48      *
49      * @param source source object from which to copy
50      */
51     public OperationOutcome(OperationOutcome source) {
52         this.actor = source.actor;
53         this.operation = source.operation;
54         this.target = source.target;
55         this.start = source.start;
56         this.end = source.end;
57         this.subRequestId = source.subRequestId;
58         this.result = source.result;
59         this.message = source.message;
60         this.finalOutcome = source.finalOutcome;
61         this.response = source.response;
62     }
63
64     /**
65      * Creates a {@link ControlLoopOperation}, populating all fields with the values from
66      * this object. Sets the outcome field to the string representation of this object's
67      * outcome.
68      *
69      * @return ControlLoopOperation
70      */
71     public ControlLoopOperation toControlLoopOperation() {
72         ControlLoopOperation clo = new ControlLoopOperation();
73
74         clo.setActor(actor);
75         clo.setOperation(operation);
76         clo.setTarget(target);
77         clo.setStart(start);
78         clo.setEnd(end);
79         clo.setSubRequestId(subRequestId);
80         clo.setOutcome(result.toString());
81         clo.setMessage(message);
82
83         return clo;
84     }
85
86     @SuppressWarnings("unchecked")
87     public <T> T getResponse() {
88         return (T) response;
89     }
90
91     /**
92      * Determines if this outcome is for the given actor and operation.
93      *
94      * @param actor actor name
95      * @param operation operation name
96      * @return {@code true} if this outcome is for the given actor and operation
97      */
98     public boolean isFor(@NonNull String actor, @NonNull String operation) {
99         // do the operation check first, as it's most likely to be unique
100         return (operation.equals(this.operation) && actor.equals(this.actor));
101     }
102
103     /**
104      * Determines if an outcome is for the given actor and operation.
105      *
106      * @param outcome outcome to be examined, or {@code null}
107      * @param actor actor name
108      * @param operation operation name
109      * @return {@code true} if this outcome is for the given actor and operation,
110      *         {@code false} it is {@code null} or not for the actor/operation
111      */
112     public static boolean isFor(OperationOutcome outcome, String actor, String operation) {
113         return (outcome != null && outcome.isFor(actor, operation));
114     }
115
116     /**
117      * Sets the result.
118      *
119      * @param result new result
120      */
121     public void setResult(@NonNull OperationResult result) {
122         this.result = result;
123     }
124 }