blob: 346f5546261c0c3e610290461b8a13d3df0b37bc [file] [log] [blame]
Daniele Moro94660a02019-12-02 12:02:07 -08001/*
2 * Copyright 2019-present Open Networking Foundation
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
17package org.opencord.bng.cli;
18
19import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Completion;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.onosproject.cli.AbstractShellCommand;
24import org.opencord.bng.BngAttachment;
25import org.opencord.bng.BngService;
26import org.opencord.bng.BngStatsService;
27
28import static java.util.Map.Entry.comparingByKey;
29
30@Service
31@Command(scope = "bng", name = "attachment-stats",
32 description = "Get the stats (registers) of the attachments")
33public class AttachmentStats extends AbstractShellCommand {
34
35 @Argument(index = 0, name = "attachmentKey", description = "Attachment Key. No ID or 0 means ALL")
36 @Completion(AttachmentKeyCompleter.class)
37 String attachmentKey = null;
38
39 @Override
40 protected void doExecute() throws Exception {
41 BngService bngService = AbstractShellCommand.get(BngService.class);
42
43 print("STATISTICS");
44 if (attachmentKey == null) {
45 // Print the statistics for all the registered attachments
46 bngService.getAttachments().forEach(this::printAttachmentStats);
47 } else {
48 printAttachmentStats(attachmentKey, bngService.getAttachment(attachmentKey));
49 }
50 }
51
52 private void printAttachmentStats(String attachmentKey, BngAttachment attachment) {
53 if (attachment != null) {
54 BngStatsService bngStatsService = AbstractShellCommand.get(BngStatsService.class);
55 print("MAC: " + attachment.macAddress().toString()
56 + "\nC_TAG: " + attachment.cTag().toShort()
57 + "\nS_TAG: " + attachment.sTag().toString()
58 + "\nIP: " + attachment.ipAddress());
59 bngStatsService.getStats(attachmentKey).entrySet().stream().sorted(comparingByKey())
60 .forEach(
61 (entry) -> {
62 print(BngCliUtils.niceCounterName(entry.getKey()));
63 print("\tPackets:" + entry.getValue().packets());
64 print("\tBytes:\t" + entry.getValue().bytes());
65 }
66 );
67 }
68 }
69}