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