Fix StatefulEntityType when entitytype is not define
[parser.git] / tosca2heat / tosca-parser / toscaparser / policy.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13
14 import logging
15
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.common.exception import UnknownFieldError
18 from toscaparser.entity_template import EntityTemplate
19 from toscaparser.triggers import Triggers
20 from toscaparser.utils import validateutils
21
22
23 SECTIONS = (TYPE, METADATA, DESCRIPTION, PROPERTIES, TARGETS, TRIGGERS) = \
24            ('type', 'metadata', 'description',
25             'properties', 'targets', 'triggers')
26
27 log = logging.getLogger('tosca')
28
29
30 class Policy(EntityTemplate):
31     '''Policies defined in Topology template.'''
32     def __init__(self, name, policy, targets=None, targets_type=None,
33                  custom_def=None):
34         super(Policy, self).__init__(name,
35                                      policy,
36                                      'policy_type',
37                                      custom_def)
38         self.meta_data = None
39         if self.METADATA in policy:
40             self.meta_data = policy.get(self.METADATA)
41             validateutils.validate_map(self.meta_data)
42         self.targets_list = targets
43         self.targets_type = targets_type
44         self.triggers = self._triggers(policy.get(TRIGGERS))
45         self.properties = None
46         if 'properties' in policy:
47             self.properties = policy['properties']
48         self._validate_keys()
49
50     @property
51     def targets(self):
52         return self.entity_tpl.get('targets')
53
54     @property
55     def description(self):
56         return self.entity_tpl.get('description')
57
58     @property
59     def metadata(self):
60         return self.entity_tpl.get('metadata')
61
62     def get_targets_type(self):
63         return self.targets_type
64
65     def get_targets_list(self):
66         return self.targets_list
67
68     def _triggers(self, triggers):
69         triggerObjs = []
70         if triggers:
71             for name, trigger_tpl in triggers.items():
72                 triggersObj = Triggers(name, trigger_tpl)
73                 triggerObjs.append(triggersObj)
74         return triggerObjs
75
76     def _validate_keys(self):
77         for key in self.entity_tpl.keys():
78             if key not in SECTIONS:
79                 ExceptionCollector.appendException(
80                     UnknownFieldError(what='Policy "%s"' % self.name,
81                                       field=key))