04e3a5926bcb826c3dd78bb7771bfc8f8d08ddc5
[osm/SO.git] / common / python / rift / mano / tosca_translator / rwmano / tosca / tosca_network_port.py
1 #
2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
12 # under the License.
13 #
14 # Copyright 2016 RIFT.io Inc
15
16
17 from rift.mano.tosca_translator.common.utils import _
18 from rift.mano.tosca_translator.rwmano.syntax.mano_resource import ManoResource
19
20 from toscaparser.common.exception import ValidationError
21
22
23 # Name used to dynamically load appropriate map class.
24 TARGET_CLASS_NAME = 'ToscaNetworkPort'
25 TOSCA_LINKS_TO = 'tosca.relationships.network.LinksTo'
26 TOSCA_BINDS_TO = 'tosca.relationships.network.BindsTo'
27
28
29 class ToscaNetworkPort(ManoResource):
30 '''Translate TOSCA node type tosca.nodes.network.Port.'''
31
32 toscatype = 'tosca.nodes.network.Port'
33
34 VALID_TYPES = ['VIRTIO', 'VPORT']
35
36 def __init__(self, log, nodetemplate, metadata=None):
37 super(ToscaNetworkPort, self).__init__(log,
38 nodetemplate,
39 type_='port',
40 metadata=metadata)
41 # Default order
42 self.order = 0
43 pass
44
45 def handle_properties(self):
46 tosca_props = self.get_tosca_props()
47 self.log.debug(_("Port {0} with tosca properties: {1}").
48 format(self.name, tosca_props))
49 port_props = {}
50 for key, value in tosca_props.items():
51 port_props[key] = value
52
53 if 'cp_type' not in port_props:
54 port_props['cp_type'] = 'VPORT'
55 else:
56 if not port_props['cp_type'] in ToscaNetworkPort.VALID_TYPES:
57 err_msg = _("Invalid port type, {0}, specified for {1}"). \
58 format(port_props['cp_type'], self.name)
59 self.log.warn(err_msg)
60 raise ValidationError(message=err_msg)
61
62 if 'vdu_intf_type' not in port_props:
63 port_props['vdu_intf_type'] = 'VIRTIO'
64 else:
65 if not port_props['vdu_intf_type'] in ToscaNetworkPort.VALID_TYPES:
66 err_msg = _("Invalid port type, {0}, specified for {1}"). \
67 format(port_props['vdu_intf_type'], self.name)
68 self.log.warn(err_msg)
69 raise ValidationError(message=err_msg)
70
71 self.properties = port_props
72
73 def handle_requirements(self, nodes):
74 tosca_reqs = self.get_tosca_reqs()
75 self.log.debug("VNF {0} requirements: {1}".
76 format(self.name, tosca_reqs))
77
78 vnf = None # Need vnf ref to generate cp refs in vld
79 vld = None
80 if len(tosca_reqs) != 2:
81 err_msg = _("Invalid configuration as incorrect number of "
82 "requirements for CP {0} are specified"). \
83 format(self)
84 self.log.error(err_msg)
85 raise ValidationError(message=err_msg)
86
87 for req in tosca_reqs:
88 if 'virtualBinding' in req:
89 target = req['virtualBinding']['target']
90 node = self.get_node_with_name(target, nodes)
91 if node:
92 vnf = node.vnf
93 if not vnf:
94 err_msg = _("No vnfs linked to a VDU {0}"). \
95 format(node)
96 self.log.error(err_msg)
97 raise ValidationError(message=err_msg)
98 cp = {}
99 cp['name'] = self.properties['name']
100 cp['type'] = self.properties['cp_type']
101 self.log.debug(_("Connection Point entry for VNF {0}:{1}").
102 format(vnf, cp))
103 if 'connection-point' not in vnf.properties:
104 vnf.properties['connection-point'] = []
105 vnf.properties['connection-point'].append(cp)
106 ext_intf = {}
107 ext_intf['name'] = self.properties['vdu_intf_name']
108 ext_intf['virtual-interface'] = \
109 {'type': self.properties['vdu_intf_type']}
110 ext_intf['vnfd-connection-point-ref'] = \
111 self.properties['name']
112 if 'external-interface' not in node.properties:
113 node.properties['external-interface'] = []
114 node.properties['external-interface'].append(ext_intf)
115 else:
116 err_msg = _("Connection point {0}, VDU {1} "
117 "specified not found"). \
118 format(self.name, target)
119 self.log.error(err_msg)
120 raise ValidationError(message=err_msg)
121 elif 'virtualLink' in req:
122 target = req['virtualLink']['target']
123 node = self.get_node_with_name(target, nodes)
124 if node:
125 vld = node
126 else:
127 err_msg = _("CP {0}, VL {1} specified not found"). \
128 format(self, target)
129 self.log.error(err_msg)
130 raise ValidationError(message=err_msg)
131
132 if vnf and vld:
133 cp_ref = {}
134 cp_ref['vnfd-connection-point-ref'] = self.properties['name']
135 cp_ref['vnfd-id-ref'] = vnf.properties['id']
136 cp_ref['member-vnf-index-ref'] = \
137 vnf._const_vnfd['member-vnf-index']
138 if 'vnfd-connection-point-ref' not in vld.properties:
139 vld.properties['vnfd-connection-point-ref'] = []
140 vld.properties['vnfd-connection-point-ref'].append(cp_ref)
141 else:
142 err_msg = _("CP {0}, VNF {1} or VL {2} not found"). \
143 format(self, vnf, vld)
144 self.log.error(err_msg)
145 raise ValidationError(message=err_msg)