Srikanth Vavilapalli | 1725e49 | 2014-12-01 17:50:52 -0800 | [diff] [blame] | 1 | import command |
| 2 | import json |
| 3 | import fmtcnv |
| 4 | |
| 5 | |
| 6 | TUNNEL_SUBMODE_COMMAND_DESCRIPTION = { |
| 7 | 'name' : 'tunnel', |
| 8 | 'short-help' : 'Enter tunnel submode, configure tunnel details', |
| 9 | 'mode' : 'config', |
| 10 | 'parent-field' : None, |
| 11 | 'command-type' : 'config-submode', |
| 12 | 'obj-type' : 'tunnel-config', |
| 13 | 'submode-name' : 'config-tunnel', |
| 14 | 'doc' : 'tunnel|tunnel', |
| 15 | 'doc-example' : 'tunnel|tunnel-example', |
| 16 | 'args' : ( |
| 17 | { |
| 18 | 'field' : 'tunnel-id', |
| 19 | 'type' : 'identifier', |
| 20 | #'completion' : 'complete-object-field', |
| 21 | 'syntax-help' : 'Enter a tunnel name', |
| 22 | 'doc' : 'tunnel|tunnel', |
| 23 | 'doc-include' : [ 'type-doc' ], |
| 24 | 'completion' : 'tunnel-id-completion', |
| 25 | 'action' : ( |
| 26 | { |
| 27 | 'proc' : 'create-tunnel', |
| 28 | }, |
| 29 | { |
| 30 | 'proc' : 'push-mode-stack', |
| 31 | }, |
| 32 | ), |
| 33 | 'no-action': ( |
| 34 | { |
| 35 | 'proc' : 'remove-tunnel', |
| 36 | } |
| 37 | ), |
| 38 | } |
| 39 | ) |
| 40 | } |
| 41 | |
| 42 | TUNNEL_CONFIG_FORMAT = { |
| 43 | 'tunnel-config' : { |
| 44 | 'field-orderings' : { |
| 45 | 'default' : [ |
| 46 | 'tunnel-id', |
| 47 | ], |
| 48 | }, |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | |
| 53 | def tunnel_node_label_completion(prefix, completions): |
| 54 | #print "tunnel_node_label_completion:",prefix,completions |
| 55 | query_url = "http://127.0.0.1:8000/rest/v1/switches" |
| 56 | result = command.sdnsh.store.rest_simple_request(query_url) |
| 57 | entries = json.loads(result) |
| 58 | for entry in entries: |
| 59 | if entry['stringAttributes']['nodeSid'].startswith(prefix): |
| 60 | completions[entry['stringAttributes']['nodeSid']+' '] = entry['stringAttributes']['nodeSid'] |
| 61 | return |
| 62 | |
| 63 | command.add_completion('tunnel-node-label-completion', tunnel_node_label_completion, |
| 64 | {'kwargs': { 'prefix' : '$text', |
| 65 | 'completions' : '$completions', |
| 66 | }}) |
| 67 | |
| 68 | def tunnel_adjacency_label_completion(prefix, data, completions): |
| 69 | #print "tunnel_adjacency_label_completion:",prefix,data,completions |
| 70 | query_url1 = "http://127.0.0.1:8000/rest/v1/switches" |
| 71 | result1 = command.sdnsh.store.rest_simple_request(query_url1) |
| 72 | entries1 = json.loads(result1) |
| 73 | node_dpid = None |
| 74 | for entry in entries1: |
| 75 | if (int (entry['stringAttributes']['nodeSid']) == int(data['node-label'])): |
| 76 | node_dpid = entry['dpid'] |
| 77 | if (node_dpid != None): |
| 78 | query_url2 = "http://127.0.0.1:8000/rest/v1/router/"+node_dpid+"/adjacency" |
| 79 | result2 = command.sdnsh.store.rest_simple_request(query_url2) |
| 80 | entries2 = json.loads(result2) |
| 81 | for entry in entries2: |
| 82 | if str(entry.get("adjacencySid")).startswith(prefix): |
| 83 | completions[str(entry.get("adjacencySid"))+' '] = entry.get("adjacencySid") |
| 84 | return |
| 85 | |
| 86 | command.add_completion('tunnel-adjacency-label-completion', tunnel_adjacency_label_completion, |
| 87 | {'kwargs': { 'prefix' : '$text', |
| 88 | 'data' : '$data', |
| 89 | 'completions' : '$completions', |
| 90 | }}) |
| 91 | |
| 92 | TUNNEL_ADJACENCY_INFO = ( |
| 93 | { |
| 94 | 'token' : 'adjacency', |
| 95 | 'short-help' : 'Set adjacency label on this node', |
| 96 | 'doc' : 'tunnel|adjacency', |
| 97 | 'doc-example' : 'tunnel|adjacency', |
| 98 | }, |
| 99 | { |
| 100 | 'field' : 'adjacency-label', |
| 101 | 'type' : 'label', |
| 102 | 'completion' : 'tunnel-adjacency-label-completion', |
| 103 | 'help-name' : 'Adjacency label', |
| 104 | 'data' : { |
| 105 | 'node_label' : '$node-label', |
| 106 | }, |
| 107 | 'action' : ( |
| 108 | { |
| 109 | 'proc' : 'create-tunnel', |
| 110 | }, |
| 111 | ), |
| 112 | } |
| 113 | ) |
| 114 | |
| 115 | # obj_type flow-entry field hard-timeout |
| 116 | TUNNEL_NODE_ENTRY_COMMAND_DESCRIPTION = { |
| 117 | 'name' : 'node', |
| 118 | 'mode' : 'config-tunnel', |
| 119 | 'short-help' : 'Set node for this tunnel', |
| 120 | 'doc' : 'tunnel|node', |
| 121 | 'doc-example' : 'tunnel|node', |
| 122 | 'parent-field' : 'tunnel', |
| 123 | 'command-type' : 'config', |
| 124 | 'args' : ( |
| 125 | { |
| 126 | 'field' : 'node-label', |
| 127 | 'completion' : 'tunnel-node-label-completion', |
| 128 | 'type' : 'label', |
| 129 | 'other' : 'switches|label', |
| 130 | # 'data-handler' : 'alias-to-value', |
| 131 | 'help-name' : 'Segment label', |
| 132 | 'action' : ( |
| 133 | { |
| 134 | 'proc' : 'create-tunnel', |
| 135 | }, |
| 136 | ), |
| 137 | }, |
| 138 | { |
| 139 | 'optional' : True, |
| 140 | 'optional-for-no' : True, |
| 141 | 'args' : TUNNEL_ADJACENCY_INFO, |
| 142 | }, |
| 143 | ) |
| 144 | } |
| 145 | |
| 146 | SWITCH_TUNNEL_COMMAND_DESCRIPTION = { |
| 147 | 'name' : 'show', |
| 148 | 'mode' : 'login', |
| 149 | 'command-type' : 'display-table', |
| 150 | 'all-help' : 'Show switch information', |
| 151 | 'short-help' : 'Show switch summary', |
| 152 | #'obj-type' : 'switches', |
| 153 | 'doc' : 'switch|show', |
| 154 | 'doc-example' : 'switch|show-example', |
| 155 | 'args' : ( |
| 156 | { |
| 157 | 'token' : 'tunnel', |
| 158 | 'field' : 'showtunnel', |
| 159 | 'sort' : ['tunnelId',], |
| 160 | 'action' : 'display-rest', |
| 161 | 'doc' : 'switch|show', |
| 162 | 'url' : [ |
| 163 | 'showtunnel', |
| 164 | ], |
| 165 | 'format' : 'show_tunnel', |
| 166 | }, |
| 167 | { |
| 168 | 'optional' : True, |
| 169 | 'choices' : ( |
| 170 | { |
| 171 | 'field' : 'showtunnel', |
| 172 | 'type' : 'enum', |
| 173 | 'values' : ('details',), |
| 174 | 'optional' : True, |
| 175 | 'format' : 'show_tunnel', |
| 176 | 'data' : { 'detail' : 'details' }, |
| 177 | }, |
| 178 | ), |
| 179 | } |
| 180 | ) |
| 181 | } |
| 182 | |
| 183 | |
| 184 | def tunnel_id_completion(prefix, completions): |
| 185 | query_url = "http://127.0.0.1:8000/rest/v1/showtunnel" |
| 186 | result = command.sdnsh.store.rest_simple_request(query_url) |
| 187 | entries = json.loads(result) |
| 188 | for entry in entries: |
| 189 | if entry['tunnelId'].startswith(prefix): |
| 190 | completions[entry['tunnelId']+' '] = entry['tunnelId'] |
| 191 | return |
| 192 | |
| 193 | command.add_completion('tunnel-id-completion', tunnel_id_completion, |
| 194 | {'kwargs': { 'prefix' : '$text', |
| 195 | 'completions' : '$completions', |
| 196 | }}) |