Fix Payload in policy
[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 import com.fasterxml.jackson.core.type.TypeReference;
29 import com.fasterxml.jackson.databind.JsonNode;
30
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.onap.clamp.clds.util.JacksonUtils;
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 node
90      * @throws IOException
91      */
92     public PolicyItem(JsonNode node) throws IOException {
93         id = AbstractModelElement.getValueByName(node, "_id");
94         recipe = AbstractModelElement.getValueByName(node, "recipe");
95         maxRetries = AbstractModelElement.getIntValueByName(node, "maxRetries");
96         retryTimeLimit = AbstractModelElement.getIntValueByName(node, "retryTimeLimit");
97         parentPolicy = AbstractModelElement.getValueByName(node, "parentPolicy");
98         parentPolicyConditions = AbstractModelElement.getValuesByName(node, "parentPolicyConditions");
99         targetResourceId = AbstractModelElement.getValueByName(node, "targetResourceId");
100         if (targetResourceId != null && targetResourceId.isEmpty()) {
101             this.setTargetResourceId(null);
102         }
103         recipeInfo = AbstractModelElement.getValueByName(node, "recipeInfo");
104         recipeLevel = AbstractModelElement.getValueByName(node, "recipeLevel");
105         recipeInput = AbstractModelElement.getValueByName(node, "recipeInput");
106         String payload = AbstractModelElement.getValueByName(node, "recipePayload");
107
108         if (payload != null && !payload.isEmpty()) {
109             if (payload.trim().startsWith("{") && payload.trim().endsWith("}")) {
110                 // Seems to be a JSON
111                 recipePayload = JacksonUtils.getObjectMapperInstance().readValue(payload,
112                     new TypeReference<Map<String, String>>() {
113                     });
114             } else {
115                 // SHould be a YAML then
116                 Yaml yaml = new Yaml();
117                 recipePayload = (Map<String, String>) yaml.load(payload);
118             }
119         }
120         oapRop = AbstractModelElement.getValueByName(node, "oapRop");
121         oapLimit = AbstractModelElement.getValueByName(node, "oapLimit");
122         actor = AbstractModelElement.getValueByName(node, "actor");
123
124         enableGuardPolicy = AbstractModelElement.getValueByName(node, "enableGuardPolicy");
125         guardPolicyType = AbstractModelElement.getValueByName(node, "guardPolicyType");
126         guardTargets = AbstractModelElement.getValueByName(node, "guardTargets");
127         minGuard = AbstractModelElement.getValueByName(node, "minGuard");
128         maxGuard = AbstractModelElement.getValueByName(node, "maxGuard");
129         limitGuard = AbstractModelElement.getValueByName(node, "limitGuard");
130         timeUnitsGuard = AbstractModelElement.getValueByName(node, "timeUnitsGuard");
131         timeWindowGuard = AbstractModelElement.getValueByName(node, "timeWindowGuard");
132         guardActiveStart = AbstractModelElement.getValueByName(node, "guardActiveStart");
133         guardActiveEnd = AbstractModelElement.getValueByName(node, "guardActiveEnd");
134     }
135
136     /**
137      * @return the id
138      */
139     public String getId() {
140         return id;
141     }
142
143     /**
144      * @return the recipe
145      */
146     public String getRecipe() {
147         return recipe;
148     }
149
150     /**
151      * @set the id
152      */
153     public void setId(String id) {
154         this.id = id;
155     }
156
157     /**
158      * @set the recipe
159      */
160     public void setRecipe(String recipe) {
161         this.recipe = recipe;
162     }
163
164     /**
165      * @set the parentPolicy
166      */
167     public void setParentPolicy(String parentPolicy) {
168         this.parentPolicy = parentPolicy;
169     }
170
171     /**
172      * @set the maxRetries
173      */
174     public void setMaxRetries(int maxRetries) {
175         this.maxRetries = maxRetries;
176     }
177
178     /**
179      * @set the retryTimeLimit
180      */
181     public void setRetryTimeLimit(int retryTimeLimit) {
182         this.retryTimeLimit = retryTimeLimit;
183     }
184
185     /**
186      * @set the parentPolicyConditions
187      */
188     public void setParentPolicyConditions(List<String> parentPolicyConditions) {
189         this.parentPolicyConditions = parentPolicyConditions;
190     }
191
192     /**
193      * @return the maxRetries
194      */
195     public int getMaxRetries() {
196         return maxRetries;
197     }
198
199     /**
200      * @return the retryTimeLimit
201      */
202     public int getRetryTimeLimit() {
203         return retryTimeLimit;
204     }
205
206     /**
207      * @return the parentPolicy
208      */
209     public String getParentPolicy() {
210         return parentPolicy;
211     }
212
213     /**
214      * @return the parentPolicyConditions
215      */
216     public List<String> getParentPolicyConditions() {
217         return parentPolicyConditions;
218     }
219
220     /**
221      * @return the actor
222      */
223     public String getActor() {
224         return actor;
225     }
226
227     /**
228      * @set the actor
229      */
230     public void setActor(String actor) {
231         this.actor = actor;
232     }
233
234     public String getTargetResourceId() {
235         return targetResourceId;
236     }
237
238     public void setTargetResourceId(String targetResourceId) {
239         this.targetResourceId = targetResourceId;
240     }
241
242     public String getRecipeInfo() {
243         return recipeInfo;
244     }
245
246     public String getRecipeLevel() {
247         return recipeLevel;
248     }
249
250     public String getRecipeInput() {
251         return recipeInput;
252     }
253
254     public Map<String, String> getRecipePayload() {
255         return recipePayload;
256     }
257
258     public String getOapRop() {
259         if (oapRop == null) {
260             oapRop = "0m";
261         }
262         return oapRop;
263     }
264
265     public String getOapLimit() {
266         if (oapLimit == null) {
267             oapLimit = "0";
268         }
269         return oapLimit;
270     }
271
272     public String getEnableGuardPolicy() {
273         return enableGuardPolicy;
274     }
275
276     public String getGuardPolicyType() {
277         return guardPolicyType;
278     }
279
280     public String getGuardTargets() {
281         return guardTargets;
282     }
283
284     public String getMinGuard() {
285         return minGuard;
286     }
287
288     public String getMaxGuard() {
289         return maxGuard;
290     }
291
292     public String getLimitGuard() {
293         return limitGuard;
294     }
295
296     public String getTimeUnitsGuard() {
297         return timeUnitsGuard;
298     }
299
300     public String getTimeWindowGuard() {
301         return timeWindowGuard;
302     }
303
304     public String getGuardActiveStart() {
305         return guardActiveStart;
306     }
307
308     public String getGuardActiveEnd() {
309         return guardActiveEnd;
310     }
311
312 }