f29ba160bdcdd0a4942b753a3cb640a1f4e3c8ce
[clamp.git] / src / main / java / org / onap / clamp / clds / model / properties / PolicyChain.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.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import org.onap.clamp.clds.util.JsonUtils;
35
36 /**
37  * Parse Policy json properties.
38  *
39  * Example json:
40  * {"Policy_1e33tn8":{"PolicyTest1":[{"name":"pname","value":"PolicyTest1"},{
41  * "name":"pid","value":"1"},{"name":"timeout","value":"345"},{
42  * "policyConfigurations":[[{"name":"recipe","value":["restart"]},{"name":
43  * "maxRetries","value":["3"]},{"name":"retryTimeLimit","value":["180"]},{"name"
44  * :"_id","value":["q2JmHD5"]},{"name":"parentPolicy","value":[""]}],[{"name":
45  * "recipe","value":["rebuild"]},{"name":"maxRetries","value":["3"]},{"name":
46  * "retryTimeLimit","value":["180"]},{"name":"_id","value":["0ZqHdrR"]},{"name":
47  * "parentPolicy","value":[""]},{"name":
48  * "targetResourceId","value":["Eace933104d443b496b8.nodes.heat.vpg"]}]]}],
49  * "PolicyTest2":[{"name":"pname","value":
50  * "PolicyTest2"},{"name":"pid","value":"2"},{"name":"timeout","value":"345"},{
51  * "policyConfigurations":[[{"name":"recipe","value":["restart"]},{"name":
52  * "maxRetries","value":["3"]},{"name":"retryTimeLimit","value":["180"]},{"name"
53  * :"_id","value":["q2JmHD5"]},{"name":"parentPolicy","value":[""]}],[{"name":
54  * "recipe","value":["rebuild"]},{"name":"maxRetries","value":["3"]},{"name":
55  * "retryTimeLimit","value":["180"]},{"name":"_id","value":["0ZqHdrR"]},{"name":
56  * "parentPolicy","value":[""]},{"name":
57  * "targetResourceId","value":["Eace933104d443b496b8.nodes.heat.vpg"]}]]}]}} f
58  *
59  */
60 public class PolicyChain {
61
62     protected static final EELFLogger logger      = EELFManager.getInstance().getLogger(PolicyChain.class);
63     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
64
65     private String                    policyId;
66     private Integer                   timeout;
67     private List<PolicyItem>          policyItems;
68     private String                    policyType;
69
70     public PolicyChain(JsonElement node) throws IOException {
71         if (node != null && node.isJsonArray() && node.getAsJsonArray().size() > 0) {
72             JsonArray operationalPolicyParameters = node.getAsJsonArray();
73             policyId = JsonUtils.getStringValueByName(node, "pid");
74             timeout = JsonUtils.getIntValueByName(node, "timeout");
75             policyType = JsonUtils.getStringValueByName(node, "policyType");
76
77             JsonArray policyConfigurations = operationalPolicyParameters.get(operationalPolicyParameters.size() - 1)
78                 .getAsJsonObject()
79                 .get("policyConfigurations")
80                 .getAsJsonArray();
81             Iterator<JsonElement> itr = policyConfigurations.iterator();
82             policyItems = new ArrayList<>();
83             while (itr.hasNext()) {
84                 policyItems.add(new PolicyItem(itr.next()));
85
86             }
87         }
88     }
89     /**
90      * @return the policyId
91      */
92     public String getPolicyId() {
93         return policyId;
94     }
95
96     /**
97      * @return the timeout
98      */
99     public Integer getTimeout() {
100         return timeout;
101     }
102
103     /**
104      * @return the policyItems
105      */
106     public List<PolicyItem> getPolicyItems() {
107         return policyItems;
108     }
109
110     /**
111      * @return the policyType
112      */
113     public String getPolicyType() {
114         return policyType;
115     }
116
117 }