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
,
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
))
52 for key
, value
in tosca_props
.items():
53 port_props
[key
] = value
55 if 'cp_type' not in port_props
:
56 port_props
['cp_type'] = 'VPORT'
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
)
64 if 'vdu_intf_type' not in port_props
:
65 port_props
['vdu_intf_type'] = 'VIRTIO'
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
)
73 self
.cp_name
= port_props
['name']
74 self
.properties
= port_props
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
))
82 vnf
= None # Need vnf ref to generate cp refs in vld
85 if len(tosca_reqs) != 2:
86 err_msg = _("Invalid configuration as incorrect number of "
87 "requirements for CP {0} are specified"). \
89 self.log.error(err_msg)
90 raise ValidationError(message=err_msg)
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
)
100 err_msg
= _("No vnfs linked to a VDU {0}"). \
102 self
.log
.error(err_msg
)
103 raise ValidationError(message
=err_msg
)
105 cp
['name'] = self
.properties
['name']
106 cp
['type'] = self
.properties
['cp_type']
107 self
.log
.debug(_("Connection Point entry for VNF {0}:{1}").
109 if 'connection-point' not in vnf
.properties
:
110 vnf
.properties
['connection-point'] = []
111 vnf
.properties
['connection-point'].append(cp
)
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
)
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
)
133 err_msg
= _("CP {0}, VL {1} specified not found"). \
135 self
.log
.error(err_msg
)
136 raise ValidationError(message
=err_msg
)
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']
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']
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
)