>From 5c4b0b636ed588055c2d92e87549ad2f554c8e7d Mon Sep 17 00:00:00 2001
From: FatRabiTree <fatrabitree@gmail.com>
Date: Wed, 26 Aug 2015 01:36:27 +0800
Subject: [PATCH] add a shortest_path app to ryu/app/

Signed-off-by: Kuan Wei Li <fatrabitree@gmail.com>
---
 ryu/app/simple_switch_shortest_path.py | 148 +++++++++++++++++++++++++++++++++
 1 file changed, 148 insertions(+)
 create mode 100644 ryu/app/simple_switch_shortest_path.py

diff --git a/ryu/app/simple_switch_shortest_path.py b/ryu/app/simple_switch_shortest_path.py
new file mode 100644
index 0000000..c282c0c
--- /dev/null
+++ b/ryu/app/simple_switch_shortest_path.py
@@ -0,0 +1,148 @@
+# Bug Fix:
+#
+#    Kuan-Wei Li (fatrabitree@gmail.com)
+#
+# Fork from: https://github.com/castroflavio/ryu/blob/master/ryu/app/shortestpath.py
+# Authors:
+#
+#    Akshar Rawal (arawal@gatech.edu)
+#    Flavio Castro (castro.flaviojr@gmail.com)
+#    Logan Blyth (lblyth3@gatech.edu)
+#    Matthew Hicks (mhicks34@gatech.edu)
+#    Uy Nguyen (unguyen3@gatech.edu)
+#
+# To run:
+#
+#    ryu--manager --observe-links ryu/app/simple_switch_shortest_path.py
+#
+# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
+#
+# 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.
+
+"""
+An OpenFlow 1.0 L2 learning switch implementation.
+"""
+from ryu.base import app_manager
+from ryu.controller import ofp_event
+from ryu.controller.handler import MAIN_DISPATCHER
+from ryu.controller.handler import set_ev_cls
+from ryu.ofproto import ofproto_v1_0
+from ryu.lib.mac import haddr_to_bin
+from ryu.lib.packet import packet
+from ryu.lib.packet import ethernet
+from ryu.lib.packet import ether_types
+
+from ryu.topology.api import get_switch, get_link
+from ryu.topology import event, switches
+from ryu.app.wsgi import ControllerBase
+from networkx import networkx
+
+
+class SimpleSwitchShortestPath(app_manager.RyuApp):
+    OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
+
+    def __init__(self, *args, **kwargs):
+        super(SimpleSwitchShortestPath, self).__init__(*args, **kwargs)
+        self.net = networkx.DiGraph()
+        self.topology_api_app = self
+
+    def add_flow(self, datapath, in_port, dst, actions):
+        ofproto = datapath.ofproto
+
+        match = datapath.ofproto_parser.OFPMatch(
+            in_port=in_port, dl_dst=haddr_to_bin(dst))
+
+        mod = datapath.ofproto_parser.OFPFlowMod(
+            datapath=datapath, match=match, cookie=0,
+            command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=0,
+            priority=ofproto.OFP_DEFAULT_PRIORITY,
+            flags=ofproto.OFPFF_SEND_FLOW_REM, actions=actions)
+        datapath.send_msg(mod)
+
+    @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
+    def _packet_in_handler(self, ev):
+        msg = ev.msg
+        datapath = msg.datapath
+        ofproto = datapath.ofproto
+
+        pkt = packet.Packet(msg.data)
+        eth = pkt.get_protocol(ethernet.ethernet)
+
+        if eth.ethertype == ether_types.ETH_TYPE_LLDP:
+            # ignore lldp packet
+            return
+        dst = eth.dst
+        src = eth.src
+
+        dpid = datapath.id
+        self.logger.info("packet in %s %s %s %s", dpid, src, dst, msg.in_port)
+
+        out_port = ofproto.OFPP_FLOOD
+        try:
+            if src not in self.net:
+                self.net.add_node(src)
+                self.net.add_edge(dpid, src, {'port': msg.in_port})
+                self.net.add_edge(src, dpid)
+
+            if dst in self.net:
+                path = networkx.shortest_path(self.net, src, dst)
+                out_port = self.net[dpid][path[path.index(dpid) + 1]]['port']
+        except:
+            pass
+        actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
+
+        # install a flow to avoid packet_in next time
+        if out_port != ofproto.OFPP_FLOOD:
+            self.add_flow(datapath, msg.in_port, dst, actions)
+
+        data = None
+        if msg.buffer_id == ofproto.OFP_NO_BUFFER:
+            data = msg.data
+
+        out = datapath.ofproto_parser.OFPPacketOut(
+            datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port,
+            actions=actions, data=data)
+        datapath.send_msg(out)
+
+    @set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
+    def _port_status_handler(self, ev):
+        msg = ev.msg
+        reason = msg.reason
+        port_no = msg.desc.port_no
+
+        ofproto = msg.datapath.ofproto
+        if reason == ofproto.OFPPR_ADD:
+            self.logger.info("port added %s", port_no)
+        elif reason == ofproto.OFPPR_DELETE:
+            self.logger.info("port deleted %s", port_no)
+        elif reason == ofproto.OFPPR_MODIFY:
+            self.logger.info("port modified %s", port_no)
+        else:
+            self.logger.info("Illeagal port state %s %s", port_no, reason)
+
+    @set_ev_cls(event.EventSwitchEnter)
+    def get_topology_data(self, ev):
+        switch_list = get_switch(self.topology_api_app, None)
+        switches = [switch.dp.id for switch in switch_list]
+        self.net.add_nodes_from(switches)
+
+        links_list = get_link(self.topology_api_app, None)
+
+        # add src to dst link to self.net
+        links = [(link.src.dpid, link.dst.dpid, {'port': link.src.port_no}) for link in links_list]
+        self.net.add_edges_from(links)
+
+        # add dst to src link to self.net
+        links = [(link.dst.dpid, link.src.dpid, {'port': link.dst.port_no}) for link in links_list]
+        self.net.add_edges_from(links)
-- 
1.9.1

