67d8ca64422e85d453654091601036dd0bc3f25e
[netvirt.git] / vpnservice / aclservice / shell / src / main / java / org / opendaylight / netvirt / aclservice / shell / DisplayAclDataCaches.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.aclservice.shell;
10
11 import java.util.Collection;
12 import java.util.Map;
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;
23
24
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;
47
48
49     @Option(name = "-op", aliases = { "--option",
50             "--op" }, description = opSelections, required = false, multiValued = false)
51     private String op;
52
53     @Option(name = "--uuid", description = "uuid for aclInterface/remoteAclId", required = false, multiValued = false)
54     private String uuidStr;
55
56
57     @Option(name = "--all", description = "display the complete selected map", required = false, multiValued = false)
58     private String all ;
59
60     @Option(name = "--key", description = "key for aclFlowPriority/aclInterfaceCache", required = false,
61             multiValued = false)
62     private String key;
63
64     public void setAclDataCache(AclDataCache aclDataCache) {
65         this.aclDataCache = aclDataCache;
66     }
67
68     public void setAclInterfaceCache(AclInterfaceCache aclInterfaceCache) {
69         this.aclInterfaceCache = aclInterfaceCache;
70     }
71
72     @Override
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");
76             return null;
77         }
78
79         if (op == null) {
80             session.getConsole().println("Please provide valid option");
81             usage();
82             session.getConsole().println(opSelStr);
83             return null;
84         }
85         switch (op) {
86             case "aclInterface":
87                 getAclInterfaceMap();
88                 break;
89             case "remoteAclId":
90                 getRemoteAclIdMap();
91                 break;
92             case "aclFlowPriority":
93                 getAclFlowPriorityMap();
94                 break;
95             case "aclInterfaceCache":
96                 getAclInterfaceCache();
97                 break;
98             default:
99                 session.getConsole().println("invalid operation");
100                 usage();
101                 session.getConsole().println(opSelStr);
102         }
103         return null;
104     }
105
106     void usage() {
107         session.getConsole().println("usage:");
108     }
109
110     void printAclInterfaceMapHelp() {
111         session.getConsole().println("invalid input");
112         usage();
113         session.getConsole().println(
114                 exeCmdStr + "aclInterface --all show | --uuid <uuid>");
115     }
116
117     void printRemoteAclIdMapHelp() {
118         session.getConsole().println("invalid input");
119         usage();
120         session.getConsole().println(
121                 exeCmdStr + "remoteAclId --all show | --uuid <uuid>");
122     }
123
124     void printAclFlowPriorityMapHelp() {
125         session.getConsole().println("invalid input");
126         usage();
127         session.getConsole().println(
128                 exeCmdStr + "aclFlowPriority --all show | --key <key>");
129     }
130
131     void printAclInterfaceCacheHelp() {
132         session.getConsole().println("invalid input");
133         usage();
134         session.getConsole().println(
135                 exeCmdStr + "aclInterfaceCache --all show | --key <key>");
136     }
137
138     private boolean validateAll(String all) {
139         if (all.equalsIgnoreCase("show")) {
140             return true;
141         }
142         return false;
143     }
144
145     protected void getAclInterfaceMap() throws Exception {
146         if (all == null && uuidStr == null) {
147             printAclInterfaceMapHelp();
148             return;
149         }
150         if (all == null && uuidStr != null) {
151             Uuid uuid;
152             try {
153                 uuid = Uuid.getDefaultInstance(uuidStr);
154             } catch (IllegalArgumentException e) {
155                 session.getConsole().println("Invalid uuid" + e.getMessage());
156                 log.error("Invalid uuid" + e);
157                 return;
158             }
159             Collection<AclInterface> aclInterfaceList = aclDataCache.getInterfaceList(uuid);
160             if (aclInterfaceList == null || aclInterfaceList.isEmpty()) {
161                 session.getConsole().println("UUID not matched");
162                 return;
163             } else {
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()));
173                 }
174                 return;
175             }
176         }
177         if (all != null && uuidStr == null) {
178             if (!validateAll(all)) {
179                 printAclInterfaceMapHelp();
180                 return;
181             }
182             Map<Uuid, Collection<AclInterface>> map = aclDataCache.getAclInterfaceMap();
183
184             if (map.isEmpty()) {
185                 session.getConsole().println("No data found");
186                 return;
187             } else {
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()));
199                     }
200                 }
201                 return;
202             }
203         }
204     }
205
206     protected void getRemoteAclIdMap() throws Exception {
207         if (all == null && uuidStr == null) {
208             printRemoteAclIdMapHelp();
209             return;
210         }
211         if (all == null && uuidStr != null) {
212             Uuid uuidRef;
213             try {
214                 uuidRef = Uuid.getDefaultInstance(uuidStr);
215             } catch (IllegalArgumentException e) {
216                 session.getConsole().println("Invalid uuid" + e.getMessage());
217                 log.error("Invalid uuid" + e);
218                 return;
219             }
220             Collection<Uuid> remoteUuidLst = aclDataCache.getRemoteAcl(uuidRef);
221             if (remoteUuidLst == null || remoteUuidLst.isEmpty()) {
222                 session.getConsole().println("UUID not matched");
223                 return;
224             } else {
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()));
229                 }
230                 return;
231             }
232         }
233         if (all != null && uuidStr == null) {
234             if (!validateAll(all)) {
235                 printRemoteAclIdMapHelp();
236                 return;
237             }
238
239             Map<Uuid, Collection<Uuid>> map = aclDataCache.getRemoteAclIdMap();
240             if (map.isEmpty()) {
241                 session.getConsole().println("No data found");
242                 return;
243             } else {
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()));
250                     }
251                 }
252                 return;
253             }
254         }
255     }
256
257     protected void getAclFlowPriorityMap() throws Exception {
258         if (all == null && key == null) {
259             printAclFlowPriorityMapHelp();
260             return;
261         }
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));
266
267             return;
268         }
269
270         if (all != null && key == null) {
271             if (!validateAll(all)) {
272                 printAclFlowPriorityMapHelp();
273                 return;
274             }
275             Map<String, Integer> map = aclDataCache.getAclFlowPriorityMap();
276             if (map.isEmpty()) {
277                 session.getConsole().println("No data found");
278                 return;
279             } else {
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()));
283                 }
284                 return;
285             }
286         }
287     }
288
289     protected void getAclInterfaceCache() throws Exception {
290         if (all == null && key == null) {
291             printAclInterfaceCacheHelp();
292             return;
293         }
294         if (all == null && key != null) {
295             AclInterface aclInterface = aclInterfaceCache.get(key);
296             if (aclInterface == null) {
297                 session.getConsole().println("No data found");
298                 return;
299             }
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()));
307
308             return;
309         }
310
311         if (all != null && key == null) {
312             if (!validateAll(all)) {
313                 printAclInterfaceCacheHelp();
314                 return;
315             }
316             Collection<Entry<String, AclInterface>> entries = aclInterfaceCache.entries();
317             if (entries.isEmpty()) {
318                 session.getConsole().println("No data found");
319                 return;
320             } else {
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()));
330                 }
331             }
332             return;
333         }
334     }
335 }
336
337
338