Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / Policy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29 import org.onap.sdc.toscaparser.api.elements.Metadata;
30 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
31 import org.onap.sdc.toscaparser.api.utils.ValidateUtils;
32
33 public class Policy extends EntityTemplate {
34
35
36     static final String TYPE = "type";
37     static final String METADATA = "metadata";
38     static final String DESCRIPTION = "description";
39     static final String PROPERTIES = "properties";
40     static final String TARGETS = "targets";
41     private static final String TRIGGERS = "triggers";
42     private static final String SECTIONS[] = {
43             TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS};
44
45     Metadata metaDataObject;
46     LinkedHashMap<String, Object> metaData = null;
47     ArrayList<Object> targetsList; // *** a list of NodeTemplate OR a list of Group ***
48     String targetsType;
49     ArrayList<Object> triggers;
50     LinkedHashMap<String, Object> properties;
51
52     public Policy(String _name,
53                   LinkedHashMap<String, Object> _policy,
54                   ArrayList<Object> targetObjects,
55                   String _targetsType,
56                   LinkedHashMap<String, Object> _customDef) {
57         this(_name, _policy, targetObjects, _targetsType, _customDef, null);
58     }
59
60     public Policy(String _name,
61                   LinkedHashMap<String, Object> _policy,
62 //                                ArrayList<NodeTemplate> targetObjects,
63                   ArrayList<Object> targetObjects,
64                   String _targetsType,
65                   LinkedHashMap<String, Object> _customDef, NodeTemplate parentNodeTemplate) {
66         super(_name, _policy, "policy_type", _customDef, parentNodeTemplate);
67
68         if (_policy.get(METADATA) != null) {
69             metaData = (LinkedHashMap<String, Object>) _policy.get(METADATA);
70             ValidateUtils.validateMap(metaData);
71             metaDataObject = new Metadata(metaData);
72         }
73
74         targetsList = targetObjects;
75         targetsType = _targetsType;
76         triggers = _triggers((LinkedHashMap<String, Object>) _policy.get(TRIGGERS));
77         properties = null;
78         if (_policy.get("properties") != null) {
79             properties = (LinkedHashMap<String, Object>) _policy.get("properties");
80         }
81         _validateKeys();
82     }
83
84     public ArrayList<String> getTargets() {
85         return (ArrayList<String>) entityTpl.get("targets");
86     }
87
88     public ArrayList<String> getDescription() {
89         return (ArrayList<String>) entityTpl.get("description");
90     }
91
92     public ArrayList<String> getmetadata() {
93         return (ArrayList<String>) entityTpl.get("metadata");
94     }
95
96     public String getTargetsType() {
97         return targetsType;
98     }
99
100     public Metadata getMetaDataObj() {
101         return metaDataObject;
102     }
103
104     public LinkedHashMap<String, Object> getMetaData() {
105         return metaData;
106     }
107
108     //  public ArrayList<NodeTemplate> getTargetsList() {
109     public ArrayList<Object> getTargetsList() {
110         return targetsList;
111     }
112
113     // entityTemplate already has a different getProperties...
114     // this is to access the local properties variable
115     public LinkedHashMap<String, Object> getPolicyProperties() {
116         return properties;
117     }
118
119     private ArrayList<Object> _triggers(LinkedHashMap<String, Object> triggers) {
120         ArrayList<Object> triggerObjs = new ArrayList<>();
121         if (triggers != null) {
122             for (Map.Entry<String, Object> me : triggers.entrySet()) {
123                 String tname = me.getKey();
124                 LinkedHashMap<String, Object> ttriggerTpl =
125                         (LinkedHashMap<String, Object>) me.getValue();
126                 Triggers triggersObj = new Triggers(tname, ttriggerTpl);
127                 triggerObjs.add(triggersObj);
128             }
129         }
130         return triggerObjs;
131     }
132
133     private void _validateKeys() {
134         for (String key : entityTpl.keySet()) {
135             boolean bFound = false;
136             for (int i = 0; i < SECTIONS.length; i++) {
137                 if (key.equals(SECTIONS[i])) {
138                     bFound = true;
139                     break;
140                 }
141             }
142             if (!bFound) {
143                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE219", String.format(
144                         "UnknownFieldError: Policy \"%s\" contains unknown field \"%s\"",
145                         name, key)));
146             }
147         }
148     }
149
150     @Override
151     public String toString() {
152         return "Policy{" +
153                 "metaData=" + metaData +
154                 ", targetsList=" + targetsList +
155                 ", targetsType='" + targetsType + '\'' +
156                 ", triggers=" + triggers +
157                 ", properties=" + properties +
158                 '}';
159     }
160
161     public int compareTo(Policy other) {
162         if (this.equals(other))
163             return 0;
164         return this.getName().compareTo(other.getName()) == 0 ? this.getType().compareTo(other.getType()) : this.getName().compareTo(other.getName());
165     }
166 }
167
168 /*python
169
170 from toscaparser.common.exception import ValidationIssueCollector
171 from toscaparser.common.exception import UnknownFieldError
172 from toscaparser.entity_template import EntityTemplate
173 from toscaparser.triggers import Triggers
174 from toscaparser.utils import validateutils
175
176
177 SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
178            ('type', 'metadata', 'description',
179             'properties', 'targets', 'triggers')
180
181 log = logging.getLogger('tosca')
182
183
184 class Policy(EntityTemplate):
185     '''Policies defined in Topology template.'''
186     def __init__(self, name, policy, targets, targets_type, custom_def=None):
187         super(Policy, self).__init__(name,
188                                      policy,
189                                      'policy_type',
190                                      custom_def)
191         self.meta_data = None
192         if self.METADATA in policy:
193             self.meta_data = policy.get(self.METADATA)
194             validateutils.validate_map(self.meta_data)
195         self.targets_list = targets
196         self.targets_type = targets_type
197         self.triggers = self._triggers(policy.get(TRIGGERS))
198         self._validate_keys()
199
200     @property
201     def targets(self):
202         return self.entity_tpl.get('targets')
203
204     @property
205     def description(self):
206         return self.entity_tpl.get('description')
207
208     @property
209     def metadata(self):
210         return self.entity_tpl.get('metadata')
211
212     def get_targets_type(self):
213         return self.targets_type
214
215     def get_targets_list(self):
216         return self.targets_list
217
218     def _triggers(self, triggers):
219         triggerObjs = []
220         if triggers:
221             for name, trigger_tpl in triggers.items():
222                 triggersObj = Triggers(name, trigger_tpl)
223                 triggerObjs.append(triggersObj)
224         return triggerObjs
225
226     def _validate_keys(self):
227         for key in self.entity_tpl.keys():
228             if key not in SECTIONS:
229                 ValidationIssueCollector.appendException(
230                     UnknownFieldError(what='Policy "%s"' % self.name,
231                                       field=key))
232 */