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