d2507ebfb9c6d5f3d80504987b40df4ca0e059b6
[sfa.git] / sfa / managers / aggregate_manager_openflow.py
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 *
7 import sys
8
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 *
13
14 import socket
15 import struct
16
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
21 SFA_START_SLICE = 103
22 SFA_STOP_SLICE = 104
23 SFA_DELETE_SLICE = 105
24 SFA_GET_SLICES = 106
25 SFA_RESET_SLICES = 107
26
27 DEBUG = 1
28
29 def print_buffer(buf):
30     for i in range(0,len(buf)):
31         print('%x' % buf[i])
32
33 def extract(sock):
34     # Shud we first obtain the message length?
35     # msg_len = socket.ntohs(sock.recv(2))
36     msg = ""
37
38     while (1):
39         try:
40             chunk = sock.recv(1)
41         except socket.error, message:
42             if 'timed out' in message:
43                 break
44             else:
45                 sys.exit("Socket error: " + message)
46
47         if len(chunk) == 0:
48             break
49         msg += chunk
50
51     print 'Done extracting %d bytes of response from aggrMgr' % len(msg)
52     return msg
53    
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) )
58     sock.settimeout(1)
59     if DEBUG: print 'Connected!'
60     return sock
61     
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)
66
67 def generate_slide_id(cred, hrn):
68     if cred == None:
69         cred = ""
70     if hrn == None:
71         hrn = ""
72     #return cred + '_' + hrn
73     return str(hrn)
74
75 def msg_aggrMgr(cred, hrn, msg_id):
76     slice_id = generate_slide_id(cred, hrn)
77
78     msg = struct.pack('> B%ds' % len(slice_id), msg_id, slice_id)
79     buf = struct.pack('> H', len(msg)+2) + msg
80
81     try:
82         aggrMgr_sock = connect_aggrMgr()
83         aggrMgr_sock.send(buf)
84         aggrMgr_sock.close()
85         return 1
86     except socket.error, message:
87         print "Socket error"
88     except IOerror, message:
89         print "IO error"
90     return 0
91
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)
96
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)
101
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)
106
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)
111
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)
116
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
119
120     try:
121         aggrMgr_sock = connect_aggrMgr()
122         aggrMgr_sock.send(buf)
123         if DEBUG: print "Sent %d bytes and closing connection" % len(buf)
124         aggrMgr_sock.close()
125
126         if DEBUG: print "----------------"
127         return 1
128     except socket.error, message:
129         print "Socket error"
130     except IOerror, message:
131         print "IO error"
132     return 0
133
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)
138
139     msg = struct.pack('> B%ds' % len(slice_id), SFA_GET_RESOURCES, slice_id)
140     buf = struct.pack('> H', len(msg)+2) + msg
141
142     try:
143         aggrMgr_sock = connect_aggrMgr()
144         aggrMgr_sock.send(buf)
145         resource_list = extract(aggrMgr_sock);
146         aggrMgr_sock.close()
147
148         if DEBUG: print "----------------"
149         return resource_list 
150     except socket.error, message:
151         print "Socket error"
152     except IOerror, message:
153         print "IO error"
154     return None
155
156 """
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.
160 """
161 def fetch_context(slice_hrn, user_hrn, contexts):
162     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
163     return base_context
164
165 def main():
166     r = RSpec()
167     r.parseFile(sys.argv[1])
168     rspec = r.toDict()
169     create_slice(None,'plc',rspec)
170     
171 if __name__ == "__main__":
172     main()