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