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