small cleanup to sdn services, added loadservice line to daemon, and updated example service for how current services are coded

This commit is contained in:
Blake J. Harnden 2017-07-05 13:32:16 -07:00
parent e39682c5b4
commit 6bfa81f3a8
3 changed files with 48 additions and 41 deletions

View file

@ -1,12 +1,13 @@
"""
sdn.py defines services to start Open vSwitch and the Ryu SDN Controller.
"""
import os
import re
from core.service import CoreService
from core.service import ServiceManager
class SdnService(CoreService):
"""
Parent class for SDN services.
@ -24,15 +25,16 @@ class SdnService(CoreService):
def generateconfig(cls, node, filename, services):
return ""
class OvsService(SdnService):
_name = "OvsService"
_group = "SDN"
_depends = ()
_dirs = ("/etc/openvswitch", "/var/run/openvswitch", "/var/log/openvswitch")
_configs = ('OvsService.sh', )
_configs = ('OvsService.sh',)
_startindex = 50
_startup = ('sh OvsService.sh',)
_shutdown = ('killall ovs-vswitchd','killall ovsdb-server')
_shutdown = ('killall ovs-vswitchd', 'killall ovsdb-server')
@classmethod
def generateconfig(cls, node, filename, services):
@ -41,7 +43,7 @@ class OvsService(SdnService):
for s in services:
if s._name == "zebra":
has_zebra = 1
# Check whether the node is running an SDN controller
has_sdn_ctrlr = 0
for s in services:
@ -53,13 +55,13 @@ class OvsService(SdnService):
cfg += "/etc/init.d/openvswitch-switch start < /dev/null\n"
cfg += "ovs-vsctl add-br ovsbr0\n"
cfg += "ifconfig ovsbr0 up\n"
for ifc in node.netifs():
if hasattr(ifc, 'control') and ifc.control == True:
if hasattr(ifc, 'control') and ifc.control is True:
continue
ifnumstr = re.findall(r"\d+", ifc.name)
ifnum = ifnumstr[0]
# create virtual interfaces
cfg += "ip link add rtr%s type veth peer name sw%s\n" % (ifnum, ifnum)
cfg += "ifconfig rtr%s up\n" % ifnum
@ -77,50 +79,50 @@ class OvsService(SdnService):
if has_zebra == 0:
cfg += "ip -6 addr add %s dev rtr%s\n" % (ifcaddr, ifnum)
else:
raise Value, "invalid address: %s", x
raise ValueError("invalid address: %s" % ifcaddr)
# add interfaces to bridge
cfg += "ovs-vsctl add-port ovsbr0 eth%s\n" % ifnum
cfg += "ovs-vsctl add-port ovsbr0 sw%s\n" % ifnum
# Add rule for default controller if there is one local (even if the controller is not local, it finds it)
cfg += "ovs-vsctl set-controller ovsbr0 tcp:127.0.0.1:6633\n"
# Setup default flows
portnum = 1
for ifc in node.netifs():
if hasattr(ifc, 'control') and ifc.control == True:
if hasattr(ifc, 'control') and ifc.control is True:
continue
cfg += "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n" % (portnum, portnum+1)
cfg += "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n" % (portnum+1, portnum)
cfg += "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n" % (portnum, portnum + 1)
cfg += "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n" % (portnum + 1, portnum)
portnum += 2
return cfg
class ryuService(SdnService):
class RyuService(SdnService):
_name = "ryuService"
_group = "SDN"
_depends = ()
_dirs = ()
_configs = ('ryuService.sh', )
_configs = ('ryuService.sh',)
_startindex = 50
_startup = ('sh ryuService.sh',)
_shutdown = ('killall ryu-manager')
_shutdown = ('killall ryu-manager',)
@classmethod
def generateconfig(cls, node, filename, services):
''' Return a string that will be written to filename, or sent to the
GUI for user customization.
'''
"""
Return a string that will be written to filename, or sent to the
GUI for user customization.
"""
app_path = "/usr/local/lib/python2.7/dist-packages/ryu/app"
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by ryuService (ryuService.py)\n"
cfg += '/usr/local/bin/ryu-manager --observe-links %s/ofctl_rest.py %s/rest_topology.py' % (app_path, app_path)
return cfg
def load_services():
ServiceManager.add(OvsService)
ServiceManager.add(ryuService)
ServiceManager.add(RyuService)

View file

@ -3,19 +3,19 @@
# Copyright (c)2010-2012 the Boeing Company.
# See the LICENSE file included in this distribution.
#
''' Sample user-defined service.
'''
import os
"""
Sample user-defined service.
"""
from core.misc.ipaddress import Ipv4Prefix
from core.misc.ipaddress import Ipv6Prefix
from core.service import CoreService
from core.service import ServiceManager
class MyService(CoreService):
''' This is a sample user-defined service.
'''
"""
This is a sample user-defined service.
"""
# a unique name is required, without spaces
_name = "MyService"
# you can create your own group here
@ -26,7 +26,7 @@ class MyService(CoreService):
_dirs = ()
# generated files (without a full path this file goes in the node's dir,
# e.g. /tmp/pycore.12345/n1.conf/)
_configs = ('myservice.sh', )
_configs = ('myservice.sh',)
# this controls the starting order vs other enabled services
_startindex = 50
# list of startup commands, also may be generated during startup
@ -36,31 +36,34 @@ class MyService(CoreService):
@classmethod
def generateconfig(cls, node, filename, services):
''' Return a string that will be written to filename, or sent to the
GUI for user customization.
'''
"""
Return a string that will be written to filename, or sent to the
GUI for user customization.
"""
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by MyService (sample.py)\n"
for ifc in node.netifs():
cfg += 'echo "Node %s has interface %s"\n' % (node.name, ifc.name)
# here we do something interesting
# here we do something interesting
cfg += "\n".join(map(cls.subnetentry, ifc.addrlist))
break
return cfg
@staticmethod
def subnetentry(x):
''' Generate a subnet declaration block given an IPv4 prefix string
for inclusion in the config file.
'''
"""
Generate a subnet declaration block given an IPv4 prefix string
for inclusion in the config file.
"""
if x.find(":") >= 0:
# this is an IPv6 address
return ""
else:
net = IPv4Prefix(x)
return 'echo " network %s"' % (net)
net = Ipv4Prefix(x)
return 'echo " network %s"' % net
# this line is required to add the above class to the list of available services
ServiceManager.add(MyService)
def load_services():
# this line is required to add the above class to the list of available services
ServiceManager.add(MyService)

View file

@ -43,6 +43,7 @@ from core.services import bird
from core.services import dockersvc
from core.services import nrl
from core.services import quagga
from core.services import sdn
from core.services import security
from core.services import startup
from core.services import ucarp
@ -364,5 +365,6 @@ if __name__ == "__main__":
ucarp.load_services()
dockersvc.load_services()
startup.load_services()
sdn.load_services()
main()