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 | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 17 | import argparse |
| 18 | import os |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 19 | import readline |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 20 | import sys |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 21 | from optparse import make_option |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 22 | from time import sleep, time |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 23 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 24 | import grpc |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 25 | import requests |
| 26 | from cmd2 import Cmd, options |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 27 | from consul import Consul |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 28 | from google.protobuf.empty_pb2 import Empty |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 29 | from simplejson import dumps |
| 30 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 31 | from cli.device import DeviceCli |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 32 | from cli.xpon import XponCli |
Chip Boling | 69abce8 | 2018-06-18 09:56:23 -0500 | [diff] [blame] | 33 | from cli.omci import OmciCli |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 34 | from cli.alarm_filters import AlarmFiltersCli |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 35 | from cli.logical_device import LogicalDeviceCli |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 36 | from cli.table import print_pb_list_as_table |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 37 | from voltha.core.flow_decomposer import * |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 38 | from voltha.protos import third_party |
| 39 | from voltha.protos import voltha_pb2 |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 40 | from voltha.protos.openflow_13_pb2 import FlowTableUpdate, FlowGroupTableUpdate |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 41 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 42 | _ = third_party |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 43 | from cli.utils import pb2dict |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 44 | |
| 45 | defs = dict( |
| 46 | # config=os.environ.get('CONFIG', './cli.yml'), |
| 47 | consul=os.environ.get('CONSUL', 'localhost:8500'), |
| 48 | voltha_grpc_endpoint=os.environ.get('VOLTHA_GRPC_ENDPOINT', |
| 49 | 'localhost:50055'), |
| 50 | voltha_sim_rest_endpoint=os.environ.get('VOLTHA_SIM_REST_ENDPOINT', |
| 51 | 'localhost:18880'), |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 52 | global_request=os.environ.get('GLOBAL_REQUEST', False) |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 53 | ) |
| 54 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 55 | banner = """\ |
Zsolt Haraszti | 313c4be | 2016-12-27 11:06:53 -0800 | [diff] [blame] | 56 | _ _ _ ___ _ ___ |
| 57 | __ _____| | |_| |_ __ _ / __| | |_ _| |
| 58 | \ V / _ \ | _| ' \/ _` | | (__| |__ | | |
| 59 | \_/\___/_|\__|_||_\__,_| \___|____|___| |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 60 | (to exit type quit or hit Ctrl-D) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 61 | """ |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 62 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 63 | |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 64 | class VolthaCli(Cmd): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 65 | prompt = 'voltha' |
| 66 | history_file_name = '.voltha_cli_history' |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 67 | |
| 68 | # Settable CLI parameters |
| 69 | voltha_grpc = 'localhost:50055' |
| 70 | voltha_sim_rest = 'localhost:18880' |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 71 | global_request = False |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 72 | max_history_lines = 500 |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 73 | default_device_id = None |
| 74 | default_logical_device_id = None |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 75 | |
| 76 | Cmd.settable.update(dict( |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 77 | voltha_grpc='Voltha GRPC endpoint in form of <host>:<port>', |
| 78 | voltha_sim_rest='Voltha simulation back door for testing in form ' |
| 79 | 'of <host>:<port>', |
| 80 | max_history_lines='Maximum number of history lines stored across ' |
| 81 | 'sessions', |
| 82 | default_device_id='Device id used when no device id is specified', |
| 83 | default_logical_device_id='Logical device id used when no device id ' |
| 84 | 'is specified', |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 85 | )) |
| 86 | |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 87 | # cleanup of superfluous commands from cmd2 |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 88 | del Cmd.do_cmdenvironment |
Steve Crooks | 05f2452 | 2017-02-27 13:32:27 -0500 | [diff] [blame] | 89 | del Cmd.do_load |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 90 | del Cmd.do__relative_load |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 91 | |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 92 | def __init__(self, voltha_grpc, voltha_sim_rest, global_request=False): |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 93 | VolthaCli.voltha_grpc = voltha_grpc |
| 94 | VolthaCli.voltha_sim_rest = voltha_sim_rest |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 95 | VolthaCli.global_request = global_request |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 96 | Cmd.__init__(self) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 97 | self.prompt = '(' + self.colorize( |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 98 | self.colorize(self.prompt, 'blue'), 'bold') + ') ' |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 99 | self.channel = None |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 100 | self.stub = None |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 101 | self.device_ids_cache = None |
| 102 | self.device_ids_cache_ts = time() |
| 103 | self.logical_device_ids_cache = None |
| 104 | self.logical_device_ids_cache_ts = time() |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 105 | |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 106 | # we override cmd2's method to avoid its optparse conflicting with our |
| 107 | # command line parsing |
| 108 | def cmdloop(self): |
| 109 | self._cmdloop() |
| 110 | |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 111 | def load_history(self): |
| 112 | """Load saved command history from local history file""" |
| 113 | try: |
| 114 | with file(self.history_file_name, 'r') as f: |
| 115 | for line in f.readlines(): |
| 116 | stripped_line = line.strip() |
| 117 | self.history.append(stripped_line) |
| 118 | readline.add_history(stripped_line) |
| 119 | except IOError: |
| 120 | pass # ignore if file cannot be read |
| 121 | |
| 122 | def save_history(self): |
| 123 | try: |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 124 | with open(self.history_file_name, 'w') as f: |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 125 | f.write('\n'.join(self.history[-self.max_history_lines:])) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 126 | except IOError as e: |
| 127 | self.perror('Could not save history in {}: {}'.format( |
| 128 | self.history_file_name, e)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 129 | else: |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 130 | self.poutput('History saved as {}'.format( |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 131 | self.history_file_name)) |
| 132 | |
| 133 | def perror(self, errmsg, statement=None): |
| 134 | # Touch it up to make sure error is prefixed and colored |
| 135 | Cmd.perror(self, self.colorize('***ERROR: ', 'red') + errmsg, |
| 136 | statement) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 137 | |
| 138 | def get_channel(self): |
| 139 | if self.channel is None: |
| 140 | self.channel = grpc.insecure_channel(self.voltha_grpc) |
| 141 | return self.channel |
| 142 | |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 143 | def get_stub(self): |
| 144 | if self.stub is None: |
| 145 | self.stub = \ |
| 146 | voltha_pb2.VolthaGlobalServiceStub(self.get_channel()) \ |
| 147 | if self.global_request else \ |
| 148 | voltha_pb2.VolthaLocalServiceStub(self.get_channel()) |
| 149 | return self.stub |
| 150 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 151 | # ~~~~~~~~~~~~~~~~~ ACTUAL COMMAND IMPLEMENTATIONS ~~~~~~~~~~~~~~~~~~~~~~~~ |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 152 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 153 | def do_reset_history(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 154 | """Reset CLI history""" |
| 155 | while self.history: |
| 156 | self.history.pop() |
| 157 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 158 | def do_launch(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 159 | """If Voltha is not running yet, launch it""" |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 160 | raise NotImplementedError('not implemented yet') |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 161 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 162 | def do_restart(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 163 | """Launch Voltha, but if it is already running, terminate it first""" |
| 164 | pass |
| 165 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 166 | def do_adapters(self, line): |
| 167 | """List loaded adapter""" |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 168 | stub = self.get_stub() |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 169 | res = stub.ListAdapters(Empty()) |
Sergio Slobodrian | 6e9fb69 | 2017-03-17 14:46:33 -0400 | [diff] [blame] | 170 | omit_fields = {'config.log_level', 'logical_device_ids'} |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 171 | print_pb_list_as_table('Adapters:', res.items, omit_fields, self.poutput) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 172 | |
| 173 | def get_devices(self): |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 174 | stub = self.get_stub() |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 175 | res = stub.ListDevices(Empty()) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 176 | return res.items |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 177 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 178 | def get_logical_devices(self): |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 179 | stub = self.get_stub() |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 180 | res = stub.ListLogicalDevices(Empty()) |
| 181 | return res.items |
| 182 | |
| 183 | def do_devices(self, line): |
| 184 | """List devices registered in Voltha""" |
| 185 | devices = self.get_devices() |
| 186 | omit_fields = { |
| 187 | 'adapter', |
| 188 | 'vendor', |
| 189 | 'model', |
| 190 | 'hardware_version', |
ggowdru | 236bd95 | 2017-06-20 20:32:55 -0700 | [diff] [blame] | 191 | 'images', |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 192 | 'firmware_version', |
Nicolas Palpacuer | b83853e | 2018-06-28 16:11:30 -0400 | [diff] [blame] | 193 | 'vendor_id' |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 194 | } |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 195 | print_pb_list_as_table('Devices:', devices, omit_fields, self.poutput) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 196 | |
| 197 | def do_logical_devices(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 198 | """List logical devices in Voltha""" |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 199 | stub = self.get_stub() |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 200 | res = stub.ListLogicalDevices(Empty()) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 201 | omit_fields = { |
| 202 | 'desc.mfr_desc', |
| 203 | 'desc.hw_desc', |
| 204 | 'desc.sw_desc', |
| 205 | 'desc.dp_desc', |
| 206 | 'desc.serial_number', |
Sergio Slobodrian | a95f99b | 2017-03-21 10:22:47 -0400 | [diff] [blame] | 207 | 'switch_features.capabilities' |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 208 | } |
Jonathan Hart | 7d4a23d | 2018-06-28 07:36:28 -0700 | [diff] [blame] | 209 | presfns = { |
| 210 | 'datapath_id': lambda x: "{0:0{1}x}".format(int(x), 16) |
| 211 | } |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 212 | print_pb_list_as_table('Logical devices:', res.items, omit_fields, |
Jonathan Hart | 7d4a23d | 2018-06-28 07:36:28 -0700 | [diff] [blame] | 213 | self.poutput, presfns=presfns) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 214 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 215 | def do_device(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 216 | """Enter device level command mode""" |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 217 | device_id = line.strip() or self.default_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 218 | if not device_id: |
| 219 | raise Exception('<device-id> parameter needed') |
Venkata Telu | 133b27d | 2018-06-12 14:22:28 -0500 | [diff] [blame] | 220 | if device_id not in self.device_ids(): |
Venkata Telu | 35cc472 | 2018-06-01 12:05:30 -0500 | [diff] [blame] | 221 | self.poutput( self.colorize('Error: ', 'red') + |
| 222 | 'There is no such device') |
| 223 | raise Exception('<device-id> is not a valid one') |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 224 | sub = DeviceCli(device_id, self.get_stub) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 225 | sub.cmdloop() |
| 226 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 227 | def do_logical_device(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 228 | """Enter logical device level command mode""" |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 229 | logical_device_id = line.strip() or self.default_logical_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 230 | if not logical_device_id: |
| 231 | raise Exception('<logical-device-id> parameter needed') |
Venkata Telu | 133b27d | 2018-06-12 14:22:28 -0500 | [diff] [blame] | 232 | if logical_device_id not in self.logical_device_ids(): |
Venkata Telu | 35cc472 | 2018-06-01 12:05:30 -0500 | [diff] [blame] | 233 | self.poutput( self.colorize('Error: ', 'red') + |
| 234 | 'There is no such device') |
| 235 | raise Exception('<logical-device-id> is not a valid one') |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 236 | sub = LogicalDeviceCli(logical_device_id, self.get_stub) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 237 | sub.cmdloop() |
| 238 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 239 | def device_ids(self, force_refresh=False): |
| 240 | if force_refresh or self.device_ids is None or \ |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 241 | (time() - self.device_ids_cache_ts) > 1: |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 242 | self.device_ids_cache = [d.id for d in self.get_devices()] |
| 243 | self.device_ids_cache_ts = time() |
| 244 | return self.device_ids_cache |
| 245 | |
| 246 | def logical_device_ids(self, force_refresh=False): |
| 247 | if force_refresh or self.logical_device_ids is None or \ |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 248 | (time() - self.logical_device_ids_cache_ts) > 1: |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 249 | self.logical_device_ids_cache = [d.id for d |
| 250 | in self.get_logical_devices()] |
| 251 | self.logical_device_ids_cache_ts = time() |
| 252 | return self.logical_device_ids_cache |
| 253 | |
| 254 | def complete_device(self, text, line, begidx, endidx): |
| 255 | if not text: |
| 256 | completions = self.device_ids()[:] |
| 257 | else: |
| 258 | completions = [d for d in self.device_ids() if d.startswith(text)] |
| 259 | return completions |
| 260 | |
| 261 | def complete_logical_device(self, text, line, begidx, endidx): |
| 262 | if not text: |
| 263 | completions = self.logical_device_ids()[:] |
| 264 | else: |
| 265 | completions = [d for d in self.logical_device_ids() |
| 266 | if d.startswith(text)] |
| 267 | return completions |
| 268 | |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 269 | def do_xpon(self, line): |
| 270 | """xpon <optional> [device_ID] - Enter xpon level command mode""" |
| 271 | device_id = line.strip() |
Nikolay Titov | 3f0c9dd | 2017-07-17 17:37:25 -0400 | [diff] [blame] | 272 | if device_id: |
| 273 | stub = self.get_stub() |
| 274 | try: |
| 275 | res = stub.GetDevice(voltha_pb2.ID(id=device_id)) |
| 276 | except Exception: |
Nikolay Titov | 176f1db | 2017-08-10 12:38:43 -0400 | [diff] [blame] | 277 | self.poutput( |
| 278 | self.colorize('Error: ', 'red') + 'No device id ' + |
| 279 | self.colorize(device_id, 'blue') + ' is found') |
Nikolay Titov | 3f0c9dd | 2017-07-17 17:37:25 -0400 | [diff] [blame] | 280 | return |
| 281 | sub = XponCli(self.get_channel, device_id) |
Nikolay Titov | 89004ec | 2017-06-19 18:22:42 -0400 | [diff] [blame] | 282 | sub.cmdloop() |
| 283 | |
Chip Boling | 69abce8 | 2018-06-18 09:56:23 -0500 | [diff] [blame] | 284 | def do_omci(self, line): |
| 285 | """omci <device_ID> - Enter OMCI level command mode""" |
| 286 | |
| 287 | device_id = line.strip() or self.default_device_id |
| 288 | if not device_id: |
| 289 | raise Exception('<device-id> parameter needed') |
| 290 | sub = OmciCli(device_id, self.get_stub) |
| 291 | sub.cmdloop() |
| 292 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 293 | def do_pdb(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 294 | """Launch PDB debug prompt in CLI (for CLI development)""" |
| 295 | from pdb import set_trace |
| 296 | set_trace() |
| 297 | |
Jonathan Hart | da93ac6 | 2018-05-01 11:25:29 -0700 | [diff] [blame] | 298 | def do_version(self, line): |
| 299 | """Show the VOLTHA core version""" |
| 300 | stub = self.get_stub() |
| 301 | voltha = stub.GetVoltha(Empty()) |
| 302 | self.poutput('{}'.format(voltha.version)) |
| 303 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 304 | def do_health(self, line): |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 305 | """Show connectivity status to Voltha status""" |
| 306 | stub = voltha_pb2.HealthServiceStub(self.get_channel()) |
| 307 | res = stub.GetHealthStatus(Empty()) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 308 | self.poutput(dumps(pb2dict(res), indent=4)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 309 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 310 | @options([ |
| 311 | make_option('-t', '--device-type', action="store", dest='device_type', |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 312 | help="Device type", default='simulated_olt'), |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 313 | make_option('-m', '--mac-address', action='store', dest='mac_address', |
| 314 | default='00:0c:e2:31:40:00'), |
| 315 | make_option('-i', '--ip-address', action='store', dest='ip_address'), |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 316 | make_option('-H', '--host_and_port', action='store', |
| 317 | dest='host_and_port'), |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 318 | ]) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 319 | def do_preprovision_olt(self, line, opts): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 320 | """Preprovision a new OLT with given device type""" |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 321 | stub = self.get_stub() |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 322 | kw = dict(type=opts.device_type) |
Zsolt Haraszti | 656ecc6 | 2016-12-28 15:08:23 -0800 | [diff] [blame] | 323 | if opts.host_and_port: |
| 324 | kw['host_and_port'] = opts.host_and_port |
| 325 | elif opts.ip_address: |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 326 | kw['ipv4_address'] = opts.ip_address |
| 327 | elif opts.mac_address: |
Venkata Telu | dc1a15b | 2018-07-06 14:31:05 -0500 | [diff] [blame] | 328 | kw['mac_address'] = opts.mac_address.lower() |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 329 | else: |
| 330 | raise Exception('Either IP address or Mac Address is needed') |
Chip Boling | 90b224d | 2017-06-02 11:51:48 -0500 | [diff] [blame] | 331 | # Pass any extra arguments past '--' to the device as custom arguments |
| 332 | kw['extra_args'] = line |
| 333 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 334 | device = voltha_pb2.Device(**kw) |
| 335 | device = stub.CreateDevice(device) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 336 | self.poutput('success (device id = {})'.format(device.id)) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 337 | self.default_device_id = device.id |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 338 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 339 | def do_enable(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 340 | """ |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 341 | Enable a device. If the <id> is not provided, it will be on the last |
| 342 | pre-provisioned device. |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 343 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 344 | device_id = line or self.default_device_id |
Venkata Telu | 0ccc430 | 2018-07-27 15:11:13 -0500 | [diff] [blame] | 345 | if device_id not in self.device_ids(): |
| 346 | self.poutput('Error: There is no such preprovisioned device') |
| 347 | return |
| 348 | |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 349 | try: |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 350 | stub = self.get_stub() |
Venkata Telu | 0ccc430 | 2018-07-27 15:11:13 -0500 | [diff] [blame] | 351 | device = stub.GetDevice(voltha_pb2.ID(id=device_id)) |
| 352 | if device.admin_state == voltha_pb2.AdminState.ENABLED: |
| 353 | self.poutput('Error: Device is already enabled') |
| 354 | return |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 355 | stub.EnableDevice(voltha_pb2.ID(id=device_id)) |
Venkata Telu | 0ccc430 | 2018-07-27 15:11:13 -0500 | [diff] [blame] | 356 | self.poutput('enabling {}'.format(device_id)) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 357 | |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 358 | while True: |
| 359 | device = stub.GetDevice(voltha_pb2.ID(id=device_id)) |
| 360 | # If this is an OLT then acquire logical device id |
| 361 | if device.oper_status == voltha_pb2.OperStatus.ACTIVE: |
| 362 | if device.type.endswith('_olt'): |
| 363 | assert device.parent_id |
| 364 | self.default_logical_device_id = device.parent_id |
| 365 | self.poutput('success (logical device id = {})'.format( |
| 366 | self.default_logical_device_id)) |
| 367 | else: |
| 368 | self.poutput('success (device id = {})'.format(device.id)) |
| 369 | break |
| 370 | self.poutput('waiting for device to be enabled...') |
| 371 | sleep(.5) |
Chip Boling | 69abce8 | 2018-06-18 09:56:23 -0500 | [diff] [blame] | 372 | except Exception as e: |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 373 | self.poutput('Error enabling {}. Error:{}'.format(device_id, e)) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 374 | |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 375 | complete_activate_olt = complete_device |
| 376 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 377 | def do_reboot(self, line): |
| 378 | """ |
| 379 | Rebooting a device. ID of the device needs to be provided |
| 380 | """ |
| 381 | device_id = line or self.default_device_id |
| 382 | self.poutput('rebooting {}'.format(device_id)) |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 383 | try: |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 384 | stub = self.get_stub() |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 385 | stub.RebootDevice(voltha_pb2.ID(id=device_id)) |
| 386 | self.poutput('rebooted {}'.format(device_id)) |
ggowdru | 64d738a | 2018-05-10 07:08:06 -0700 | [diff] [blame] | 387 | except Exception as e: |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 388 | self.poutput('Error rebooting {}. Error:{}'.format(device_id, e)) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 389 | |
sathishg | 5ae8622 | 2017-06-28 15:16:29 +0530 | [diff] [blame] | 390 | def do_self_test(self, line): |
| 391 | """ |
| 392 | Self Test a device. ID of the device needs to be provided |
| 393 | """ |
| 394 | device_id = line or self.default_device_id |
| 395 | self.poutput('Self Testing {}'.format(device_id)) |
| 396 | try: |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 397 | stub = self.get_stub() |
sathishg | 5ae8622 | 2017-06-28 15:16:29 +0530 | [diff] [blame] | 398 | res = stub.SelfTest(voltha_pb2.ID(id=device_id)) |
| 399 | self.poutput('Self Tested {}'.format(device_id)) |
| 400 | self.poutput(dumps(pb2dict(res), indent=4)) |
ggowdru | 64d738a | 2018-05-10 07:08:06 -0700 | [diff] [blame] | 401 | except Exception as e: |
sathishg | 5ae8622 | 2017-06-28 15:16:29 +0530 | [diff] [blame] | 402 | self.poutput('Error in self test {}. Error:{}'.format(device_id, e)) |
| 403 | |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 404 | def do_delete(self, line): |
| 405 | """ |
| 406 | Deleting a device. ID of the device needs to be provided |
| 407 | """ |
| 408 | device_id = line or self.default_device_id |
| 409 | self.poutput('deleting {}'.format(device_id)) |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 410 | try: |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 411 | stub = self.get_stub() |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 412 | stub.DeleteDevice(voltha_pb2.ID(id=device_id)) |
| 413 | self.poutput('deleted {}'.format(device_id)) |
ggowdru | 64d738a | 2018-05-10 07:08:06 -0700 | [diff] [blame] | 414 | except Exception as e: |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 415 | self.poutput('Error deleting {}. Error:{}'.format(device_id, e)) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 416 | |
| 417 | def do_disable(self, line): |
| 418 | """ |
| 419 | Disable a device. ID of the device needs to be provided |
| 420 | """ |
| 421 | device_id = line |
Venkata Telu | f468de8 | 2018-08-07 13:03:58 -0500 | [diff] [blame] | 422 | if device_id not in self.device_ids(): |
| 423 | self.poutput('Error: There is no such device') |
| 424 | return |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 425 | try: |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 426 | stub = self.get_stub() |
Venkata Telu | f468de8 | 2018-08-07 13:03:58 -0500 | [diff] [blame] | 427 | device = stub.GetDevice(voltha_pb2.ID(id=device_id)) |
| 428 | if device.admin_state == voltha_pb2.AdminState.DISABLED: |
| 429 | self.poutput('Error: Device is already disabled') |
| 430 | return |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 431 | stub.DisableDevice(voltha_pb2.ID(id=device_id)) |
Venkata Telu | f468de8 | 2018-08-07 13:03:58 -0500 | [diff] [blame] | 432 | self.poutput('disabling {}'.format(device_id)) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 433 | |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 434 | # Do device query and verify that the device admin status is |
| 435 | # DISABLED and Operational Status is unknown |
| 436 | device = stub.GetDevice(voltha_pb2.ID(id=device_id)) |
ggowdru | 64d738a | 2018-05-10 07:08:06 -0700 | [diff] [blame] | 437 | if device.admin_state == voltha_pb2.AdminState.DISABLED: |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 438 | self.poutput('disabled successfully {}'.format(device_id)) |
| 439 | else: |
| 440 | self.poutput('disabling failed {}. Admin State:{} ' |
| 441 | 'Operation State: {}'.format(device_id, |
| 442 | device.admin_state, |
| 443 | device.oper_status)) |
ggowdru | 64d738a | 2018-05-10 07:08:06 -0700 | [diff] [blame] | 444 | except Exception as e: |
Khen Nursimulu | c60afa1 | 2017-03-13 14:33:50 -0400 | [diff] [blame] | 445 | self.poutput('Error disabling {}. Error:{}'.format(device_id, e)) |
Khen Nursimulu | d068d81 | 2017-03-06 11:44:18 -0500 | [diff] [blame] | 446 | |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 447 | def do_test(self, line): |
| 448 | """Enter test mode, which makes a bunch on new commands available""" |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 449 | sub = TestCli(self.history, self.voltha_grpc, |
| 450 | self.get_stub, self.voltha_sim_rest) |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 451 | sub.cmdloop() |
| 452 | |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 453 | def do_alarm_filters(self, line): |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 454 | sub = AlarmFiltersCli(self.get_stub) |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 455 | sub.cmdloop() |
| 456 | |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 457 | |
| 458 | class TestCli(VolthaCli): |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 459 | def __init__(self, history, voltha_grpc, get_stub, voltha_sim_rest): |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 460 | VolthaCli.__init__(self, voltha_grpc, voltha_sim_rest) |
| 461 | self.history = history |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 462 | self.get_stub = get_stub |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 463 | self.prompt = '(' + self.colorize(self.colorize('test', 'cyan'), |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 464 | 'bold') + ') ' |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 465 | |
| 466 | def get_device(self, device_id, depth=0): |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 467 | stub = self.get_stub() |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 468 | res = stub.GetDevice(voltha_pb2.ID(id=device_id), |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 469 | metadata=(('get-depth', str(depth)),)) |
Zsolt Haraszti | 50cae7d | 2017-01-08 22:27:07 -0800 | [diff] [blame] | 470 | return res |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 471 | |
| 472 | def do_arrive_onus(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 473 | """ |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 474 | Simulate the arrival of ONUs (available only on simulated_olt) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 475 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 476 | device_id = line or self.default_device_id |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 477 | |
| 478 | # verify that device is of type simulated_olt |
| 479 | device = self.get_device(device_id) |
| 480 | assert device.type == 'simulated_olt', ( |
| 481 | 'Cannot use it on this device type (only on simulated_olt type)') |
| 482 | |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 483 | requests.get('http://{}/devices/{}/detect_onus'.format( |
| 484 | self.voltha_sim_rest, device_id |
| 485 | )) |
| 486 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 487 | complete_arrive_onus = VolthaCli.complete_device |
| 488 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 489 | def get_logical_ports(self, logical_device_id): |
| 490 | """ |
| 491 | Return the NNI port number and the first usable UNI port of logical |
| 492 | device, and the vlan associated with the latter. |
| 493 | """ |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 494 | stub = self.get_stub() |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 495 | ports = stub.ListLogicalDevicePorts( |
| 496 | voltha_pb2.ID(id=logical_device_id)).items |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 497 | nni = None |
| 498 | unis = [] |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 499 | for port in ports: |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 500 | if port.root_port: |
| 501 | assert nni is None, "There shall be only one root port" |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 502 | nni = port.ofp_port.port_no |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 503 | else: |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 504 | uni = port.ofp_port.port_no |
| 505 | uni_device = self.get_device(port.device_id) |
| 506 | vlan = uni_device.vlan |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 507 | unis.append((uni, vlan)) |
| 508 | |
| 509 | assert nni is not None, "No NNI port found" |
| 510 | assert unis, "Not a single UNI?" |
| 511 | |
| 512 | return nni, unis |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 513 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 514 | def do_install_eapol_flow(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 515 | """ |
| 516 | Install an EAPOL flow on the given logical device. If device is not |
| 517 | given, it will be applied to logical device of the last pre-provisioned |
| 518 | OLT device. |
| 519 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 520 | logical_device_id = line or self.default_logical_device_id |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 521 | |
| 522 | # gather NNI and UNI port IDs |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 523 | nni_port_no, unis = self.get_logical_ports(logical_device_id) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 524 | |
| 525 | # construct and push flow rule |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 526 | stub = self.get_stub() |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 527 | for uni_port_no, _ in unis: |
| 528 | update = FlowTableUpdate( |
| 529 | id=logical_device_id, |
| 530 | flow_mod=mk_simple_flow_mod( |
| 531 | priority=2000, |
| 532 | match_fields=[in_port(uni_port_no), eth_type(0x888e)], |
| 533 | actions=[ |
| 534 | # push_vlan(0x8100), |
| 535 | # set_field(vlan_vid(4096 + 4000)), |
| 536 | output(ofp.OFPP_CONTROLLER) |
| 537 | ] |
| 538 | ) |
| 539 | ) |
| 540 | res = stub.UpdateLogicalDeviceFlowTable(update) |
| 541 | self.poutput('success for uni {} ({})'.format(uni_port_no, res)) |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 542 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 543 | complete_install_eapol_flow = VolthaCli.complete_logical_device |
| 544 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 545 | def do_install_all_controller_bound_flows(self, line): |
| 546 | """ |
| 547 | Install all flow rules for controller bound flows, including EAPOL, |
| 548 | IGMP and DHCP. If device is not given, it will be applied to logical |
| 549 | device of the last pre-provisioned OLT device. |
| 550 | """ |
| 551 | logical_device_id = line or self.default_logical_device_id |
| 552 | |
| 553 | # gather NNI and UNI port IDs |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 554 | nni_port_no, unis = self.get_logical_ports(logical_device_id) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 555 | |
| 556 | # construct and push flow rules |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 557 | stub = self.get_stub() |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 558 | |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 559 | for uni_port_no, _ in unis: |
| 560 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 561 | id=logical_device_id, |
| 562 | flow_mod=mk_simple_flow_mod( |
| 563 | priority=2000, |
| 564 | match_fields=[ |
| 565 | in_port(uni_port_no), |
| 566 | eth_type(0x888e) |
| 567 | ], |
| 568 | actions=[output(ofp.OFPP_CONTROLLER)] |
| 569 | ) |
| 570 | )) |
| 571 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 572 | id=logical_device_id, |
| 573 | flow_mod=mk_simple_flow_mod( |
| 574 | priority=1000, |
| 575 | match_fields=[ |
| 576 | in_port(uni_port_no), |
| 577 | eth_type(0x800), |
| 578 | ip_proto(2) |
| 579 | ], |
| 580 | actions=[output(ofp.OFPP_CONTROLLER)] |
| 581 | ) |
| 582 | )) |
| 583 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 584 | id=logical_device_id, |
| 585 | flow_mod=mk_simple_flow_mod( |
| 586 | priority=1000, |
| 587 | match_fields=[ |
| 588 | in_port(uni_port_no), |
| 589 | eth_type(0x800), |
| 590 | ip_proto(17), |
| 591 | udp_dst(67) |
| 592 | ], |
| 593 | actions=[output(ofp.OFPP_CONTROLLER)] |
| 594 | ) |
| 595 | )) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 596 | self.poutput('success') |
| 597 | |
| 598 | complete_install_all_controller_bound_flows = \ |
| 599 | VolthaCli.complete_logical_device |
| 600 | |
| 601 | def do_install_all_sample_flows(self, line): |
| 602 | """ |
| 603 | Install all flows that are representative of the virtualized access |
| 604 | scenario in a PON network. |
| 605 | """ |
| 606 | logical_device_id = line or self.default_logical_device_id |
| 607 | |
| 608 | # gather NNI and UNI port IDs |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 609 | nni_port_no, unis = self.get_logical_ports(logical_device_id) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 610 | |
| 611 | # construct and push flow rules |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 612 | stub = self.get_stub() |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 613 | |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 614 | for uni_port_no, c_vid in unis: |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 615 | # Controller-bound flows |
| 616 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 617 | id=logical_device_id, |
| 618 | flow_mod=mk_simple_flow_mod( |
| 619 | priority=2000, |
| 620 | match_fields=[in_port(uni_port_no), eth_type(0x888e)], |
| 621 | actions=[ |
| 622 | # push_vlan(0x8100), |
| 623 | # set_field(vlan_vid(4096 + 4000)), |
| 624 | output(ofp.OFPP_CONTROLLER) |
| 625 | ] |
| 626 | ) |
| 627 | )) |
| 628 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 629 | id=logical_device_id, |
| 630 | flow_mod=mk_simple_flow_mod( |
| 631 | priority=1000, |
| 632 | match_fields=[eth_type(0x800), ip_proto(2)], |
| 633 | actions=[output(ofp.OFPP_CONTROLLER)] |
| 634 | ) |
| 635 | )) |
| 636 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 637 | id=logical_device_id, |
| 638 | flow_mod=mk_simple_flow_mod( |
| 639 | priority=1000, |
| 640 | match_fields=[eth_type(0x800), ip_proto(17), udp_dst(67)], |
| 641 | actions=[output(ofp.OFPP_CONTROLLER)] |
| 642 | ) |
| 643 | )) |
| 644 | |
| 645 | # Unicast flows: |
| 646 | # Downstream flow 1 |
| 647 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 648 | id=logical_device_id, |
| 649 | flow_mod=mk_simple_flow_mod( |
| 650 | priority=500, |
| 651 | match_fields=[ |
| 652 | in_port(nni_port_no), |
| 653 | vlan_vid(4096 + 1000), |
| 654 | metadata(c_vid) # here to mimic an ONOS artifact |
| 655 | ], |
| 656 | actions=[pop_vlan()], |
| 657 | next_table_id=1 |
| 658 | ) |
| 659 | )) |
| 660 | # Downstream flow 2 |
| 661 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 662 | id=logical_device_id, |
| 663 | flow_mod=mk_simple_flow_mod( |
| 664 | priority=500, |
| 665 | table_id=1, |
| 666 | match_fields=[in_port(nni_port_no), vlan_vid(4096 + c_vid)], |
| 667 | actions=[set_field(vlan_vid(4096 + 0)), output(uni_port_no)] |
| 668 | ) |
| 669 | )) |
| 670 | # Upstream flow 1 for 0-tagged case |
| 671 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 672 | id=logical_device_id, |
| 673 | flow_mod=mk_simple_flow_mod( |
| 674 | priority=500, |
| 675 | match_fields=[in_port(uni_port_no), vlan_vid(4096 + 0)], |
| 676 | actions=[set_field(vlan_vid(4096 + c_vid))], |
| 677 | next_table_id=1 |
| 678 | ) |
| 679 | )) |
| 680 | # Upstream flow 1 for untagged case |
| 681 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 682 | id=logical_device_id, |
| 683 | flow_mod=mk_simple_flow_mod( |
| 684 | priority=500, |
| 685 | match_fields=[in_port(uni_port_no), vlan_vid(0)], |
| 686 | actions=[push_vlan(0x8100), set_field(vlan_vid(4096 + c_vid))], |
| 687 | next_table_id=1 |
| 688 | ) |
| 689 | )) |
| 690 | # Upstream flow 2 for s-tag |
| 691 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 692 | id=logical_device_id, |
| 693 | flow_mod=mk_simple_flow_mod( |
| 694 | priority=500, |
| 695 | table_id=1, |
| 696 | match_fields=[in_port(uni_port_no), vlan_vid(4096 + c_vid)], |
| 697 | actions=[ |
| 698 | push_vlan(0x8100), |
| 699 | set_field(vlan_vid(4096 + 1000)), |
| 700 | output(nni_port_no) |
| 701 | ] |
| 702 | ) |
| 703 | )) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 704 | |
| 705 | # Push a few multicast flows |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 706 | # 1st with one bucket for our uni 0 |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 707 | stub.UpdateLogicalDeviceFlowGroupTable(FlowGroupTableUpdate( |
| 708 | id=logical_device_id, |
| 709 | group_mod=mk_multicast_group_mod( |
| 710 | group_id=1, |
| 711 | buckets=[ |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 712 | ofp.ofp_bucket(actions=[ |
| 713 | pop_vlan(), |
| 714 | output(unis[0][0]) |
| 715 | ]) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 716 | ] |
| 717 | ) |
| 718 | )) |
| 719 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 720 | id=logical_device_id, |
| 721 | flow_mod=mk_simple_flow_mod( |
| 722 | priority=1000, |
| 723 | match_fields=[ |
| 724 | in_port(nni_port_no), |
| 725 | eth_type(0x800), |
| 726 | vlan_vid(4096 + 140), |
| 727 | ipv4_dst(0xe4010101) |
| 728 | ], |
| 729 | actions=[group(1)] |
| 730 | ) |
| 731 | )) |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 732 | |
| 733 | # 2nd with one bucket for uni 0 and 1 |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 734 | stub.UpdateLogicalDeviceFlowGroupTable(FlowGroupTableUpdate( |
| 735 | id=logical_device_id, |
| 736 | group_mod=mk_multicast_group_mod( |
| 737 | group_id=2, |
| 738 | buckets=[ |
Nathan Knuth | 6b7b6ff | 2017-02-12 03:30:48 -0800 | [diff] [blame] | 739 | ofp.ofp_bucket(actions=[pop_vlan(), output(unis[0][0])]) |
Stephane Barbarie | 4db8ca2 | 2017-04-24 10:30:20 -0400 | [diff] [blame] | 740 | # ofp.ofp_bucket(actions=[pop_vlan(), output(unis[1][0])]) |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 741 | ] |
| 742 | ) |
| 743 | )) |
| 744 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 745 | id=logical_device_id, |
| 746 | flow_mod=mk_simple_flow_mod( |
| 747 | priority=1000, |
| 748 | match_fields=[ |
| 749 | in_port(nni_port_no), |
| 750 | eth_type(0x800), |
| 751 | vlan_vid(4096 + 140), |
| 752 | ipv4_dst(0xe4020202) |
| 753 | ], |
| 754 | actions=[group(2)] |
| 755 | ) |
| 756 | )) |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 757 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 758 | # 3rd with empty bucket |
| 759 | stub.UpdateLogicalDeviceFlowGroupTable(FlowGroupTableUpdate( |
| 760 | id=logical_device_id, |
| 761 | group_mod=mk_multicast_group_mod( |
| 762 | group_id=3, |
| 763 | buckets=[] |
| 764 | ) |
| 765 | )) |
| 766 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 767 | id=logical_device_id, |
| 768 | flow_mod=mk_simple_flow_mod( |
| 769 | priority=1000, |
| 770 | match_fields=[ |
| 771 | in_port(nni_port_no), |
| 772 | eth_type(0x800), |
| 773 | vlan_vid(4096 + 140), |
| 774 | ipv4_dst(0xe4030303) |
| 775 | ], |
| 776 | actions=[group(3)] |
| 777 | ) |
| 778 | )) |
| 779 | |
| 780 | self.poutput('success') |
| 781 | |
| 782 | complete_install_all_sample_flows = VolthaCli.complete_logical_device |
| 783 | |
Nathan Knuth | 5f4163e | 2017-01-11 18:21:10 -0600 | [diff] [blame] | 784 | def do_install_dhcp_flows(self, line): |
| 785 | """ |
| 786 | Install all dhcp flows that are representative of the virtualized access |
| 787 | scenario in a PON network. |
| 788 | """ |
| 789 | logical_device_id = line or self.default_logical_device_id |
| 790 | |
| 791 | # gather NNI and UNI port IDs |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 792 | nni_port_no, unis = self.get_logical_ports(logical_device_id) |
Nathan Knuth | 5f4163e | 2017-01-11 18:21:10 -0600 | [diff] [blame] | 793 | |
| 794 | # construct and push flow rules |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 795 | stub = self.get_stub() |
Nathan Knuth | 5f4163e | 2017-01-11 18:21:10 -0600 | [diff] [blame] | 796 | |
| 797 | # Controller-bound flows |
Zsolt Haraszti | b9a5f75 | 2017-02-11 06:07:08 -0800 | [diff] [blame] | 798 | for uni_port_no, _ in unis: |
| 799 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 800 | id=logical_device_id, |
| 801 | flow_mod=mk_simple_flow_mod( |
| 802 | priority=1000, |
| 803 | match_fields=[ |
| 804 | in_port(uni_port_no), |
| 805 | eth_type(0x800), |
| 806 | ip_proto(17), |
| 807 | udp_dst(67) |
| 808 | ], |
| 809 | actions=[output(ofp.OFPP_CONTROLLER)] |
| 810 | ) |
| 811 | )) |
Nathan Knuth | 5f4163e | 2017-01-11 18:21:10 -0600 | [diff] [blame] | 812 | |
| 813 | self.poutput('success') |
| 814 | |
| 815 | complete_install_dhcp_flows = VolthaCli.complete_logical_device |
| 816 | |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 817 | def do_delete_all_flows(self, line): |
| 818 | """ |
| 819 | Remove all flows and flow groups from given logical device |
| 820 | """ |
| 821 | logical_device_id = line or self.default_logical_device_id |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 822 | stub = self.get_stub() |
Zsolt Haraszti | 85f1285 | 2016-12-24 08:30:58 -0800 | [diff] [blame] | 823 | stub.UpdateLogicalDeviceFlowTable(FlowTableUpdate( |
| 824 | id=logical_device_id, |
| 825 | flow_mod=ofp.ofp_flow_mod( |
| 826 | command=ofp.OFPFC_DELETE, |
| 827 | table_id=ofp.OFPTT_ALL, |
| 828 | cookie_mask=0, |
| 829 | out_port=ofp.OFPP_ANY, |
| 830 | out_group=ofp.OFPG_ANY |
| 831 | ) |
| 832 | )) |
| 833 | stub.UpdateLogicalDeviceFlowGroupTable(FlowGroupTableUpdate( |
| 834 | id=logical_device_id, |
| 835 | group_mod=ofp.ofp_group_mod( |
| 836 | command=ofp.OFPGC_DELETE, |
| 837 | group_id=ofp.OFPG_ALL |
| 838 | ) |
| 839 | )) |
| 840 | self.poutput('success') |
| 841 | |
| 842 | complete_delete_all_flows = VolthaCli.complete_logical_device |
| 843 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 844 | def do_send_simulated_upstream_eapol(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 845 | """ |
| 846 | Send an EAPOL upstream from a simulated OLT |
| 847 | """ |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 848 | device_id = line or self.default_device_id |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 849 | requests.get('http://{}/devices/{}/test_eapol_in'.format( |
| 850 | self.voltha_sim_rest, device_id |
| 851 | )) |
| 852 | |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 853 | complete_send_simulated_upstream_eapol = VolthaCli.complete_device |
| 854 | |
| 855 | def do_inject_eapol_start(self, line): |
Zsolt Haraszti | d036b7e | 2016-12-23 15:36:01 -0800 | [diff] [blame] | 856 | """ |
| 857 | Send out an an EAPOL start message into the given Unix interface |
| 858 | """ |
| 859 | pass |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 860 | |
| 861 | |
| 862 | if __name__ == '__main__': |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 863 | |
| 864 | parser = argparse.ArgumentParser() |
| 865 | |
| 866 | _help = '<hostname>:<port> to consul agent (default: %s)' % defs['consul'] |
| 867 | parser.add_argument( |
| 868 | '-C', '--consul', action='store', default=defs['consul'], help=_help) |
| 869 | |
| 870 | _help = 'Lookup Voltha endpoints based on service entries in Consul' |
| 871 | parser.add_argument( |
| 872 | '-L', '--lookup', action='store_true', help=_help) |
| 873 | |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 874 | _help = 'All requests to the Voltha gRPC service are global' |
| 875 | parser.add_argument( |
| 876 | '-G', '--global_request', action='store_true', help=_help) |
| 877 | |
Zsolt Haraszti | 9b485fb | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 878 | _help = '<hostname>:<port> of Voltha gRPC service (default={})'.format( |
| 879 | defs['voltha_grpc_endpoint']) |
| 880 | parser.add_argument('-g', '--grpc-endpoint', action='store', |
| 881 | default=defs['voltha_grpc_endpoint'], help=_help) |
| 882 | |
| 883 | _help = '<hostname>:<port> of Voltha simulated adapter backend for ' \ |
| 884 | 'testing (default={})'.format( |
| 885 | defs['voltha_sim_rest_endpoint']) |
| 886 | parser.add_argument('-s', '--sim-rest-endpoint', action='store', |
| 887 | default=defs['voltha_sim_rest_endpoint'], help=_help) |
| 888 | |
| 889 | args = parser.parse_args() |
| 890 | |
| 891 | if args.lookup: |
| 892 | host = args.consul.split(':')[0].strip() |
| 893 | port = int(args.consul.split(':')[1].strip()) |
| 894 | consul = Consul(host=host, port=port) |
| 895 | |
| 896 | _, services = consul.catalog.service('voltha-grpc') |
| 897 | if not services: |
| 898 | print('No voltha-grpc service registered in consul; exiting') |
| 899 | sys.exit(1) |
| 900 | args.grpc_endpoint = '{}:{}'.format(services[0]['ServiceAddress'], |
| 901 | services[0]['ServicePort']) |
| 902 | |
| 903 | _, services = consul.catalog.service('voltha-sim-rest') |
| 904 | if not services: |
| 905 | print('No voltha-sim-rest service registered in consul; exiting') |
| 906 | sys.exit(1) |
| 907 | args.sim_rest_endpoint = '{}:{}'.format(services[0]['ServiceAddress'], |
| 908 | services[0]['ServicePort']) |
| 909 | |
khenaidoo | 108f05c | 2017-07-06 11:15:29 -0400 | [diff] [blame] | 910 | c = VolthaCli(args.grpc_endpoint, args.sim_rest_endpoint, |
| 911 | args.global_request) |
Zsolt Haraszti | 8017520 | 2016-12-24 00:17:51 -0800 | [diff] [blame] | 912 | c.poutput(banner) |
Zsolt Haraszti | a133a45 | 2016-12-22 01:26:57 -0800 | [diff] [blame] | 913 | c.load_history() |
| 914 | c.cmdloop() |
| 915 | c.save_history() |