Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 1 | import command |
| 2 | import json |
| 3 | import fmtcnv |
| 4 | POLICY_CONFIG_FORMAT = { |
| 5 | 'policy-config' : { |
| 6 | 'field-orderings' : { |
| 7 | 'default' : [ |
| 8 | 'policy-id', |
| 9 | ], |
| 10 | }, |
| 11 | }, |
| 12 | } |
| 13 | |
| 14 | POLICY_SUBMODE_COMMAND_DESCRIPTION = { |
| 15 | 'name' : 'policy', |
| 16 | 'short-help' : 'Enter policy submode, configure SR policy details', |
| 17 | 'mode' : 'config', |
| 18 | 'command-type' : 'config-submode', |
| 19 | 'obj-type' : 'policy-config', |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 20 | 'submode-name' : 'config-policy', #config-policy-tunnel or config-policy-loadbalance or config-policy-avoid |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 21 | 'parent-field' : None, |
| 22 | 'doc' : 'policy|policy', |
| 23 | 'doc-example' : 'policy|policy-example', |
| 24 | #'completion' : 'policy-id-completion', |
| 25 | 'args' : { |
| 26 | 'action' : ( |
| 27 | { |
| 28 | 'proc' : 'create-policy', |
| 29 | }, |
| 30 | { |
| 31 | 'proc' : 'push-mode-stack', |
| 32 | }, |
| 33 | ), |
| 34 | 'no-action' : ( |
| 35 | { |
| 36 | 'proc' : 'remove-policy', |
| 37 | }, |
| 38 | ), |
| 39 | 'choices' : ( |
| 40 | ( |
| 41 | { |
| 42 | 'field' : 'policy-id', |
| 43 | 'type' : 'identifier', |
| 44 | 'completion' : 'complete-object-field', |
| 45 | 'syntax-help' : 'Enter a policy name', |
| 46 | 'doc' : 'policy|policy', |
| 47 | }, |
| 48 | { |
| 49 | 'token' : 'policy-type', |
| 50 | 'optional-for-no' : True, |
| 51 | 'short-help' : 'Set type of policy', |
| 52 | 'doc' : 'policy|policy', |
| 53 | 'completion' : 'complete-object-field', |
| 54 | }, |
| 55 | { |
| 56 | 'field' : 'policy-type', |
| 57 | 'optional-for-no' : True, |
| 58 | 'type' : 'enum', |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 59 | 'values' : ('tunnel-flow','loadbalance','avoid','deny'), |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 60 | 'completion' : 'complete-object-field', |
| 61 | 'syntax-help' : 'Enter a policy type', |
| 62 | 'doc' : 'policy|policy', |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 63 | }, |
| 64 | ), |
| 65 | ), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | SRC_IP_MATCH = { |
| 70 | 'choices' : ( |
| 71 | ( |
| 72 | { |
| 73 | 'field' : 'src_ip', |
| 74 | 'type' : 'cidr-range', |
| 75 | 'help-name' : 'src-cidr', |
| 76 | #'data-handler' : 'split-cidr-data-inverse', |
| 77 | #'dest-ip' : 'src-ip', |
| 78 | #'dest-netmask' : 'src-ip-mask', |
| 79 | 'data' : { |
| 80 | 'dst_ip' : '0.0.0.0/0', |
| 81 | }, |
| 82 | 'doc' : 'vns|vns-access-list-cidr-range', |
| 83 | } |
| 84 | ), |
| 85 | ( |
| 86 | { |
| 87 | 'token' : 'any', |
| 88 | 'data' : { |
| 89 | 'src_ip' : '0.0.0.0/0', |
| 90 | 'dst_ip' : '0.0.0.0/0', |
| 91 | }, |
| 92 | 'doc' : 'vns|vns-access-list-ip-any', |
| 93 | } |
| 94 | ), |
| 95 | ) |
| 96 | } |
| 97 | |
| 98 | SRC_PORT_MATCH = ( |
| 99 | { |
| 100 | 'field' : 'src_tp_port_op', |
| 101 | 'type' : 'enum', |
| 102 | 'values' : ('eq', 'neq'), |
| 103 | 'doc' : 'vns|vns-access-list-port-op-+', |
| 104 | }, |
| 105 | { |
| 106 | 'choices' : ( |
| 107 | { |
| 108 | 'field' : 'src_tp_port', |
| 109 | 'base-type' : 'hex-or-decimal-integer', |
| 110 | 'range' : (0,65535), |
| 111 | 'data-handler' : 'hex-to-integer', |
| 112 | 'doc' : 'vns|vns-access-list-port-hex', |
| 113 | 'doc-include' : [ 'range' ], |
| 114 | }, |
| 115 | { |
| 116 | 'field' : 'src_tp_port', |
| 117 | 'type' : 'enum', |
| 118 | 'values' : fmtcnv.tcp_name_to_number_dict, |
| 119 | 'permute' : 'skip', |
| 120 | 'doc' : 'vns|vns-access-list-port-type', |
| 121 | }, |
| 122 | ), |
| 123 | }, |
| 124 | ) |
| 125 | |
| 126 | |
| 127 | DST_IP_MATCH = { |
| 128 | 'choices' : ( |
| 129 | ( |
| 130 | { |
| 131 | 'field' : 'dst_ip', |
| 132 | 'type' : 'cidr-range', |
| 133 | 'help-name' : 'dst-cidr', |
| 134 | #'data-handler' : 'split-cidr-data-inverse', |
| 135 | #'dest-ip' : 'dst-ip', |
| 136 | #'dest-netmask' : 'dst-ip-mask', |
| 137 | 'doc' : 'vns|vns-access-list-cidr-range', |
| 138 | }, |
| 139 | ), |
| 140 | ( |
| 141 | { |
| 142 | 'token' : 'any', |
| 143 | 'data' : { |
| 144 | 'dst_ip' : '0.0.0.0/0', |
| 145 | }, |
| 146 | 'doc' : 'vns|vns-access-list-ip-any', |
| 147 | } |
| 148 | ), |
| 149 | ) |
| 150 | } |
| 151 | |
| 152 | |
| 153 | DST_PORT_MATCH = ( |
| 154 | { |
| 155 | 'field' : 'dst_tp_port_op', |
| 156 | 'type' : 'enum', |
| 157 | 'values' : ('eq', 'neq'), |
| 158 | 'doc' : 'vns|vns-access-list-port-op+', |
| 159 | }, |
| 160 | { |
| 161 | 'choices' : ( |
| 162 | { |
| 163 | 'field' : 'dst_tp_port', |
| 164 | 'base-type' : 'hex-or-decimal-integer', |
| 165 | 'range' : (0,65535), |
| 166 | 'data-handler' : 'hex-to-integer', |
| 167 | 'doc' : 'vns|vns-access-list-port-hex', |
| 168 | }, |
| 169 | { |
| 170 | 'field' : 'dst_tp_port', |
| 171 | 'type' : 'enum', |
| 172 | 'values' : fmtcnv.tcp_name_to_number_dict, |
| 173 | 'permute' : 'skip' |
| 174 | }, |
| 175 | ), |
| 176 | } |
| 177 | ) |
| 178 | |
| 179 | POLICY_FLOW_ENTRY_COMMAND_DESCRIPTION = { |
| 180 | 'name' : 'flow-entry', |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 181 | 'mode' : 'config-policy*', |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 182 | 'command-type' : 'config', |
| 183 | 'short-help' : 'Configure flow entry', |
| 184 | 'doc' : 'flow-entry|flow-entry', |
| 185 | 'doc-example' : 'flow-entry|flow-entry-example', |
| 186 | 'parent-field' : 'policy', |
| 187 | 'args' : { |
| 188 | 'action' : ( |
| 189 | { |
| 190 | 'proc' : 'create-policy', |
| 191 | }, |
| 192 | ), |
| 193 | 'choices' : ( |
| 194 | ( |
| 195 | { |
| 196 | 'choices' : ( |
| 197 | { |
| 198 | 'field' : 'proto_type', |
| 199 | 'type' : 'enum', |
| 200 | 'values' : ('ip','tcp','udp'), |
| 201 | 'doc' : 'vns|vns-access-list-entry-type-+', |
| 202 | }, |
| 203 | { |
| 204 | 'field' : 'proto_type', |
| 205 | 'base-type' : 'hex-or-decimal-integer', |
| 206 | 'range' : (0,255), |
| 207 | 'help-name' : 'ip protocol', |
| 208 | 'data-handler' : 'hex-to-integer', |
| 209 | 'doc' : 'vns|vns-access-entry-type-ip-protocol', |
| 210 | 'doc-include' : [ 'range' ], |
| 211 | }, |
| 212 | ) |
| 213 | }, |
| 214 | # Complexity arises from the SRC_IP match part |
| 215 | # being, required, while the port match |
| 216 | # is optional, as is the DST_IP match, but the |
| 217 | # DST_PORT_MATCH is only possible to describe when |
| 218 | # the DST_IP part is included |
| 219 | SRC_IP_MATCH, |
| 220 | { |
| 221 | 'optional' : True, |
| 222 | 'optional-for-no' : True, |
| 223 | 'args' : SRC_PORT_MATCH, |
| 224 | }, |
| 225 | { |
| 226 | 'optional' : True, |
| 227 | 'optional-for-no' : True, |
| 228 | 'args' : ( |
| 229 | DST_IP_MATCH, |
| 230 | { |
| 231 | 'optional' : True, |
| 232 | 'optional-for-no' : True, |
| 233 | 'args' : DST_PORT_MATCH, |
| 234 | }, |
| 235 | ), |
| 236 | }, |
| 237 | ), |
| 238 | ), |
| 239 | }, |
| 240 | } |
| 241 | POLICY_TUNNEL_ID_COMMAND_DESCRIPTION = { |
| 242 | 'name' : 'tunnel', |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 243 | 'mode' : 'config-policy-tunnel', |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 244 | #'obj-type' : 'policy-config', |
| 245 | 'command-type' : 'config', |
| 246 | 'short-help' : 'Configure tunnel id', |
| 247 | #'doc' : 'policy|tunnel', |
| 248 | #'doc-example' : 'policy|policy-tunnel-example', |
| 249 | 'parent-field' : 'policy', |
| 250 | 'args' : { |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 251 | 'field' : 'tunnel-id', |
| 252 | 'completion' : 'tunnel-id-completion', |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 253 | 'action' : ( |
| 254 | { |
| 255 | 'proc' : 'create-policy', |
| 256 | }, |
| 257 | ), |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 258 | 'type' : 'identifier', |
| 259 | 'syntax-help' : 'Enter tunnel id', |
| 260 | 'doc' : 'policy|tunnel-id', |
| 261 | 'doc-include' : [ 'type-doc' ], |
| 262 | } |
| 263 | } |
| 264 | |
Srikanth Vavilapalli | b5c3ca5 | 2014-12-15 15:59:33 -0800 | [diff] [blame] | 265 | POLICY_TUNNELSET_ID_COMMAND_DESCRIPTION = { |
| 266 | 'name' : 'tunnelset', |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 267 | 'mode' : 'config-policy-loadbalance', |
Srikanth Vavilapalli | b5c3ca5 | 2014-12-15 15:59:33 -0800 | [diff] [blame] | 268 | #'obj-type' : 'policy-config', |
| 269 | 'command-type' : 'config', |
| 270 | 'short-help' : 'Configure tunnelset id', |
| 271 | #'doc' : 'policy|tunnel', |
| 272 | #'doc-example' : 'policy|policy-tunnel-example', |
| 273 | 'parent-field' : 'policy', |
| 274 | 'args' : { |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 275 | 'field' : 'tunnelset-id', |
| 276 | 'completion' : 'tunnelset-id-completion', |
Srikanth Vavilapalli | b5c3ca5 | 2014-12-15 15:59:33 -0800 | [diff] [blame] | 277 | 'action' : ( |
| 278 | { |
| 279 | 'proc' : 'create-policy', |
| 280 | }, |
| 281 | ), |
Srikanth Vavilapalli | b5c3ca5 | 2014-12-15 15:59:33 -0800 | [diff] [blame] | 282 | 'type' : 'identifier', |
| 283 | 'syntax-help' : 'Enter tunnelset id', |
| 284 | 'doc' : 'policy|tunnelset-id', |
| 285 | 'doc-include' : [ 'type-doc' ], |
| 286 | } |
| 287 | } |
| 288 | |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 289 | POLICY_PRIORITY_COMMAND_DESCRIPTION = { |
| 290 | 'name' : 'priority', |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 291 | 'mode' : 'config-policy*', |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 292 | 'command-type' : 'config', |
| 293 | 'short-help' : 'Configure policy priority', |
| 294 | 'doc' : 'policy|priority', |
| 295 | 'doc-example' : 'policy|policy-priority-example', |
| 296 | 'parent-field' : 'policy', |
| 297 | 'args' : { |
| 298 | 'action' : ( |
| 299 | { |
| 300 | 'proc' : 'create-policy', |
| 301 | }, |
| 302 | ), |
| 303 | 'field' : 'priority', |
| 304 | 'base-type' : 'integer', |
| 305 | 'range' : (0, 65535), |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | SHOW_POLICY_COMMAND_DESCRIPTION = { |
| 310 | 'name' : 'show', |
| 311 | 'mode' : 'login', |
| 312 | 'command-type' : 'display-table', |
| 313 | 'all-help' : 'Show switch information', |
| 314 | 'short-help' : 'Show switch summary', |
| 315 | #'obj-type' : 'switches', |
| 316 | 'doc' : 'switch|show', |
| 317 | 'doc-example' : 'switch|show-example', |
| 318 | 'args' : ( |
| 319 | { |
| 320 | 'token' : 'policy', |
| 321 | 'field' : 'showpolicy', |
| 322 | 'action' : 'display-rest', |
| 323 | 'sort' : ['policyId',], |
| 324 | 'doc' : 'switch|show', |
| 325 | 'url' : [ |
| 326 | 'showpolicy', |
| 327 | ], |
| 328 | 'format' : 'show_policy', |
| 329 | }, |
| 330 | ) |
| 331 | } |
| 332 | |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 333 | def tunnel_id_completion(prefix, completions): |
| 334 | query_url = "http://127.0.0.1:8000/rest/v1/showtunnel" |
| 335 | result = command.sdnsh.store.rest_simple_request(query_url) |
| 336 | entries = json.loads(result) |
| 337 | for entry in entries: |
| 338 | if entry['tunnelId'].startswith(prefix): |
| 339 | completions[entry['tunnelId']+' '] = entry['tunnelId'] |
| 340 | return |
| 341 | |
| 342 | command.add_completion('tunnel-id-completion', tunnel_id_completion, |
| 343 | {'kwargs': { 'prefix' : '$text', |
| 344 | 'completions' : '$completions', |
| 345 | }}) |
| 346 | |
Srikanth Vavilapalli | b5c3ca5 | 2014-12-15 15:59:33 -0800 | [diff] [blame] | 347 | def tunnelset_id_completion(prefix, completions): |
| 348 | query_url = "http://127.0.0.1:8000/rest/v1/showtunnelset" |
| 349 | result = command.sdnsh.store.rest_simple_request(query_url) |
| 350 | entries = json.loads(result) |
| 351 | for entry in entries: |
| 352 | if entry['tunnelsetId'].startswith(prefix): |
| 353 | completions[entry['tunnelsetId']+' '] = entry['tunnelsetId'] |
| 354 | return |
| 355 | |
| 356 | command.add_completion('tunnelset-id-completion', tunnelset_id_completion, |
| 357 | {'kwargs': { 'prefix' : '$text', |
| 358 | 'completions' : '$completions', |
| 359 | }}) |
Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 360 | |
| 361 | def policy_id_completion(prefix, completions): |
| 362 | query_url = "http://127.0.0.1:8000/rest/v1/showpolicy" |
| 363 | result = command.sdnsh.store.rest_simple_request(query_url) |
| 364 | entries = json.loads(result) |
| 365 | for entry in entries: |
| 366 | if entry['policyId'].startswith(prefix): |
| 367 | completions[entry['policyId']+' '] = entry['policyId'] |
| 368 | return |
| 369 | |
| 370 | command.add_completion('policy-id-completion', policy_id_completion, |
| 371 | {'kwargs': { 'prefix' : '$text', |
| 372 | 'completions' : '$completions', |
Srikanth Vavilapalli | 40d79f8 | 2014-12-17 14:29:24 -0800 | [diff] [blame^] | 373 | }}) |