namespace module is gone, plxrn provides PL-specific translations
[sfa.git] / sfa / managers / aggregate_manager_openflow.py
1 import sys
2
3 import socket
4 import struct
5
6 #The following is not essential
7 #from soaplib.wsgi_soap import SimpleWSGISoapApp
8 #from soaplib.serializers.primitive import *
9 #from soaplib.serializers.clazz import *
10
11 from sfa.util.faults import *
12 from sfa.util.xrn import urn_to_hrn
13 from sfa.util.rspec import RSpec
14 from sfa.server.registry import Registries
15 from sfa.util.config import Config
16 from sfa.plc.nodes import *
17
18 # Message IDs for all the SFA light calls
19 # This will be used by the aggrMgr controller
20 SFA_GET_RESOURCES = 101
21 SFA_CREATE_SLICE = 102
22 SFA_START_SLICE = 103
23 SFA_STOP_SLICE = 104
24 SFA_DELETE_SLICE = 105
25 SFA_GET_SLICES = 106
26 SFA_RESET_SLICES = 107
27
28 DEBUG = 1
29
30 def print_buffer(buf):
31     for i in range(0,len(buf)):
32         print('%x' % buf[i])
33
34 def extract(sock):
35     # Shud we first obtain the message length?
36     # msg_len = socket.ntohs(sock.recv(2))
37     msg = ""
38
39     while (1):
40         try:
41             chunk = sock.recv(1)
42         except socket.error, message:
43             if 'timed out' in message:
44                 break
45             else:
46                 sys.exit("Socket error: " + message)
47
48         if len(chunk) == 0:
49             break
50         msg += chunk
51
52     print 'Done extracting %d bytes of response from aggrMgr' % len(msg)
53     return msg
54    
55 def connect(server, port):
56     '''Connect to the Aggregate Manager module'''
57     sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
58     sock.connect ( ( server, port) )
59     sock.settimeout(1)
60     if DEBUG: print 'Connected!'
61     return sock
62     
63 def connect_aggrMgr():
64     (aggr_mgr_ip, aggr_mgr_port) = Config().get_openflow_aggrMgr_info()
65     if DEBUG: print """Connecting to port %d of %s""" % (aggr_mgr_port, aggr_mgr_ip)
66     return connect(aggr_mgr_ip, aggr_mgr_port)
67
68 def generate_slide_id(cred, hrn):
69     if cred == None:
70         cred = ""
71     if hrn == None:
72         hrn = ""
73     #return cred + '_' + hrn
74     return str(hrn)
75
76 def msg_aggrMgr(cred, hrn, msg_id):
77     slice_id = generate_slide_id(cred, hrn)
78
79     msg = struct.pack('> B%ds' % len(slice_id), msg_id, slice_id)
80     buf = struct.pack('> H', len(msg)+2) + msg
81
82     try:
83         aggrMgr_sock = connect_aggrMgr()
84         aggrMgr_sock.send(buf)
85         aggrMgr_sock.close()
86         return 1
87     except socket.error, message:
88         print "Socket error"
89     except IOerror, message:
90         print "IO error"
91     return 0
92
93 def start_slice(cred, xrn):
94     hrn = urn_to_hrn(xrn)[0]
95     if DEBUG: print "Received start_slice call"
96     return msg_aggrMgr(SFA_START_SLICE)
97
98 def stop_slice(cred, xrn):
99     hrn = urn_to_hrn(xrn)[0]
100     if DEBUG: print "Received stop_slice call"
101     return msg_aggrMgr(SFA_STOP_SLICE)
102
103 def delete_slice(cred, xrn):
104     hrn = urn_to_hrn(xrn)[0]
105     if DEBUG: print "Received delete_slice call"
106     return msg_aggrMgr(SFA_DELETE_SLICE)
107
108 def reset_slices(cred, xrn):
109     hrn = urn_to_hrn(xrn)[0]
110     if DEBUG: print "Received reset_slices call"
111     return msg_aggrMgr(SFA_RESET_SLICES)
112
113 def create_slice(cred, xrn, rspec):
114     hrn = urn_to_hrn(xrn)[0]
115     if DEBUG: print "Received create_slice call"
116     slice_id = generate_slide_id(cred, hrn)
117
118     msg = struct.pack('> B%ds%ds' % (len(slice_id)+1, len(rspec)), SFA_CREATE_SLICE, slice_id, rspec)
119     buf = struct.pack('> H', len(msg)+2) + msg
120
121     try:
122         aggrMgr_sock = connect_aggrMgr()
123         aggrMgr_sock.send(buf)
124         if DEBUG: print "Sent %d bytes and closing connection" % len(buf)
125         aggrMgr_sock.close()
126
127         if DEBUG: print "----------------"
128         return 1
129     except socket.error, message:
130         print "Socket error"
131     except IOerror, message:
132         print "IO error"
133     return 0
134
135 def get_rspec(cred, xrn=None):
136     hrn = urn_to_hrn(xrn)[0]
137     if DEBUG: print "Received get_rspec call"
138     slice_id = generate_slide_id(cred, hrn)
139
140     msg = struct.pack('> B%ds' % len(slice_id), SFA_GET_RESOURCES, slice_id)
141     buf = struct.pack('> H', len(msg)+2) + msg
142
143     try:
144         aggrMgr_sock = connect_aggrMgr()
145         aggrMgr_sock.send(buf)
146         resource_list = extract(aggrMgr_sock);
147         aggrMgr_sock.close()
148
149         if DEBUG: print "----------------"
150         return resource_list 
151     except socket.error, message:
152         print "Socket error"
153     except IOerror, message:
154         print "IO error"
155     return None
156
157 """
158 Returns the request context required by sfatables. At some point, this mechanism should be changed
159 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
160 return the basic information needed in a dict.
161 """
162 def fetch_context(slice_hrn, user_hrn, contexts):
163     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
164     return base_context
165
166 def main():
167     r = RSpec()
168     r.parseFile(sys.argv[1])
169     rspec = r.toDict()
170     create_slice(None,'plc',rspec)
171     
172 if __name__ == "__main__":
173     main()