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