Rework some sonar critical and major bugs
[clamp.git] / src / main / java / org / onap / clamp / clds / model / prop / PolicyChain.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.model.prop;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import com.fasterxml.jackson.databind.JsonNode;
33
34 /**
35  * Parse Policy json properties.
36  *
37  * Example json:
38  * {"Policy_1e33tn8":{"PolicyTest1":[{"name":"pname","value":"PolicyTest1"},{
39  * "name":"pid","value":"1"},{"name":"timeout","value":"345"},{
40  * "policyConfigurations":[[{"name":"recipe","value":["restart"]},{"name":
41  * "maxRetries","value":["3"]},{"name":"retryTimeLimit","value":["180"]},{"name"
42  * :"_id","value":["q2JmHD5"]},{"name":"parentPolicy","value":[""]}],[{"name":
43  * "recipe","value":["rebuild"]},{"name":"maxRetries","value":["3"]},{"name":
44  * "retryTimeLimit","value":["180"]},{"name":"_id","value":["0ZqHdrR"]},{"name":
45  * "parentPolicy","value":[""]}]]}],"PolicyTest2":[{"name":"pname","value":
46  * "PolicyTest2"},{"name":"pid","value":"2"},{"name":"timeout","value":"345"},{
47  * "policyConfigurations":[[{"name":"recipe","value":["restart"]},{"name":
48  * "maxRetries","value":["3"]},{"name":"retryTimeLimit","value":["180"]},{"name"
49  * :"_id","value":["q2JmHD5"]},{"name":"parentPolicy","value":[""]}],[{"name":
50  * "recipe","value":["rebuild"]},{"name":"maxRetries","value":["3"]},{"name":
51  * "retryTimeLimit","value":["180"]},{"name":"_id","value":["0ZqHdrR"]},{"name":
52  * "parentPolicy","value":[""]}]]}]}} f
53  *
54  */
55 public class PolicyChain {
56
57     protected static final EELFLogger       logger      = EELFManager.getInstance().getLogger(PolicyChain.class);
58     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
59
60     private String                  policyId;
61     private Integer                 timeout;
62     private List<PolicyItem>        policyItems;
63
64     public PolicyChain(JsonNode node) {
65
66         policyId = AbstractModelElement.getValueByName(node, "pid");
67         timeout = AbstractModelElement.getIntValueByName(node, "timeout");
68
69         // process policy configurations
70         JsonNode policyNode = node.get(node.size() - 1).get("policyConfigurations");
71         Iterator<JsonNode> itr = policyNode.elements();
72         policyItems = new ArrayList<>();
73         while (itr.hasNext()) {
74             policyItems.add(new PolicyItem(itr.next()));
75         }
76     }
77
78     /**
79      * @return the policyId
80      */
81     public String getPolicyId() {
82         return policyId;
83     }
84
85     /**
86      * @return the timeout
87      */
88     public Integer getTimeout() {
89         return timeout;
90     }
91
92     /**
93      * @return the policyItems
94      */
95     public List<PolicyItem> getPolicyItems() {
96         return policyItems;
97     }
98
99 }