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