Signed-off-by: Isaku Yamahata <[email protected]> --- ryu/controller/link_set.py | 122 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 122 insertions(+), 0 deletions(-) create mode 100644 ryu/controller/link_set.py
diff --git a/ryu/controller/link_set.py b/ryu/controller/link_set.py new file mode 100644 index 0000000..d2f9fb8 --- /dev/null +++ b/ryu/controller/link_set.py @@ -0,0 +1,122 @@ +# Copyright (C) 2012 Isaku Yamahata <yamahata at private email ne jp> +# +# 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. + +import logging +import simplejson +import time + +from ryu.lib import dpid as lib_dpid + + +LOG = logging.getLogger(__name__) + + +class DPPort(object): + def __init__(self, dpid, port_no): + super(DPPort, self).__init__() + self.dpid = dpid + self.port_no = port_no + + def __str__(self): + return 'DPPort<%s, %d>' % (lib_dpid.dpid_to_str(self.dpid), + self.port_no) + + +class Link(object): + def __init__(self, src, dst): + super(Link, self).__init__() + self.src = src + self.dst = dst + + def __str__(self): + return 'LINK<%s, %s>' % (self.src, self.dst) + + +class LinkSet(dict): + """ + dict: Link -> timestamp + """ + def __init__(self): + super(LinkSet, self).__init__() + self._map = {} + + def update_link(self, src_dpid, src_port_no, dst_dpid, dst_port_no): + src = DPPort(src_dpid, src_port_no) + dst = DPPort(dst_dpid, dst_port_no) + link = Link(src, dst) + + # TODO:XXX link event + self[link] = time.time() + self._map[src] = dst + + # return if the reverse link is also up or not + rev_link = Link(dst, src) + return rev_link in self._map + + def link_down(self, link): + del self[link] + del self._map[link.src] + # TODO:XXX link event + + def rev_link_set_timestamp(self, rev_link, timestamp): + # rev_link may or may not in LinkSet + if rev_link in self: + self[rev_link] = timestamp + + def port_deleted(self, dpid, port_no): + src = DPPort(dpid, port_no) + dst = self._map.get(src, None) + if dst is None: + raise KeyError() + + link = Link(src, dst) + rev_link = Link(dst, src) + + del self[link] + del self._map[src] + self.pop(rev_link, None) + self._map.pop(dst, None) + + # TODO:XXX link event + + return dst + + @staticmethod + def _format_link(link, timestamp, now): + # XXX: convert into string + return { + 'timestamp': now - timestamp, + 'dp1': link.src.dpid, + 'port1': link.src.port_no, + 'dp2': link.dst.dpid, + 'port2': link.dst.port_no, + } + + def _format_response(self, iteritems): + now = time.time() + response = { + 'identifier': 'name', + 'items': [self._format_link(link, ts, now) + for link, ts in iteritems], + } + return simplejson.dumps(response) + + def get_links(self): + return self._format_response(self.iteritems()) + + def get_dp_links(self, dpid): + iteritems = ((link, ts) for link, ts in self.iteritems() + if (link.src.dpid == dpid or link.dst.dpid == dpid)) + return self._format_response(iteritems) -- 1.7.1.1 ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
