Merge "The grouptype's parent_type definition is error"
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / interfaces.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 from toscaparser.common.exception import ExceptionCollector
14 from toscaparser.common.exception import UnknownFieldError
15 from toscaparser.elements.statefulentitytype import StatefulEntityType
16
17 SECTIONS = (LIFECYCLE, CONFIGURE, LIFECYCLE_SHORTNAME,
18             CONFIGURE_SHORTNAME) = \
19            ('tosca.interfaces.node.lifecycle.Standard',
20             'tosca.interfaces.relationship.Configure',
21             'Standard', 'Configure')
22
23 INTERFACEVALUE = (IMPLEMENTATION, INPUTS) = ('implementation', 'inputs')
24
25
26 class InterfacesDef(StatefulEntityType):
27     '''TOSCA built-in interfaces type.'''
28
29     def __init__(self, node_type, interfacetype,
30                  node_template=None, name=None, value=None):
31         self.ntype = node_type
32         self.node_template = node_template
33         self.type = interfacetype
34         self.name = name
35         self.value = value
36         self.implementation = None
37         self.inputs = None
38         self.defs = {}
39         if interfacetype == LIFECYCLE_SHORTNAME:
40             interfacetype = LIFECYCLE
41         if interfacetype == CONFIGURE_SHORTNAME:
42             interfacetype = CONFIGURE
43         if node_type:
44             self.defs = self.TOSCA_DEF[interfacetype]
45         if value:
46             if isinstance(self.value, dict):
47                 for i, j in self.value.items():
48                     if i == IMPLEMENTATION:
49                         self.implementation = j
50                     elif i == INPUTS:
51                         self.inputs = j
52                     else:
53                         what = ('"interfaces" of template "%s"' %
54                                 self.node_template.name)
55                         ExceptionCollector.appendException(
56                             UnknownFieldError(what=what, field=i))
57             else:
58                 self.implementation = value
59
60     @property
61     def lifecycle_ops(self):
62         if self.defs:
63             if self.type == LIFECYCLE:
64                 return self._ops()
65
66     @property
67     def configure_ops(self):
68         if self.defs:
69             if self.type == CONFIGURE:
70                 return self._ops()
71
72     def _ops(self):
73         ops = []
74         for name in list(self.defs.keys()):
75             ops.append(name)
76         return ops