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