04e3a5926bcb826c3dd78bb7771bfc8f8d08ddc5
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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
14 # Copyright 2016 RIFT.io Inc
17 from rift
.mano
.tosca_translator
.common
.utils
import _
18 from rift
.mano
.tosca_translator
.rwmano
.syntax
.mano_resource
import ManoResource
20 from toscaparser
.common
.exception
import ValidationError
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'
29 class ToscaNetworkPort(ManoResource
):
30 '''Translate TOSCA node type tosca.nodes.network.Port.'''
32 toscatype
= 'tosca.nodes.network.Port'
34 VALID_TYPES
= ['VIRTIO', 'VPORT']
36 def __init__(self
, log
, nodetemplate
, metadata
=None):
37 super(ToscaNetworkPort
, self
).__init
__(log
,
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
))
50 for key
, value
in tosca_props
.items():
51 port_props
[key
] = value
53 if 'cp_type' not in port_props
:
54 port_props
['cp_type'] = 'VPORT'
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
)
62 if 'vdu_intf_type' not in port_props
:
63 port_props
['vdu_intf_type'] = 'VIRTIO'
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
)
71 self
.properties
= port_props
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
))
78 vnf
= None # Need vnf ref to generate cp refs in vld
80 if len(tosca_reqs
) != 2:
81 err_msg
= _("Invalid configuration as incorrect number of "
82 "requirements for CP {0} are specified"). \
84 self
.log
.error(err_msg
)
85 raise ValidationError(message
=err_msg
)
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
)
94 err_msg
= _("No vnfs linked to a VDU {0}"). \
96 self
.log
.error(err_msg
)
97 raise ValidationError(message
=err_msg
)
99 cp
['name'] = self
.properties
['name']
100 cp
['type'] = self
.properties
['cp_type']
101 self
.log
.debug(_("Connection Point entry for VNF {0}:{1}").
103 if 'connection-point' not in vnf
.properties
:
104 vnf
.properties
['connection-point'] = []
105 vnf
.properties
['connection-point'].append(cp
)
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
)
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
)
127 err_msg
= _("CP {0}, VL {1} specified not found"). \
129 self
.log
.error(err_msg
)
130 raise ValidationError(message
=err_msg
)
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
)
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
)