genericparser seed code
[modeling/etsicatalog.git] / genericparser / pub / utils / toscaparsers / sdmodel.py
1 # Copyright (c) 2019, CMCC Technologies. Co., Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import logging
15
16 from genericparser.pub.utils.toscaparsers.basemodel import BaseInfoModel
17 from genericparser.pub.utils.toscaparsers.servicemodel import SdcServiceModel
18
19 logger = logging.getLogger(__name__)
20
21
22 class SdInfoModel(BaseInfoModel):
23     def __init__(self, path, params):
24         super(SdInfoModel, self).__init__(path, params)
25
26     def parseModel(self, tosca):
27         self.metadata = self.buildMetadata(tosca)
28         self.inputs = self.build_inputs(tosca)
29
30         sdcModle = SdcServiceModel(tosca)
31         if sdcModle:
32             self.service = sdcModle.ns
33             if hasattr(tosca, 'nodetemplates'):
34                 self.basepath = sdcModle.basepath
35                 self.vnfs = sdcModle.vnfs
36                 self.pnfs = sdcModle.pnfs
37                 self.vls = sdcModle.vls
38                 self.graph = sdcModle.graph
39
40     def build_inputs(self, tosca):
41         """ Get all the inputs for complex type"""
42         result_inputs = {}
43
44         if not tosca.inputs:
45             return {}
46
47         for input in tosca.inputs:
48             type = input.schema.type
49             if type.__eq__('list') or type.__eq__('map'):
50                 complex_input = []
51                 entry_schema = self.get_entry_schema(input.schema.schema['entry_schema'])
52                 self.get_child_input_repeat(complex_input, entry_schema, input)
53                 result_inputs[input.schema.name] = complex_input
54
55             else:
56                 simple_input = {
57                     "type": input.schema.type,
58                     "description": input.schema.description,
59                     "required": input.schema.required,
60                 }
61                 result_inputs[input.schema.name] = simple_input
62         return result_inputs
63
64     def get_child_input_repeat(self, complex_input, entry_schema, input):
65         custom_defs = input.custom_defs
66         properties = custom_defs[entry_schema]['properties']
67         for key, value in properties.iteritems():
68             if value['type'].__eq__('list'):
69                 child_complex_input = []
70                 child_entry_schema = self.get_entry_schema(value['entry_schema'])
71                 self.get_child_input_repeat(child_complex_input, child_entry_schema, input)
72                 complex_input.append({key: child_complex_input})
73             else:
74                 if 'description' in value.keys():
75                     simple_input = {
76                         key: "",
77                         "type": value['type'],
78                         "required": value['required'],
79                         "description": value['description'],
80                     }
81                 else:
82                     simple_input = {
83                         key: "",
84                         "type": value['type'],
85                         "required": value['required'],
86                     }
87                 complex_input.append(simple_input)
88
89     def get_entry_schema(self, entry_schema):
90         if isinstance(entry_schema, dict):
91             if 'type' in entry_schema.keys():
92                 entry_schema = entry_schema['type']
93         return entry_schema