+class VnfInputParameterSubstitution(object):
+ """
+ This class is responsible for substituting input parameters into a VNFD.
+ """
+
+ def __init__(self, log, const_vnfd, project):
+ """Create an instance of VnfInputParameterSubstitution
+
+ Arguments:
+ log - a logger for this object to use
+ const_vnfd - id refs for vnfs in a ns
+ project - project for the VNFs
+ """
+
+ self.log = log
+ self.member_vnf_index = const_vnfd.member_vnf_index
+ self.vnfd_id_ref = const_vnfd.vnfd_id_ref
+ self.project = project
+
+ def __call__(self, vnfr, nsr_config):
+ """Substitutes vnf input parameters from the NSR config into the VNFD
+
+ This call modifies the provided VNFD with the input parameters that are
+ contained in the NSR config.
+
+ Arguments:
+ vnfr - a GI VNFR object
+ nsr_config - a GI NSR Config object
+
+ """
+
+ def compose_xpath(xpath, id):
+ prefix = "/rw-project:project[rw-project:name={}]".format(quoted_key(self.project.name)) + \
+ "/vnfr:vnfr-catalog/vnfr:vnfr[vnfr:id={}]/vnfr:vnfd/".format(quoted_key(id))
+
+ suffix = '/'.join(xpath.split('/')[3:]).replace('vnfd', 'vnfr')
+ return prefix + suffix
+
+ def substitute_xpath(ip_xpath, substitute_value, vnfr):
+ vnfr_xpath = compose_xpath(ip_xpath, vnfr.id)
+
+ try:
+ verify_xpath_wildcarded = xpath.getxattr(vnfr, vnfr_xpath)
+
+ self.log.debug(
+ "vnf-input-parameter:{} = {}, for VNF : [member-vnf-index : {}, vnfd-id-ref : {}]".format(
+ ip_xpath,
+ substitute_value,
+ self.member_vnf_index,
+ self.vnfd_id_ref
+ )
+ )
+ try:
+ xpath.setxattr(vnfr, vnfr_xpath, substitute_value)
+
+ except Exception as e:
+ self.log.exception(e)
+
+ except Exception as e:
+ self.log.exception("Wildcarded xpath {} is listy in nature. Can not update. Exception => {}"
+ .format(ip_xpath, e))
+
+ if vnfr is None or nsr_config is None:
+ return
+
+ optional_input_parameters = set()
+ for input_parameter in nsr_config.nsd.input_parameter_xpath:
+ optional_input_parameters.add(input_parameter.xpath)
+
+ # Apply the input parameters to the vnfr
+ if nsr_config.vnf_input_parameter:
+ for param in nsr_config.vnf_input_parameter:
+ if (param.member_vnf_index_ref == self.member_vnf_index and param.vnfd_id_ref == self.vnfd_id_ref):
+ if param.input_parameter:
+ for ip in param.input_parameter:
+ if ip.xpath not in optional_input_parameters:
+ msg = "Substitution Failed. Tried to set an invalid vnf input parameter ({}) for vnf [member-vnf-index : {}, vnfd-id-ref : {}]"
+ self.log.error(msg.format(ip.xpath, self.member_vnf_index, self.vnfd_id_ref))
+ continue
+
+ try:
+ substitute_xpath(ip.xpath, ip.value, vnfr)
+ except Exception as e:
+ self.log.exception(e)
+ else:
+ self.log.debug("Substituting Xpaths with default Values")
+ for input_parameter in nsr_config.nsd.input_parameter_xpath:
+ if input_parameter.default_value is not None:
+ try:
+ if "vnfd-catalog" in input_parameter.xpath:
+ substitute_xpath(input_parameter.xpath, input_parameter.default_value, vnfr)
+ except Exception as e:
+ self.log.exception(e)
+
+