Jian Li | c87b23b | 2017-04-24 15:28:10 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | package org.onosproject.mapping.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.apache.karaf.shell.commands.Option; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.mapping.Mapping; |
| 23 | import org.onosproject.mapping.MappingKey; |
| 24 | import org.onosproject.mapping.MappingTreatment; |
| 25 | import org.onosproject.mapping.MappingValue; |
| 26 | import org.onosproject.mapping.MappingService; |
| 27 | import org.onosproject.mapping.MappingStore; |
| 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static com.google.common.collect.Lists.newArrayList; |
| 35 | |
| 36 | /** |
| 37 | * A command for querying mapping information. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "mappings", |
| 40 | description = "Lists mappings") |
| 41 | public class MappingsListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String DB = "map_database"; |
| 44 | private static final String CACHE = "map_cache"; |
| 45 | |
| 46 | private static final String SUMMARY_FORMAT = "deviceId=%s, mappingCount=%d"; |
| 47 | private static final String MAPPING_ID_FORMAT = " id=%s"; |
| 48 | private static final String MAPPING_KEY_FORMAT = " key=%s"; |
| 49 | private static final String MAPPING_VALUE_FORMAT = " value="; |
| 50 | private static final String MAPPING_ACTION_FORMAT = " action=%s"; |
| 51 | private static final String MAPPING_TREATMENTS_FORMAT = " treatments="; |
| 52 | private static final String MAPPING_TREATMENT_LONG_FORMAT = |
| 53 | " address=%s, instructions=%s"; |
| 54 | private static final String MAPPING_TREATMENT_SHORT_FORMAT = " %s"; |
| 55 | |
| 56 | @Argument(index = 0, name = "type", |
| 57 | description = "Shows mappings with specified type", |
| 58 | required = true, multiValued = false) |
| 59 | private String type = null; |
| 60 | |
| 61 | @Argument(index = 1, name = "deviceId", description = "Device identity", |
| 62 | required = false, multiValued = false) |
| 63 | private String deviceId = null; |
| 64 | |
| 65 | @Option(name = "-s", aliases = "--short", |
| 66 | description = "Print more succinct output for each mapping", |
| 67 | required = false, multiValued = false) |
| 68 | private boolean shortOutput = false; |
| 69 | |
| 70 | private MappingService mappingService = |
| 71 | AbstractShellCommand.get(MappingService.class); |
| 72 | private List<Mapping> mappings; |
| 73 | |
| 74 | @Override |
| 75 | protected void execute() { |
| 76 | |
| 77 | MappingStore.Type typeEnum = null; |
| 78 | |
| 79 | if (type.equals(DB)) { |
| 80 | typeEnum = MappingStore.Type.MAP_DATABASE; |
| 81 | } else if (type.equals(CACHE)) { |
| 82 | typeEnum = MappingStore.Type.MAP_CACHE; |
| 83 | } |
| 84 | |
| 85 | DeviceService deviceService = get(DeviceService.class); |
| 86 | Iterable<Device> devices = deviceService.getDevices(); |
| 87 | |
| 88 | if (deviceId != null) { |
| 89 | mappings = newArrayList(mappingService.getMappingEntries(typeEnum, |
| 90 | DeviceId.deviceId(deviceId))); |
| 91 | printMappings(DeviceId.deviceId(deviceId), mappings); |
| 92 | |
| 93 | } else { |
| 94 | |
| 95 | for (Device d : devices) { |
| 96 | mappings = newArrayList(mappingService.getMappingEntries(typeEnum, d.id())); |
| 97 | printMappings(d.id(), mappings); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Prints out mapping information. |
| 104 | * |
| 105 | * @param deviceId device identifier |
| 106 | * @param mappings a collection of mapping |
| 107 | */ |
| 108 | private void printMappings(DeviceId deviceId, List<Mapping> mappings) { |
| 109 | |
| 110 | print(SUMMARY_FORMAT, deviceId, mappings.size()); |
| 111 | |
| 112 | for (Mapping m : mappings) { |
| 113 | print(MAPPING_ID_FORMAT, Long.toHexString(m.id().value())); |
| 114 | print(MAPPING_KEY_FORMAT, printMappingKey(m.key())); |
| 115 | printMappingValue(m.value()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Prints out mapping key. |
| 121 | * |
| 122 | * @param key mapping key |
| 123 | * @return string format of mapping key |
| 124 | */ |
| 125 | private String printMappingKey(MappingKey key) { |
| 126 | StringBuilder builder = new StringBuilder(); |
| 127 | |
| 128 | if (key.address() != null) { |
| 129 | builder.append(key.address().toString()); |
| 130 | } |
| 131 | |
| 132 | return builder.toString(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Prints out mapping value. |
| 137 | * |
| 138 | * @param value mapping value |
| 139 | * @return string format of mapping value |
| 140 | */ |
| 141 | private void printMappingValue(MappingValue value) { |
| 142 | |
| 143 | print(MAPPING_VALUE_FORMAT); |
| 144 | |
| 145 | if (value.action() != null) { |
| 146 | print(MAPPING_ACTION_FORMAT, value.action().toString()); |
| 147 | } |
| 148 | |
| 149 | if (!value.treatments().isEmpty()) { |
| 150 | print(MAPPING_TREATMENTS_FORMAT); |
| 151 | for (MappingTreatment treatment : value.treatments()) { |
| 152 | printMappingTreatment(treatment); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Prints out mapping treatment. |
| 160 | * |
| 161 | * @param treatment mapping treatment |
| 162 | * @return string format of mapping treatment |
| 163 | */ |
| 164 | private void printMappingTreatment(MappingTreatment treatment) { |
| 165 | if (treatment != null) { |
| 166 | if (shortOutput) { |
| 167 | print(MAPPING_TREATMENT_SHORT_FORMAT, treatment.address()); |
| 168 | } else { |
| 169 | print(MAPPING_TREATMENT_LONG_FORMAT, treatment.address(), |
| 170 | treatment.instructions()); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |