aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/PolicyType.java
blob: c60bed1a50d40ad9af81a25d458f84381d08e430 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package org.openecomp.sdc.toscaparser.api.elements;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.utils.TOSCAVersionProperty;
import org.openecomp.sdc.toscaparser.api.utils.ThreadLocalsHolder;

public class PolicyType extends StatefulEntityType {
	
	private static final String DERIVED_FROM = "derived_from";
	private static final String METADATA = "metadata";
	private static final String PROPERTIES = "properties";
	private static final String VERSION = "version";
	private static final String DESCRIPTION = "description";
	private static final String TARGETS = "targets";
	private static final String TRIGGERS = "triggers";
	private static final String TYPE = "type";

	private static final String SECTIONS[] = {
			DERIVED_FROM, METADATA, PROPERTIES, VERSION, DESCRIPTION, TARGETS, TRIGGERS, TYPE
	};

	private LinkedHashMap<String,Object> customDef;
	private String policyDescription;
	private Object policyVersion;
	private LinkedHashMap<String,Object> properties;
	private LinkedHashMap<String,Object> parentPolicies;
	private LinkedHashMap<String,Object> metaData;
	private ArrayList<String> targetsList;
	
	
	public PolicyType(String _type, LinkedHashMap<String,Object> _customDef) {
		super(_type,POLICY_PREFIX,_customDef);
		
		type = _type;
		customDef = _customDef;
		_validateKeys();
		
        metaData = null;
        if(defs != null && defs.get(METADATA) != null) {
            metaData = (LinkedHashMap<String,Object>)defs.get(METADATA);
            _validateMetadata(metaData);
        }

        properties = null;
        if(defs != null && defs.get(PROPERTIES) != null) {
        	properties = (LinkedHashMap<String,Object>)defs.get(PROPERTIES);
        }
        parentPolicies = _getParentPolicies();

        policyVersion = null;
        if(defs != null && defs.get(VERSION) != null) {
            policyVersion = (new TOSCAVersionProperty(
                defs.get(VERSION))).getVersion();
        }

        policyDescription = null;
        if(defs != null && defs.get(DESCRIPTION) != null) {
        	policyDescription = (String)defs.get(DESCRIPTION);
        }
            
        targetsList = null;
        if(defs != null && defs.get(TARGETS) != null) {
        	targetsList = (ArrayList<String>)defs.get(TARGETS);
            _validateTargets(targetsList,customDef);
        }
		
	}

	private LinkedHashMap<String,Object> _getParentPolicies() {
		LinkedHashMap<String,Object> policies = new LinkedHashMap<>();
		String parentPolicy;
		if(getParentType() != null) {
			parentPolicy = getParentType().getType();
		}
		else {
			parentPolicy = null;
		}
		if(parentPolicy != null) {
			while(parentPolicy != null && !parentPolicy.equals("tosca.policies.Root")) {
				policies.put(parentPolicy, TOSCA_DEF.get(parentPolicy));
				parentPolicy = (String)
					((LinkedHashMap<String,Object>)policies.get(parentPolicy)).get("derived_from);");
			}
		}
		return policies;
	}

	public String getType() {
		return type;
	}

	public PolicyType getParentType() {
        // Return a policy statefulentity of this node is derived from
		if(defs == null) {
			return null;
		}
        String ppolicyEntity = derivedFrom(defs);
        if(ppolicyEntity != null) {
            return new PolicyType(ppolicyEntity,customDef);
        }
        return null;
	}
	
	public Object getPolicy(String name) {
        // Return the definition of a policy field by name
        if(defs != null &&  defs.get(name) != null) {
            return defs.get(name);
        }
        return null;
	}

	public ArrayList<String> getTargets() {
        // Return targets
        return targetsList;
	}
	
	public String getDescription() {
		return policyDescription;
	}

	public Object getVersion() {
		return policyVersion;
	}
	
	private void _validateKeys() {
		for(String key: defs.keySet()) {
			boolean bFound = false;
			for(String sect: SECTIONS) {
				if(key.equals(sect)) {
					bFound = true;
					break;
				}
			}
			if(!bFound) {
                ThreadLocalsHolder.getCollector().appendException(String.format(
                    "UnknownFieldError: Policy \"%s\" contains unknown field \"%s\"",
                    type,key));
			}
		}
	}
	
	private void _validateTargets(ArrayList<String> _targetsList,
								  LinkedHashMap<String,Object> _customDef) {
		for(String nodetype: _targetsList) {
			if(_customDef.get(nodetype) == null) {
                ThreadLocalsHolder.getCollector().appendException(String.format(
                    "InvalidTypeError: \"%s\" defined in targets for policy \"%s\"",
                    nodetype,type));
				
			}
		}
	}
 
	private void _validateMetadata(LinkedHashMap<String,Object> _metaData) {
		String mtype = (String)_metaData.get("type");
		if(mtype != null && !mtype.equals("map") && !mtype.equals("tosca:map")) {
            ThreadLocalsHolder.getCollector().appendException(String.format(
                "InvalidTypeError: \"%s\" defined in policy for metadata",
                mtype));
		}
		for(String entrySchema: metaData.keySet()) {
			Object estob = metaData.get(entrySchema);
			if(estob instanceof LinkedHashMap) {
				String est = (String)
						((LinkedHashMap<String,Object>)estob).get("type");
				if(!est.equals("string")) {
	                ThreadLocalsHolder.getCollector().appendException(String.format(
	                    "InvalidTypeError: \"%s\" defined in policy for metadata \"%s\"",
	                    est,entrySchema));
				}
			}
		}
	}

}

/*python

from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import InvalidTypeError
from toscaparser.common.exception import UnknownFieldError
from toscaparser.elements.statefulentitytype import StatefulEntityType
from toscaparser.utils.validateutils import TOSCAVersionProperty


class PolicyType(StatefulEntityType):

    '''TOSCA built-in policies type.'''
    SECTIONS = (DERIVED_FROM, METADATA, PROPERTIES, VERSION, DESCRIPTION, TARGETS) = \
               ('derived_from', 'metadata', 'properties', 'version',
                'description', 'targets')

    def __init__(self, ptype, custom_def=None):
        super(PolicyType, self).__init__(ptype, self.POLICY_PREFIX,
                                         custom_def)
        self.type = ptype
        self.custom_def = custom_def
        self._validate_keys()

        self.meta_data = None
        if self.METADATA in self.defs:
            self.meta_data = self.defs[self.METADATA]
            self._validate_metadata(self.meta_data)

        self.properties = None
        if self.PROPERTIES in self.defs:
            self.properties = self.defs[self.PROPERTIES]
        self.parent_policies = self._get_parent_policies()

        self.policy_version = None
        if self.VERSION in self.defs:
            self.policy_version = TOSCAVersionProperty(
                self.defs[self.VERSION]).get_version()

        self.policy_description = self.defs[self.DESCRIPTION] \
            if self.DESCRIPTION in self.defs else None

        self.targets_list = None
        if self.TARGETS in self.defs:
            self.targets_list = self.defs[self.TARGETS]
            self._validate_targets(self.targets_list, custom_def)

    def _get_parent_policies(self):
        policies = {}
        parent_policy = self.parent_type.type if self.parent_type else None
        if parent_policy:
            while parent_policy != 'tosca.policies.Root':
                policies[parent_policy] = self.TOSCA_DEF[parent_policy]
                parent_policy = policies[parent_policy]['derived_from']
        return policies

    @property
    def parent_type(self):
        '''Return a policy statefulentity of this node is derived from.'''
        if not hasattr(self, 'defs'):
            return None
        ppolicy_entity = self.derived_from(self.defs)
        if ppolicy_entity:
            return PolicyType(ppolicy_entity, self.custom_def)

    def get_policy(self, name):
        '''Return the definition of a policy field by name.'''
        if name in self.defs:
            return self.defs[name]

    @property
    def targets(self):
        '''Return targets.'''
        return self.targets_list

    @property
    def description(self):
        return self.policy_description

    @property
    def version(self):
        return self.policy_version

    def _validate_keys(self):
        for key in self.defs.keys():
            if key not in self.SECTIONS:
                ExceptionCollector.appendException(
                    UnknownFieldError(what='Policy "%s"' % self.type,
                                      field=key))

    def _validate_targets(self, targets_list, custom_def):
        for nodetype in targets_list:
            if nodetype not in custom_def:
                ExceptionCollector.appendException(
                    InvalidTypeError(what='"%s" defined in targets for '
                                     'policy "%s"' % (nodetype, self.type)))

    def _validate_metadata(self, meta_data):
        if not meta_data.get('type') in ['map', 'tosca:map']:
            ExceptionCollector.appendException(
                InvalidTypeError(what='"%s" defined in policy for '
                                 'metadata' % (meta_data.get('type'))))

        for entry_schema, entry_schema_type in meta_data.items():
            if isinstance(entry_schema_type, dict) and not \
                    entry_schema_type.get('type') == 'string':
                ExceptionCollector.appendException(
                    InvalidTypeError(what='"%s" defined in policy for '
                                     'metadata "%s"'
                                     % (entry_schema_type.get('type'),
                                        entry_schema)))
*/