Stone, Avi (as206k) | 879e94b | 2018-04-12 16:41:45 +0300 | [diff] [blame] | 1 | from toscalib.templates.constant import * |
| 2 | import logging |
| 3 | |
| 4 | |
| 5 | class SubstitutionRule (object): |
| 6 | def __init__(self, type, item_name, prop_name, value): |
| 7 | self.type = type |
| 8 | self.item = item_name |
| 9 | self.property = prop_name |
| 10 | self.value = value |
| 11 | |
| 12 | def _update_pointer(self, src_node, dst_template): |
| 13 | if type(self.value) is not list and len(self.value) < 1: |
| 14 | logging.warning( 'Incorrect mapping rule for property '+ self.property+ ': '+ self.value) |
| 15 | return |
| 16 | |
| 17 | if self.type == SUB_PROPERTY: |
| 18 | if self.value[0] == SUB_INPUT: |
| 19 | # if hasattr(dst_template, 'inputs') and dst_template.inputs.has_key(self.value[1]): |
| 20 | if hasattr(dst_template, 'inputs') and self.value[1] in dst_template.inputs: |
| 21 | if src_node is not None: |
| 22 | src_node.properties[self.property].sub_pointer = dst_template.inputs[self.value[1]] |
| 23 | if src_node.properties[self.property].required is True or src_node.properties[self.property].filled is True: |
| 24 | dst_template.inputs[self.value[1]].used = True |
| 25 | elif src_node is not None and src_node.properties[self.property].required is True: |
| 26 | logging.warning( 'Incorrect mapping rule for property '+ self.property+ ': no input named '+ self.value[1]) |
| 27 | # elif dst_template.node_dict.has_key(self.value[0]): |
| 28 | elif self.value[0] in dst_template.node_dict: |
| 29 | target_node = dst_template.node_dict[self.value[0]] |
| 30 | target_prop_item = target_node._get_property_item(self.value[1]) |
| 31 | if target_prop_item is not None: |
| 32 | if src_node is not None: |
| 33 | src_prop_item = src_node._get_property_item(self.property) |
| 34 | if src_prop_item.required is True or src_prop_item.filled is True: |
| 35 | target_prop_item.used = True |
| 36 | if src_prop_item is not None: |
| 37 | src_prop_item.sub_pointer = target_prop_item |
| 38 | else: |
| 39 | logging.warning( 'Incorrect mapping rule for property '+ self.property+ ': no property named '+ self.value[1]+ ' in node '+ self.value[0]) |
| 40 | else: |
| 41 | logging.warning('Incorrect mapping rule for property '+ self.property+ ': no node named '+ self.value[0]) |
| 42 | |
| 43 | elif self.type == SUB_ATTRIBUTE: |
| 44 | if self.value[0] == SUB_OUTPUT: |
| 45 | # if hasattr(dst_template, 'outputs') and dst_template.outputs.has_key(self.value[1]): |
| 46 | if hasattr(dst_template, 'outputs') and self.value[1] in dst_template.outputs: |
| 47 | if src_node is not None: |
| 48 | src_node.attributes[self.property].sub_pointer = dst_template.outputs[self.value[1]] |
| 49 | else: |
| 50 | logging.warning( 'Incorrect mapping rule for attribute '+ self.property+ ': no output named '+ self.value[1]) |
| 51 | |
| 52 | elif self.type == SUB_CAPABILITY: |
| 53 | if self.property is None: |
| 54 | # if dst_template.node_dict.has_key(self.value[0]): |
| 55 | if self.value[0] in dst_template.node_dict: |
| 56 | target_node = dst_template.node_dict[self.value[0]] |
| 57 | target_cap_item = target_node._get_capability_item(self.value[1]) |
| 58 | if target_cap_item is not None: |
| 59 | if src_node is not None: |
| 60 | src_cap_item = src_node._get_capability_item(self.item) |
| 61 | if src_cap_item is not None: |
| 62 | src_cap_item.sub_pointer = target_cap_item |
| 63 | for prop_name in src_cap_item.properties.keys(): |
| 64 | src_cap_item.properties[prop_name].sub_pointer = target_cap_item.properties[prop_name] |
| 65 | else: |
| 66 | logging.warning( 'Incorrect mapping rule for capability '+ self.item+ ': no capability named '+ self.value[1]+ ' in node '+ self.value[0]) |
| 67 | else: |
| 68 | logging.warning( 'Incorrect mapping rule for capability '+ self.item+ ': no node named '+ self.value[0]) |
| 69 | elif self.property == SUB_CAP_ID: |
| 70 | if self.value[0] == SUB_OUTPUT: |
| 71 | # if hasattr(dst_template, 'outputs') and dst_template.outputs.has_key(self.value[1]): |
| 72 | if hasattr(dst_template, 'outputs') and self.value[1] in dst_template.outputs: |
| 73 | target_node = dst_template.outputs[self.value[1]] |
| 74 | if src_node is not None: |
| 75 | src_cap_item = src_node._get_capability_item(self.item) |
| 76 | if src_cap_item is not None: |
| 77 | src_cap_item.sub_pointer = target_node |
| 78 | # elif dst_template.node_dict.has_key(self.value[0]): |
| 79 | elif self.value[0] in dst_template.node_dict: |
| 80 | target_node = dst_template.node_dict[self.value[0]] |
| 81 | if len(self.value) < 2: |
| 82 | target_item = target_node |
| 83 | # elif target_node.capabilities.has_key(self.value[1]) and len(self.value) > 1: |
| 84 | elif len(self.value) > 1 and self.value[1] in target_node.capabilities : |
| 85 | target_item = target_node._get_capability_property(self.value[1], self.value[2]) |
| 86 | elif self.value[1] in target_node.properties: |
| 87 | target_item = target_node._get_property_item(self.value[1]) |
| 88 | else: |
| 89 | target_item = None |
| 90 | logging.warning( 'Incorrect mapping rule for capability '+ self.item+ ': no capability/property named '+ self.value[1]+ ' in node '+ self.value[0]) |
| 91 | |
| 92 | if target_item is not None and src_node is not None: |
| 93 | src_cap_item = src_node._get_capability_item(self.item) |
| 94 | if src_cap_item is not None: |
| 95 | src_cap_item.sub_pointer = target_item |
| 96 | else: |
| 97 | if self.value[0] == SUB_INPUT: |
| 98 | # if hasattr(dst_template, 'inputs') and dst_template.inputs.has_key(self.value[1]): |
| 99 | if hasattr(dst_template, 'inputs') and self.value[1] in dst_template.inputs: |
| 100 | if src_node is not None: |
| 101 | src_cap_prop_item = src_node._get_capability_property(self.item, self.property) |
| 102 | src_cap_prop_item.sub_pointer = dst_template.inputs[self.value[1]] |
| 103 | if src_cap_prop_item.required is True or src_cap_prop_item.filled is True: |
| 104 | dst_template.inputs[self.value[1]].used = True |
| 105 | else: |
| 106 | logging.warning( 'Incorrect mapping rule for capability '+ self.item+ ': no input named '+ self.value[1]) |
| 107 | # elif dst_template.node_dict.has_key(self.value[0]): |
| 108 | elif self.value[0] in dst_template.node_dict: |
| 109 | target_node = dst_template.node_dict[self.value[0]] |
| 110 | |
| 111 | # if target_node.capabilities.has_key(self.value[1]): |
| 112 | if self.value[1] in target_node.capabilities: |
| 113 | target_cap_property = target_node._get_capability_property(self.value[1], self.value[2]) |
| 114 | if target_cap_property is not None: |
| 115 | if src_node is not None: |
| 116 | src_cap_prop_item = src_node._get_capability_property(self.item, self.property) |
| 117 | if src_cap_prop_item is not None: |
| 118 | src_cap_prop_item.sub_pointer = target_cap_property |
| 119 | else: |
| 120 | logging.warning( 'Incorrect mapping rule for capability '+ self.item+ ': no property named '+ self.value[2]+ ' in capability '+ self.value[0]+ '->'+ self.value[1]) |
| 121 | # elif target_node.properties.has_key(self.value[1]): |
| 122 | elif self.value[1] in target_node.properties: |
| 123 | target_prop_item = target_node._get_property_item(self.value[1]) |
| 124 | if src_node is not None: |
| 125 | src_cap_prop_item = src_node._get_capability_property(self.item, self.property) |
| 126 | if src_cap_prop_item is not None: |
| 127 | src_cap_prop_item.sub_pointer = target_prop_item |
| 128 | else: |
| 129 | logging.warning( 'Incorrect mapping rule for capability '+ self.item+ ': no capability/property named '+ self.value[1]+ ' in node '+ self.value[0]) |
| 130 | else: |
| 131 | logging.warning( 'Incorrect mapping rule for capability '+ self.item+ ': no node named '+ self.value[0]) |
| 132 | |
| 133 | elif self.type == SUB_REQUIREMENT: |
| 134 | if self.property is None: |
| 135 | # if dst_template.node_dict.has_key(self.value[0]): |
| 136 | if self.value[0] in dst_template.node_dict: |
| 137 | target_node = dst_template.node_dict[self.value[0]] |
| 138 | target_req_item = target_node._get_requirement_item_first(self.value[1]) |
| 139 | if target_req_item is not None: |
| 140 | if src_node is not None: |
| 141 | src_req_item = src_node._get_requirement_item_first(self.item) |
| 142 | if src_req_item is not None: |
| 143 | src_req_item.sub_pointer = target_req_item |
| 144 | else: |
| 145 | logging.warning( 'Incorrect mapping rule for requirement '+ self.item+ ': no requirement named '+ self.value[1]+ ' in node '+ self.value[0]) |
| 146 | else: |
| 147 | logging.warning( 'Incorrect mapping rule for requirement '+ self.item+ ': no node named '+ self.value[0]) |
| 148 | elif self.property == SUB_REQ_ID: |
| 149 | if self.value[0] == SUB_INPUT: |
| 150 | # if hasattr(dst_template, 'inputs') and dst_template.inputs.has_key(self.value[1]): |
| 151 | if hasattr(dst_template, 'inputs') and self.value[1] in dst_template.inputs: |
| 152 | if src_node is not None: |
| 153 | src_req_item = src_node._get_requirement_item_first(self.item) |
| 154 | if src_req_item is not None: |
| 155 | src_req_item.sub_pointer = dst_template.inputs[self.value[1]] |
| 156 | dst_template.inputs[self.value[1]].used = True |
| 157 | else: |
| 158 | logging.warning( 'Incorrect mapping rule for property '+ self.property+ ': no input named '+ self.value[1]) |
| 159 | |
| 160 | # elif dst_template.node_dict.has_key(self.value[0]): |
| 161 | elif self.value[0] in dst_template.node_dict: |
| 162 | target_node = dst_template.node_dict[self.value[0]] |
| 163 | target_prop_item = target_node._get_property_item(self.value[1]) |
| 164 | if target_prop_item is not None: |
| 165 | if src_node is not None: |
| 166 | src_req_item = src_node._get_requirement_item_first(self.item) |
| 167 | if src_req_item is not None: |
| 168 | src_req_item.sub_pointer = target_prop_item |
| 169 | else: |
| 170 | logging.warning( 'Incorrect mapping rule for requirement '+ self.item+ ': no property named '+ self.value[1]+ ' in node '+ self.value[0]) |
| 171 | else: |
| 172 | logging.warning( 'Incorrect mapping rule for requirement '+ self.item+ ': no node named '+ self.value[0]) |
| 173 | else: |
| 174 | logging.warning( 'Incorrect mapping rule for requirement '+ self.item+ ': wrong property name '+ self.property) |
| 175 | |
| 176 | else: |
| 177 | logging.warning('Incorrect mapping rule type: '+ self.type) |
| 178 | |
| 179 | |