1 from sfa.util.faults import *
2 from sfa.util.namespace import *
3 from sfa.util.rspec import RSpec
4 from sfa.server.registry import Registries
5 from sfa.util.config import Config
6 from sfa.plc.nodes import *
9 #The following is not essential
10 #from soaplib.wsgi_soap import SimpleWSGISoapApp
11 #from soaplib.serializers.primitive import *
12 #from soaplib.serializers.clazz import *
17 # Message IDs for all the SFA light calls
18 # This will be used by the aggrMgr controller
19 SFA_GET_RESOURCES = 101
20 SFA_CREATE_SLICE = 102
23 SFA_DELETE_SLICE = 105
25 SFA_RESET_SLICES = 107
29 def print_buffer(buf):
30 for i in range(0,len(buf)):
34 # Shud we first obtain the message length?
35 # msg_len = socket.ntohs(sock.recv(2))
41 except socket.error, message:
42 if 'timed out' in message:
45 sys.exit("Socket error: " + message)
51 print 'Done extracting %d bytes of response from aggrMgr' % len(msg)
54 def connect(server, port):
55 '''Connect to the Aggregate Manager module'''
56 sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
57 sock.connect ( ( server, port) )
59 if DEBUG: print 'Connected!'
62 def connect_aggrMgr():
63 (aggr_mgr_ip, aggr_mgr_port) = Config().get_openflow_aggrMgr_info()
64 if DEBUG: print """Connecting to port %d of %s""" % (aggr_mgr_port, aggr_mgr_ip)
65 return connect(aggr_mgr_ip, aggr_mgr_port)
67 def generate_slide_id(cred, hrn):
72 #return cred + '_' + hrn
75 def msg_aggrMgr(cred, hrn, msg_id):
76 slice_id = generate_slide_id(cred, hrn)
78 msg = struct.pack('> B%ds' % len(slice_id), msg_id, slice_id)
79 buf = struct.pack('> H', len(msg)+2) + msg
82 aggrMgr_sock = connect_aggrMgr()
83 aggrMgr_sock.send(buf)
86 except socket.error, message:
88 except IOerror, message:
92 def start_slice(cred, xrn):
93 hrn = urn_to_hrn(xrn)[0]
94 if DEBUG: print "Received start_slice call"
95 return msg_aggrMgr(SFA_START_SLICE)
97 def stop_slice(cred, xrn):
98 hrn = urn_to_hrn(xrn)[0]
99 if DEBUG: print "Received stop_slice call"
100 return msg_aggrMgr(SFA_STOP_SLICE)
102 def delete_slice(cred, xrn):
103 hrn = urn_to_hrn(xrn)[0]
104 if DEBUG: print "Received delete_slice call"
105 return msg_aggrMgr(SFA_DELETE_SLICE)
107 def reset_slices(cred, xrn):
108 hrn = urn_to_hrn(xrn)[0]
109 if DEBUG: print "Received reset_slices call"
110 return msg_aggrMgr(SFA_RESET_SLICES)
112 def create_slice(cred, xrn, rspec):
113 hrn = urn_to_hrn(xrn)[0]
114 if DEBUG: print "Received create_slice call"
115 slice_id = generate_slide_id(cred, hrn)
117 msg = struct.pack('> B%ds%ds' % (len(slice_id)+1, len(rspec)), SFA_CREATE_SLICE, slice_id, rspec)
118 buf = struct.pack('> H', len(msg)+2) + msg
121 aggrMgr_sock = connect_aggrMgr()
122 aggrMgr_sock.send(buf)
123 if DEBUG: print "Sent %d bytes and closing connection" % len(buf)
126 if DEBUG: print "----------------"
128 except socket.error, message:
130 except IOerror, message:
134 def get_rspec(cred, xrn=None):
135 hrn = urn_to_hrn(xrn)[0]
136 if DEBUG: print "Received get_rspec call"
137 slice_id = generate_slide_id(cred, hrn)
139 msg = struct.pack('> B%ds' % len(slice_id), SFA_GET_RESOURCES, slice_id)
140 buf = struct.pack('> H', len(msg)+2) + msg
143 aggrMgr_sock = connect_aggrMgr()
144 aggrMgr_sock.send(buf)
145 resource_list = extract(aggrMgr_sock);
148 if DEBUG: print "----------------"
150 except socket.error, message:
152 except IOerror, message:
157 Returns the request context required by sfatables. At some point, this mechanism should be changed
158 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
159 return the basic information needed in a dict.
161 def fetch_context(slice_hrn, user_hrn, contexts):
162 base_context = {'sfa':{'user':{'hrn':user_hrn}}}
167 r.parseFile(sys.argv[1])
169 create_slice(None,'plc',rspec)
171 if __name__ == "__main__":