aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/toscaparser/api/Triggers.java
blob: c78978f382ef8d59dded573ad187bf4c50ccbfa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.toscaparser.api;

import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
import org.onap.sdc.toscaparser.api.utils.ValidateUtils;

import java.util.LinkedHashMap;

public class Triggers extends EntityTemplate {

    private static final String DESCRIPTION = "description";
    private static final String EVENT = "event_type";
    private static final String SCHEDULE = "schedule";
    private static final String TARGET_FILTER = "target_filter";
    private static final String CONDITION = "condition";
    private static final String ACTION = "action";

    private static final String[] SECTIONS = {
            DESCRIPTION, EVENT, SCHEDULE, TARGET_FILTER, CONDITION, ACTION
    };

    private static final String METER_NAME = "meter_name";
    private static final String CONSTRAINT = "constraint";
    private static final String PERIOD = "period";
    private static final String EVALUATIONS = "evaluations";
    private static final String METHOD = "method";
    private static final String THRESHOLD = "threshold";
    private static final String COMPARISON_OPERATOR = "comparison_operator";

    private static final String[] CONDITION_KEYNAMES = {
            METER_NAME, CONSTRAINT, PERIOD, EVALUATIONS, METHOD, THRESHOLD, COMPARISON_OPERATOR
    };

    private String name;
    private LinkedHashMap<String, Object> triggerTpl;

    public Triggers(String name, LinkedHashMap<String, Object> triggerTpl) {
        super(); // dummy. don't want super
        this.name = name;
        this.triggerTpl = triggerTpl;
        validateKeys();
        validateCondition();
        validateInput();
    }

    public String getDescription() {
        return (String) triggerTpl.get("description");
    }

    public String getEvent() {
        return (String) triggerTpl.get("event_type");
    }

    public LinkedHashMap<String, Object> getSchedule() {
        return (LinkedHashMap<String, Object>) triggerTpl.get("schedule");
    }

    public LinkedHashMap<String, Object> getTargetFilter() {
        return (LinkedHashMap<String, Object>) triggerTpl.get("target_filter");
    }

    public LinkedHashMap<String, Object> getCondition() {
        return (LinkedHashMap<String, Object>) triggerTpl.get("condition");
    }

    public LinkedHashMap<String, Object> getAction() {
        return (LinkedHashMap<String, Object>) triggerTpl.get("action");
    }

    private void validateKeys() {
        for (String key : triggerTpl.keySet()) {
            boolean bFound = false;
            for (int i = 0; i < SECTIONS.length; i++) {
                if (key.equals(SECTIONS[i])) {
                    bFound = true;
                    break;
                }
            }
            if (!bFound) {
                ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE249", String.format(
                        "UnknownFieldError: Triggers \"%s\" contains unknown field \"%s\"",
                        name, key)));
            }
        }
    }

    private void validateCondition() {
        for (String key : getCondition().keySet()) {
            boolean bFound = false;
            for (int i = 0; i < CONDITION_KEYNAMES.length; i++) {
                if (key.equals(CONDITION_KEYNAMES[i])) {
                    bFound = true;
                    break;
                }
            }
            if (!bFound) {
                ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE250", String.format(
                        "UnknownFieldError: Triggers \"%s\" contains unknown field \"%s\"",
                        name, key)));
            }
        }
    }

    private void validateInput() {
        for (String key : getCondition().keySet()) {
            Object value = getCondition().get(key);
            if (key.equals(PERIOD) || key.equals(EVALUATIONS)) {
                ValidateUtils.validateInteger(value);
            } else if (key.equals(THRESHOLD)) {
                ValidateUtils.validateNumeric(value);
            } else if (key.equals(METER_NAME) || key.equals(METHOD)) {
                ValidateUtils.validateString(value);
            }
        }
    }

    @Override
    public String toString() {
        return "Triggers{"
                + "name='" + name + '\''
                + ", triggerTpl=" + triggerTpl
                + '}';
    }
}

/*python

from toscaparser.common.exception import ValidationIssueCollector
from toscaparser.common.exception import UnknownFieldError
from toscaparser.entity_template import EntityTemplate

SECTIONS = (DESCRIPTION, EVENT, SCHEDULE, TARGET_FILTER, CONDITION, ACTION) = \
           ('description', 'event_type', 'schedule',
            'target_filter', 'condition', 'action')
CONDITION_KEYNAMES = (CONTRAINT, PERIOD, EVALUATIONS, METHOD) = \
                     ('constraint', 'period', 'evaluations', 'method')
log = logging.getLogger('tosca')


class Triggers(EntityTemplate):

    '''Triggers defined in policies of topology template'''

    def __init__(self, name, trigger_tpl):
        self.name = name
        self.trigger_tpl = trigger_tpl
        self._validate_keys()
        self._validate_condition()

    def get_description(self):
        return self.trigger_tpl['description']

    def get_event(self):
        return self.trigger_tpl['event_type']

    def get_schedule(self):
        return self.trigger_tpl['schedule']

    def get_target_filter(self):
        return self.trigger_tpl['target_filter']

    def get_condition(self):
        return self.trigger_tpl['condition']

    def get_action(self):
        return self.trigger_tpl['action']

    def _validate_keys(self):
        for key in self.trigger_tpl.keys():
            if key not in SECTIONS:
                ValidationIssueCollector.appendException(
                    UnknownFieldError(what='Triggers "%s"' % self.name,
                                      field=key))

    def _validate_condition(self):
        for key in self.get_condition():
            if key not in CONDITION_KEYNAMES:
                ValidationIssueCollector.appendException(
                    UnknownFieldError(what='Triggers "%s"' % self.name,
                                      field=key))
*/