Merge "SDNR Actor enhancements"
[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 ControlLoopResponse controlLoopResponse;
47
48     /**
49      * Copy constructor.
50      *
51      * @param source source object from which to copy
52      */
53     public OperationOutcome(OperationOutcome source) {
54         this.actor = source.actor;
55         this.operation = source.operation;
56         this.target = source.target;
57         this.start = source.start;
58         this.end = source.end;
59         this.subRequestId = source.subRequestId;
60         this.result = source.result;
61         this.message = source.message;
62         this.finalOutcome = source.finalOutcome;
63         this.controlLoopResponse = source.controlLoopResponse;
64     }
65
66     /**
67      * Creates a {@link ControlLoopOperation}, populating all fields with the values from
68      * this object. Sets the outcome field to the string representation of this object's
69      * outcome.
70      *
71      * @return
72      */
73     public ControlLoopOperation toControlLoopOperation() {
74         ControlLoopOperation clo = new ControlLoopOperation();
75
76         clo.setActor(actor);
77         clo.setOperation(operation);
78         clo.setTarget(target);
79         clo.setStart(start);
80         clo.setEnd(end);
81         clo.setSubRequestId(subRequestId);
82         clo.setOutcome(result.toString());
83         clo.setMessage(message);
84
85         return clo;
86     }
87
88     /**
89      * Determines if this outcome is for the given actor and operation.
90      *
91      * @param actor actor name
92      * @param operation operation name
93      * @return {@code true} if this outcome is for the given actor and operation
94      */
95     public boolean isFor(@NonNull String actor, @NonNull String operation) {
96         // do the operation check first, as it's most likely to be unique
97         return (operation.equals(this.operation) && actor.equals(this.actor));
98     }
99
100     /**
101      * Determines if an outcome is for the given actor and operation.
102      *
103      * @param outcome outcome to be examined, or {@code null}
104      * @param actor actor name
105      * @param operation operation name
106      * @return {@code true} if this outcome is for the given actor and operation,
107      *         {@code false} it is {@code null} or not for the actor/operation
108      */
109     public static boolean isFor(OperationOutcome outcome, String actor, String operation) {
110         return (outcome != null && outcome.isFor(actor, operation));
111     }
112
113     /**
114      * Sets the result.
115      *
116      * @param result new result
117      */
118     public void setResult(@NonNull PolicyResult result) {
119         this.result = result;
120     }
121 }