Merge "Replace jackson usages with GSON"
[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  * <p>
41  * Example json:
42  * "Policy_005sny1":[[{"name":"timeout","value":"5"}],{"policyConfigurations":[[
43  * {"name":"recipe","value":["restart"]},{"name":"maxRetries","value":["3"]},{
44  * "name":"retryTimeLimit","value":["180"]},{"name":"_id","value":["vf3RtPi"]},{
45  * "name":"location","value":["san_diego"]},{"name":"resource","value":["vCTS"]}
46  * ,{"name":"onMaxRetriesLimit","value":[""]},{"name":"onTimeLimit","value":[""]
47  * },{"name":"onOtherFailure","value":[""]},{"name":"policy_parent","value":[""]
48  * }],[{"name":"recipe","value":["rebuild"]},{"name":"maxRetries","value":["3"]}
49  * ,{"name":"retryTimeLimit","value":["180"]},{"name":"_id","value":["89z8Ncl"]}
50  * ,{"name":"location","value":["san_diego"]},{"name":"resource","value":["vCTS"
51  * ]},{"name":"onMaxRetriesLimit","value":[""]},{"name":"onTimeLimit","value":[
52  * ""]},{"name":"onOtherFailure","value":[""]},{"name":"policy_parent","value":[
53  * "vf3RtPi"]},{"name":
54  * "targetResourceId","value":["Eace933104d443b496b8.nodes.heat.vpg"]}]]}]
55  */
56 public class PolicyItem implements Cloneable {
57     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(PolicyItem.class);
58     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
59
60     private String id;
61     private String recipe;
62     private int maxRetries;
63     private int retryTimeLimit;
64     private String parentPolicy;
65     private List<String> parentPolicyConditions;
66     private String actor;
67     private String targetResourceId;
68     private String recipeInfo;
69     private String recipeLevel;
70     private String recipeInput;
71     private Map<String, String> recipePayload;
72     private String oapRop;
73     private String oapLimit;
74
75     private String enableGuardPolicy;
76     private String guardPolicyType;
77     private String guardTargets;
78     private String minGuard;
79     private String maxGuard;
80     private String limitGuard;
81     private String timeUnitsGuard;
82     private String timeWindowGuard;
83     private String guardActiveStart;
84     private String guardActiveEnd;
85
86     /**
87      * Parse Policy given json node.
88      *
89      * @param item
90      * @throws IOException
91      */
92     public PolicyItem(JsonElement item) throws IOException {
93         id = JsonUtils.getStringValueByName(item, "_id");
94         recipe = JsonUtils.getStringValueByName(item, "recipe");
95         maxRetries = JsonUtils.getIntValueByName(item, "maxRetries");
96         retryTimeLimit = JsonUtils.getIntValueByName(item, "retryTimeLimit");
97         parentPolicy = JsonUtils.getStringValueByName(item, "parentPolicy");
98         parentPolicyConditions = JsonUtils.getStringValuesByName(item, "parentPolicyConditions");
99         targetResourceId = JsonUtils.getStringValueByName(item, "targetResourceId");
100         if (targetResourceId != null && targetResourceId.isEmpty()) {
101             this.setTargetResourceId(null);
102         }
103         recipeInfo = JsonUtils.getStringValueByName(item, "recipeInfo");
104         recipeLevel = JsonUtils.getStringValueByName(item, "recipeLevel");
105         recipeInput = JsonUtils.getStringValueByName(item, "recipeInput");
106         String payload = JsonUtils.getStringValueByName(item, "recipePayload");
107
108         if (payload != null && !payload.isEmpty()) {
109             if (payload.trim().startsWith("{") && payload.trim().endsWith("}")) {
110                 // Seems to be a JSON
111                 recipePayload = JsonUtils.GSON.fromJson(payload, new TypeToken<Map<String, String>>() {}.getType());
112             } else {
113                 // SHould be a YAML then
114                 recipePayload = new Yaml().load(payload);
115             }
116         }
117         oapRop = JsonUtils.getStringValueByName(item, "oapRop");
118         oapLimit = JsonUtils.getStringValueByName(item, "oapLimit");
119         actor = JsonUtils.getStringValueByName(item, "actor");
120
121         enableGuardPolicy = JsonUtils.getStringValueByName(item, "enableGuardPolicy");
122         guardPolicyType = JsonUtils.getStringValueByName(item, "guardPolicyType");
123         guardTargets = JsonUtils.getStringValueByName(item, "guardTargets");
124         minGuard = JsonUtils.getStringValueByName(item, "minGuard");
125         maxGuard = JsonUtils.getStringValueByName(item, "maxGuard");
126         limitGuard = JsonUtils.getStringValueByName(item, "limitGuard");
127         timeUnitsGuard = JsonUtils.getStringValueByName(item, "timeUnitsGuard");
128         timeWindowGuard = JsonUtils.getStringValueByName(item, "timeWindowGuard");
129         guardActiveStart = JsonUtils.getStringValueByName(item, "guardActiveStart");
130         guardActiveEnd = JsonUtils.getStringValueByName(item, "guardActiveEnd");
131     }
132
133     /**
134      * @return the id
135      */
136     public String getId() {
137         return id;
138     }
139
140     /**
141      * @return the recipe
142      */
143     public String getRecipe() {
144         return recipe;
145     }
146
147     /**
148      * @set the id
149      */
150     public void setId(String id) {
151         this.id = id;
152     }
153
154     /**
155      * @set the recipe
156      */
157     public void setRecipe(String recipe) {
158         this.recipe = recipe;
159     }
160
161     /**
162      * @set the parentPolicy
163      */
164     public void setParentPolicy(String parentPolicy) {
165         this.parentPolicy = parentPolicy;
166     }
167
168     /**
169      * @set the maxRetries
170      */
171     public void setMaxRetries(int maxRetries) {
172         this.maxRetries = maxRetries;
173     }
174
175     /**
176      * @set the retryTimeLimit
177      */
178     public void setRetryTimeLimit(int retryTimeLimit) {
179         this.retryTimeLimit = retryTimeLimit;
180     }
181
182     /**
183      * @set the parentPolicyConditions
184      */
185     public void setParentPolicyConditions(List<String> parentPolicyConditions) {
186         this.parentPolicyConditions = parentPolicyConditions;
187     }
188
189     /**
190      * @return the maxRetries
191      */
192     public int getMaxRetries() {
193         return maxRetries;
194     }
195
196     /**
197      * @return the retryTimeLimit
198      */
199     public int getRetryTimeLimit() {
200         return retryTimeLimit;
201     }
202
203     /**
204      * @return the parentPolicy
205      */
206     public String getParentPolicy() {
207         return parentPolicy;
208     }
209
210     /**
211      * @return the parentPolicyConditions
212      */
213     public List<String> getParentPolicyConditions() {
214         return parentPolicyConditions;
215     }
216
217     /**
218      * @return the actor
219      */
220     public String getActor() {
221         return actor;
222     }
223
224     /**
225      * @set the actor
226      */
227     public void setActor(String actor) {
228         this.actor = actor;
229     }
230
231     public String getTargetResourceId() {
232         return targetResourceId;
233     }
234
235     public void setTargetResourceId(String targetResourceId) {
236         this.targetResourceId = targetResourceId;
237     }
238
239     public String getRecipeInfo() {
240         return recipeInfo;
241     }
242
243     public String getRecipeLevel() {
244         return recipeLevel;
245     }
246
247     public String getRecipeInput() {
248         return recipeInput;
249     }
250
251     public Map<String, String> getRecipePayload() {
252         return recipePayload;
253     }
254
255     public String getOapRop() {
256         if (oapRop == null) {
257             oapRop = "0m";
258         }
259         return oapRop;
260     }
261
262     public String getOapLimit() {
263         if (oapLimit == null) {
264             oapLimit = "0";
265         }
266         return oapLimit;
267     }
268
269     public String getEnableGuardPolicy() {
270         return enableGuardPolicy;
271     }
272
273     public String getGuardPolicyType() {
274         return guardPolicyType;
275     }
276
277     public String getGuardTargets() {
278         return guardTargets;
279     }
280
281     public String getMinGuard() {
282         return minGuard;
283     }
284
285     public String getMaxGuard() {
286         return maxGuard;
287     }
288
289     public String getLimitGuard() {
290         return limitGuard;
291     }
292
293     public String getTimeUnitsGuard() {
294         return timeUnitsGuard;
295     }
296
297     public String getTimeWindowGuard() {
298         return timeWindowGuard;
299     }
300
301     public String getGuardActiveStart() {
302         return guardActiveStart;
303     }
304
305     public String getGuardActiveEnd() {
306         return guardActiveEnd;
307     }
308
309 }