Mohammad Shahid | 0cf9c0e | 2017-08-09 15:58:19 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.gluon.rsc.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Command; |
| 19 | import org.apache.karaf.shell.commands.Option; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.gluon.manager.GluonManager; |
| 22 | import org.onosproject.gluon.rsc.GluonServer; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import static org.onosproject.gluon.manager.GluonManager.getAllServersIP; |
| 27 | import static org.onosproject.gluon.rsc.GluonConstants.ACTIVE_SERVER; |
| 28 | import static org.onosproject.gluon.rsc.GluonConstants.GLUON_DEFAULT_PORT; |
| 29 | import static org.onosproject.gluon.rsc.GluonConstants.GLUON_HTTP; |
| 30 | import 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") |
| 37 | public 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 | } |