Fix Checkstyle issues
[clamp.git] / src / main / java / org / onap / clamp / clds / model / properties / PolicyItem.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.model.properties;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import com.google.gson.JsonElement;
30 import com.google.gson.reflect.TypeToken;
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.onap.clamp.clds.util.JsonUtils;
36 import org.yaml.snakeyaml.Yaml;
37
38 /**
39  * Parse policyConfigurations from Policy json properties.
40  * Example json:
41  * "Policy_005sny1":[[{"name":"timeout","value":"5"}],{"policyConfigurations":[[
42  * {"name":"recipe","value":["restart"]},{"name":"maxRetries","value":["3"]},{
43  * "name":"retryTimeLimit","value":["180"]},{"name":"_id","value":["vf3RtPi"]},{
44  * "name":"location","value":["san_diego"]},{"name":"resource","value":["vCTS"]}
45  * ,{"name":"onMaxRetriesLimit","value":[""]},{"name":"onTimeLimit","value":[""]
46  * },{"name":"onOtherFailure","value":[""]},{"name":"policy_parent","value":[""]
47  * }],[{"name":"recipe","value":["rebuild"]},{"name":"maxRetries","value":["3"]}
48  * ,{"name":"retryTimeLimit","value":["180"]},{"name":"_id","value":["89z8Ncl"]}
49  * ,{"name":"location","value":["san_diego"]},{"name":"resource","value":["vCTS"
50  * ]},{"name":"onMaxRetriesLimit","value":[""]},{"name":"onTimeLimit","value":[
51  * ""]},{"name":"onOtherFailure","value":[""]},{"name":"policy_parent","value":[
52  * "vf3RtPi"]},{"name":
53  * "targetResourceId","value":["Eace933104d443b496b8.nodes.heat.vpg"]}]]}]
54  */
55 public class PolicyItem implements Cloneable {
56     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyItem.class);
57     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
58
59     private String id;
60     private String recipe;
61     private int maxRetries;
62     private int retryTimeLimit;
63     private String parentPolicy;
64     private List<String> parentPolicyConditions;
65     private String actor;
66     private String targetResourceId;
67     private String recipeInfo;
68     private String recipeLevel;
69     private String recipeInput;
70     private Map<String, String> recipePayload;
71     private String oapRop;
72     private String oapLimit;
73
74     private String enableGuardPolicy;
75     private String guardPolicyType;
76     private String guardTargets;
77     private String minGuard;
78     private String maxGuard;
79     private String limitGuard;
80     private String timeUnitsGuard;
81     private String timeWindowGuard;
82     private String guardActiveStart;
83     private String guardActiveEnd;
84
85     /**
86      * Parse Policy given JSON node.
87      *
88      * @param item policy in JSON format
89      * @throws IOException IO exception
90      */
91     public PolicyItem(JsonElement item) throws IOException {
92         id = JsonUtils.getStringValueByName(item, "_id");
93         recipe = JsonUtils.getStringValueByName(item, "recipe");
94         maxRetries = JsonUtils.getIntValueByName(item, "maxRetries");
95         retryTimeLimit = JsonUtils.getIntValueByName(item, "retryTimeLimit");
96         parentPolicy = JsonUtils.getStringValueByName(item, "parentPolicy");
97         parentPolicyConditions = JsonUtils.getStringValuesByName(item, "parentPolicyConditions");
98         targetResourceId = JsonUtils.getStringValueByName(item, "targetResourceId");
99         if (targetResourceId != null && targetResourceId.isEmpty()) {
100             this.setTargetResourceId(null);
101         }
102         recipeInfo = JsonUtils.getStringValueByName(item, "recipeInfo");
103         recipeLevel = JsonUtils.getStringValueByName(item, "recipeLevel");
104         recipeInput = JsonUtils.getStringValueByName(item, "recipeInput");
105         String payload = JsonUtils.getStringValueByName(item, "recipePayload");
106
107         if (payload != null && !payload.isEmpty()) {
108             if (payload.trim().startsWith("{") && payload.trim().endsWith("}")) {
109                 // Seems to be a JSON
110                 recipePayload = JsonUtils.GSON.fromJson(payload, new TypeToken<Map<String, String>>() {}.getType());
111             } else {
112                 // SHould be a YAML then
113                 recipePayload = new Yaml().load(payload);
114             }
115         }
116         oapRop = JsonUtils.getStringValueByName(item, "oapRop");
117         oapLimit = JsonUtils.getStringValueByName(item, "oapLimit");
118         actor = JsonUtils.getStringValueByName(item, "actor");
119
120         enableGuardPolicy = JsonUtils.getStringValueByName(item, "enableGuardPolicy");
121         guardPolicyType = JsonUtils.getStringValueByName(item, "guardPolicyType");
122         guardTargets = JsonUtils.getStringValueByName(item, "guardTargets");
123         minGuard = JsonUtils.getStringValueByName(item, "minGuard");
124         maxGuard = JsonUtils.getStringValueByName(item, "maxGuard");
125         limitGuard = JsonUtils.getStringValueByName(item, "limitGuard");
126         timeUnitsGuard = JsonUtils.getStringValueByName(item, "timeUnitsGuard");
127         timeWindowGuard = JsonUtils.getStringValueByName(item, "timeWindowGuard");
128         guardActiveStart = JsonUtils.getStringValueByName(item, "guardActiveStart");
129         guardActiveEnd = JsonUtils.getStringValueByName(item, "guardActiveEnd");
130     }
131
132     /**
133      * Get the id.
134      * @return the id
135      */
136     public String getId() {
137         return id;
138     }
139
140     /**
141      * Get the recipe.
142      * @return the recipe
143      */
144     public String getRecipe() {
145         return recipe;
146     }
147
148     /**
149      * Set the id.
150      * @param the id
151      */
152     public void setId(String id) {
153         this.id = id;
154     }
155
156     /**
157      * Set the recipe.
158      * @param the recipe
159      */
160     public void setRecipe(String recipe) {
161         this.recipe = recipe;
162     }
163
164     /**
165      * Set the parent policy.
166      * @param the parentPolicy
167      */
168     public void setParentPolicy(String parentPolicy) {
169         this.parentPolicy = parentPolicy;
170     }
171
172     /**
173      * Set the max retries.
174      * @param the maxRetries
175      */
176     public void setMaxRetries(int maxRetries) {
177         this.maxRetries = maxRetries;
178     }
179
180     /**
181      * Set the retry time limit.
182      * @param the retryTimeLimit
183      */
184     public void setRetryTimeLimit(int retryTimeLimit) {
185         this.retryTimeLimit = retryTimeLimit;
186     }
187
188     /**
189      * Set the parent policy conditions.
190      * @param the parentPolicyConditions
191      */
192     public void setParentPolicyConditions(List<String> parentPolicyConditions) {
193         this.parentPolicyConditions = parentPolicyConditions;
194     }
195
196     /**
197      * Get the max retires.
198      * @return the maxRetries
199      */
200     public int getMaxRetries() {
201         return maxRetries;
202     }
203
204     /**
205      * Get the retry time limit.
206      * @return the retryTimeLimit
207      */
208     public int getRetryTimeLimit() {
209         return retryTimeLimit;
210     }
211
212     /**
213      * Get the parent policy.
214      * @return the parentPolicy
215      */
216     public String getParentPolicy() {
217         return parentPolicy;
218     }
219
220     /**
221      * Get the list of parent policy conditions.
222      * @return the parentPolicyConditions
223      */
224     public List<String> getParentPolicyConditions() {
225         return parentPolicyConditions;
226     }
227
228     /**
229      * Get the actor.
230      * @return the actor
231      */
232     public String getActor() {
233         return actor;
234     }
235
236     /**
237      * Set the actor.
238      * @param the actor
239      */
240     public void setActor(String actor) {
241         this.actor = actor;
242     }
243
244     public String getTargetResourceId() {
245         return targetResourceId;
246     }
247
248     public void setTargetResourceId(String targetResourceId) {
249         this.targetResourceId = targetResourceId;
250     }
251
252     public String getRecipeInfo() {
253         return recipeInfo;
254     }
255
256     public String getRecipeLevel() {
257         return recipeLevel;
258     }
259
260     public String getRecipeInput() {
261         return recipeInput;
262     }
263
264     public Map<String, String> getRecipePayload() {
265         return recipePayload;
266     }
267
268     /**
269      * Get oap rop.
270      * @return The oap rop?
271      */
272     public String getOapRop() {
273         if (oapRop == null) {
274             oapRop = "0m";
275         }
276         return oapRop;
277     }
278
279     /**
280      * Get oap limit.
281      * @return the oap limit
282      */
283     public String getOapLimit() {
284         if (oapLimit == null) {
285             oapLimit = "0";
286         }
287         return oapLimit;
288     }
289
290     public String getEnableGuardPolicy() {
291         return enableGuardPolicy;
292     }
293
294     public String getGuardPolicyType() {
295         return guardPolicyType;
296     }
297
298     public String getGuardTargets() {
299         return guardTargets;
300     }
301
302     public String getMinGuard() {
303         return minGuard;
304     }
305
306     public String getMaxGuard() {
307         return maxGuard;
308     }
309
310     public String getLimitGuard() {
311         return limitGuard;
312     }
313
314     public String getTimeUnitsGuard() {
315         return timeUnitsGuard;
316     }
317
318     public String getTimeWindowGuard() {
319         return timeWindowGuard;
320     }
321
322     public String getGuardActiveStart() {
323         return guardActiveStart;
324     }
325
326     public String getGuardActiveEnd() {
327         return guardActiveEnd;
328     }
329
330 }