aea1861a9a699b7725389b5e82b627eb01ab3e7a
[sfa.git] / sfa / rspecs / aggregates / rspec_manager_openflow.py
1 import sys
2
3 #The following is not essential
4 #from soaplib.wsgi_soap import SimpleWSGISoapApp
5 #from soaplib.serializers.primitive import *
6 #from soaplib.serializers.clazz import *
7
8 import socket
9 import struct
10
11 from sfa.util.faults import *
12 from sfa.util.rspec import RSpec
13 from sfa.server.registry import Registries
14 from sfa.util.config import Config
15 from sfa.plc.nodes import *
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, hrn):
93     if DEBUG: print "Received start_slice call"
94     return msg_aggrMgr(SFA_START_SLICE)
95
96 def stop_slice(cred, hrn):
97     if DEBUG: print "Received stop_slice call"
98     return msg_aggrMgr(SFA_STOP_SLICE)
99
100 def delete_slice(cred, hrn):
101     if DEBUG: print "Received delete_slice call"
102     return msg_aggrMgr(SFA_DELETE_SLICE)
103
104 def reset_slices(cred, hrn):
105     if DEBUG: print "Received reset_slices call"
106     return msg_aggrMgr(SFA_RESET_SLICES)
107
108 def create_slice(cred, hrn, rspec):
109     if DEBUG: print "Received create_slice call"
110     slice_id = generate_slide_id(cred, hrn)
111
112     msg = struct.pack('> B%ds%ds' % (len(slice_id)+1, len(rspec)), SFA_CREATE_SLICE, slice_id, rspec)
113     buf = struct.pack('> H', len(msg)+2) + msg
114
115     try:
116         aggrMgr_sock = connect_aggrMgr()
117         aggrMgr_sock.send(buf)
118         if DEBUG: print "Sent %d bytes and closing connection" % len(buf)
119         aggrMgr_sock.close()
120
121         if DEBUG: print "----------------"
122         return 1
123     except socket.error, message:
124         print "Socket error"
125     except IOerror, message:
126         print "IO error"
127     return 0
128
129 def ListResources(cred, hrn=None):
130     if DEBUG: print "Received ListResources call"
131     slice_id = generate_slide_id(cred, hrn)
132
133     msg = struct.pack('> B%ds' % len(slice_id), SFA_GET_RESOURCES, slice_id)
134     buf = struct.pack('> H', len(msg)+2) + msg
135
136     try:
137         aggrMgr_sock = connect_aggrMgr()
138         aggrMgr_sock.send(buf)
139         resource_list = extract(aggrMgr_sock);
140         aggrMgr_sock.close()
141
142         if DEBUG: print "----------------"
143         return resource_list 
144     except socket.error, message:
145         print "Socket error"
146     except IOerror, message:
147         print "IO error"
148     return None
149
150 """
151 Returns the request context required by sfatables. At some point, this mechanism should be changed
152 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
153 return the basic information needed in a dict.
154 """
155 def fetch_context(slice_hrn, user_hrn, contexts):
156     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
157     return base_context
158
159 def main():
160     r = RSpec()
161     r.parseFile(sys.argv[1])
162     rspec = r.toDict()
163     create_slice(None,'plc',rspec)
164     
165 if __name__ == "__main__":
166     main()