8 from toscaparser.functions import GetInput
9 from toscaparser.tosca_template import ToscaTemplate
11 from lcm.pub.utils.toscaparser.dataentityext import DataEntityExt
14 class BaseInfoModel(object):
16 def buildToscaTemplate(self, path, params):
19 file_name = self._check_download_file(path)
20 valid_params = self._validate_input_params(file_name, params)
21 return self._create_tosca_template(file_name, valid_params)
23 if file_name != None and file_name != path and os.path.exists(file_name):
29 def _validate_input_params(self, path, params):
31 if params and len(params) > 0:
32 tmp = self._create_tosca_template(path, None)
33 for key,value in params.items():
34 if hasattr(tmp, 'inputs') and len(tmp.inputs) > 0:
35 for input_def in tmp.inputs:
36 if (input_def.name == key):
37 valid_params[key] = DataEntityExt.validate_datatype(input_def.type, value)
41 def _create_tosca_template(self, file_name, valid_params):
44 tosca_tpl = ToscaTemplate(file_name, valid_params)
45 print "-----------------------------"
46 print '\n'.join(['%s:%s' % item for item in tosca_tpl.__dict__.items()])
47 print "-----------------------------"
50 if tosca_tpl != None and hasattr(tosca_tpl, "temp_dir") and os.path.exists(tosca_tpl.temp_dir):
52 shutil.rmtree(tosca_tpl.temp_dir)
56 def _check_download_file(self, path):
57 if (path.startswith("ftp") or path.startswith("sftp")):
58 return self.downloadFileFromFtpServer(path)
59 elif (path.startswith("http")):
60 return self.download_file_from_httpserver(path)
63 def download_file_from_httpserver(self, path):
64 path = path.encode("utf-8")
65 tmps = str.split(path, '/')
66 localFileName = tmps[len(tmps) - 1]
67 urllib.urlretrieve(path, localFileName)
70 def downloadFileFromFtpServer(self, path):
71 path = path.encode("utf-8")
72 tmp = str.split(path, '://')
74 tmp = str.split(tmp[1], ':')
77 tmp = str.split(tmp[1], '@')
79 index = tmp[1].index('/')
80 hostIp = tmp[1][0:index]
81 remoteFileName = tmp[1][index:len(tmp[1])]
82 if protocol.lower() == 'ftp':
89 userPwd = str.split(tmp[1], '@')[0]
90 hostIp = str.split(tmp[1], '@')[1]
91 index = tmp[2].index('/')
92 hostPort = tmp[2][0:index]
93 remoteFileName = tmp[2][index:len(tmp[2])]
95 localFileName = str.split(remoteFileName, '/')
96 localFileName = localFileName[len(localFileName) - 1]
98 if protocol.lower() == 'sftp':
99 self.sftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
101 self.ftp_get(userName, userPwd, hostIp, hostPort, remoteFileName, localFileName)
104 def buidMetadata(self, tosca):
105 if 'metadata' in tosca.tpl:
106 self.metadata = copy.deepcopy(tosca.tpl['metadata'])
108 def buildProperties(self, nodeTemplate, parsed_params):
110 isMappingParams = parsed_params and len(parsed_params) > 0
111 for k, item in nodeTemplate.get_properties().items():
112 properties[k] = item.value
113 if isinstance(item.value, GetInput):
114 if item.value.result() and isMappingParams:
115 properties[k] = DataEntityExt.validate_datatype(item.type, item.value.result())
118 tmp[item.value.name] = item.value.input_name
120 if 'attributes' in nodeTemplate.entity_tpl:
121 for k, item in nodeTemplate.entity_tpl['attributes'].items():
122 properties[k] = str(item)
126 def verify_properties(self, props, inputs, parsed_params):
128 if (props and len(props) > 0):
129 for key, value in props.items():
130 ret_props[key] = self._verify_value(value, inputs, parsed_params)
131 # if isinstance(value, str):
132 # ret_props[key] = self._verify_string(inputs, parsed_params, value);
134 # if isinstance(value, list):
135 # ret_props[key] = map(lambda x: self._verify_dict(inputs, parsed_params, x), value)
137 # if isinstance(value, dict):
138 # ret_props[key] = self._verify_map(inputs, parsed_params, value)
140 # ret_props[key] = value
143 def build_requirements(self, node_template):
145 for req in node_template.requirements:
146 for req_name, req_value in req.items():
147 if (isinstance(req_value, dict)):
148 if ('node' in req_value and req_value['node'] not in node_template.templates):
149 continue # No target requirement for aria parser, not add to result.
150 rets.append({req_name : req_value})
153 def buildCapabilities(self, nodeTemplate, inputs, ret):
154 capabilities = json.dumps(nodeTemplate.entity_tpl.get('capabilities', None))
155 match = re.findall(r'\{"get_input":\s*"([\w|\-]+)"\}',capabilities)
157 aa= [input_def for input_def in inputs
158 if m == input_def.name][0]
159 capabilities = re.sub(r'\{"get_input":\s*"([\w|\-]+)"\}', json.dumps(aa.default), capabilities,1)
160 if capabilities != 'null':
161 ret['capabilities'] = json.loads(capabilities)
163 def buildArtifacts(self, nodeTemplate, inputs, ret):
164 artifacts = json.dumps(nodeTemplate.entity_tpl.get('artifacts', None))
165 match = re.findall(r'\{"get_input":\s*"([\w|\-]+)"\}',artifacts)
167 aa= [input_def for input_def in inputs
168 if m == input_def.name][0]
169 artifacts = re.sub(r'\{"get_input":\s*"([\w|\-]+)"\}', json.dumps(aa.default), artifacts,1)
170 if artifacts != 'null':
171 ret['artifacts'] = json.loads(artifacts)
173 def build_interfaces(self, node_template):
174 if 'interfaces' in node_template.entity_tpl:
175 return node_template.entity_tpl['interfaces']
178 def isVnf(self, node):
179 return node['nodeType'].upper().find('.VNF.') >= 0 or node['nodeType'].upper().endswith('.VNF')
181 def isPnf(self, node):
182 return node['nodeType'].upper().find('.PNF.') >= 0 or node['nodeType'].upper().endswith('.PNF')
184 def isCp(self, node):
185 return node['nodeType'].upper().find('.CP.') >= 0 or node['nodeType'].upper().endswith('.CP')
187 def isVl(self, node):
188 return node['nodeType'].upper().find('.VIRTUALLINK.') >= 0 or node['nodeType'].upper().find('.VL.') >= 0 or \
189 node['nodeType'].upper().endswith('.VIRTUALLINK') or node['nodeType'].upper().endswith('.VL')
191 def get_requirement_node_name(self, req_value):
192 return self.get_prop_from_obj(req_value, 'node')
194 def get_prop_from_obj(self, obj, prop):
195 if isinstance(obj, str):
197 if (isinstance(obj, dict) and prop in obj):
201 def getNodeDependencys(self, node):
202 return self.getRequirementByName(node, 'dependency')
204 def getVirtualLinks(self, node):
205 return self.getRequirementByName(node, 'virtualLink')
207 def getVirtualbindings(self, node):
208 return self.getRequirementByName(node, 'virtualbinding')
211 def getRequirementByName(self, node, requirementName):
213 if 'requirements' in node:
214 for item in node['requirements']:
215 for key, value in item.items():
216 if key == requirementName:
217 requirements.append(value)
220 def get_networks(self, node):
222 if 'requirements' in node:
223 for item in node['requirements']:
224 for key, value in item.items():
225 if key.upper().find('VIRTUALLINK') >=0:
226 rets.append({"key_name":key, "vl_id":self.get_requirement_node_name(value)})
229 def _verify_value(self, value, inputs, parsed_params):
230 if isinstance(value, str):
231 return self._verify_string(inputs, parsed_params, value)
232 if isinstance(value, list) or isinstance(value, dict):
233 return self._verify_object(value, inputs, parsed_params)
236 def _verify_object(self, value, inputs, parsed_params):
237 s = self._verify_string(inputs, parsed_params, json.dumps(value))
240 def _get_input_name(self, getInput):
241 input_name = getInput.split(':')[1]
242 input_name = input_name.strip()
243 return input_name.replace('"', '').replace('}', '')
245 def _verify_string(self, inputs, parsed_params, value):
246 getInputs = re.findall(r'{"get_input": "[a-zA-Z_0-9]+"}', value)
247 for getInput in getInputs:
248 input_name = self._get_input_name(getInput)
249 if parsed_params and input_name in parsed_params:
250 value = value.replace(getInput, json.dumps(parsed_params[input_name]))
252 for input_def in inputs:
253 if input_def.default and input_name == input_def.name:
254 value = value.replace(getInput, json.dumps(input_def.default))
257 def get_node_vl_id(self, node):
258 vl_ids = map(lambda x: self.get_requirement_node_name(x), self.getVirtualLinks(node))
263 def get_node_by_name(self, node_templates, name):
264 for node in node_templates:
265 if node['name'] == name: