2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netvirt.aclservice.shell;
11 import java.util.Collection;
13 import java.util.Map.Entry;
14 import org.apache.karaf.shell.commands.Command;
15 import org.apache.karaf.shell.commands.Option;
16 import org.apache.karaf.shell.console.OsgiCommandSupport;
17 import org.opendaylight.netvirt.aclservice.api.AclInterfaceCache;
18 import org.opendaylight.netvirt.aclservice.api.utils.AclDataCache;
19 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 @Command(scope = "aclservice", name = "display-acl-data-cache", description = " ")
26 public class DisplayAclDataCaches extends OsgiCommandSupport {
27 private static final Logger LOGGER = LoggerFactory.getLogger(DisplayAclDataCaches.class);
28 private AclDataCache aclDataCache;
29 private AclInterfaceCache aclInterfaceCache;
30 private static final String KEY_TAB = " %-8s";
31 private static final String ACL_INT_TAB = " %-4s %-4s %-4s %-4s %-4s %-4s %-6s %-20s %-20s %-4s";
32 private static final String ACL_INT_TAB_FOR = KEY_TAB + ACL_INT_TAB;
33 private static final String ACL_INT_HEAD = String.format(ACL_INT_TAB_FOR, "UUID", "PortSecurityEnabled",
34 "InterfaceId", "LPortTag", "DpId", "ElanId", "VpnId", "SecurityGroups", "AllowedAddressPairs",
35 "SubnetIpPrefixes", "MarkedForDelete")
36 + "\n -------------------------------------------------------------------------------------------------";
37 private static final String REM_ID_TAB = " %-20s ";
38 private static final String REM_ID_TAB_FOR = KEY_TAB + REM_ID_TAB;
39 private static final String REM_ID_HEAD = String.format(REM_ID_TAB_FOR, "UUID", "Values")
40 + "\n -------------------------------------------------------------------------";
41 private static final String ACL_DATA_TAB_FOR = " %-8s %-8s ";
42 private static final String ACL_DATA_HEAD = String.format(ACL_DATA_TAB_FOR, "Key", "Value")
43 + "\n -------------------------------------------------------------------------";
44 private final String exeCmdStr = "exec display-acl-data-cache -op ";
45 private final String opSelections = "[ aclInterface | remoteAclId | aclFlowPriority | aclInterfaceCache ]";
46 private final String opSelStr = exeCmdStr + opSelections;
49 @Option(name = "-op", aliases = { "--option",
50 "--op" }, description = opSelections, required = false, multiValued = false)
53 @Option(name = "--uuid", description = "uuid for aclInterface/remoteAclId", required = false, multiValued = false)
54 private String uuidStr;
57 @Option(name = "--all", description = "display the complete selected map", required = false, multiValued = false)
60 @Option(name = "--key", description = "key for aclFlowPriority/aclInterfaceCache", required = false,
64 public void setAclDataCache(AclDataCache aclDataCache) {
65 this.aclDataCache = aclDataCache;
68 public void setAclInterfaceCache(AclInterfaceCache aclInterfaceCache) {
69 this.aclInterfaceCache = aclInterfaceCache;
73 protected Object doExecute() throws Exception {
74 if (aclDataCache == null) {
75 session.getConsole().println("Failed to handle the command, AclData reference is null at this point");
80 session.getConsole().println("Please provide valid option");
82 session.getConsole().println(opSelStr);
92 case "aclFlowPriority":
93 getAclFlowPriorityMap();
95 case "aclInterfaceCache":
96 getAclInterfaceCache();
99 session.getConsole().println("invalid operation");
101 session.getConsole().println(opSelStr);
107 session.getConsole().println("usage:");
110 void printAclInterfaceMapHelp() {
111 session.getConsole().println("invalid input");
113 session.getConsole().println(
114 exeCmdStr + "aclInterface --all show | --uuid <uuid>");
117 void printRemoteAclIdMapHelp() {
118 session.getConsole().println("invalid input");
120 session.getConsole().println(
121 exeCmdStr + "remoteAclId --all show | --uuid <uuid>");
124 void printAclFlowPriorityMapHelp() {
125 session.getConsole().println("invalid input");
127 session.getConsole().println(
128 exeCmdStr + "aclFlowPriority --all show | --key <key>");
131 void printAclInterfaceCacheHelp() {
132 session.getConsole().println("invalid input");
134 session.getConsole().println(
135 exeCmdStr + "aclInterfaceCache --all show | --key <key>");
138 private boolean validateAll(String all) {
139 if (all.equalsIgnoreCase("show")) {
145 protected void getAclInterfaceMap() throws Exception {
146 if (all == null && uuidStr == null) {
147 printAclInterfaceMapHelp();
150 if (all == null && uuidStr != null) {
153 uuid = Uuid.getDefaultInstance(uuidStr);
154 } catch (IllegalArgumentException e) {
155 session.getConsole().println("Invalid uuid" + e.getMessage());
156 log.error("Invalid uuid" + e);
159 Collection<AclInterface> aclInterfaceList = aclDataCache.getInterfaceList(uuid);
160 if (aclInterfaceList == null || aclInterfaceList.isEmpty()) {
161 session.getConsole().println("UUID not matched");
164 session.getConsole().println(String.format(ACL_INT_HEAD));
165 session.getConsole().print(String.format(KEY_TAB, uuid.toString()));
166 for (AclInterface aclInterface : aclInterfaceList) {
167 session.getConsole().println(String.format(ACL_INT_TAB,
168 aclInterface.isPortSecurityEnabled(), aclInterface.getInterfaceId(),
169 aclInterface.getLPortTag(), aclInterface.getDpId(), aclInterface.getElanId(),
170 aclInterface.getVpnId(), aclInterface.getSecurityGroups(),
171 aclInterface.getAllowedAddressPairs(), aclInterface.getSubnetIpPrefixes(),
172 aclInterface.isMarkedForDelete()));
177 if (all != null && uuidStr == null) {
178 if (!validateAll(all)) {
179 printAclInterfaceMapHelp();
182 Map<Uuid, Collection<AclInterface>> map = aclDataCache.getAclInterfaceMap();
185 session.getConsole().println("No data found");
188 session.getConsole().println(String.format(ACL_INT_HEAD));
189 for (Entry<Uuid, Collection<AclInterface>> entry: map.entrySet()) {
190 Uuid key = entry.getKey();
191 session.getConsole().print(String.format(KEY_TAB, key.toString()));
192 for (AclInterface aclInterface: entry.getValue()) {
193 session.getConsole().println(String.format(ACL_INT_TAB,
194 aclInterface.isPortSecurityEnabled(), aclInterface.getInterfaceId(),
195 aclInterface.getLPortTag(), aclInterface.getDpId(), aclInterface.getElanId(),
196 aclInterface.getVpnId(), aclInterface.getSecurityGroups(),
197 aclInterface.getAllowedAddressPairs(), aclInterface.getSubnetIpPrefixes(),
198 aclInterface.isMarkedForDelete()));
206 protected void getRemoteAclIdMap() throws Exception {
207 if (all == null && uuidStr == null) {
208 printRemoteAclIdMapHelp();
211 if (all == null && uuidStr != null) {
214 uuidRef = Uuid.getDefaultInstance(uuidStr);
215 } catch (IllegalArgumentException e) {
216 session.getConsole().println("Invalid uuid" + e.getMessage());
217 log.error("Invalid uuid" + e);
220 Collection<Uuid> remoteUuidLst = aclDataCache.getRemoteAcl(uuidRef);
221 if (remoteUuidLst == null || remoteUuidLst.isEmpty()) {
222 session.getConsole().println("UUID not matched");
225 session.getConsole().println(String.format(REM_ID_HEAD));
226 session.getConsole().print(String.format(KEY_TAB, uuidRef.toString()));
227 for (Uuid uuid : remoteUuidLst) {
228 session.getConsole().println(String.format(REM_ID_TAB, uuid.getValue()));
233 if (all != null && uuidStr == null) {
234 if (!validateAll(all)) {
235 printRemoteAclIdMapHelp();
239 Map<Uuid, Collection<Uuid>> map = aclDataCache.getRemoteAclIdMap();
241 session.getConsole().println("No data found");
244 session.getConsole().println(String.format(REM_ID_HEAD));
245 for (Entry<Uuid, Collection<Uuid>> entry: map.entrySet()) {
246 Uuid key = entry .getKey();
247 session.getConsole().print(String.format(KEY_TAB, key.toString()));
248 for (Uuid uuid: entry.getValue()) {
249 session.getConsole().println(String.format(REM_ID_TAB, uuid.getValue()));
257 protected void getAclFlowPriorityMap() throws Exception {
258 if (all == null && key == null) {
259 printAclFlowPriorityMapHelp();
262 if (all == null && key != null) {
263 Integer val = aclDataCache.getAclFlowPriority(key);
264 session.getConsole().println(String.format(ACL_DATA_HEAD));
265 session.getConsole().println(String.format(ACL_DATA_TAB_FOR, key, val));
270 if (all != null && key == null) {
271 if (!validateAll(all)) {
272 printAclFlowPriorityMapHelp();
275 Map<String, Integer> map = aclDataCache.getAclFlowPriorityMap();
277 session.getConsole().println("No data found");
280 session.getConsole().println(String.format(ACL_DATA_HEAD));
281 for (Map.Entry<String, Integer> entry : map.entrySet()) {
282 session.getConsole().println(String.format(ACL_DATA_TAB_FOR, entry.getKey(), entry.getValue()));
289 protected void getAclInterfaceCache() throws Exception {
290 if (all == null && key == null) {
291 printAclInterfaceCacheHelp();
294 if (all == null && key != null) {
295 AclInterface aclInterface = aclInterfaceCache.get(key);
296 if (aclInterface == null) {
297 session.getConsole().println("No data found");
300 session.getConsole().println(String.format(ACL_INT_HEAD));
301 session.getConsole().println(String.format(ACL_INT_TAB_FOR, key,
302 aclInterface.isPortSecurityEnabled(), aclInterface.getInterfaceId(),
303 aclInterface.getLPortTag(), aclInterface.getDpId(), aclInterface.getElanId(),
304 aclInterface.getVpnId(), aclInterface.getSecurityGroups(),
305 aclInterface.getAllowedAddressPairs(), aclInterface.getSubnetIpPrefixes(),
306 aclInterface.isMarkedForDelete()));
311 if (all != null && key == null) {
312 if (!validateAll(all)) {
313 printAclInterfaceCacheHelp();
316 Collection<Entry<String, AclInterface>> entries = aclInterfaceCache.entries();
317 if (entries.isEmpty()) {
318 session.getConsole().println("No data found");
321 session.getConsole().println(String.format(ACL_INT_HEAD));
322 for (Map.Entry<String, AclInterface> entry : entries) {
323 AclInterface aclInterface = entry.getValue();
324 session.getConsole().println(String.format(ACL_INT_TAB_FOR, entry.getKey(),
325 aclInterface.isPortSecurityEnabled(), aclInterface.getInterfaceId(),
326 aclInterface.getLPortTag(), aclInterface.getDpId(), aclInterface.getElanId(),
327 aclInterface.getVpnId(), aclInterface.getSecurityGroups(),
328 aclInterface.getAllowedAddressPairs(), aclInterface.getSubnetIpPrefixes(),
329 aclInterface.isMarkedForDelete()));