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