Pingping Lin | de77ee5 | 2015-06-03 17:16:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
Pingping Lin | de77ee5 | 2015-06-03 17:16:07 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.virtualbng.cli; |
| 18 | import java.util.Iterator; |
| 19 | import java.util.Map; |
| 20 | import java.util.Map.Entry; |
| 21 | |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.virtualbng.VbngConfigurationService; |
| 26 | |
| 27 | /** |
| 28 | * Command to show the list of vBNG IP address mapping entries. |
| 29 | */ |
| 30 | @Command(scope = "onos", name = "vbngs", |
| 31 | description = "Lists all vBNG IP address mapping entries") |
| 32 | public class MappingListCommand extends AbstractShellCommand { |
| 33 | |
| 34 | private static final String FORMAT_HEADER = |
| 35 | " Private IP - Public IP"; |
| 36 | private static final String FORMAT_MAPPING = |
| 37 | " %s - %s"; |
| 38 | |
| 39 | @Override |
| 40 | protected void execute() { |
| 41 | |
| 42 | VbngConfigurationService service = |
| 43 | AbstractShellCommand.get(VbngConfigurationService.class); |
| 44 | |
| 45 | // Print all mapping entries |
| 46 | printMappingEntries(service.getIpAddressMappings()); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Prints all vBNG IP address mapping entries. |
| 51 | * |
| 52 | * @param map the map from private IP address to public address |
| 53 | */ |
| 54 | private void printMappingEntries(Map<IpAddress, IpAddress> map) { |
| 55 | print(FORMAT_HEADER); |
| 56 | |
| 57 | Iterator<Entry<IpAddress, IpAddress>> entries = |
| 58 | map.entrySet().iterator(); |
| 59 | while (entries.hasNext()) { |
| 60 | Entry<IpAddress, IpAddress> entry = entries.next(); |
| 61 | print(FORMAT_MAPPING, entry.getKey(), entry.getValue()); |
| 62 | } |
| 63 | |
| 64 | print(""); |
| 65 | } |
| 66 | } |