Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-ControlloopPolicy / src / main / java / org / openecomp / policy / controlloop / policy / builder / ControlLoopPolicyBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.policy.controlloop.policy.builder;
22
23 import java.util.Map;
24
25 import org.openecomp.policy.asdc.Resource;
26 import org.openecomp.policy.asdc.Service;
27 import org.openecomp.policy.controlloop.policy.ControlLoop;
28 import org.openecomp.policy.controlloop.policy.OperationsAccumulateParams;
29 import org.openecomp.policy.controlloop.policy.Policy;
30 import org.openecomp.policy.controlloop.policy.PolicyResult;
31 import org.openecomp.policy.controlloop.policy.Target;
32 import org.openecomp.policy.controlloop.policy.builder.impl.ControlLoopPolicyBuilderImpl;
33
34 public interface ControlLoopPolicyBuilder {
35         
36         /**
37          * Adds one or more services to the ControlLoop
38          * 
39          * 
40          * @param service
41          * @return
42          * @throws BuilderException
43          */
44         public ControlLoopPolicyBuilder addService(Service... services) throws BuilderException;
45         
46         /**
47          * @param services
48          * @return
49          * @throws BuilderException
50          */
51         public ControlLoopPolicyBuilder removeService(Service... services) throws BuilderException;
52         
53         /**
54          * @return
55          * @throws BuilderException
56          */
57         public ControlLoopPolicyBuilder removeAllServices() throws BuilderException;
58         
59         /**
60          * Adds one or more resources to the ControlLoop
61          * 
62          * 
63          * @param resource
64          * @return
65          * @throws BuilderException
66          */
67         public ControlLoopPolicyBuilder addResource(Resource... resources) throws BuilderException;
68         
69         /**
70          * @param resources
71          * @return
72          * @throws BuilderException
73          */
74         public ControlLoopPolicyBuilder removeResource(Resource... resources) throws BuilderException;
75         
76         /**
77          * @return
78          * @throws BuilderException
79          */
80         public ControlLoopPolicyBuilder removeAllResources() throws BuilderException;
81         
82         /**
83          *  @param abatement
84          *  @return
85          *  @throws BuilderException
86          */
87         public ControlLoopPolicyBuilder setAbatement(Boolean abatement) throws BuilderException;
88         
89         
90         /**
91          * Sets the overall timeout value for the Control Loop. If any operational policies have retries and timeouts,
92          * then this overall timeout value should exceed all those values.
93          * 
94          * @param timeout
95          * @return
96          * @throws BuilderException
97          */
98         public ControlLoopPolicyBuilder setTimeout(Integer timeout) throws BuilderException;
99         
100         /**
101          * Scans the operational policies and calculate an minimum overall timeout for the Control Loop.
102          * 
103          * 
104          * @return Integer
105          */
106         public Integer calculateTimeout();
107         
108         /**
109          * Sets the initial trigger policy when a DCAE Closed Loop Event arrives in the ECOMP Policy Platform.
110          * 
111          * 
112          * @param name
113          * @param description
114          * @param actor
115          * @param target
116          * @param recipe
117          * @param retries
118          * @param timeout
119          * @return Policy
120          * @throws BuilderException
121          */
122         public Policy setTriggerPolicy(String name, String description, String actor, Target target, String recipe, Map<String, String> payload, Integer retries, Integer timeout) throws BuilderException;
123         
124         /**
125          * 
126          * Changes the trigger policy to point to another existing Policy.
127          * 
128          * 
129          * @param id
130          * @return ControlLoop
131          * @throws BuilderException
132          */
133         public ControlLoop      setTriggerPolicy(String id) throws BuilderException;
134         
135         /**
136          * @return
137          */
138         public boolean  isOpenLoop();
139         
140         /**
141          * @return
142          * @throws BuilderException
143          */
144         public Policy   getTriggerPolicy() throws BuilderException;
145         
146         /**
147          * Simply returns a copy of the ControlLoop information.
148          * 
149          * 
150          * @return ControlLoop
151          */
152         public ControlLoop      getControlLoop();
153         
154         /**
155          * Creates a policy that is chained to the result of another Policy.
156          * 
157          * 
158          * @param name
159          * @param description
160          * @param actor
161          * @param target
162          * @param recipe
163          * @param retries
164          * @param timeout
165          * @param policyID
166          * @param results
167          * @return
168          * @throws BuilderException
169          */
170         public Policy setPolicyForPolicyResult(String name, String description, String actor,
171                         Target target, String recipe, Map<String, String> payload, Integer retries, Integer timeout, String policyID, PolicyResult... results) throws BuilderException;
172         
173         
174         /**
175          * Sets the policy result(s) to an existing Operational Policy.
176          * 
177          * 
178          * @param policyResultID
179          * @param policyID
180          * @param results
181          * @return
182          * @throws BuilderException
183          */
184         public Policy setPolicyForPolicyResult(String policyResultID, String policyID, PolicyResult... results) throws BuilderException;
185         
186         /**
187          * Removes an Operational Policy. Be mindful that if any other Operational Policies have results that point to this policy, any
188          * policies that have results pointing to this policy will have their result reset to the appropriate default FINAL_* result.
189          * 
190          * 
191          * @param policyID
192          * @return
193          * @throws BuilderException
194          */
195         public boolean removePolicy(String policyID) throws BuilderException;
196         
197         /**
198          * Resets a policy's results to defualt FINAL_* codes.
199          * 
200          * 
201          * @return Policy
202          * @throws BuilderException - Policy does not exist
203          */
204         public Policy   resetPolicyResults(String policyID) throws BuilderException;
205         
206         /**
207          * Removes all existing Operational Policies and reverts back to an Open Loop.
208          * 
209          * @return
210          */
211         public ControlLoopPolicyBuilder removeAllPolicies();
212                 
213         /**
214          * Adds an operationsAccumulateParams to an existing operational policy
215          * 
216          * @return Policy
217          * @throws BuilderException - Policy does not exist
218          */
219         public Policy addOperationsAccumulateParams(String policyID, OperationsAccumulateParams operationsAccumulateParams) throws BuilderException;
220         
221         /**
222          * This will compile and build the YAML specification for the Control Loop Policy. Please iterate the Results object for details.
223          * The Results object will contains warnings and errors. If the specification compiled successfully, you will be able to retrieve the
224          * YAML.
225          * 
226          * @return Results
227          */
228         public Results  buildSpecification();
229         
230         /**
231          * The Factory is used to build a ControlLoopPolicyBuilder implementation.
232          * 
233          * @author pameladragosh
234          *
235          */
236         public static class Factory {
237                 private Factory(){
238                         // Private Constructor.
239                 }
240                 
241                 /**
242                  * Builds a basic Control Loop with an overall timeout. Use this method if you wish to create an OpenLoop, or if you 
243                  * want to interactively build a Closed Loop.
244                  * 
245                  * @param controlLoopName - Per Closed Loop AID v1.0, unique string for the closed loop.
246                  * @param timeout - Overall timeout for the Closed Loop to execute.
247                  * @return ControlLoopPolicyBuilder object
248                  * @throws BuilderException
249                  */
250                 public static ControlLoopPolicyBuilder  buildControlLoop (String controlLoopName, Integer timeout) throws BuilderException {
251                         return new ControlLoopPolicyBuilderImpl(controlLoopName, timeout);
252                 }
253                 
254                 /**
255                  * Build a Control Loop for a resource and services associated with the resource.
256                  * 
257                  * @param controlLoopName - Per Closed Loop AID v1.0, unique string for the closed loop.
258                  * @param timeout - Overall timeout for the Closed Loop to execute.
259                  * @param resource - Resource this closed loop is for. Should come from ASDC, but if not available use resourceName to distinguish.
260                  * @param services - Zero or more services associated with this resource. Should come from ASDC, but if not available use serviceName to distinguish.
261                  * @return ControlLoopPolicyBuilder object
262                  * @throws BuilderException
263                  */
264                 public static ControlLoopPolicyBuilder  buildControlLoop (String controlLoopName, Integer timeout, Resource resource, Service... services) throws BuilderException {
265                         
266                         ControlLoopPolicyBuilder builder = new ControlLoopPolicyBuilderImpl(controlLoopName, timeout, resource, services);
267                         
268                         return builder;
269                 }
270                 
271                 /**
272                  * @param controlLoopName
273                  * @param timeout
274                  * @param service
275                  * @param resources
276                  * @return
277                  * @throws BuilderException
278                  */
279                 public static ControlLoopPolicyBuilder  buildControlLoop (String controlLoopName, Integer timeout, Service service, Resource... resources) throws BuilderException {
280                         
281                         ControlLoopPolicyBuilder builder = new ControlLoopPolicyBuilderImpl(controlLoopName, timeout, service, resources);
282                         
283                         return builder;
284                 }
285         }
286
287 }