2c32cbd86a59febdde57cc9b1f42d3b299a456f5
[nemo.git] / nemo-impl / src / main / resources / nemo-odl.py
1 #!/usr/bin/python
2 #Filename:nemo-odl.py
3 import requests,json
4 import argparse, sys
5 from requests.auth import HTTPBasicAuth
6
7 USERNAME='admin'
8 PASSWORD='admin'
9
10 PREDEFINE_USERROLE="http://%s:8181/restconf/config/nemo-user:user-roles/"
11 PREDEFINE_NODETYPE="http://%s:8181/restconf/config/nemo-object:node-definitions/"
12 PREDEFINE_CONNECTIONTYPE="http://%s:8181/restconf/config/nemo-object:connection-definitions/"
13 PREDEFINE_FLOWMATCHTYPE="http://%s:8181/restconf/config/nemo-object:match-item-definitions/"
14 PREDEFINE_FLOWPROPERTY="http://%s:8181/restconf/config/nemo-object:flow-property-definitions/"
15 PREDEFINE_OPERATIONACTION="http://%s:8181/restconf/config/nemo-operation:action-definitions/"
16 PREDEFINE_OPERATIONCONDITION="http://%s:8181/restconf/config/nemo-operation:condition-parameter-definitions/"
17 PHYSICAL_PORT_ATTRIBUTE="http://%s:8181/restconf/config/generic-physical-network:physical-port-attribute-definitions/"
18 PHYSICAL_NODE_ATTRIBUTE="http://%s:8181/restconf/config/generic-physical-network:physical-node-attribute-definitions/"
19
20
21
22 def add_predefined_userrole(contHost):
23         data={
24                 "user-roles": {
25                         "user-role": [
26                                                         {
27                                                                 "role-name": "tenant",
28                                                                 "role-description": "It's a non-administor user"
29                                                         }
30                                                 ]
31                                         }
32                 }
33         put(PREDEFINE_USERROLE % contHost, data)
34
35 def add_predefined_nodetypes(contHost):
36         data={
37                 "node-definitions": {
38                                 "node-definition": [
39                                                 {
40                                                         "node-type": "host",
41                                                         "property-definition": [
42                                                                         {
43                                                                                 "property-name": "name",
44                                                                                 "property-value-type": "string"
45                                                                         },
46                                                                         {
47                                                                                 "property-name": "location",
48                                                                                 "property-value-type": "string"
49                                                                         },
50                                                                         {
51                                                                                 #For example, one legal mac-address is 00:01:0a:90:78:02.
52                                                                                 "property-name": "mac-address",
53                                                                                 "property-value-type": "string"
54                                                                         },
55                                                                         {
56                                                                                 #For example, one legal ip-address is 10.0.2.0/24.
57                                                                                 "property-name": "ip-address",
58                                                                                 "property-value-type": "string"
59                                                                         }
60                                                                 ]
61                                                 },
62                                                 {
63                                                         "node-type": "l2-group",
64                                                         "property-definition": [
65                                                                         {
66                                                                                 #For example, one legal ip-address is 10.0.2.0/24.
67                                                                                 "property-name": "ip-prefix",
68                                                                                 "property-value-type": "string"
69                                                                         },
70                                                                         {
71                                                                                 #For example, one legal gateway-ip is 10.0.2.1.
72                                                                                 "property-name": "gateway-ip",
73                                                                                 "property-value-type": "string"
74                                                                         },
75                                                                         {
76                                                                                 "property-name": "location",
77                                                                                 "property-value-type": "string"
78                                                                         }
79                                                                 ]
80                                                 },
81                                                 {
82                                                         "node-type": "l3-group",
83                                                         "property-definition": [
84                                                                         {
85                                                                                 #For example, one legal ip-address is 10.0.2.0/24.
86                                                                                 "property-name": "ip-prefix",
87                                                                                 "property-value-type": "string"
88                                                                         }
89                                                                 ]
90                                                 },
91                                                 {
92                                                         "node-type": "ext-group",
93                                                         "property-definition": [
94                                                                         {
95                                                                                 "property-name": "location",
96                                                                                 "property-value-type": "string",
97                                                                                 "is-required": "required"
98                                                                         },
99                                                                         {
100                                                                                 "property-name": "ac-info-network",
101                                                                                 "property-value-type": "string",
102                                                                                 "is-required": "required"
103                                                                         },
104                                                                         {
105                                                                                 "property-name": "ac-info-protocol",
106                                                                                 "property-value-type": "string",
107                                                                                 "is-required": "required"
108                                                                          },
109                                                                         {
110                                                                                 "property-name": "ip-prefix",
111                                                                                 "property-value-type": "string"
112                                                                          }
113                                                                 ]
114                                                 },
115                                                 {
116                                                         "node-type": "chain-group"
117                                                 },
118                                                 {
119                                                         "node-type": "fw",
120                                                         "property-definition": [
121                                                                         {
122                                                                                 "property-name": "location",
123                                                                                 "property-value-type": "string",
124                                                                                 "is-required": "required"
125                                                                         },
126                                                                         {
127                                                                                 "property-name": "operating-mode",
128                                                                                 "property-value-type": "string",
129                                                                                 "is-required": "required"
130                                                                         }
131                                                                 ]
132                                                 },
133                                                 {
134                                                         "node-type": "lb",
135                                                         "property-definition": [
136                                                                         {
137                                                                                 "property-name": "location",
138                                                                                 "property-value-type": "string",
139                                                                                 "is-required": "required"
140                                                                         },
141                                                                         {
142                                                                                 "property-name": "operating-mode",
143                                                                                 "property-value-type": "string",
144                                                                                 "is-required": "required"
145                                                                         }
146                                                                 ]
147                                                 },
148                                                 {
149                                                         "node-type": "cache",
150                                                         "property-definition": [
151                                                                         {
152                                                                                 "property-name": "location",
153                                                                                 "property-value-type": "string",
154                                                                                 "is-required": "required"
155                                                                         },
156                                                                         {
157                                                                                 "property-name": "operating-mode",
158                                                                                 "property-value-type": "string",
159                                                                                 "is-required": "required"
160                                                                         }
161                                                                 ]
162                                                 }
163                                         ]
164                                 }
165                 }
166         put(PREDEFINE_NODETYPE % contHost, data)
167
168 def add_predefined_connectiontypes(contHost):
169         data={
170                         "connection-definitions": {
171                                 "connection-definition":[
172                                                 {
173                                                         "connection-type": "p2p",
174                                                         "property-definition": [
175                                                                         {
176                                                                                 #the unit of bandwidth is mbps.
177                                                                                 "property-name": "bandwidth",
178                                                                                 "property-value-type": "int"
179                                                                         }
180                                                                 ]
181                                                 },
182                                                 {
183                                                         "connection-type": "p2mp",
184                                                         "property-definition": [
185                                                                         {
186                                                                                 #the unit of bandwidth is mbps.
187                                                                                 "property-name": "bandwidth",
188                                                                                 "property-value-type": "int"
189                                                                         }
190                                                                 ]
191                                                 },
192                                                 {
193                                                         "connection-type": "mesh",
194                                                         "property-definition": [
195                                                                         {
196                                                                                 #the unit of bandwidth is mbps.
197                                                                                 "property-name": "bandwidth",
198                                                                                 "property-value-type": "int"
199                                                                         }
200                                                                 ]
201                                                 },
202                                                 {
203                                                         "connection-type": "chain",
204                                                         "property-definition": [
205                                                                         {
206                                                                                 #the unit of bandwidth is mbps.
207                                                                                 "property-name": "bandwidth",
208                                                                                 "property-value-type": "int"
209                                                                         }
210                                                                 ]
211                                                 }
212                                         ]
213                                 }
214                 }
215         put(PREDEFINE_CONNECTIONTYPE % contHost, data)
216
217 def add_flow_matchtypes(contHost):
218         data={
219                         "match-item-definitions": {
220                                 "match-item-definition": [
221                                                 {
222                                                         #For example, one legal eth-type is arp.
223                                                         "match-item-name": "eth-type",
224                                                         "match-item-value-type": "string"
225                                                 },
226                                                 {
227                                                         #For example, one legal src-mac is 00:00:0a:b7:01:90;
228                                                         "match-item-name": "src-mac",
229                                                         "match-item-value-type": "string"
230                                                 },
231                                                 {
232                                                         #For example, one legal src-mac is 00:00:0a:b7:01:90;
233                                                         "match-item-name": "dst-mac",
234                                                         "match-item-value-type": "string"
235                                                 },
236                                                 {
237                                                         #For example, one legal proto is http;
238                                                         "match-item-name": "proto",
239                                                         "match-item-value-type": "string"
240                                                 },
241                                                 {
242                                                         #For example, one legal src-ip is 10.0.2.1;
243                                                         "match-item-name": "src-ip",
244                                                         "match-item-value-type": "string"
245                                                 },
246                                                 {
247                                                         #For example, one legal dst-ip is 10.0.1.2;
248                                                         "match-item-name": "dst-ip",
249                                                         "match-item-value-type": "string"
250                                                 },
251                                                 {
252                                                         #For example, one legal src-port is 80;
253                                                         "match-item-name": "src-port",
254                                                         "match-item-value-type": "int"
255                                                 },
256                                                 {
257                                                         #For example, one legal dst-port is 22;
258                                                         "match-item-name": "dst-port",
259                                                         "match-item-value-type": "int"
260                                                 }
261                                         ]
262                                 }
263                 }
264         put(PREDEFINE_FLOWMATCHTYPE % contHost, data)
265
266 def add_flow_properties(contHost):
267         data={
268                         "flow-property-definitions": {
269                                         "property-definition": [
270                                                                         {
271                                                                                 "property-name": "path",
272                                                                                 "property-value-type": "string"
273                                                                         }
274                                                                 ]
275                                         }
276                 }
277         put(PREDEFINE_FLOWPROPERTY % contHost, data)
278
279 def add_operation_actions(contHost):
280         data={
281                         "action-definitions": {
282                                 "action-definition": [
283                                                 {
284                                                         "action-name": "deny"
285                                                 },
286                                                                 {
287                                                         "action-name": "allow"
288                                                 },
289                                                 {
290                                                         "action-name": "go-through",
291                                                         "parameter-value-type": "string"
292                                                 },
293                                                 {
294                                                         "action-name": "qos-bandwidth",
295                                                         "parameter-value-type": "int"
296                                                 }
297                                         ]
298                                 }
299                 }
300         put(PREDEFINE_OPERATIONACTION % contHost, data)
301
302 def add_operation_conditions(contHost):
303         data={
304                         "condition-parameter-definitions": {
305                                 "condition-parameter-definition": [
306                                                 {
307                                                         "parameter-name": "time",
308                                                         "parameter-value-type": "string",
309                                                         "parameter-match-patterns": {
310                                                          "parameter-match-pattern": ["less-than" ,"not-less-than","equal","not-equal",
311                                                                                         "greater-than","not-greater-than","between","periodical"]
312                                                          }                                                              }
313                                         ]
314                                 }
315                 }
316         put(PREDEFINE_OPERATIONCONDITION % contHost, data)
317
318 def add_port_attributes(conHost):
319         data={
320                         "physical-port-attribute-definitions":{
321                                         "physical-port-attribute-definition":[
322                                                         {
323                                                                 "attribute-name":"location",
324                                                                 "attribute-value-type":"string",
325                                                                 "attribute-match-patterns":{
326                                                                         "attribute-match-pattern":["equal"]
327                                                                 }
328                                                         }
329                                                 ]
330                         }
331                 }
332         put(PHYSICAL_PORT_ATTRIBUTE % conHost, data)
333
334 def add_node_attributes(conHost):
335         data={
336                         "physical-node-attribute-definitions":{
337                                 "physical-node-attribute-definition":[
338                                         {
339                                                 "attribute-name":"location",
340                                                 "attribute-value-type":"string",
341                                                 "attribute-match-patterns":{
342                                                         "attribute-match-pattern":["equal"]
343                                                 }
344                                         },
345                                         {
346                                                 "attribute-name":"capacity",
347                                                 "attribute-value-type":"int",
348                                                 "attribute-match-patterns":{
349                                                         "attribute-match-pattern":["equal"]
350                                                 }
351                                         }
352                                 ]
353                         }
354                 }
355         put(PHYSICAL_NODE_ATTRIBUTE % conHost, data)
356
357
358
359 def put(url, data):
360     headers = {'Content-type': 'application/yang.data+json',
361                'Accept': 'application/yang.data+json'}
362     print "PUT %s" % url
363     print json.dumps(data, indent=4, sort_keys=True)
364     r = requests.put(url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(USERNAME, PASSWORD))
365     print r.text
366     r.raise_for_status()
367
368 if __name__ == '__main__':
369
370         parser = argparse.ArgumentParser()
371         parser.add_argument('--controller', default='127.0.0.1', help='controller IP')
372         args=parser.parse_args()
373
374         print args.controller
375
376         add_predefined_userrole(args.controller)
377         add_predefined_nodetypes(args.controller)
378         add_predefined_connectiontypes(args.controller)
379         add_flow_matchtypes(args.controller)
380         add_flow_properties(args.controller)
381         add_operation_actions(args.controller)
382         add_operation_conditions(args.controller)
383         add_port_attributes(args.controller)
384         add_node_attributes(args.controller)