Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2016 the original author or authors. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 17 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 18 | import readline |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 19 | from optparse import make_option |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 20 | from time import sleep, time |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 21 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 22 | import grpc |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 23 | import requests |
| 24 | from cmd2 import Cmd, options |
| 25 | from google.protobuf.empty_pb2 import Empty |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 26 | from simplejson import dumps |
| 27 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 28 | from cli.device import DeviceCli |
| 29 | from cli.logical_device import LogicalDeviceCli |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 30 | from cli.table import TablePrinter, print_pb_table |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 31 | from voltha.core.flow_decomposer import * |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 32 | from voltha.protos import third_party |
| 33 | from voltha.protos import voltha_pb2 |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 34 | from voltha.protos.openflow_13_pb2 import FlowTableUpdate |
| 35 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 36 | _ = third_party |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 37 | from cli.utils import pb2dict, dict2line |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 38 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 39 | banner = """\ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 40 | _ _ _ _ _ |
| 41 | __ _____| | |_| |_ __ _ __| (_) |
| 42 | \ V / _ \ | _| ' \/ _` | / _| | | |
| 43 | \_/\___/_|\__|_||_\__,_| \__|_|_| |
| 44 | (to exit type quit or hit Ctrl-D) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 45 | """ |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 46 | |
| 47 | class VolthaCli(Cmd): |
| 48 | |
| 49 | prompt = 'voltha' |
| 50 | history_file_name = '.voltha_cli_history' |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 51 | |
| 52 | # Settable CLI parameters |
| 53 | voltha_grpc = 'localhost:50055' |
| 54 | voltha_sim_rest = 'localhost:18880' |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 55 | max_history_lines = 500 |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 56 | default_device_id = None |
| 57 | default_logical_device_id = None |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 58 | |
| 59 | Cmd.settable.update(dict( |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 60 | voltha_grpc='Voltha GRPC endpoint in form of <host>:<port>', |
| 61 | voltha_sim_rest='Voltha simulation back door for testing in form ' |
| 62 | 'of <host>:<port>', |
| 63 | max_history_lines='Maximum number of history lines stored across ' |
| 64 | 'sessions', |
| 65 | default_device_id='Device id used when no device id is specified', |
| 66 | default_logical_device_id='Logical device id used when no device id ' |
| 67 | 'is specified', |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 68 | )) |
| 69 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 70 | # cleanup of superflous commands from cmd2 |
| 71 | del Cmd.do_cmdenvironment |
| 72 | # del Cmd.do_eof |
| 73 | del Cmd.do_exit |
| 74 | del Cmd.do_q |
| 75 | del Cmd.do_hi |
| 76 | del Cmd.do_l |
| 77 | del Cmd.do_li |
| 78 | del Cmd.do_r |
| 79 | del Cmd.do__load |
| 80 | del Cmd.do__relative_load |
| 81 | Cmd.do_edit = Cmd.do_ed |
| 82 | |
| 83 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 84 | def __init__(self, *args, **kw): |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 85 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 86 | Cmd.__init__(self, *args, **kw) |
| 87 | self.prompt = '(' + self.colorize( |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 88 | self.colorize(self.prompt, 'blue'), 'bold') + ') ' |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 89 | self.channel = None |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 90 | self.device_ids_cache = None |
| 91 | self.device_ids_cache_ts = time() |
| 92 | self.logical_device_ids_cache = None |
| 93 | self.logical_device_ids_cache_ts = time() |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 94 | |
| 95 | def load_history(self): |
| 96 | """Load saved command history from local history file""" |
| 97 | try: |
| 98 | with file(self.history_file_name, 'r') as f: |
| 99 | for line in f.readlines(): |
| 100 | stripped_line = line.strip() |
| 101 | self.history.append(stripped_line) |
| 102 | readline.add_history(stripped_line) |
| 103 | except IOError: |
| 104 | pass # ignore if file cannot be read |
| 105 | |
| 106 | def save_history(self): |
| 107 | try: |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 108 | with open(self.history_file_name, 'w') as f: |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 109 | f.write('\n'.join(self.history[-self.max_history_lines:])) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 110 | except IOError as e: |
| 111 | self.perror('Could not save history in {}: {}'.format( |
| 112 | self.history_file_name, e)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 113 | else: |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 114 | self.perror('History saved as {}'.format( |
| 115 | self.history_file_name)) |
| 116 | |
| 117 | def perror(self, errmsg, statement=None): |
| 118 | # Touch it up to make sure error is prefixed and colored |
| 119 | Cmd.perror(self, self.colorize('***ERROR: ', 'red') + errmsg, |
| 120 | statement) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 121 | |
| 122 | def get_channel(self): |
| 123 | if self.channel is None: |
| 124 | self.channel = grpc.insecure_channel(self.voltha_grpc) |
| 125 | return self.channel |
| 126 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 127 | # ~~~~~~~~~~~~~~~~~ ACTUAL COMMAND IMPLEMENTATIONS ~~~~~~~~~~~~~~~~~~~~~~~~ |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 128 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 129 | def do_reset_history(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 130 | """Reset CLI history""" |
| 131 | while self.history: |
| 132 | self.history.pop() |
| 133 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 134 | def do_launch(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 135 | """If Voltha is not running yet, launch it""" |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 136 | raise NotImplementedError('not implemented yet') |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 137 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 138 | def do_restart(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 139 | """Launch Voltha, but if it is already running, terminate it first""" |
| 140 | pass |
| 141 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 142 | def do_adapters(self, line): |
| 143 | """List loaded adapter""" |
| 144 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 145 | res = stub.ListAdapters(Empty()) |
| 146 | omit_fields = {} |
| 147 | print_pb_table('Adapters:', res.items, omit_fields, self.poutput) |
| 148 | |
| 149 | def get_devices(self): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 150 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 151 | res = stub.ListDevices(Empty()) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 152 | return res.items |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 153 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 154 | def get_logical_devices(self): |
| 155 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 156 | res = stub.ListLogicalDevices(Empty()) |
| 157 | return res.items |
| 158 | |
| 159 | def do_devices(self, line): |
| 160 | """List devices registered in Voltha""" |
| 161 | devices = self.get_devices() |
| 162 | omit_fields = { |
| 163 | 'adapter', |
| 164 | 'vendor', |
| 165 | 'model', |
| 166 | 'hardware_version', |
| 167 | 'software_version', |
| 168 | 'firmware_version', |
| 169 | 'serial_number' |
| 170 | } |
| 171 | print_pb_table('Devices:', devices, omit_fields, self.poutput) |
| 172 | |
| 173 | def do_logical_devices(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 174 | """List logical devices in Voltha""" |
| 175 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 176 | res = stub.ListLogicalDevices(Empty()) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 177 | omit_fields = { |
| 178 | 'desc.mfr_desc', |
| 179 | 'desc.hw_desc', |
| 180 | 'desc.sw_desc', |
| 181 | 'desc.dp_desc', |
| 182 | 'desc.serial_number', |
| 183 | 'switch_features.capabilities' |
| 184 | } |
| 185 | print_pb_table('Logical devices:', res.items, omit_fields, |
| 186 | self.poutput) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 187 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 188 | def do_device(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 189 | """Enter device level command mode""" |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 190 | device_id = line.strip() or self.default_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 191 | if not device_id: |
| 192 | raise Exception('<device-id> parameter needed') |
| 193 | sub = DeviceCli(self.get_channel, device_id) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 194 | sub.cmdloop() |
| 195 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 196 | def do_logical_device(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 197 | """Enter logical device level command mode""" |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 198 | logical_device_id = line.strip() or self.default_logical_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 199 | if not logical_device_id: |
| 200 | raise Exception('<logical-device-id> parameter needed') |
| 201 | sub = LogicalDeviceCli(self.get_channel, logical_device_id) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 202 | sub.cmdloop() |
| 203 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 204 | def device_ids(self, force_refresh=False): |
| 205 | if force_refresh or self.device_ids is None or \ |
| 206 | (time() - self.device_ids_cache_ts) > 1: |
| 207 | self.device_ids_cache = [d.id for d in self.get_devices()] |
| 208 | self.device_ids_cache_ts = time() |
| 209 | return self.device_ids_cache |
| 210 | |
| 211 | def logical_device_ids(self, force_refresh=False): |
| 212 | if force_refresh or self.logical_device_ids is None or \ |
| 213 | (time() - self.logical_device_ids_cache_ts) > 1: |
| 214 | self.logical_device_ids_cache = [d.id for d |
| 215 | in self.get_logical_devices()] |
| 216 | self.logical_device_ids_cache_ts = time() |
| 217 | return self.logical_device_ids_cache |
| 218 | |
| 219 | def complete_device(self, text, line, begidx, endidx): |
| 220 | if not text: |
| 221 | completions = self.device_ids()[:] |
| 222 | else: |
| 223 | completions = [d for d in self.device_ids() if d.startswith(text)] |
| 224 | return completions |
| 225 | |
| 226 | def complete_logical_device(self, text, line, begidx, endidx): |
| 227 | if not text: |
| 228 | completions = self.logical_device_ids()[:] |
| 229 | else: |
| 230 | completions = [d for d in self.logical_device_ids() |
| 231 | if d.startswith(text)] |
| 232 | return completions |
| 233 | |
| 234 | def do_pdb(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 235 | """Launch PDB debug prompt in CLI (for CLI development)""" |
| 236 | from pdb import set_trace |
| 237 | set_trace() |
| 238 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 239 | def do_health(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 240 | """Show connectivity status to Voltha status""" |
| 241 | stub = voltha_pb2.HealthServiceStub(self.get_channel()) |
| 242 | res = stub.GetHealthStatus(Empty()) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 243 | self.poutput(dumps(pb2dict(res), indent=4)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 244 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 245 | def do_test(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 246 | """Enter test mode, which makes a bunch on new commands available""" |
| 247 | sub = TestCli(self.history, self.get_channel) |
| 248 | sub.cmdloop() |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 249 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 250 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 251 | class TestCli(VolthaCli): |
| 252 | |
| 253 | def __init__(self, history, get_channel): |
| 254 | VolthaCli.__init__(self) |
| 255 | self.history = history |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 256 | self.get_channel = get_channel |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 257 | self.prompt = '(' + self.colorize(self.colorize('test', 'cyan'), |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 258 | 'bold') + ') ' |
| 259 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 260 | @options([ |
| 261 | make_option('-t', '--device-type', action="store", dest='device_type', |
| 262 | help="Device type", default='simulated_olt'), |
| 263 | make_option('-m', '--mac-address', action='store', dest='mac_address', |
| 264 | default='00:0c:e2:31:40:00'), |
| 265 | make_option('-i', '--ip-address', action='store', dest='ip_address'), |
| 266 | ]) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 267 | def do_preprovision_olt(self, line, opts): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 268 | """Preprovision a new OLT with given device type""" |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 269 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 270 | kw = dict(type=opts.device_type) |
| 271 | if opts.ip_address: |
| 272 | kw['ipv4_address'] = opts.ip_address |
| 273 | elif opts.mac_address: |
| 274 | kw['mac_address'] = opts.mac_address |
| 275 | else: |
| 276 | raise Exception('Either IP address or Mac Address is needed') |
| 277 | device = voltha_pb2.Device(**kw) |
| 278 | device = stub.CreateDevice(device) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 279 | self.poutput('success (device id = {})'.format(device.id)) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 280 | self.default_device_id = device.id |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 281 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 282 | def do_activate_olt(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 283 | """ |
| 284 | Activate an OLT. If the <id> is not provided, it will be on the last |
| 285 | pre-provisioned OLT. |
| 286 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 287 | device_id = line or self.default_device_id |
| 288 | self.poutput('activating {}'.format(device_id)) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 289 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 290 | stub.ActivateDevice(voltha_pb2.ID(id=device_id)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 291 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 292 | # try to acquire logical device id |
| 293 | while True: |
| 294 | device = stub.GetDevice(voltha_pb2.ID(id=device_id)) |
| 295 | if device.oper_status == voltha_pb2.OperStatus.ACTIVE: |
| 296 | assert device.parent_id |
| 297 | self.default_logical_device_id = device.parent_id |
| 298 | break |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 299 | self.poutput('waiting for device to be activated...') |
| 300 | sleep(.5) |
| 301 | self.poutput('success (logical device id = {})'.format( |
| 302 | self.default_logical_device_id)) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 303 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 304 | complete_activate_olt = VolthaCli.complete_device |
| 305 | |
| 306 | def do_arrive_onus(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 307 | """ |
| 308 | Simulate the arrival of ONUs |
| 309 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 310 | device_id = line or self.default_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 311 | requests.get('http://{}/devices/{}/detect_onus'.format( |
| 312 | self.voltha_sim_rest, device_id |
| 313 | )) |
| 314 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 315 | complete_arrive_onus = VolthaCli.complete_device |
| 316 | |
| 317 | def do_install_eapol_flow(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 318 | """ |
| 319 | Install an EAPOL flow on the given logical device. If device is not |
| 320 | given, it will be applied to logical device of the last pre-provisioned |
| 321 | OLT device. |
| 322 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 323 | logical_device_id = line or self.default_logical_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 324 | update = FlowTableUpdate( |
| 325 | id=logical_device_id, |
| 326 | flow_mod = mk_simple_flow_mod( |
| 327 | priority=2000, |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 328 | match_fields=[in_port(241), eth_type(0x888e)], |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 329 | actions=[ |
| 330 | push_vlan(0x8100), |
| 331 | set_field(vlan_vid(4096 + 4000)), |
| 332 | output(ofp.OFPP_CONTROLLER) |
| 333 | ] |
| 334 | ) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 335 | ) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 336 | stub = voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 337 | res = stub.UpdateLogicalDeviceFlowTable(update) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 338 | self.poutput('success ({})'.format(res)) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 339 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 340 | complete_install_eapol_flow = VolthaCli.complete_logical_device |
| 341 | |
| 342 | def do_send_simulated_upstream_eapol(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 343 | """ |
| 344 | Send an EAPOL upstream from a simulated OLT |
| 345 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 346 | device_id = line or self.default_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 347 | requests.get('http://{}/devices/{}/test_eapol_in'.format( |
| 348 | self.voltha_sim_rest, device_id |
| 349 | )) |
| 350 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 351 | complete_send_simulated_upstream_eapol = VolthaCli.complete_device |
| 352 | |
| 353 | def do_inject_eapol_start(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 354 | """ |
| 355 | Send out an an EAPOL start message into the given Unix interface |
| 356 | """ |
| 357 | pass |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 358 | |
| 359 | |
| 360 | if __name__ == '__main__': |
| 361 | c = VolthaCli() |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame^] | 362 | c.poutput(banner) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 363 | c.load_history() |
| 364 | c.cmdloop() |
| 365 | c.save_history() |