aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/toscaparser/api/Policy.java
blob: ca8ac5500b1ae10712981759febda5d19b861712 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*-
 * ============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 java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import org.onap.sdc.toscaparser.api.elements.Metadata;
import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
import org.onap.sdc.toscaparser.api.utils.ValidateUtils;

public class Policy extends EntityTemplate {


    static final String TYPE = "type";
    static final String METADATA = "metadata";
    static final String DESCRIPTION = "description";
    static final String PROPERTIES = "properties";
    static final String TARGETS = "targets";
    private static final String TRIGGERS = "triggers";
    private static final String SECTIONS[] = {
            TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS};

    Metadata metaDataObject;
    LinkedHashMap<String, Object> metaData = null;
    ArrayList<Object> targetsList; // *** a list of NodeTemplate OR a list of Group ***
    String targetsType;
    ArrayList<Object> triggers;
    LinkedHashMap<String, Object> properties;

    public Policy(String _name,
                  LinkedHashMap<String, Object> _policy,
                  ArrayList<Object> targetObjects,
                  String _targetsType,
                  LinkedHashMap<String, Object> _customDef) {
        this(_name, _policy, targetObjects, _targetsType, _customDef, null);
    }

    public Policy(String _name,
                  LinkedHashMap<String, Object> _policy,
//				  ArrayList<NodeTemplate> targetObjects,
                  ArrayList<Object> targetObjects,
                  String _targetsType,
                  LinkedHashMap<String, Object> _customDef, NodeTemplate parentNodeTemplate) {
        super(_name, _policy, "policy_type", _customDef, parentNodeTemplate);

        if (_policy.get(METADATA) != null) {
            metaData = (LinkedHashMap<String, Object>) _policy.get(METADATA);
            ValidateUtils.validateMap(metaData);
            metaDataObject = new Metadata(metaData);
        }

        targetsList = targetObjects;
        targetsType = _targetsType;
        triggers = _triggers((LinkedHashMap<String, Object>) _policy.get(TRIGGERS));
        properties = null;
        if (_policy.get("properties") != null) {
            properties = (LinkedHashMap<String, Object>) _policy.get("properties");
        }
        _validateKeys();
    }

    public ArrayList<String> getTargets() {
        return (ArrayList<String>) entityTpl.get("targets");
    }

    public ArrayList<String> getDescription() {
        return (ArrayList<String>) entityTpl.get("description");
    }

    public ArrayList<String> getmetadata() {
        return (ArrayList<String>) entityTpl.get("metadata");
    }

    public String getTargetsType() {
        return targetsType;
    }

    public Metadata getMetaDataObj() {
        return metaDataObject;
    }

    public LinkedHashMap<String, Object> getMetaData() {
        return metaData;
    }

    //	public ArrayList<NodeTemplate> getTargetsList() {
    public ArrayList<Object> getTargetsList() {
        return targetsList;
    }

    // entityTemplate already has a different getProperties...
    // this is to access the local properties variable
    public LinkedHashMap<String, Object> getPolicyProperties() {
        return properties;
    }

    private ArrayList<Object> _triggers(LinkedHashMap<String, Object> triggers) {
        ArrayList<Object> triggerObjs = new ArrayList<>();
        if (triggers != null) {
            for (Map.Entry<String, Object> me : triggers.entrySet()) {
                String tname = me.getKey();
                LinkedHashMap<String, Object> ttriggerTpl =
                        (LinkedHashMap<String, Object>) me.getValue();
                Triggers triggersObj = new Triggers(tname, ttriggerTpl);
                triggerObjs.add(triggersObj);
            }
        }
        return triggerObjs;
    }

    private void _validateKeys() {
        for (String key : entityTpl.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("JE219", String.format(
                        "UnknownFieldError: Policy \"%s\" contains unknown field \"%s\"",
                        name, key)));
            }
        }
    }

    @Override
    public String toString() {
        return "Policy{" +
                "metaData=" + metaData +
                ", targetsList=" + targetsList +
                ", targetsType='" + targetsType + '\'' +
                ", triggers=" + triggers +
                ", properties=" + properties +
                '}';
    }

    public int compareTo(Policy other) {
        if (this.equals(other))
            return 0;
        return this.getName().compareTo(other.getName()) == 0 ? this.getType().compareTo(other.getType()) : this.getName().compareTo(other.getName());
    }
}

/*python

from toscaparser.common.exception import ValidationIssueCollector
from toscaparser.common.exception import UnknownFieldError
from toscaparser.entity_template import EntityTemplate
from toscaparser.triggers import Triggers
from toscaparser.utils import validateutils


SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
           ('type', 'metadata', 'description',
            'properties', 'targets', 'triggers')

log = logging.getLogger('tosca')


class Policy(EntityTemplate):
    '''Policies defined in Topology template.'''
    def __init__(self, name, policy, targets, targets_type, custom_def=None):
        super(Policy, self).__init__(name,
                                     policy,
                                     'policy_type',
                                     custom_def)
        self.meta_data = None
        if self.METADATA in policy:
            self.meta_data = policy.get(self.METADATA)
            validateutils.validate_map(self.meta_data)
        self.targets_list = targets
        self.targets_type = targets_type
        self.triggers = self._triggers(policy.get(TRIGGERS))
        self._validate_keys()

    @property
    def targets(self):
        return self.entity_tpl.get('targets')

    @property
    def description(self):
        return self.entity_tpl.get('description')

    @property
    def metadata(self):
        return self.entity_tpl.get('metadata')

    def get_targets_type(self):
        return self.targets_type

    def get_targets_list(self):
        return self.targets_list

    def _triggers(self, triggers):
        triggerObjs = []
        if triggers:
            for name, trigger_tpl in triggers.items():
                triggersObj = Triggers(name, trigger_tpl)
                triggerObjs.append(triggersObj)
        return triggerObjs

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