| peusterm | 79ef6ae | 2016-07-08 13:53:57 +0200 | [diff] [blame] | 1 | """ |
| 2 | Copyright (c) 2015 SONATA-NFV and Paderborn University |
| 3 | ALL RIGHTS RESERVED. |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | |
| 17 | Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION] |
| 18 | nor the names of its contributors may be used to endorse or promote |
| 19 | products derived from this software without specific prior written |
| 20 | permission. |
| 21 | |
| 22 | This work has been performed in the framework of the SONATA project, |
| 23 | funded by the European Commission under Grant number 671517 through |
| 24 | the Horizon 2020 and 5G-PPP programmes. The authors would like to |
| 25 | acknowledge the contributions of their colleagues of the SONATA |
| 26 | partner consortium (www.sonata-nfv.eu). |
| 27 | """ |
| stevenvanrossem | e131bf5 | 2016-07-14 11:42:09 +0200 | [diff] [blame] | 28 | |
| 29 | """ |
| 30 | Distributed Cloud Emulator (dcemulator) |
| 31 | Networking and monitoring functions |
| 32 | (c) 2015 by Steven Van Rossem <steven.vanrossem@intec.ugent.be> |
| 33 | """ |
| 34 | |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 35 | import logging |
| 36 | from flask_restful import Resource |
| 37 | from flask import request |
| 38 | import json |
| 39 | |
| 40 | logging.basicConfig(level=logging.INFO) |
| 41 | |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 42 | CORS_HEADER = {'Access-Control-Allow-Origin': '*'} |
| 43 | |
| stevenvanrossem | 4fac2af | 2016-12-22 01:26:02 +0100 | [diff] [blame] | 44 | # the global net is set from the topology file, and connected via connectDCNetwork function in rest_api_endpoint.py |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 45 | net = None |
| 46 | |
| 47 | |
| 48 | class NetworkAction(Resource): |
| 49 | """ |
| 50 | Add or remove chains between VNFs. These chain links are implemented as flow entries in the networks' SDN switches. |
| 51 | :param vnf_src_name: VNF name of the source of the link |
| 52 | :param vnf_dst_name: VNF name of the destination of the link |
| 53 | :param vnf_src_interface: VNF interface name of the source of the link |
| 54 | :param vnf_dst_interface: VNF interface name of the destination of the link |
| 55 | :param weight: weight of the link (can be useful for routing calculations) |
| 56 | :param match: OpenFlow match format of the flow entry |
| 57 | :param bidirectional: boolean value if the link needs to be implemented from src to dst and back |
| 58 | :param cookie: cookie value, identifier of the flow entry to be installed. |
| stevenvanrossem | 65819b8 | 2016-08-05 18:21:47 +0200 | [diff] [blame] | 59 | :param priority: integer indicating the priority of the flow entry |
| stevenvanrossem | bf1754e | 2016-11-17 10:20:52 +0100 | [diff] [blame] | 60 | :param skip_vlan_tag: boolean to indicate whether a new vlan tag should be created for this chain |
| 61 | :param monitor: boolean to indicate whether a new vlan tag should be created for this chain |
| 62 | :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] | 63 | :return: message string indicating if the chain action is succesful or not |
| 64 | """ |
| 65 | |
| 66 | global net |
| 67 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 68 | def put(self): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 69 | logging.debug("REST CALL: network chain add") |
| 70 | command = 'add-flow' |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 71 | return self._NetworkAction(command=command) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 72 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 73 | def delete(self): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 74 | logging.debug("REST CALL: network chain remove") |
| 75 | command = 'del-flows' |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 76 | return self._NetworkAction(command=command) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 77 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 78 | def _NetworkAction(self, command=None): |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 79 | # call DCNetwork method, not really datacenter specific API for now... |
| 80 | # no check if vnfs are really connected to this datacenter... |
| 81 | try: |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 82 | # check if json data is a dict |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 83 | data = request.args |
| 84 | # try json payload |
| 85 | if data is None: |
| 86 | data = request.json |
| 87 | # then no data |
| stevenvanrossem | 1027edc | 2016-07-14 22:02:02 +0200 | [diff] [blame] | 88 | if data is None: |
| 89 | data = {} |
| 90 | elif type(data) is not dict: |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 91 | data = json.loads(request.json) |
| 92 | |
| stevenvanrossem | 3c544ac | 2017-02-08 12:11:07 +0100 | [diff] [blame] | 93 | vnf_src_name = data.get("vnf_src_name") |
| 94 | vnf_dst_name = data.get("vnf_dst_name") |
| stevenvanrossem | ff6b404 | 2016-07-14 20:51:37 +0200 | [diff] [blame] | 95 | vnf_src_interface = data.get("vnf_src_interface") |
| 96 | vnf_dst_interface = data.get("vnf_dst_interface") |
| 97 | weight = data.get("weight") |
| 98 | match = data.get("match") |
| 99 | bidirectional = data.get("bidirectional") |
| 100 | cookie = data.get("cookie") |
| stevenvanrossem | 61699eb | 2016-08-05 15:57:59 +0200 | [diff] [blame] | 101 | priority = data.get("priority") |
| stevenvanrossem | becc7c5 | 2016-11-07 05:52:01 +0100 | [diff] [blame] | 102 | skip_vlan_tag = data.get("skip_vlan_tag") |
| 103 | monitor = data.get("monitor") |
| 104 | monitor_placement = data.get("monitor_placement") |
| 105 | |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 106 | c = net.setChain( |
| 107 | vnf_src_name, vnf_dst_name, |
| 108 | vnf_src_interface=vnf_src_interface, |
| 109 | vnf_dst_interface=vnf_dst_interface, |
| 110 | cmd=command, |
| 111 | weight=weight, |
| 112 | match=match, |
| 113 | bidirectional=bidirectional, |
| stevenvanrossem | 61699eb | 2016-08-05 15:57:59 +0200 | [diff] [blame] | 114 | cookie=cookie, |
| stevenvanrossem | becc7c5 | 2016-11-07 05:52:01 +0100 | [diff] [blame] | 115 | priority=priority, |
| 116 | skip_vlan_tag=skip_vlan_tag, |
| 117 | monitor=monitor, |
| 118 | monitor_placement=monitor_placement) |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 119 | # return setChain response |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 120 | return str(c), 200, CORS_HEADER |
| stevenvanrossem | 73efd19 | 2016-06-29 01:44:07 +0200 | [diff] [blame] | 121 | except Exception as ex: |
| 122 | logging.exception("API error.") |
| peusterm | dfc1460 | 2017-01-13 08:22:45 +0100 | [diff] [blame] | 123 | return ex.message, 500, CORS_HEADER |