| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 1 | # Copyright (c) 2015 SONATA-NFV and Paderborn University |
| 2 | # ALL RIGHTS RESERVED. |
| 3 | # |
| 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 |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 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. |
| 15 | # |
| 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 |
| 19 | # permission. |
| 20 | # |
| 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). |
| 26 | # |
| 27 | # Distributed Cloud Emulator (dcemulator) |
| 28 | # Networking and monitoring functions |
| 29 | # (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be> |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 30 | import logging |
| 31 | from flask_restful import Resource |
| 32 | from flask import request |
| stevenvanrossem | f371201 | 2017-05-04 00:01:52 +0200 | [diff] [blame] | 33 | import networkx |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 34 | |
| peusterm | 5b42874 | 2017-06-16 10:08:11 +0200 | [diff] [blame] | 35 | logging.basicConfig() |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 36 | |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 37 | CORS_HEADER = {'Access-Control-Allow-Origin': '*'} |
| 38 | |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 39 | # the global net is set from the topology file, and connected via |
| 40 | # connectDCNetwork function in rest_api_endpoint.py |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 41 | net = None |
| 42 | |
| 43 | |
| 44 | class NetworkAction(Resource): |
| 45 | """ |
| 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. |
| stevenvanrossem | 65819b8 | 2016-08-05 18:21:47 +0200 | [diff] [blame] | 55 | :param priority: integer indicating the priority of the flow entry |
| stevenvanrossem | bf1754e | 2016-11-17 10:20:52 +0100 | [diff] [blame] | 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 |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 59 | :return: message string indicating if the chain action is succesful or not |
| 60 | """ |
| 61 | |
| 62 | global net |
| 63 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 64 | def put(self): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 65 | logging.debug("REST CALL: network chain add") |
| 66 | command = 'add-flow' |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 67 | return self._NetworkAction(command=command) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 68 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 69 | def delete(self): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 70 | logging.debug("REST CALL: network chain remove") |
| 71 | command = 'del-flows' |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 72 | return self._NetworkAction(command=command) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 73 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 74 | def _NetworkAction(self, command=None): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 75 | # call DCNetwork method, not really datacenter specific API for now... |
| 76 | # no check if vnfs are really connected to this datacenter... |
| 77 | try: |
| stevenvanrossem | 284ba2b | 2017-06-01 16:17:51 +0200 | [diff] [blame] | 78 | # check json payload |
| stevenvanrossem | 167aa3c | 2017-06-01 16:45:42 +0200 | [diff] [blame] | 79 | logging.debug("json: {}".format(request.json)) |
| 80 | logging.debug("args: {}".format(request.args)) |
| stevenvanrossem | 284ba2b | 2017-06-01 16:17:51 +0200 | [diff] [blame] | 81 | |
| stevenvanrossem | f693a3b | 2017-06-01 15:15:59 +0200 | [diff] [blame] | 82 | data = request.json |
| stevenvanrossem | 1027edc | 2016-07-14 22:02:02 +0200 | [diff] [blame] | 83 | if data is None: |
| stevenvanrossem | 1085e7e | 2017-06-01 16:37:52 +0200 | [diff] [blame] | 84 | data = request.args |
| 85 | if data is None: |
| stevenvanrossem | 1027edc | 2016-07-14 22:02:02 +0200 | [diff] [blame] | 86 | data = {} |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 87 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 88 | vnf_src_name = data.get("vnf_src_name") |
| 89 | vnf_dst_name = data.get("vnf_dst_name") |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 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") |
| stevenvanrossem | 61699eb | 2016-08-05 15:57:59 +0200 | [diff] [blame] | 96 | priority = data.get("priority") |
| stevenvanrossem | becc7c5 | 2016-11-07 05:52:01 +0100 | [diff] [blame] | 97 | skip_vlan_tag = data.get("skip_vlan_tag") |
| 98 | monitor = data.get("monitor") |
| 99 | monitor_placement = data.get("monitor_placement") |
| 100 | |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 101 | c = net.setChain( |
| 102 | vnf_src_name, vnf_dst_name, |
| 103 | vnf_src_interface=vnf_src_interface, |
| 104 | vnf_dst_interface=vnf_dst_interface, |
| 105 | cmd=command, |
| 106 | weight=weight, |
| 107 | match=match, |
| 108 | bidirectional=bidirectional, |
| stevenvanrossem | 61699eb | 2016-08-05 15:57:59 +0200 | [diff] [blame] | 109 | cookie=cookie, |
| stevenvanrossem | becc7c5 | 2016-11-07 05:52:01 +0100 | [diff] [blame] | 110 | priority=priority, |
| 111 | skip_vlan_tag=skip_vlan_tag, |
| 112 | monitor=monitor, |
| 113 | monitor_placement=monitor_placement) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 114 | # return setChain response |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 115 | return str(c), 200, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 116 | except Exception as ex: |
| 117 | logging.exception("API error.") |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 118 | return ex.message, 500, CORS_HEADER |
| stevenvanrossem | ba51a81 | 2017-04-23 01:22:59 +0200 | [diff] [blame] | 119 | |
| 120 | |
| 121 | class DrawD3jsgraph(Resource): |
| 122 | |
| 123 | global net |
| 124 | |
| 125 | def get(self): |
| 126 | nodes = list() |
| 127 | nodes2 = list() |
| 128 | links = list() |
| 129 | # add all DCs |
| stevenvanrossem | f371201 | 2017-05-04 00:01:52 +0200 | [diff] [blame] | 130 | node_attr = networkx.get_node_attributes(net.DCNetwork_graph, 'type') |
| stevenvanrossem | ba51a81 | 2017-04-23 01:22:59 +0200 | [diff] [blame] | 131 | for node_name in net.DCNetwork_graph.nodes(): |
| 132 | nodes2.append(node_name) |
| stevenvanrossem | f371201 | 2017-05-04 00:01:52 +0200 | [diff] [blame] | 133 | type = node_attr[node_name] |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 134 | node_dict = {"name": node_name, "group": type} |
| stevenvanrossem | ba51a81 | 2017-04-23 01:22:59 +0200 | [diff] [blame] | 135 | nodes.append(node_dict) |
| 136 | |
| 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) |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 142 | edge_dict = {"source": node1_index, |
| 143 | "target": node2_index, "value": 10} |
| stevenvanrossem | ba51a81 | 2017-04-23 01:22:59 +0200 | [diff] [blame] | 144 | links.append(edge_dict) |
| 145 | |
| peusterm | 72f0988 | 2018-05-15 17:10:27 +0200 | [diff] [blame] | 146 | json = {"nodes": nodes, "links": links} |
| peusterm | 5b42874 | 2017-06-16 10:08:11 +0200 | [diff] [blame] | 147 | return json, 200, CORS_HEADER |