Merge "Add verigraph code base"
[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 INTERFACE_DEF_RESERVED_WORDS = ['type', 'inputs', 'derived_from', 'version',
26                                 'description']
27
28
29 class InterfacesDef(StatefulEntityType):
30     '''TOSCA built-in interfaces type.'''
31
32     def __init__(self, node_type, interfacetype,
33                  node_template=None, name=None, value=None):
34         self.ntype = node_type
35         self.node_template = node_template
36         self.type = interfacetype
37         self.name = name
38         self.value = value
39         self.implementation = None
40         self.inputs = None
41         self.defs = {}
42         if interfacetype == LIFECYCLE_SHORTNAME:
43             interfacetype = LIFECYCLE
44         if interfacetype == CONFIGURE_SHORTNAME:
45             interfacetype = CONFIGURE
46         if hasattr(self.ntype, 'interfaces') \
47            and self.ntype.interfaces \
48            and interfacetype in self.ntype.interfaces:
49             interfacetype = self.ntype.interfaces[interfacetype]['type']
50         if node_type:
51             if self.node_template and self.node_template.custom_def \
52                and interfacetype in self.node_template.custom_def:
53                 self.defs = self.node_template.custom_def[interfacetype]
54             else:
55                 self.defs = self.TOSCA_DEF[interfacetype]
56         if value:
57             if isinstance(self.value, dict):
58                 for i, j in self.value.items():
59                     if i == IMPLEMENTATION:
60                         self.implementation = j
61                     elif i == INPUTS:
62                         self.inputs = j
63                     else:
64                         what = ('"interfaces" of template "%s"' %
65                                 self.node_template.name)
66                         ExceptionCollector.appendException(
67                             UnknownFieldError(what=what, field=i))
68             else:
69                 self.implementation = value
70
71     @property
72     def lifecycle_ops(self):
73         if self.defs:
74             if self.type == LIFECYCLE:
75                 return self._ops()
76
77     @property
78     def configure_ops(self):
79         if self.defs:
80             if self.type == CONFIGURE:
81                 return self._ops()
82
83     def _ops(self):
84         ops = []
85         for name in list(self.defs.keys()):
86             ops.append(name)
87         return ops