1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
27 # Distributed Cloud Emulator (dcemulator)
28 # Networking and monitoring functions
29 # (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be>
31 from flask_restful
import Resource
32 from flask
import request
37 CORS_HEADER
= {'Access-Control-Allow-Origin': '*'}
39 # the global net is set from the topology file, and connected via
40 # connectDCNetwork function in rest_api_endpoint.py
44 class NetworkAction(Resource
):
46 Add or remove chains between VNFs. These chain links are implemented as flow entries in the networks' SDN switches.
47 :param vnf_src_name: VNF name of the source of the link
48 :param vnf_dst_name: VNF name of the destination of the link
49 :param vnf_src_interface: VNF interface name of the source of the link
50 :param vnf_dst_interface: VNF interface name of the destination of the link
51 :param weight: weight of the link (can be useful for routing calculations)
52 :param match: OpenFlow match format of the flow entry
53 :param bidirectional: boolean value if the link needs to be implemented from src to dst and back
54 :param cookie: cookie value, identifier of the flow entry to be installed.
55 :param priority: integer indicating the priority of the flow entry
56 :param skip_vlan_tag: boolean to indicate whether a new vlan tag should be created for this chain
57 :param monitor: boolean to indicate whether a new vlan tag should be created for this chain
58 :param monitor_placement: 'tx'=place the monitoring flowrule at the beginning of the chain, 'rx'=place at the end of the chain
59 :return: message string indicating if the chain action is succesful or not
65 logging
.debug("REST CALL: network chain add")
67 return self
._NetworkAction
(command
=command
)
70 logging
.debug("REST CALL: network chain remove")
72 return self
._NetworkAction
(command
=command
)
74 def _NetworkAction(self
, command
=None):
75 # call DCNetwork method, not really datacenter specific API for now...
76 # no check if vnfs are really connected to this datacenter...
79 logging
.debug("json: {}".format(request
.json
))
80 logging
.debug("args: {}".format(request
.args
))
88 vnf_src_name
= data
.get("vnf_src_name")
89 vnf_dst_name
= data
.get("vnf_dst_name")
90 vnf_src_interface
= data
.get("vnf_src_interface")
91 vnf_dst_interface
= data
.get("vnf_dst_interface")
92 weight
= data
.get("weight")
93 match
= data
.get("match")
94 bidirectional
= data
.get("bidirectional")
95 cookie
= data
.get("cookie")
96 priority
= data
.get("priority")
97 skip_vlan_tag
= data
.get("skip_vlan_tag")
98 monitor
= data
.get("monitor")
99 monitor_placement
= data
.get("monitor_placement")
102 vnf_src_name
, vnf_dst_name
,
103 vnf_src_interface
=vnf_src_interface
,
104 vnf_dst_interface
=vnf_dst_interface
,
108 bidirectional
=bidirectional
,
111 skip_vlan_tag
=skip_vlan_tag
,
113 monitor_placement
=monitor_placement
)
114 # return setChain response
115 return str(c
), 200, CORS_HEADER
116 except Exception as ex
:
117 logging
.exception("API error.")
118 return ex
.message
, 500, CORS_HEADER
121 class DrawD3jsgraph(Resource
):
130 node_attr
= networkx
.get_node_attributes(net
.DCNetwork_graph
, 'type')
131 for node_name
in net
.DCNetwork_graph
.nodes():
132 nodes2
.append(node_name
)
133 type = node_attr
[node_name
]
134 node_dict
= {"name": node_name
, "group": type}
135 nodes
.append(node_dict
)
137 # add links between other DCs
138 for node1_name
in net
.DCNetwork_graph
.nodes():
139 node1_index
= nodes2
.index(node1_name
)
140 for node2_name
in net
.DCNetwork_graph
.neighbors(node1_name
):
141 node2_index
= nodes2
.index(node2_name
)
142 edge_dict
= {"source": node1_index
,
143 "target": node2_index
, "value": 10}
144 links
.append(edge_dict
)
146 json
= {"nodes": nodes
, "links": links
}
147 return json
, 200, CORS_HEADER