Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / Triggers.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 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25 import org.onap.sdc.toscaparser.api.utils.ValidateUtils;
26
27 import java.util.LinkedHashMap;
28
29 public class Triggers extends EntityTemplate {
30
31     private static final String DESCRIPTION = "description";
32     private static final String EVENT = "event_type";
33     private static final String SCHEDULE = "schedule";
34     private static final String TARGET_FILTER = "target_filter";
35     private static final String CONDITION = "condition";
36     private static final String ACTION = "action";
37
38     private static final String[] SECTIONS = {
39             DESCRIPTION, EVENT, SCHEDULE, TARGET_FILTER, CONDITION, ACTION
40     };
41
42     private static final String METER_NAME = "meter_name";
43     private static final String CONSTRAINT = "constraint";
44     private static final String PERIOD = "period";
45     private static final String EVALUATIONS = "evaluations";
46     private static final String METHOD = "method";
47     private static final String THRESHOLD = "threshold";
48     private static final String COMPARISON_OPERATOR = "comparison_operator";
49
50     private static final String[] CONDITION_KEYNAMES = {
51             METER_NAME, CONSTRAINT, PERIOD, EVALUATIONS, METHOD, THRESHOLD, COMPARISON_OPERATOR
52     };
53
54     private String name;
55     private LinkedHashMap<String, Object> triggerTpl;
56
57     public Triggers(String name, LinkedHashMap<String, Object> triggerTpl) {
58         super(); // dummy. don't want super
59         this.name = name;
60         this.triggerTpl = triggerTpl;
61         validateKeys();
62         validateCondition();
63         validateInput();
64     }
65
66     public String getDescription() {
67         return (String) triggerTpl.get("description");
68     }
69
70     public String getEvent() {
71         return (String) triggerTpl.get("event_type");
72     }
73
74     public LinkedHashMap<String, Object> getSchedule() {
75         return (LinkedHashMap<String, Object>) triggerTpl.get("schedule");
76     }
77
78     public LinkedHashMap<String, Object> getTargetFilter() {
79         return (LinkedHashMap<String, Object>) triggerTpl.get("target_filter");
80     }
81
82     public LinkedHashMap<String, Object> getCondition() {
83         return (LinkedHashMap<String, Object>) triggerTpl.get("condition");
84     }
85
86     public LinkedHashMap<String, Object> getAction() {
87         return (LinkedHashMap<String, Object>) triggerTpl.get("action");
88     }
89
90     private void validateKeys() {
91         for (String key : triggerTpl.keySet()) {
92             boolean bFound = false;
93             for (int i = 0; i < SECTIONS.length; i++) {
94                 if (key.equals(SECTIONS[i])) {
95                     bFound = true;
96                     break;
97                 }
98             }
99             if (!bFound) {
100                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE249", String.format(
101                         "UnknownFieldError: Triggers \"%s\" contains unknown field \"%s\"",
102                         name, key)));
103             }
104         }
105     }
106
107     private void validateCondition() {
108         for (String key : getCondition().keySet()) {
109             boolean bFound = false;
110             for (int i = 0; i < CONDITION_KEYNAMES.length; i++) {
111                 if (key.equals(CONDITION_KEYNAMES[i])) {
112                     bFound = true;
113                     break;
114                 }
115             }
116             if (!bFound) {
117                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE250", String.format(
118                         "UnknownFieldError: Triggers \"%s\" contains unknown field \"%s\"",
119                         name, key)));
120             }
121         }
122     }
123
124     private void validateInput() {
125         for (String key : getCondition().keySet()) {
126             Object value = getCondition().get(key);
127             if (key.equals(PERIOD) || key.equals(EVALUATIONS)) {
128                 ValidateUtils.validateInteger(value);
129             } else if (key.equals(THRESHOLD)) {
130                 ValidateUtils.validateNumeric(value);
131             } else if (key.equals(METER_NAME) || key.equals(METHOD)) {
132                 ValidateUtils.validateString(value);
133             }
134         }
135     }
136
137     @Override
138     public String toString() {
139         return "Triggers{"
140                 + "name='" + name + '\''
141                 + ", triggerTpl=" + triggerTpl
142                 + '}';
143     }
144 }
145
146 /*python
147
148 from toscaparser.common.exception import ValidationIssueCollector
149 from toscaparser.common.exception import UnknownFieldError
150 from toscaparser.entity_template import EntityTemplate
151
152 SECTIONS = (DESCRIPTION, EVENT, SCHEDULE, TARGET_FILTER, CONDITION, ACTION) = \
153            ('description', 'event_type', 'schedule',
154             'target_filter', 'condition', 'action')
155 CONDITION_KEYNAMES = (CONTRAINT, PERIOD, EVALUATIONS, METHOD) = \
156                      ('constraint', 'period', 'evaluations', 'method')
157 log = logging.getLogger('tosca')
158
159
160 class Triggers(EntityTemplate):
161
162     '''Triggers defined in policies of topology template'''
163
164     def __init__(self, name, trigger_tpl):
165         self.name = name
166         self.trigger_tpl = trigger_tpl
167         self._validate_keys()
168         self._validate_condition()
169
170     def get_description(self):
171         return self.trigger_tpl['description']
172
173     def get_event(self):
174         return self.trigger_tpl['event_type']
175
176     def get_schedule(self):
177         return self.trigger_tpl['schedule']
178
179     def get_target_filter(self):
180         return self.trigger_tpl['target_filter']
181
182     def get_condition(self):
183         return self.trigger_tpl['condition']
184
185     def get_action(self):
186         return self.trigger_tpl['action']
187
188     def _validate_keys(self):
189         for key in self.trigger_tpl.keys():
190             if key not in SECTIONS:
191                 ValidationIssueCollector.appendException(
192                     UnknownFieldError(what='Triggers "%s"' % self.name,
193                                       field=key))
194
195     def _validate_condition(self):
196         for key in self.get_condition():
197             if key not in CONDITION_KEYNAMES:
198                 ValidationIssueCollector.appendException(
199                     UnknownFieldError(what='Triggers "%s"' % self.name,
200                                       field=key))
201 */