From: tierno Date: Wed, 11 Dec 2019 15:30:44 +0000 (+0000) Subject: Fix 975 SDN-net error when plugin cannot be loaded X-Git-Tag: v7.0.1rc1~8 X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FRO.git;a=commitdiff_plain;h=4126d05e24ada55226bb13a9d556655811cedadc;hp=1d2f2609c00490a2b25ffedfc01ff97bc3ed571d Fix 975 SDN-net error when plugin cannot be loaded Do not validate SDN type Adding SDN floodlight openflow plugin Change-Id: I3b997ed16cc67c18f0deee57e38a4e355a7eab4d Signed-off-by: tierno --- diff --git a/Dockerfile-local b/Dockerfile-local index 3c9540e5..6fc109be 100644 --- a/Dockerfile-local +++ b/Dockerfile-local @@ -57,6 +57,8 @@ RUN /root/RO/RO/osm_ro/scripts/install-osm-im.sh --develop && \ python3 -m pip install -e /root/RO/RO-SDN-dynpac && \ python3 -m pip install -e /root/RO/RO-SDN-tapi && \ python3 -m pip install -e /root/RO/RO-SDN-onos_vpls && \ + python3 -m pip install -e /root/RO/RO-SDN-onos_openflow && \ + python3 -m pip install -e /root/RO/RO-SDN-floodlight_openflow && \ rm -rf /root/.cache && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* diff --git a/RO-SDN-floodlight_openflow/Makefile b/RO-SDN-floodlight_openflow/Makefile new file mode 100644 index 00000000..c1965f33 --- /dev/null +++ b/RO-SDN-floodlight_openflow/Makefile @@ -0,0 +1,24 @@ +## +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## + +all: clean package + +clean: + rm -rf dist deb_dist osm_rosdn_floodlightof-*.tar.gz osm_rosdn_floodlightof.egg-info .eggs + +package: + python3 setup.py --command-packages=stdeb.command sdist_dsc + cd deb_dist/osm-rosdn-floodlightof*/ && dpkg-buildpackage -rfakeroot -uc -us + diff --git a/RO-SDN-floodlight_openflow/osm_rosdn_floodlightof/floodlight_of.py b/RO-SDN-floodlight_openflow/osm_rosdn_floodlightof/floodlight_of.py new file mode 100644 index 00000000..a2740006 --- /dev/null +++ b/RO-SDN-floodlight_openflow/osm_rosdn_floodlightof/floodlight_of.py @@ -0,0 +1,485 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +## +# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U. +# This file is part of openvim +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +# For those usages not covered by the Apache License, Version 2.0 please +# contact with: nfvlabs@tid.es +## + +""" +Implement the plugging for floodligth openflow controller +It creates the class OF_conn to create dataplane connections +with static rules based on packet destination MAC address +""" + +__author__ = "Pablo Montes, Alfonso Tierno" +__date__ = "$28-oct-2014 12:07:15$" + +import json +import requests +import logging +from osm_ro.wim.openflow_conn import OpenflowConn, OpenflowConnUnexpectedResponse, OpenflowConnConnectionException + + +class OfConnFloodLight(OpenflowConn): + """ + Openflow Connector for Floodlight. + No MAC learning is used + version 0.9 or 1.X is autodetected + version 1.X is in progress, not finished!!! + """ + + def __init__(self, params): + """ + Constructor + :param params: dictionary with the following keys: + of_dpid: DPID to use for this controller ?? Does a controller have a dpid? + url: must be [http://HOST:PORT/] + of_user: user credentials, can be missing or None + of_password: password credentials + of_debug: debug level for logging. Default to ERROR + other keys are ignored + Raise an exception if same parameter is missing or wrong + """ + # check params + url = params.get("of_url") + if not url: + raise ValueError("'url' must be provided") + if not url.startswith("http"): + url = "http://" + url + if not url.endswith("/"): + url = url + "/" + self.url = url + + OpenflowConn.__init__(self, params) + + self.name = "Floodlight" + self.dpid = str(params["of_dpid"]) + + self.pp2ofi = {} # From Physical Port to OpenFlow Index + self.ofi2pp = {} # From OpenFlow Index to Physical Port + self.headers = {'content-type': 'application/json', 'Accept': 'application/json'} + self.version = None + self.logger = logging.getLogger('SDN.floodlightOF') + self.logger.setLevel(params.get("of_debug", "ERROR")) + self._set_version(params.get("of_version")) + + def _set_version(self, version): + """ + set up a version of the controller. + Depending on the version it fills the self.ver_names with the naming used in this version + :param version: Openflow controller version + :return: Raise an ValueError exception if same parameter is missing or wrong + """ + # static version names + if version is None: + self.version = None + elif version == "0.9": + self.version = version + self.name = "Floodlightv0.9" + self.ver_names = { + "dpid": "dpid", + "URLmodifier": "staticflowentrypusher", + "destmac": "dst-mac", + "vlanid": "vlan-id", + "inport": "ingress-port", + "setvlan": "set-vlan-id", + "stripvlan": "strip-vlan", + } + elif version[0] == "1": # version 1.X + self.version = version + self.name = "Floodlightv1.X" + self.ver_names = { + "dpid": "switchDPID", + "URLmodifier": "staticflowpusher", + "destmac": "eth_dst", + "vlanid": "eth_vlan_vid", + "inport": "in_port", + "setvlan": "set_vlan_vid", + "stripvlan": "strip_vlan", + } + else: + raise ValueError("Invalid version for floodlight controller") + + def get_of_switches(self): + """ + Obtain a a list of switches or DPID detected by this controller + :return: list where each element a tuple pair (DPID, IP address) + Raise an OpenflowconnConnectionException or OpenflowconnConnectionException exception if same + parameter is missing or wrong + """ + try: + of_response = requests.get(self.url + "wm/core/controller/switches/json", headers=self.headers) + error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text) + if of_response.status_code != 200: + self.logger.warning("get_of_switches " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + self.logger.debug("get_of_switches " + error_text) + info = of_response.json() + if not isinstance(info, (list, tuple)): + self.logger.error("get_of_switches. Unexpected response not a list %s", str(type(info))) + raise OpenflowConnUnexpectedResponse("Unexpected response, not a list. Wrong version?") + if len(info) == 0: + return info + # autodiscover version + if self.version is None: + if 'dpid' in info[0] and 'inetAddress' in info[0]: + self._set_version("0.9") + # elif 'switchDPID' in info[0] and 'inetAddress' in info[0]: + # self._set_version("1.X") + else: + self.logger.error("get_of_switches. Unexpected response, not found 'dpid' or 'switchDPID' " + "field: %s", str(info[0])) + raise OpenflowConnUnexpectedResponse("Unexpected response, not found 'dpid' or " + "'switchDPID' field. Wrong version?") + + switch_list = [] + for switch in info: + switch_list.append((switch[self.ver_names["dpid"]], switch['inetAddress'])) + return switch_list + except requests.exceptions.RequestException as e: + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("get_of_switches " + error_text) + raise OpenflowConnConnectionException(error_text) + except Exception as e: + # ValueError in the case that JSON can not be decoded + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("get_of_switches " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + + def get_of_rules(self, translate_of_ports=True): + """ + Obtain the rules inserted at openflow controller + :param translate_of_ports: if True it translates ports from openflow index to physical switch name + :return: list where each item is a dictionary with the following content: + priority: rule priority + name: rule name (present also as the master dict key) + ingress_port: match input port of the rule + dst_mac: match destination mac address of the rule, can be missing or None if not apply + vlan_id: match vlan tag of the rule, can be missing or None if not apply + actions: list of actions, composed by a pair tuples: + (vlan, None/int): for stripping/setting a vlan tag + (out, port): send to this port + switch: DPID, all + Raise an openflowconnUnexpectedResponse exception if fails with text_error + """ + + try: + # get translation, autodiscover version + if len(self.ofi2pp) == 0: + self.obtain_port_correspondence() + + of_response = requests.get(self.url + "wm/{}/list/{}/json".format(self.ver_names["URLmodifier"], self.dpid), + headers=self.headers) + error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text) + if of_response.status_code != 200: + self.logger.warning("get_of_rules " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + self.logger.debug("get_of_rules " + error_text) + info = of_response.json() + if type(info) != dict: + self.logger.error("get_of_rules. Unexpected response not a dict %s", str(type(info))) + raise OpenflowConnUnexpectedResponse("Unexpected response, not a dict. Wrong version?") + rule_list = [] + for switch, switch_info in info.items(): + if switch_info is None: + continue + if str(switch) != self.dpid: + continue + for name, details in switch_info.items(): + rule = { + "name": name, + "switch": str(switch) + } + # rule["active"] = "true" + rule["priority"] = int(details["priority"]) + if self.version[0] == "0": + if translate_of_ports: + rule["ingress_port"] = self.ofi2pp[details["match"]["inputPort"]] + else: + rule["ingress_port"] = str(details["match"]["inputPort"]) + dst_mac = details["match"]["dataLayerDestination"] + if dst_mac != "00:00:00:00:00:00": + rule["dst_mac"] = dst_mac + vlan = details["match"]["dataLayerVirtualLan"] + if vlan != -1: + rule["vlan_id"] = vlan + actionlist = [] + for action in details["actions"]: + if action["type"] == "OUTPUT": + if translate_of_ports: + port = self.ofi2pp[action["port"]] + else: + port = action["port"] + actionlist.append(("out", port)) + elif action["type"] == "STRIP_VLAN": + actionlist.append(("vlan", None)) + elif action["type"] == "SET_VLAN_ID": + actionlist.append(("vlan", action["virtualLanIdentifier"])) + else: + actionlist.append((action["type"], str(action))) + self.logger.warning("get_of_rules() Unknown action in rule %s: %s", rule["name"], + str(action)) + rule["actions"] = actionlist + elif self.version[0] == "1": + if translate_of_ports: + rule["ingress_port"] = self.ofi2pp[details["match"]["in_port"]] + else: + rule["ingress_port"] = details["match"]["in_port"] + if "eth_dst" in details["match"]: + dst_mac = details["match"]["eth_dst"] + if dst_mac != "00:00:00:00:00:00": + rule["dst_mac"] = dst_mac + if "eth_vlan_vid" in details["match"]: + vlan = int(details["match"]["eth_vlan_vid"], 16) & 0xFFF + rule["vlan_id"] = str(vlan) + actionlist = [] + for action in details["instructions"]["instruction_apply_actions"]: + if action == "output": + if translate_of_ports: + port = self.ofi2pp[details["instructions"]["instruction_apply_actions"]["output"]] + else: + port = details["instructions"]["instruction_apply_actions"]["output"] + actionlist.append(("out", port)) + elif action == "strip_vlan": + actionlist.append(("vlan", None)) + elif action == "set_vlan_vid": + actionlist.append( + ("vlan", details["instructions"]["instruction_apply_actions"]["set_vlan_vid"])) + else: + self.logger.error("get_of_rules Unknown action in rule %s: %s", rule["name"], + str(action)) + # actionlist.append((action, str(details["instructions"]["instruction_apply_actions"]))) + rule_list.append(rule) + return rule_list + except requests.exceptions.RequestException as e: + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("get_of_rules " + error_text) + raise OpenflowConnConnectionException(error_text) + except Exception as e: + # ValueError in the case that JSON can not be decoded + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("get_of_rules " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + + def obtain_port_correspondence(self): + """ + Obtain the correspondence between physical and openflow port names + :return: dictionary: with physical name as key, openflow name as value + Raise an openflowconnUnexpectedResponse exception if fails with text_error + """ + try: + of_response = requests.get(self.url + "wm/core/controller/switches/json", headers=self.headers) + # print vim_response.status_code + error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text) + if of_response.status_code != 200: + self.logger.warning("obtain_port_correspondence " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + self.logger.debug("obtain_port_correspondence " + error_text) + info = of_response.json() + + if not isinstance(info, (list, tuple)): + raise OpenflowConnUnexpectedResponse("unexpected openflow response, not a list. Wrong version?") + + index = -1 + if len(info) > 0: + # autodiscover version + if self.version is None: + if 'dpid' in info[0] and 'ports' in info[0]: + self._set_version("0.9") + elif 'switchDPID' in info[0]: + self._set_version("1.X") + else: + raise OpenflowConnUnexpectedResponse("unexpected openflow response, Wrong version?") + + for i, info_item in enumerate(info): + if info_item[self.ver_names["dpid"]] == self.dpid: + index = i + break + if index == -1: + text = "DPID '{}' not present in controller {}".format(self.dpid, self.url) + # print self.name, ": get_of_controller_info ERROR", text + raise OpenflowConnUnexpectedResponse(text) + else: + if self.version[0] == "0": + ports = info[index]["ports"] + else: # version 1.X + of_response = requests.get(self.url + "wm/core/switch/{}/port-desc/json".format(self.dpid), + headers=self.headers) + # print vim_response.status_code + error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text) + if of_response.status_code != 200: + self.logger.warning("obtain_port_correspondence " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + self.logger.debug("obtain_port_correspondence " + error_text) + info = of_response.json() + if type(info) != dict: + raise OpenflowConnUnexpectedResponse("unexpected openflow port-desc response, " + "not a dict. Wrong version?") + if "portDesc" not in info: + raise OpenflowConnUnexpectedResponse("unexpected openflow port-desc response, " + "'portDesc' not found. Wrong version?") + if type(info["portDesc"]) != list and type(info["portDesc"]) != tuple: + raise OpenflowConnUnexpectedResponse("unexpected openflow port-desc response at " + "'portDesc', not a list. Wrong version?") + ports = info["portDesc"] + for port in ports: + self.pp2ofi[str(port["name"])] = str(port["portNumber"]) + self.ofi2pp[port["portNumber"]] = str(port["name"]) + # print self.name, ": get_of_controller_info ports:", self.pp2ofi + return self.pp2ofi + except requests.exceptions.RequestException as e: + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("obtain_port_correspondence " + error_text) + raise OpenflowConnConnectionException(error_text) + except Exception as e: + # ValueError in the case that JSON can not be decoded + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("obtain_port_correspondence " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + + def del_flow(self, flow_name): + """ + Delete an existing rule + :param flow_name: this is the rule name + :return: None if ok + Raise an openflowconnUnexpectedResponse exception if fails with text_error + """ + try: + if self.version is None: + self.get_of_switches() + + of_response = requests.delete(self.url + "wm/{}/json".format(self.ver_names["URLmodifier"]), + headers=self.headers, + data='{{"switch":"{}","name":"{}"}}'.format(self.dpid, flow_name)) + error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text) + if of_response.status_code != 200: + self.logger.warning("del_flow " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + self.logger.debug("del_flow OK " + error_text) + return None + + except requests.exceptions.RequestException as e: + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("del_flow " + error_text) + raise OpenflowConnConnectionException(error_text) + except Exception as e: + # ValueError in the case that JSON can not be decoded + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("del_flow " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + + def new_flow(self, data): + """ + Insert a new static rule + :param data: dictionary with the following content: + priority: rule priority + name: rule name + ingress_port: match input port of the rule + dst_mac: match destination mac address of the rule, missing or None if not apply + vlan_id: match vlan tag of the rule, missing or None if not apply + actions: list of actions, composed by a pair tuples with these posibilities: + ('vlan', None/int): for stripping/setting a vlan tag + ('out', port): send to this port + :return: None if ok + Raise an openflowconnUnexpectedResponse exception if fails with text_error + """ + # get translation, autodiscover version + if len(self.pp2ofi) == 0: + self.obtain_port_correspondence() + + try: + # We have to build the data for the floodlight call from the generic data + sdata = {'active': "true", "name": data["name"]} + if data.get("priority"): + sdata["priority"] = str(data["priority"]) + if data.get("vlan_id"): + sdata[self.ver_names["vlanid"]] = data["vlan_id"] + if data.get("dst_mac"): + sdata[self.ver_names["destmac"]] = data["dst_mac"] + sdata['switch'] = self.dpid + if not data['ingress_port'] in self.pp2ofi: + error_text = 'Error. Port {} is not present in the switch'.format(data['ingress_port']) + self.logger.warning("new_flow " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + + sdata[self.ver_names["inport"]] = self.pp2ofi[data['ingress_port']] + sdata['actions'] = "" + + for action in data['actions']: + if len(sdata['actions']) > 0: + sdata['actions'] += ',' + if action[0] == "vlan": + if action[1] is None: + sdata['actions'] += self.ver_names["stripvlan"] + else: + sdata['actions'] += self.ver_names["setvlan"] + "=" + str(action[1]) + elif action[0] == 'out': + sdata['actions'] += "output=" + self.pp2ofi[action[1]] + + of_response = requests.post(self.url + "wm/{}/json".format(self.ver_names["URLmodifier"]), + headers=self.headers, data=json.dumps(sdata)) + error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text) + if of_response.status_code != 200: + self.logger.warning("new_flow " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + self.logger.debug("new_flow OK" + error_text) + return None + + except requests.exceptions.RequestException as e: + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("new_flow " + error_text) + raise OpenflowConnConnectionException(error_text) + except Exception as e: + # ValueError in the case that JSON can not be decoded + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("new_flow " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + + def clear_all_flows(self): + """ + Delete all existing rules + :return: None if ok + Raise an openflowconnUnexpectedResponse exception if fails with text_error + """ + + try: + # autodiscover version + if self.version is None: + sw_list = self.get_of_switches() + if len(sw_list) == 0: # empty + return None + + url = self.url + "wm/{}/clear/{}/json".format(self.ver_names["URLmodifier"], self.dpid) + of_response = requests.get(url) + error_text = "Openflow response {}: {}".format(of_response.status_code, of_response.text) + if of_response.status_code < 200 or of_response.status_code >= 300: + self.logger.warning("clear_all_flows " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) + self.logger.debug("clear_all_flows OK " + error_text) + return None + except requests.exceptions.RequestException as e: + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("clear_all_flows " + error_text) + raise OpenflowConnConnectionException(error_text) + except Exception as e: + # ValueError in the case that JSON can not be decoded + error_text = type(e).__name__ + ": " + str(e) + self.logger.error("clear_all_flows " + error_text) + raise OpenflowConnUnexpectedResponse(error_text) diff --git a/RO-SDN-floodlight_openflow/osm_rosdn_floodlightof/sdnconn_floodlightof.py b/RO-SDN-floodlight_openflow/osm_rosdn_floodlightof/sdnconn_floodlightof.py new file mode 100644 index 00000000..395b18d8 --- /dev/null +++ b/RO-SDN-floodlight_openflow/osm_rosdn_floodlightof/sdnconn_floodlightof.py @@ -0,0 +1,41 @@ +## +# Copyright 2019 Telefonica Investigacion y Desarrollo, S.A.U. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +## +"""The SdnConnectorFloodLightOf connector is responsible for creating services using pro active operflow rules. +""" + +import logging +from osm_ro.wim.openflow_conn import SdnConnectorOpenFlow +from .floodlight_of import OfConnFloodLight + + +class SdnConnectorFloodLightOf(SdnConnectorOpenFlow): + + def __init__(self, wim, wim_account, config=None, logger=None): + """Creates a connectivity based on pro-active openflow rules + """ + self.logger = logging.getLogger('openmano.sdnconn.floodlightof') + super().__init__(wim, wim_account, config, logger) + of_params = { + "of_url": wim["wim_url"], + "of_dpid": config.get("dpid"), + "of_user": wim_account["user"], + "of_password": wim_account["password"], + } + self.openflow_conn = OfConnFloodLight(of_params) + super().__init__(wim, wim_account, config, logger, self.openflow_conn) diff --git a/RO-SDN-floodlight_openflow/requirements.txt b/RO-SDN-floodlight_openflow/requirements.txt new file mode 100644 index 00000000..a6f6d655 --- /dev/null +++ b/RO-SDN-floodlight_openflow/requirements.txt @@ -0,0 +1,18 @@ +## +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## + +requests +git+https://osm.etsi.org/gerrit/osm/RO.git#egg=osm-ro&subdirectory=RO + diff --git a/RO-SDN-floodlight_openflow/setup.py b/RO-SDN-floodlight_openflow/setup.py new file mode 100644 index 00000000..93a19881 --- /dev/null +++ b/RO-SDN-floodlight_openflow/setup.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +## +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## + +from setuptools import setup + +_name = "osm_rosdn_floodlightof" + +README = """ +=========== +osm-rosdn_floodlightof +=========== + +osm-ro plugin for floodlight SDN using pre-computed openflow rules +""" + +setup( + name=_name, + description='OSM RO plugin for SDN with floodlight openflow rules', + long_description=README, + version_command=('git describe --match v* --tags --long --dirty', 'pep440-git-full'), + # version=VERSION, + # python_requires='>3.5.0', + author='ETSI OSM', + author_email='alfonso.tiernosepulveda@telefonica.com', + maintainer='Alfonso Tierno', + maintainer_email='alfonso.tiernosepulveda@telefonica.com', + url='https://osm.etsi.org/gitweb/?p=osm/RO.git;a=summary', + license='Apache 2.0', + + packages=[_name], + include_package_data=True, + dependency_links=["git+https://osm.etsi.org/gerrit/osm/RO.git#egg=osm-ro"], + install_requires=["requests", "osm-ro"], + setup_requires=['setuptools-version-command'], + entry_points={ + 'osm_rosdn.plugins': ['rosdn_floodlightof = osm_rosdn_floodlightof.sdnconn_floodlightof:' + 'SdnConnectorFloodLightOf'], + }, +) diff --git a/RO-SDN-floodlight_openflow/stdeb.cfg b/RO-SDN-floodlight_openflow/stdeb.cfg new file mode 100644 index 00000000..0c718e4f --- /dev/null +++ b/RO-SDN-floodlight_openflow/stdeb.cfg @@ -0,0 +1,19 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +[DEFAULT] +X-Python3-Version : >= 3.5 +Depends3: python3-requests, python3-osm-ro + diff --git a/RO-SDN-floodlight_openflow/tox.ini b/RO-SDN-floodlight_openflow/tox.ini new file mode 100644 index 00000000..e95d02e2 --- /dev/null +++ b/RO-SDN-floodlight_openflow/tox.ini @@ -0,0 +1,41 @@ +## +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +## + +[tox] +envlist = flake8 +toxworkdir={homedir}/.tox + +[testenv] +basepython = python3 +install_command = python3 -m pip install -r requirements.txt -U {opts} {packages} +# deps = -r{toxinidir}/test-requirements.txt +commands=python3 -m unittest discover -v + +[testenv:flake8] +basepython = python3 +deps = flake8 +commands = flake8 osm_rosdn_floodlightof --max-line-length 120 \ + --exclude .svn,CVS,.gz,.git,__pycache__,.tox,local,temp --ignore W291,W293,E226,W504 + +[testenv:unittest] +basepython = python3 +commands = python3 -m unittest osm_rosdn_floodlightof.tests + +[testenv:build] +basepython = python3 +deps = stdeb + setuptools-version-command +commands = python3 setup.py --command-packages=stdeb.command bdist_deb + diff --git a/RO-SDN-onos_openflow/osm_rosdn_onosof/onos_of.py b/RO-SDN-onos_openflow/osm_rosdn_onosof/onos_of.py index 060d1d37..68833982 100644 --- a/RO-SDN-onos_openflow/osm_rosdn_onosof/onos_of.py +++ b/RO-SDN-onos_openflow/osm_rosdn_onosof/onos_of.py @@ -47,9 +47,9 @@ class OfConnOnos(OpenflowConn): """ def __init__(self, params): """ Constructor. - Params: dictionary with the following keys: + :param params: dictionary with the following keys: of_dpid: DPID to use for this controller ?? Does a controller have a dpid? - url: must be [http://HOST:PORT/ + url: must be [http://HOST:PORT/] of_user: user credentials, can be missing or None of_password: password credentials of_debug: debug level for logging. Default to ERROR @@ -69,7 +69,7 @@ class OfConnOnos(OpenflowConn): url = url + "/" self.url = url + "onos/v1/" - #internal variables + # internal variables self.name = "onosof" self.headers = {'content-type':'application/json','accept':'application/json',} @@ -87,7 +87,7 @@ class OfConnOnos(OpenflowConn): self.auth = self.auth.decode() self.headers['authorization'] = 'Basic ' + self.auth - self.logger = logging.getLogger('vim.OF.onos') + self.logger = logging.getLogger('SDN.onosOF') self.logger.setLevel( getattr(logging, params.get("of_debug", "ERROR")) ) self.ip_address = None diff --git a/RO-SDN-onos_openflow/setup.py b/RO-SDN-onos_openflow/setup.py index 380adc7d..7050c0ce 100644 --- a/RO-SDN-onos_openflow/setup.py +++ b/RO-SDN-onos_openflow/setup.py @@ -25,12 +25,12 @@ README = """ osm-rosdn_onosof =========== -osm-ro pluging for onosof (ietfl2vpn) SDN +osm-ro plugin for onos SDN using pre-computed openflow rules """ setup( name=_name, - description='OSM ro sdn plugin for onosof (ietfl2vpn)', + description='OSM RO plugin for SDN with onos openflow rules', long_description=README, version_command=('git describe --match v* --tags --long --dirty', 'pep440-git-full'), # version=VERSION, @@ -44,7 +44,7 @@ setup( packages=[_name], include_package_data=True, - dependency_links=["git+https://osm.etsi.org/gerrit/osm/RO.git#egg=osm-ro"], + dependency_links=["git+https://osm.etsi.org/gerrit/osm/RO.git#egg=osm-ro&subdirectory=RO"], install_requires=["requests", "osm-ro"], setup_requires=['setuptools-version-command'], entry_points={ diff --git a/RO/osm_ro/nfvo.py b/RO/osm_ro/nfvo.py index 4f5ba797..36109cb8 100644 --- a/RO/osm_ro/nfvo.py +++ b/RO/osm_ro/nfvo.py @@ -5756,15 +5756,18 @@ def vim_action_create(mydb, tenant_id, datacenter, item, descriptor): return vim_action_get(mydb, tenant_id, datacenter, item, content) def sdn_controller_create(mydb, tenant_id, sdn_controller): - wim_id = ovim.new_of_controller(sdn_controller) - - thread_name = get_non_used_vim_name(sdn_controller['name'], wim_id, wim_id, None) - new_thread = vim_thread(task_lock, plugins, thread_name, wim_id, None, db=db) - new_thread.start() - thread_id = wim_id - vim_threads["running"][thread_id] = new_thread - logger.debug('New SDN controller created with uuid {}'.format(wim_id)) - return wim_id + try: + wim_id = ovim.new_of_controller(sdn_controller) + + thread_name = get_non_used_vim_name(sdn_controller['name'], wim_id, wim_id, None) + new_thread = vim_thread(task_lock, plugins, thread_name, wim_id, None, db=db) + new_thread.start() + thread_id = wim_id + vim_threads["running"][thread_id] = new_thread + logger.debug('New SDN controller created with uuid {}'.format(wim_id)) + return wim_id + except ovimException as e: + raise NfvoException(e) from e def sdn_controller_update(mydb, tenant_id, controller_id, sdn_controller): data = ovim.edit_of_controller(controller_id, sdn_controller) diff --git a/RO/osm_ro/openmano_schemas.py b/RO/osm_ro/openmano_schemas.py index 8fd2889b..3dd72e27 100644 --- a/RO/osm_ro/openmano_schemas.py +++ b/RO/osm_ro/openmano_schemas.py @@ -1185,23 +1185,26 @@ instance_scenario_action_schema = { sdn_controller_properties={ "name": name_schema, - "dpid": {"type":"string", "pattern":"^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}, + "dpid": {"type": "string", "pattern": "^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){7}$"}, + "description": name_schema, "ip": ip_schema, "port": port_schema, - "type": {"type": "string", "enum": ["opendaylight","floodlight","onos"]}, - "version": {"type" : "string", "minLength":1, "maxLength":12}, + "type": nameshort_schema, + "url": name_schema, + "version": {"type": "string", "minLength": 1, "maxLength": 12}, "user": nameshort_schema, - "password": passwd_schema + "password": passwd_schema, + "config": object_schema, } sdn_controller_schema = { - "title":"sdn controller information schema", + "title": "sdn controller information schema", "$schema": "http://json-schema.org/draft-04/schema#", - "type":"object", + "type": "object", "properties":{ "sdn_controller":{ - "type":"object", - "properties":sdn_controller_properties, - "required": ["name", "port", 'ip', 'dpid', 'type'], + "type": "object", + "properties": sdn_controller_properties, + "required": ["name", 'type'], "additionalProperties": False } }, @@ -1210,13 +1213,13 @@ sdn_controller_schema = { } sdn_controller_edit_schema = { - "title":"sdn controller update information schema", + "title": "sdn controller update information schema", "$schema": "http://json-schema.org/draft-04/schema#", - "type":"object", - "properties":{ - "sdn_controller":{ - "type":"object", - "properties":sdn_controller_properties, + "type": "object", + "properties": { + "sdn_controller": { + "type": "object", + "properties": sdn_controller_properties, "additionalProperties": False } }, diff --git a/RO/osm_ro/sdn.py b/RO/osm_ro/sdn.py index 91cc9b2c..290882cf 100755 --- a/RO/osm_ro/sdn.py +++ b/RO/osm_ro/sdn.py @@ -37,9 +37,7 @@ class SdnException(Exception): Exception.__init__(self, message) -class Sdn(): - running_info = {} # TODO OVIM move the info of running threads from config_dic to this static variable - of_module = {} +class Sdn: def __init__(self, db, plugins): self.db = db @@ -81,7 +79,6 @@ class Sdn(): return content def edit_openflow_rules(self, network_id=None): - """ To make actions over the net. The action is to reinstall the openflow rules network_id can be 'all' @@ -160,15 +157,20 @@ class Sdn(): """ Create a new openflow controller into DB :param ofc_data: Dict openflow controller data - :return: openflow controller dpid + :return: openflow controller uuid """ db_wim = { "uuid": str(uuid4()), "name": ofc_data["name"], - "description": "", + "description": ofc_data.get("description"), "type": ofc_data["type"], - "wim_url": "{}:{}".format(ofc_data["ip"], ofc_data["port"]), + "wim_url": ofc_data.get("url"), } + if not db_wim["wim_url"]: + if not ofc_data.get("ip") or not ofc_data.get("port"): + raise SdnException("Provide either 'url' or both 'ip' and 'port'") + db_wim["wim_url"] = "{}:{}".format(ofc_data["ip"], ofc_data["port"]) + db_wim_account = { "uuid": str(uuid4()), "name": ofc_data["name"], @@ -176,9 +178,15 @@ class Sdn(): "sdn": "true", "user": ofc_data.get("user"), "password": ofc_data.get("password"), - "config": yaml.safe_dump({"dpid": ofc_data["dpid"], "version": ofc_data.get("version")}, - default_flow_style=True, width=256) } + db_wim_account_config = ofc_data.get("config", {}) + if ofc_data.get("dpid"): + db_wim_account_config["dpid"] = ofc_data["dpid"] + if ofc_data.get("version"): + db_wim_account_config["version"] = ofc_data["version"] + + db_wim_account["config"] = yaml.safe_dump(db_wim_account_config, default_flow_style=True, width=256) + db_tables = [ {"wims": db_wim}, {"wim_accounts": db_wim_account}, @@ -196,46 +204,63 @@ class Sdn(): raise SdnException("No data received during uptade OF contorller", http_code=HTTPStatus.INTERNAL_SERVER_ERROR.value) - old_of_controller = self.show_of_controller(of_id) + # get database wim_accounts + wim_account = self._get_of_controller(of_id) - if old_of_controller: - result, content = self.db.update_rows('ofcs', ofc_data, WHERE={'uuid': of_id}, log=False) - if result >= 0: - return ofc_data - else: - raise SdnException("Error uptating OF contorller with uuid {}".format(of_id), - http_code=-result) - else: - raise SdnException("Error uptating OF contorller with uuid {}".format(of_id), - http_code=HTTPStatus.INTERNAL_SERVER_ERROR.value) + db_wim_update = {x: ofc_data[x] for x in ("name", "description", "type", "wim_url")} + db_wim_account_update = {x: ofc_data[x] for x in ("name", "user", "password")} + db_wim_account_config = ofc_data.get("config", {}) - def delete_of_controller(self, of_id): - """ - Delete an openflow controller from DB. - :param of_id: openflow controller dpid - :return: - """ + if ofc_data.get("ip") or ofc_data.get("port"): + if not ofc_data.get("ip") or not ofc_data.get("port"): + raise SdnException("Provide or both 'ip' and 'port'") + db_wim_update["wim_url"] = "{}:{}".format(ofc_data["ip"], ofc_data["port"]) + + if ofc_data.get("dpid"): + db_wim_account_config["dpid"] = ofc_data["dpid"] + if ofc_data.get("version"): + db_wim_account_config["version"] = ofc_data["version"] + + if db_wim_account_config: + db_wim_account_update["config"] = yaml.load(wim_account["config"]) or {} + db_wim_account_update["config"].update(db_wim_account_config) + + if db_wim_account_update: + self.db.update_rows('wim_accounts', db_wim_account_update, WHERE={'uuid': of_id}) + if db_wim_update: + self.db.update_rows('wims', db_wim_account_update, WHERE={'uuid': wim_account["wim_id"]}) + + def _get_of_controller(self, of_id): wim_accounts = self.db.get_rows(FROM='wim_accounts', WHERE={"uuid": of_id, "sdn": "true"}) + if not wim_accounts: raise SdnException("Cannot find sdn controller with id='{}'".format(of_id), http_code=HTTPStatus.NOT_FOUND.value) elif len(wim_accounts) > 1: raise SdnException("Found more than one sdn controller with id='{}'".format(of_id), http_code=HTTPStatus.CONFLICT.value) + return wim_accounts[0] + + def delete_of_controller(self, of_id): + """ + Delete an openflow controller from DB. + :param of_id: openflow controller dpid + :return: + """ + wim_account = self._get_of_controller(of_id) self.db.delete_row(FROM='wim_accounts', WHERE={"uuid": of_id}) - self.db.delete_row(FROM='wims', WHERE={"uuid": wim_accounts[0]["wim_id"]}) + self.db.delete_row(FROM='wims', WHERE={"uuid": wim_account["wim_id"]}) return of_id - def _format_of_controller(self, wim_account, wim=None): + @staticmethod + def _format_of_controller(wim_account, wim=None): of_data = {x: wim_account[x] for x in ("uuid", "name", "user")} if isinstance(wim_account["config"], str): config = yaml.load(wim_account["config"], Loader=yaml.Loader) of_data["dpid"] = config.get("dpid") of_data["version"] = config.get("version") if wim: - ip, port = wim["wim_url"].split(":") - of_data["ip"] = ip - of_data["port"] = port + of_data["url"] = wim["wim_url"] of_data["type"] = wim["type"] return of_data @@ -245,15 +270,9 @@ class Sdn(): :param db_filter: List with where query parameters :return: """ - wim_accounts = self.db.get_rows(FROM='wim_accounts', WHERE={"uuid": of_id, "sdn": "true"}) - if not wim_accounts: - raise SdnException("Cannot find sdn controller with id='{}'".format(of_id), - http_code=HTTPStatus.NOT_FOUND.value) - elif len(wim_accounts) > 1: - raise SdnException("Found more than one sdn controller with id='{}'".format(of_id), - http_code=HTTPStatus.CONFLICT.value) - wims = self.db.get_rows(FROM='wims', WHERE={"uuid": wim_accounts[0]["wim_id"]}) - return self._format_of_controller(wim_accounts[0], wims[0]) + wim_account = self._get_of_controller(of_id) + wims = self.db.get_rows(FROM='wims', WHERE={"uuid": wim_account["wim_id"]}) + return self._format_of_controller(wim_account, wims[0]) def get_of_controllers(self, filter=None): """ @@ -277,10 +296,8 @@ class Sdn(): :return: """ # get wim from wim_account - wim_accounts = self.db.get_rows(FROM='wim_accounts', WHERE={"uuid": sdn_id}) - if not wim_accounts: - raise SdnException("Not found sdn id={}".format(sdn_id), http_code=HTTPStatus.NOT_FOUND.value) - wim_id = wim_accounts[0]["wim_id"] + wim_account = self._get_of_controller(sdn_id) + wim_id = wim_account["wim_id"] db_wim_port_mappings = [] for map in maps: new_map = { diff --git a/RO/osm_ro/vim_thread.py b/RO/osm_ro/vim_thread.py index 366656bd..587c0600 100644 --- a/RO/osm_ro/vim_thread.py +++ b/RO/osm_ro/vim_thread.py @@ -606,9 +606,10 @@ class vim_thread(threading.Thread): elif not self.vim and not self.sdnconnector: task["status"] = "FAILED" task["error_msg"] = self.error_status - database_update = {"status": "VIM_ERROR", "error_msg": task["error_msg"]} + database_update = {"status": "VIM_ERROR" if self.datacenter_tenant_id else "WIM_ERROR", + "error_msg": task["error_msg"]} elif task["item_id"] != related_tasks[0]["item_id"] and task["action"] in ("FIND", "CREATE"): - # Do nothing, just copy values from one to another and updata database + # Do nothing, just copy values from one to another and update database task["status"] = related_tasks[0]["status"] task["error_msg"] = related_tasks[0]["error_msg"] task["vim_id"] = related_tasks[0]["vim_id"] @@ -712,7 +713,7 @@ class vim_thread(threading.Thread): next_refresh = time.time() if task["extra"].get("vim_status") == "BUILD": next_refresh += self.REFRESH_BUILD - elif task["extra"].get("vim_status") in ("ERROR", "VIM_ERROR"): + elif task["extra"].get("vim_status") in ("ERROR", "VIM_ERROR", "WIM_ERROR"): next_refresh += self.REFRESH_ERROR elif task["extra"].get("vim_status") == "DELETED": next_refresh += self.REFRESH_DELETE @@ -1194,7 +1195,7 @@ class vim_thread(threading.Thread): task["status"] = "DONE" task["extra"]["vim_info"] = {} # task["extra"]["sdn_net_id"] = sdn_net_id - task["extra"]["vim_status"] = "BUILD" + task["extra"]["vim_status"] = sdn_status task["extra"]["created"] = True task["extra"]["created_items"] = created_items task["extra"]["connected_ports"] = connected_ports diff --git a/RO/osm_ro/wim/openflow_conn.py b/RO/osm_ro/wim/openflow_conn.py index 7d029f7f..655860e4 100644 --- a/RO/osm_ro/wim/openflow_conn.py +++ b/RO/osm_ro/wim/openflow_conn.py @@ -308,7 +308,7 @@ class SdnConnectorOpenFlow(SdnConnectorBase): except OpenflowConnNotFoundException: pass except OpenflowConnException as e: - error_text = "Cannot remove rule '{}': {}".format(flow['name'], e) + error_text = "Cannot remove rule '{}': {}".format(flow_id, e) error_list.append(error_text) self.logger.error(error_text) created_items["installed_rules_ids"] = new_installed_rules_ids @@ -322,8 +322,9 @@ class SdnConnectorOpenFlow(SdnConnectorBase): except (SdnConnectorError, OpenflowConnException) as e: raise SdnConnectorError("Error while {}: {}".format(step, e)) from e except Exception as e: + error_text = "Error while {}: {}".format(step, e) self.logger.critical(error_text, exc_info=True) - raise SdnConnectorError("Error while {}: {}".format(step, e)) + raise SdnConnectorError(error_text) def _compute_net_flows(self, net_id, ports): new_flows = [] diff --git a/devops-stages/stage-build.sh b/devops-stages/stage-build.sh index b48eaa34..28ad87c4 100755 --- a/devops-stages/stage-build.sh +++ b/devops-stages/stage-build.sh @@ -24,43 +24,19 @@ cp RO/deb_dist/python3-osm-ro_*.deb deb_dist/ make -C RO-client clean package cp RO-client/deb_dist/python3-osm-roclient_*.deb deb_dist/ -# VIM vmware plugin -make -C RO-VIM-vmware clean package -cp RO-VIM-vmware/deb_dist/python3-osm-rovim-vmware_*.deb deb_dist/ - -# VIM Openstack plugin -make -C RO-VIM-openstack clean package -cp RO-VIM-openstack/deb_dist/python3-osm-rovim-openstack_*.deb deb_dist/ - -# VIM Openvim plugin -make -C RO-VIM-openvim clean package -cp RO-VIM-openvim/deb_dist/python3-osm-rovim-openvim_*.deb deb_dist/ - -# VIM AWS plugin -make -C RO-VIM-aws clean package -cp RO-VIM-aws/deb_dist/python3-osm-rovim-aws_*.deb deb_dist/ - -# VIM fos plugin -make -C RO-VIM-fos clean package -cp RO-VIM-fos/deb_dist/python3-osm-rovim-fos_*.deb deb_dist/ - -# VIM azure plugin -make -C RO-VIM-azure clean package -cp RO-VIM-azure/deb_dist/python3-osm-rovim-azure_*.deb deb_dist/ - -# VIM Opennebula plugin -make -C RO-VIM-opennebula clean package -cp RO-VIM-opennebula/deb_dist/python3-osm-rovim-opennebula_*.deb deb_dist/ - -# SDN Dynpack plugin -make -C RO-SDN-dynpac clean package -cp RO-SDN-dynpac/deb_dist/python3-osm-rosdn-dynpac_*.deb deb_dist/ - -# SDN Tapi plugin -make -C RO-SDN-tapi clean package -cp RO-SDN-tapi/deb_dist/python3-osm-rosdn-tapi_*.deb deb_dist/ - -# SDN Onos openflow -make -C RO-SDN-onos_openflow clean package -cp RO-SDN-onos_openflow/deb_dist/python3-osm-rosdn-onosof_*.deb deb_dist/ +# VIM plugings: vmware openstack AWS fos azure Opennebula +for vim_plugin in RO-VIM-* +do + make -C $vim_plugin clean package + cp ${vim_plugin}/deb_dist/python3-osm-rovim*.deb deb_dist/ +done + +# SDN plugins + +# SDN plugins: Dynpack Tapi Onosof Floodlightof +for sdn_plugin in RO-SDN-* +do + make -C $sdn_plugin clean package + cp ${sdn_plugin}/deb_dist/python3-osm-rosdn*.deb deb_dist/ +done