2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.toscaparser.api;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
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;
33 public class Policy extends EntityTemplate {
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};
45 Metadata metaDataObject;
46 LinkedHashMap<String, Object> metaData = null;
47 ArrayList<Object> targetsList; // *** a list of NodeTemplate OR a list of Group ***
49 ArrayList<Object> triggers;
50 LinkedHashMap<String, Object> properties;
52 public Policy(String _name,
53 LinkedHashMap<String, Object> _policy,
54 ArrayList<Object> targetObjects,
56 LinkedHashMap<String, Object> _customDef) {
57 this(_name, _policy, targetObjects, _targetsType, _customDef, null);
60 public Policy(String _name,
61 LinkedHashMap<String, Object> _policy,
62 // ArrayList<NodeTemplate> targetObjects,
63 ArrayList<Object> targetObjects,
65 LinkedHashMap<String, Object> _customDef, NodeTemplate parentNodeTemplate) {
66 super(_name, _policy, "policy_type", _customDef, parentNodeTemplate);
68 if (_policy.get(METADATA) != null) {
69 metaData = (LinkedHashMap<String, Object>) _policy.get(METADATA);
70 ValidateUtils.validateMap(metaData);
71 metaDataObject = new Metadata(metaData);
74 targetsList = targetObjects;
75 targetsType = _targetsType;
76 triggers = _triggers((LinkedHashMap<String, Object>) _policy.get(TRIGGERS));
78 if (_policy.get("properties") != null) {
79 properties = (LinkedHashMap<String, Object>) _policy.get("properties");
84 public ArrayList<String> getTargets() {
85 return (ArrayList<String>) entityTpl.get("targets");
88 public ArrayList<String> getDescription() {
89 return (ArrayList<String>) entityTpl.get("description");
92 public ArrayList<String> getmetadata() {
93 return (ArrayList<String>) entityTpl.get("metadata");
96 public String getTargetsType() {
100 public Metadata getMetaDataObj() {
101 return metaDataObject;
104 public LinkedHashMap<String, Object> getMetaData() {
108 // public ArrayList<NodeTemplate> getTargetsList() {
109 public ArrayList<Object> getTargetsList() {
113 // entityTemplate already has a different getProperties...
114 // this is to access the local properties variable
115 public LinkedHashMap<String, Object> getPolicyProperties() {
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);
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])) {
143 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE219", String.format(
144 "UnknownFieldError: Policy \"%s\" contains unknown field \"%s\"",
151 public String toString() {
153 "metaData=" + metaData +
154 ", targetsList=" + targetsList +
155 ", targetsType='" + targetsType + '\'' +
156 ", triggers=" + triggers +
157 ", properties=" + properties +
161 public int compareTo(Policy other) {
162 if (this.equals(other))
164 return this.getName().compareTo(other.getName()) == 0 ? this.getType().compareTo(other.getType()) : this.getName().compareTo(other.getName());
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
177 SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
178 ('type', 'metadata', 'description',
179 'properties', 'targets', 'triggers')
181 log = logging.getLogger('tosca')
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,
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()
202 return self.entity_tpl.get('targets')
205 def description(self):
206 return self.entity_tpl.get('description')
210 return self.entity_tpl.get('metadata')
212 def get_targets_type(self):
213 return self.targets_type
215 def get_targets_list(self):
216 return self.targets_list
218 def _triggers(self, triggers):
221 for name, trigger_tpl in triggers.items():
222 triggersObj = Triggers(name, trigger_tpl)
223 triggerObjs.append(triggersObj)
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,