blob: 6ff250a525650cc391cf3057f0f8d4964a255cd7 [file] [log] [blame]
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -08001#! /usr/bin/env python
2# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
3
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -07004import copy
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -08005import pprint
6import os
7import sys
8import subprocess
9import json
10import argparse
11import io
12import time
13
14from flask import Flask, json, Response, render_template, make_response, request
15
16#
Naoki Shiota862cc3b2013-12-13 15:42:50 -080017# curl http://127.0.0.1:8080/wm/onos/topology/route/00:00:00:00:00:00:0a:01/1/00:00:00:00:00:00:0a:04/1/json
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080018#
19
20## Global Var ##
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -070021ControllerIP = "127.0.0.1"
22ControllerPort = 8080
23MonitoringEnabled = False
Pavlin Radoslavovb549f082013-03-29 04:28:27 -070024MonitoringByOnos = False
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070025ReadFromFile = ""
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080026
27DEBUG=0
28pp = pprint.PrettyPrinter(indent=4)
29
30app = Flask(__name__)
31
32## Worker Functions ##
33def log_error(txt):
34 print '%s' % (txt)
35
36def debug(txt):
37 if DEBUG:
38 print '%s' % (txt)
39
Naoki Shiota862cc3b2013-12-13 15:42:50 -080040# @app.route("/wm/onos/topology/route/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080041#
42# Sample output:
43# {'dstPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:02'}}, 'srcPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:01'}}, 'flowEntries': [{'outPort': {'value': 1}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 0}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:01'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}, {'outPort': {'value': 0}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 9}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:02'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}]}
44#
45def shortest_path(v1, p1, v2, p2):
46 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -080047 command = "curl -s http://%s:%s/wm/onos/topology/route/%s/%s/%s/%s/json" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080048 debug("shortest_path %s" % command)
Pavlin Radoslavoveb2d5a22013-03-14 19:58:14 -070049 parsedResult = []
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080050
51 result = os.popen(command).read()
52 debug("result %s" % result)
53 if len(result) == 0:
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070054 log_error("No Path found from %s/%s to %s/%s" % (v1, p1, v2, p2))
55 else:
56 parsedResult = json.loads(result)
57 debug("parsed %s" % parsedResult)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080058
59 except:
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070060 log_error("Controller IF has issue: No Path found from %s/%s to %s/%s" % (v1, p1, v2, p2))
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080061
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070062 return parsedResult
63
64def print_data_path(data_path):
65 if len(data_path) == 0:
66 return
67
68 srcSwitch = data_path['srcPort']['dpid']['value'];
69 srcPort = data_path['srcPort']['port']['value'];
70 dstSwitch = data_path['dstPort']['dpid']['value'];
71 dstPort = data_path['dstPort']['port']['value'];
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080072
73 print "DataPath: (src = %s/%s dst = %s/%s)" % (srcSwitch, srcPort, dstSwitch, dstPort);
74
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -070075 for f in data_path['flowEntries']:
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080076 inPort = f['inPort']['value'];
77 outPort = f['outPort']['value'];
78 dpid = f['dpid']['value']
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -070079 print " FlowEntry: (%s, %s, %s)" % (inPort, dpid, outPort)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080080
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080081def add_flow_path(flow_path):
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -070082 flow_path_json = json.dumps(flow_path)
83
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080084 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -080085 command = "curl -s -H 'Content-Type: application/json' -d '%s' http://%s:%s/wm/onos/flows/add/json" % (flow_path_json, ControllerIP, ControllerPort)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080086 debug("add_flow_path %s" % command)
87 result = os.popen(command).read()
88 debug("result %s" % result)
89 # parsedResult = json.loads(result)
90 # debug("parsed %s" % parsedResult)
91 except:
92 log_error("Controller IF has issue")
93 exit(1)
94
Pavlin Radoslavovb549f082013-03-29 04:28:27 -070095def add_shortest_path_flow(flow_path):
96 flow_path_json = json.dumps(flow_path)
97
98 try:
Pavlin Radoslavov953fb0a2013-12-13 18:55:14 -080099 command = "curl -s -H 'Content-Type: application/json' -d '%s' http://%s:%s/wm/onos/flows/add/json" % (flow_path_json, ControllerIP, ControllerPort)
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700100 debug("add_shortest_path_flow %s" % command)
101 result = os.popen(command).read()
102 debug("result %s" % result)
103 # parsedResult = json.loads(result)
104 # debug("parsed %s" % parsedResult)
105 except:
106 log_error("Controller IF has issue")
107 exit(1)
108
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700109def delete_flow_path(flow_id):
Naoki Shiota862cc3b2013-12-13 15:42:50 -0800110 command = "curl -s \"http://%s:%s/wm/onos/flows/delete/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700111 debug("delete_flow_path %s" % command)
112 result = os.popen(command).read()
113 debug("result %s" % result)
114 # parsedResult = json.loads(result)
115 # debug("parsed %s" % parsedResult)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700116
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700117def extract_flow_args(my_args):
118 # Check the arguments
119 if len(my_args) < 6:
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800120 log_error(usage_msg)
121 exit(1)
122
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700123 # Extract the mandatory arguments
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700124 my_flow_id = my_args[0]
125 my_installer_id = my_args[1]
126 my_src_dpid = my_args[2]
127 my_src_port = my_args[3]
128 my_dst_dpid = my_args[4]
129 my_dst_port = my_args[5]
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700130
131 #
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800132 # Extract the "flowPathFlags", "idleTimeout", "hardTimeout",
133 # "match" and "action" arguments.
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700134 #
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700135 flowPathFlags = 0L
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800136 idleTimeout = 0
137 hardTimeout = 0
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700138 match = {}
139 matchInPortEnabled = True # NOTE: Enabled by default
140 actions = []
141 actionOutputEnabled = True # NOTE: Enabled by default
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700142 idx = 6
143 while idx < len(my_args):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700144 action = {}
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700145 arg1 = my_args[idx]
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700146 idx = idx + 1
147 # Extract the second argument
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700148 if idx >= len(my_args):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700149 error_arg = "ERROR: Missing or invalid '" + arg1 + "' argument"
150 log_error(error_arg)
151 log_error(usage_msg)
152 exit(1)
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700153 arg2 = my_args[idx]
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700154 idx = idx + 1
155
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700156 if arg1 == "flowPathFlags":
157 if "DISCARD_FIRST_HOP_ENTRY" in arg2:
158 flowPathFlags = flowPathFlags + 0x1
159 if "KEEP_ONLY_FIRST_HOP_ENTRY" in arg2:
160 flowPathFlags = flowPathFlags + 0x2
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800161 elif arg1 == "idleTimeout":
162 idleTimeout = arg2
163 elif arg1 == "hardTimeout":
164 hardTimeout = arg2
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700165 elif arg1 == "matchInPort":
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700166 # Just mark whether inPort matching is enabled
167 matchInPortEnabled = arg2 in ['True', 'true']
168 # inPort = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700169 # inPort['value'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700170 # match['inPort'] = inPort
171 ## match['matchInPort'] = True
172 elif arg1 == "matchSrcMac":
173 srcMac = {}
174 srcMac['value'] = arg2
175 match['srcMac'] = srcMac
176 # match['matchSrcMac'] = True
177 elif arg1 == "matchDstMac":
178 dstMac = {}
179 dstMac['value'] = arg2
180 match['dstMac'] = dstMac
181 # match['matchDstMac'] = True
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700182 elif arg1 == "matchEthernetFrameType":
183 match['ethernetFrameType'] = int(arg2, 0)
184 # match['matchEthernetFrameType'] = True
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700185 elif arg1 == "matchVlanId":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700186 match['vlanId'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700187 # match['matchVlanId'] = True
188 elif arg1 == "matchVlanPriority":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700189 match['vlanPriority'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700190 # match['matchVlanPriority'] = True
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700191 elif arg1 == "matchSrcIPv4Net":
192 srcIPv4Net = {}
193 srcIPv4Net['value'] = arg2
194 match['srcIPv4Net'] = srcIPv4Net
195 # match['matchSrcIPv4Net'] = True
196 elif arg1 == "matchDstIPv4Net":
197 dstIPv4Net = {}
198 dstIPv4Net['value'] = arg2
199 match['dstIPv4Net'] = dstIPv4Net
200 # match['matchDstIPv4Net'] = True
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700201 elif arg1 == "matchIpProto":
202 match['ipProto'] = int(arg2, 0)
203 # match['matchIpProto'] = True
204 elif arg1 == "matchIpToS":
205 match['ipToS'] = int(arg2, 0)
206 # match['matchIpToS'] = True
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700207 elif arg1 == "matchSrcTcpUdpPort":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700208 match['srcTcpUdpPort'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700209 # match['matchSrcTcpUdpPort'] = True
210 elif arg1 == "matchDstTcpUdpPort":
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700211 match['dstTcpUdpPort'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700212 # match['matchDstTcpUdpPort'] = True
213 elif arg1 == "actionOutput":
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700214 # Mark whether ACTION_OUTPUT action is enabled
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700215 actionOutputEnabled = arg2 in ['True', 'true']
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700216 # If ACTION_OUTPUT is explicitly enabled, add an entry with a fake
217 # port number. We need this entry to preserve the action ordering.
218 if actionOutputEnabled == True:
219 actionOutput = {}
220 outPort = {}
221 outPort['value'] = 0xffff
222 actionOutput['port'] = outPort
223 actionOutput['maxLen'] = 0
224 action['actionOutput'] = actionOutput
225 # action['actionType'] = 'ACTION_OUTPUT'
226 actions.append(action)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700227 #
228 elif arg1 == "actionSetVlanId":
229 vlanId = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700230 vlanId['vlanId'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700231 action['actionSetVlanId'] = vlanId
232 # action['actionType'] = 'ACTION_SET_VLAN_VID'
233 actions.append(copy.deepcopy(action))
234 elif arg1 == "actionSetVlanPriority":
235 vlanPriority = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700236 vlanPriority['vlanPriority'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700237 action['actionSetVlanPriority'] = vlanPriority
238 # action['actionType'] = 'ACTION_SET_VLAN_PCP'
239 actions.append(copy.deepcopy(action))
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700240 elif arg1 == "actionStripVlan":
241 stripVlan = {}
242 stripVlan['stripVlan'] = arg2 in ['True', 'true']
243 action['actionStripVlan'] = stripVlan
244 # action['actionType'] = 'ACTION_STRIP_VLAN'
245 actions.append(copy.deepcopy(action))
246 elif arg1 == "actionSetEthernetSrcAddr":
247 ethernetSrcAddr = {}
248 ethernetSrcAddr['value'] = arg2
249 setEthernetSrcAddr = {}
250 setEthernetSrcAddr['addr'] = ethernetSrcAddr
251 action['actionSetEthernetSrcAddr'] = setEthernetSrcAddr
252 # action['actionType'] = 'ACTION_SET_DL_SRC'
253 actions.append(copy.deepcopy(action))
254 elif arg1 == "actionSetEthernetDstAddr":
255 ethernetDstAddr = {}
256 ethernetDstAddr['value'] = arg2
257 setEthernetDstAddr = {}
258 setEthernetDstAddr['addr'] = ethernetDstAddr
259 action['actionSetEthernetDstAddr'] = setEthernetDstAddr
260 # action['actionType'] = 'ACTION_SET_DL_DST'
261 actions.append(copy.deepcopy(action))
262 elif arg1 == "actionSetIPv4SrcAddr":
263 IPv4SrcAddr = {}
264 IPv4SrcAddr['value'] = arg2
265 setIPv4SrcAddr = {}
266 setIPv4SrcAddr['addr'] = IPv4SrcAddr
267 action['actionSetIPv4SrcAddr'] = setIPv4SrcAddr
268 # action['actionType'] = 'ACTION_SET_NW_SRC'
269 actions.append(copy.deepcopy(action))
270 elif arg1 == "actionSetIPv4DstAddr":
271 IPv4DstAddr = {}
272 IPv4DstAddr['value'] = arg2
273 setIPv4DstAddr = {}
274 setIPv4DstAddr['addr'] = IPv4DstAddr
275 action['actionSetIPv4DstAddr'] = setIPv4DstAddr
276 # action['actionType'] = 'ACTION_SET_NW_DST'
277 actions.append(copy.deepcopy(action))
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700278 elif arg1 == "actionSetIpToS":
279 ipToS = {}
280 ipToS['ipToS'] = int(arg2, 0)
281 action['actionSetIpToS'] = ipToS
282 # action['actionType'] = 'ACTION_SET_NW_TOS'
283 actions.append(copy.deepcopy(action))
284 elif arg1 == "actionSetTcpUdpSrcPort":
285 tcpUdpSrcPort = {}
286 tcpUdpSrcPort['port'] = int(arg2, 0)
287 action['actionSetTcpUdpSrcPort'] = tcpUdpSrcPort
288 # action['actionType'] = 'ACTION_SET_TP_SRC'
289 actions.append(copy.deepcopy(action))
290 elif arg1 == "actionSetTcpUdpDstPort":
291 tcpUdpDstPort = {}
292 tcpUdpDstPort['port'] = int(arg2, 0)
293 action['actionSetTcpUdpDstPort'] = tcpUdpDstPort
294 # action['actionType'] = 'ACTION_SET_TP_DST'
295 actions.append(copy.deepcopy(action))
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700296 elif arg1 == "actionEnqueue":
297 # TODO: Implement ACTION_ENQUEUE
298 actionEnqueue = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700299 # actionEnqueue['queueId'] = int(arg2, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700300 # enqueuePort = {}
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700301 # enqueuePort['value'] = int(arg3, 0)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700302 # actionEnqueue['port'] = enqueuePort
303 # action['actionEnqueue'] = actionEnqueue
304 # # action['actionType'] = 'ACTION_ENQUEUE'
305 # actions.append(copy.deepcopy(action))
306 #
307 else:
308 log_error("ERROR: Unknown argument '%s'" % (arg1))
309 log_error(usage_msg)
310 exit(1)
311
Pavlin Radoslavov8a002d92013-03-13 20:20:43 -0700312 return {
313 'my_flow_id' : my_flow_id,
314 'my_installer_id' : my_installer_id,
315 'my_src_dpid' : my_src_dpid,
316 'my_src_port' : my_src_port,
317 'my_dst_dpid' : my_dst_dpid,
318 'my_dst_port' : my_dst_port,
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700319 'flowPathFlags' : flowPathFlags,
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800320 'idleTimeout' : idleTimeout,
321 'hardTimeout' : hardTimeout,
Pavlin Radoslavov8a002d92013-03-13 20:20:43 -0700322 'match' : match,
323 'matchInPortEnabled' : matchInPortEnabled,
324 'actions' : actions,
325 'actionOutputEnabled' : actionOutputEnabled
326 }
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700327
328def compute_data_path(parsed_args):
329
330 my_src_dpid = parsed_args['my_src_dpid']
331 my_src_port = parsed_args['my_src_port']
332 my_dst_dpid = parsed_args['my_dst_dpid']
333 my_dst_port = parsed_args['my_dst_port']
334
335 # Compute the shortest path
336 data_path = shortest_path(my_src_dpid, my_src_port, my_dst_dpid, my_dst_port)
337
338 debug("Data Path: %s" % data_path)
339 return data_path
340
341def compute_flow_path(parsed_args, data_path):
342
343 my_flow_id = parsed_args['my_flow_id']
344 my_installer_id = parsed_args['my_installer_id']
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700345 myFlowPathFlags = parsed_args['flowPathFlags']
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800346 myIdleTimeout = parsed_args['idleTimeout']
347 myHardTimeout = parsed_args['hardTimeout']
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700348 match = parsed_args['match']
349 matchInPortEnabled = parsed_args['matchInPortEnabled']
350 actions = parsed_args['actions']
351 actionOutputEnabled = parsed_args['actionOutputEnabled']
352 my_data_path = copy.deepcopy(data_path)
353
354 flow_id = {}
355 flow_id['value'] = my_flow_id
356 installer_id = {}
357 installer_id['value'] = my_installer_id
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700358 flowPathFlags = {}
359 flowPathFlags['flags'] = myFlowPathFlags
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700360
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700361 flowEntryActions = {}
362
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700363 flow_path = {}
364 flow_path['flowId'] = flow_id
365 flow_path['installerId'] = installer_id
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700366 # NOTE: The 'flowPathType' might be rewritten later
367 flow_path['flowPathType'] = 'FP_TYPE_EXPLICIT_PATH'
Pavlin Radoslavov7d4a40e2013-10-27 23:39:40 -0700368 flow_path['flowPathUserState'] = 'FP_USER_ADD'
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700369 flow_path['flowPathFlags'] = flowPathFlags
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800370 flow_path['idleTimeout'] = myIdleTimeout
371 flow_path['hardTimeout'] = myHardTimeout
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700372
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -0700373 if (len(match) > 0):
374 flow_path['flowEntryMatch'] = copy.deepcopy(match)
375
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700376 #
377 # Add the match conditions to each flow entry
378 #
379 if (len(match) > 0) or matchInPortEnabled:
380 idx = 0
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700381 while idx < len(my_data_path['flowEntries']):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700382 if matchInPortEnabled:
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700383 inPort = my_data_path['flowEntries'][idx]['inPort']
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700384 match['inPort'] = copy.deepcopy(inPort)
385 # match['matchInPort'] = True
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700386 my_data_path['flowEntries'][idx]['flowEntryMatch'] = copy.deepcopy(match)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700387 idx = idx + 1
388
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700389
390 if (len(actions) > 0):
391 flowEntryActions['actions'] = copy.deepcopy(actions)
392 flow_path['flowEntryActions'] = flowEntryActions
393
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700394 #
395 # Set the actions for each flow entry
396 # NOTE: The actions from the command line are aplied
397 # ONLY to the first flow entry.
398 #
399 # If ACTION_OUTPUT action is enabled, then apply it
400 # to each flow entry.
401 #
402 if (len(actions) > 0) or actionOutputEnabled:
403 idx = 0
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700404 while idx < len(my_data_path['flowEntries']):
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700405 if idx > 0:
406 actions = [] # Reset the actions for all but first entry
407 action = {}
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700408 outPort = my_data_path['flowEntries'][idx]['outPort']
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700409 actionOutput = {}
410 actionOutput['port'] = copy.deepcopy(outPort)
411 # actionOutput['maxLen'] = 0 # TODO: not used for now
412 action['actionOutput'] = copy.deepcopy(actionOutput)
413 # action['actionType'] = 'ACTION_OUTPUT'
414 actions.append(copy.deepcopy(action))
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700415 flowEntryActions = {}
416 flowEntryActions['actions'] = copy.deepcopy(actions)
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700417
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700418 my_data_path['flowEntries'][idx]['flowEntryActions'] = flowEntryActions
Pavlin Radoslavovf13923a2013-03-11 19:42:17 -0700419 idx = idx + 1
420
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700421 flow_path['dataPath'] = my_data_path
422 debug("Flow Path: %s" % flow_path)
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700423 return flow_path
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800424
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700425def exec_monitoring_by_onos(parsed_args):
426 idx = 0
427 while idx < len(parsed_args):
428 data_path = {}
429 src_dpid = {}
430 src_port = {}
431 dst_dpid = {}
432 dst_port = {}
433 src_switch_port = {}
434 dst_switch_port = {}
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700435 flow_entries = []
436
437 src_dpid['value'] = parsed_args[idx]['my_src_dpid']
438 src_port['value'] = parsed_args[idx]['my_src_port']
439 dst_dpid['value'] = parsed_args[idx]['my_dst_dpid']
440 dst_port['value'] = parsed_args[idx]['my_dst_port']
441 src_switch_port['dpid'] = src_dpid
442 src_switch_port['port'] = src_port
443 dst_switch_port['dpid'] = dst_dpid
444 dst_switch_port['port'] = dst_port
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700445
446 data_path['srcPort'] = copy.deepcopy(src_switch_port)
447 data_path['dstPort'] = copy.deepcopy(dst_switch_port)
448 data_path['flowEntries'] = copy.deepcopy(flow_entries)
449
450 #
451 # XXX: Explicitly disable the InPort matching, and
452 # the Output action, because they get in the way
453 # during the compute_flow_path() processing.
454 #
455 parsed_args[idx]['matchInPortEnabled'] = False
456 parsed_args[idx]['actionOutputEnabled'] = False
457
458 flow_path = compute_flow_path(parsed_args[idx], data_path)
Pavlin Radoslavovd28cf7c2013-10-26 11:27:43 -0700459 flow_path['flowPathType'] = 'FP_TYPE_SHORTEST_PATH'
460
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700461 add_shortest_path_flow(flow_path)
462
463 idx = idx + 1
464
465
466def exec_processing_by_script(parsed_args):
467 #
468 # Initialization
469 #
470 last_data_paths = []
471 idx = 0
472 while idx < len(parsed_args):
473 last_data_path = []
474 last_data_paths.append(copy.deepcopy(last_data_path))
475 idx = idx + 1
476
477 #
478 # Do the work: install and/or periodically monitor each flow
479 #
480 while True:
481 idx = 0
482 while idx < len(parsed_args):
483 last_data_path = last_data_paths[idx]
484 my_flow_id = parsed_args[idx]['my_flow_id']
485 data_path = compute_data_path(parsed_args[idx])
486 if data_path != last_data_path:
487 print_data_path(data_path)
488 if len(last_data_path) > 0:
489 delete_flow_path(my_flow_id)
490 if len(data_path) > 0:
491 flow_path = compute_flow_path(parsed_args[idx], data_path)
492 add_flow_path(flow_path)
493 last_data_paths[idx] = copy.deepcopy(data_path)
494 idx = idx + 1
495
496 if MonitoringEnabled != True:
497 break
498 time.sleep(1)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800499
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700500
501if __name__ == "__main__":
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700502 usage_msg = "Usage: %s [Flags] <flow-id> <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port> [Flow Path Flags] [Match Conditions] [Actions]\n" % (sys.argv[0])
Pavlin Radoslavov7be3bac2013-03-27 09:59:34 -0700503 usage_msg = usage_msg + "\n"
Pavlin Radoslavov051abb42013-12-05 17:24:50 -0800504 usage_msg = usage_msg + " <flow-id> The Flow ID, or -1 if it should be assigned by ONOS\n"
505 usage_msg = usage_msg + "\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700506 usage_msg = usage_msg + " Flags:\n"
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700507 usage_msg = usage_msg + " -m [monitorname] Monitor and maintain the installed shortest path(s)\n"
508 usage_msg = usage_msg + " If 'monitorname' is specified and is set to 'ONOS'\n"
Pavlin Radoslavovd3128c82013-11-11 14:58:00 -0800509 usage_msg = usage_msg + " (case insensitive), then the flow generation and\n"
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700510 usage_msg = usage_msg + " maintanenance is done by ONOS itself.\n"
511 usage_msg = usage_msg + " Otherwise, it is done by this script.\n"
512 usage_msg = usage_msg + " -f <filename> Read the flow(s) to install from a file\n"
513 usage_msg = usage_msg + " File format: one line per flow starting with <flow-id>\n"
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700514 usage_msg = usage_msg + "\n"
Pavlin Radoslavov204b2862013-07-12 14:15:36 -0700515 usage_msg = usage_msg + " Flow Path Flags:\n"
516 usage_msg = usage_msg + " flowPathFlags <Flags> (flag names separated by ',')\n"
517 usage_msg = usage_msg + "\n"
518 usage_msg = usage_msg + " Known flags:\n"
519 usage_msg = usage_msg + " DISCARD_FIRST_HOP_ENTRY : Discard the first-hop flow entry\n"
520 usage_msg = usage_msg + " KEEP_ONLY_FIRST_HOP_ENTRY : Keep only the first-hop flow entry\n"
521 usage_msg = usage_msg + "\n"
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800522 usage_msg = usage_msg + " Timeouts (in seconds in the [0, 65535] interval):\n"
523 usage_msg = usage_msg + " idleTimeout <idleTimeoutInSeconds> (default to 0: no timeout)\n"
524 usage_msg = usage_msg + " hardTimeout <hardTimeoutInSeconds> (default to 0: no timeout)\n"
525 usage_msg = usage_msg + "\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700526 usage_msg = usage_msg + " Match Conditions:\n"
527 usage_msg = usage_msg + " matchInPort <True|False> (default to True)\n"
528 usage_msg = usage_msg + " matchSrcMac <source MAC address>\n"
529 usage_msg = usage_msg + " matchDstMac <destination MAC address>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700530 usage_msg = usage_msg + " matchEthernetFrameType <Ethernet frame type>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700531 usage_msg = usage_msg + " matchVlanId <VLAN ID>\n"
532 usage_msg = usage_msg + " matchVlanPriority <VLAN priority>\n"
Pavlin Radoslavovad3a1e62013-07-09 13:30:16 -0700533 usage_msg = usage_msg + " matchSrcIPv4Net <source IPv4 network address>\n"
534 usage_msg = usage_msg + " matchDstIPv4Net <destination IPv4 network address>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700535 usage_msg = usage_msg + " matchIpProto <IP protocol>\n"
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800536 usage_msg = usage_msg + " matchIpToS <IP ToS> (DSCP field, 6 bits)\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700537 usage_msg = usage_msg + " matchSrcTcpUdpPort <source TCP/UDP port>\n"
538 usage_msg = usage_msg + " matchDstTcpUdpPort <destination TCP/UDP port>\n"
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700539 usage_msg = usage_msg + "\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700540 usage_msg = usage_msg + " Actions:\n"
541 usage_msg = usage_msg + " actionOutput <True|False> (default to True)\n"
Pavlin Radoslavovc1bafd12013-07-12 17:00:35 -0700542 usage_msg = usage_msg + " actionSetVlanId <VLAN ID>\n"
543 usage_msg = usage_msg + " actionSetVlanPriority <VLAN priority>\n"
544 usage_msg = usage_msg + " actionStripVlan <True|False>\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700545 usage_msg = usage_msg + " actionSetEthernetSrcAddr <source MAC address>\n"
546 usage_msg = usage_msg + " actionSetEthernetDstAddr <destination MAC address>\n"
547 usage_msg = usage_msg + " actionSetIPv4SrcAddr <source IPv4 address>\n"
548 usage_msg = usage_msg + " actionSetIPv4DstAddr <destination IPv4 address>\n"
Pavlin Radoslavov4efa80f2013-12-10 11:18:24 -0800549 usage_msg = usage_msg + " actionSetIpToS <IP ToS> (DSCP field, 6 bits)\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700550 usage_msg = usage_msg + " actionSetTcpUdpSrcPort <source TCP/UDP port>\n"
551 usage_msg = usage_msg + " actionSetTcpUdpDstPort <destination TCP/UDP port>\n"
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -0700552 usage_msg = usage_msg + " Actions (not implemented yet):\n"
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700553 usage_msg = usage_msg + " actionEnqueue <dummy argument>\n"
554
555 # app.debug = False;
556
557 # Usage info
558 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
559 print(usage_msg)
560 exit(0)
561
562 #
563 # Check the flags
564 #
565 start_argv_index = 1
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700566 idx = 1
567 while idx < len(sys.argv):
568 arg1 = sys.argv[idx]
569 idx = idx + 1
570 if arg1 == "-m":
571 MonitoringEnabled = True
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700572 if idx < len(sys.argv):
573 arg2 = sys.argv[idx]
574 if arg2.lower() == "onos":
575 MonitoringByOnos = True
576 idx = idx + 1
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700577 start_argv_index = idx
578 elif arg1 == "-f":
579 if idx >= len(sys.argv):
580 error_arg = "ERROR: Missing or invalid '" + arg1 + "' argument"
581 log_error(error_arg)
582 log_error(usage_msg)
583 exit(1)
584 ReadFromFile = sys.argv[idx]
585 idx = idx + 1
586 start_argv_index = idx
587 else:
588 break;
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700589
590 #
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700591 # Read the arguments from a file or from the remaining command line options
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700592 #
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700593 my_lines = []
594 if len(ReadFromFile) > 0:
595 f = open(ReadFromFile, "rt")
596 my_line = f.readline()
597 while my_line:
598 if len(my_line.rstrip()) > 0 and my_line[0] != "#":
599 my_token_line = my_line.rstrip().split()
600 my_lines.append(my_token_line)
601 my_line = f.readline()
602 else:
603 my_lines.append(copy.deepcopy(sys.argv[start_argv_index:]))
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700604
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700605 #
606 # Initialization
607 #
608 last_data_paths = []
609 parsed_args = []
610 idx = 0
611 while idx < len(my_lines):
612 last_data_path = []
613 last_data_paths.append(copy.deepcopy(last_data_path))
614 #
615 # Parse the flow arguments
616 #
617 my_args = my_lines[idx]
618 parsed_args.append(copy.deepcopy(extract_flow_args(my_args)))
619 # Cleanup leftover state
620 my_flow_id = parsed_args[idx]['my_flow_id']
621 delete_flow_path(my_flow_id)
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700622
Pavlin Radoslavov2a0bd8b2013-03-15 18:45:51 -0700623 idx = idx + 1
624
625 #
Pavlin Radoslavovb549f082013-03-29 04:28:27 -0700626 if MonitoringByOnos == True:
627 exec_monitoring_by_onos(parsed_args)
628 else:
629 exec_processing_by_script(parsed_args)
Pavlin Radoslavov3f3778a2013-03-13 08:16:57 -0700630