TOSCA to YANG Translator Initial commit
[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 self.vnf = None
44 self.cp_name = None
45 pass
46
47 def handle_properties(self):
48 tosca_props = self.get_tosca_props()
49 self.log.debug(_("Port {0} with tosca properties: {1}").
50 format(self.name, tosca_props))
51 port_props = {}
52 for key, value in tosca_props.items():
53 port_props[key] = value
54
55 if 'cp_type' not in port_props:
56 port_props['cp_type'] = 'VPORT'
57 else:
58 if not port_props['cp_type'] in ToscaNetworkPort.VALID_TYPES:
59 err_msg = _("Invalid port type, {0}, specified for {1}"). \
60 format(port_props['cp_type'], self.name)
61 self.log.warn(err_msg)
62 raise ValidationError(message=err_msg)
63
64 if 'vdu_intf_type' not in port_props:
65 port_props['vdu_intf_type'] = 'VIRTIO'
66 else:
67 if not port_props['vdu_intf_type'] in ToscaNetworkPort.VALID_TYPES:
68 err_msg = _("Invalid port type, {0}, specified for {1}"). \
69 format(port_props['vdu_intf_type'], self.name)
70 self.log.warn(err_msg)
71 raise ValidationError(message=err_msg)
72
73 self.cp_name = port_props['name']
74 self.properties = port_props
75
76 def handle_requirements(self, nodes):
77 tosca_reqs = self.get_tosca_reqs()
78 tosca_caps = self.get_tosca_caps()
79 self.log.debug("VNF {0} requirements: {1}".
80 format(self.name, tosca_reqs))
81
82 vnf = None # Need vnf ref to generate cp refs in vld
83 vld = None
84 '''
85 if len(tosca_reqs) != 2:
86 err_msg = _("Invalid configuration as incorrect number of "
87 "requirements for CP {0} are specified"). \
88 format(self)
89 self.log.error(err_msg)
90 raise ValidationError(message=err_msg)
91 '''
92 for req in tosca_reqs:
93 if 'virtualBinding' in req:
94 target = req['virtualBinding']['target']
95 node = self.get_node_with_name(target, nodes)
96 if node:
97 vnf = node.vnf
98 self.vnf = node._vnf
99 if not vnf:
100 err_msg = _("No vnfs linked to a VDU {0}"). \
101 format(node)
102 self.log.error(err_msg)
103 raise ValidationError(message=err_msg)
104 cp = {}
105 cp['name'] = self.properties['name']
106 cp['type'] = self.properties['cp_type']
107 self.log.debug(_("Connection Point entry for VNF {0}:{1}").
108 format(vnf, cp))
109 if 'connection-point' not in vnf.properties:
110 vnf.properties['connection-point'] = []
111 vnf.properties['connection-point'].append(cp)
112 ext_intf = {}
113 ext_intf['name'] = self.properties['vdu_intf_name']
114 ext_intf['virtual-interface'] = \
115 {'type': self.properties['vdu_intf_type']}
116 ext_intf['vnfd-connection-point-ref'] = \
117 self.properties['name']
118 if 'external-interface' not in node.properties:
119 node.properties['external-interface'] = []
120 node.properties['external-interface'].append(ext_intf)
121 else:
122 err_msg = _("Connection point {0}, VDU {1} "
123 "specified not found"). \
124 format(self.name, target)
125 self.log.error(err_msg)
126 raise ValidationError(message=err_msg)
127 elif 'virtualLink' in req:
128 target = req['virtualLink']['target']
129 node = self.get_node_with_name(target, nodes)
130 if node:
131 vld = node
132 else:
133 err_msg = _("CP {0}, VL {1} specified not found"). \
134 format(self, target)
135 self.log.error(err_msg)
136 raise ValidationError(message=err_msg)
137
138 if 'sfc' in tosca_caps and vnf:
139 if 'sfc_type' in tosca_caps['sfc']:
140 vnf.properties['service-function-chain'] = tosca_caps['sfc']['sfc_type'].upper()
141 if 'sf_type' in tosca_caps['sfc']:
142 vnf.properties['service-function-type'] = tosca_caps['sfc']['sf_type']
143
144 if vnf:
145 cp_ref = {}
146 cp_ref['vnfd-connection-point-ref'] = self.properties['name']
147 cp_ref['vnfd-id-ref'] = vnf.properties['id']
148 cp_ref['member-vnf-index-ref'] = \
149 vnf._const_vnfd['member-vnf-index']
150 else:
151 err_msg = _("CP {0}, VNF {1} not found"). \
152 format(self, vnf, vld)
153 self.log.error(err_msg)
154 raise ValidationError(message=err_msg)