blob: 3ef4a4348cd9bd5cf1f72a5a37ea7a0baaeb8b2f [file] [log] [blame]
Mohammad Shahid0cf9c0e2017-08-09 15:58:19 +05301/*
2 * Copyright 2017-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 */
16package org.onosproject.gluon.rsc.cli;
17
18import org.apache.karaf.shell.commands.Command;
19import org.apache.karaf.shell.commands.Option;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.gluon.manager.GluonManager;
22import org.onosproject.gluon.rsc.GluonServer;
23
24import java.util.Map;
25
26import static org.onosproject.gluon.manager.GluonManager.getAllServersIP;
27import static org.onosproject.gluon.rsc.GluonConstants.ACTIVE_SERVER;
28import static org.onosproject.gluon.rsc.GluonConstants.GLUON_DEFAULT_PORT;
29import static org.onosproject.gluon.rsc.GluonConstants.GLUON_HTTP;
30import static org.onosproject.gluon.rsc.GluonConstants.SERVER_POOL;
31
32/**
33 * Supports for querying Gluon Servers list and statistics.
34 */
35@Command(scope = "onos", name = "gluon-server-list",
36 description = "Gluon server list")
37public class GluonServerListCommand extends AbstractShellCommand {
38
39 @Option(name = "-i", aliases = "--server-ip",
40 description = "Supports for querying Gluon server statistics",
41 required = false, multiValued = false)
42 String ipAddress = null;
43
44 @Option(name = "-p", aliases = "--port", description = "Gluon server port",
45 required = false, multiValued = false)
46 String port = GLUON_DEFAULT_PORT;
47
48 protected Map<String, GluonServer> serverMap = getAllServersIP();
49
50 private static final String SERVER_STATISTICS =
51 "Server %s details:\nVersion: %s\nPort: %s\nReal time data:\n" +
52 "\tSet Statistics : %s\n\tDelete Statistics: %s\n" +
53 "Batch data:\n\tGet Statistics : %s";
54
55
56 @Override
57 public void execute() {
58 try {
59 String serverUrl = GLUON_HTTP + ipAddress + ":" + port;
60 if (ipAddress != null && checkServerPool(serverUrl)) {
61 for (Map.Entry<String,
62 GluonServer> server : serverMap.entrySet()) {
63
64 if (serverUrl.equals(server.getKey())) {
65 //Gets Etcd object reference
66 GluonServer gluonServer = server.getValue();
67 //Gets Etcd version from server list
68 print(SERVER_STATISTICS, ipAddress, gluonServer.version,
69 port, gluonServer.getSetCount(),
70 gluonServer.getDelCount(),
71 gluonServer.getGetCount());
72 }
73 }
74 } else {
75 int totalServers = GluonManager.getTotalServers();
76 log.info(ACTIVE_SERVER, totalServers);
77 print("Number of active servers: " + totalServers);
78 printServersIP();
79 }
80 } catch (Exception e) {
81 print(null, e.getMessage());
82 }
83 }
84
85 /**
86 * Prints all servers IPs in table format.
87 */
88 protected void printServersIP() {
89 int countServer = 1;
90 for (Map.Entry<String, GluonServer> server : serverMap.entrySet()) {
91 String serverUrl = server.getKey();
92 String[] serverIP = serverUrl.split("//");
93 print("Server %d: %s", countServer, serverIP[1]);
94 countServer++;
95 }
96 }
97
98 /**
99 * Returns boolean if given IP available in server pool.
100 *
101 * @param ipAddr Ip Address
102 * @return boolean
103 */
104 protected boolean checkServerPool(String ipAddr) {
105 boolean isServerAvailable;
106 if (serverMap.containsKey(ipAddr)) {
107 isServerAvailable = true;
108 } else {
109 print(SERVER_POOL);
110 isServerAvailable = false;
111 }
112 return isServerAvailable;
113 }
114}