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.rsc.GluonServer; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.net.InetAddress; |
| 25 | import java.net.URL; |
| 26 | import java.net.URLConnection; |
| 27 | import java.util.regex.Matcher; |
| 28 | import java.util.regex.Pattern; |
| 29 | |
| 30 | import static org.onosproject.gluon.manager.GluonManager.createServer; |
| 31 | import static org.onosproject.gluon.rsc.GluonConstants.GLUON_DEFAULT_PORT; |
| 32 | import static org.onosproject.gluon.rsc.GluonConstants.GLUON_HTTP; |
| 33 | import static org.onosproject.gluon.rsc.GluonConstants.INVALID_MODE; |
| 34 | import static org.onosproject.gluon.rsc.GluonConstants.INVALID_RANGE; |
| 35 | import static org.onosproject.gluon.rsc.GluonConstants.KEY_TYPE; |
| 36 | import static org.onosproject.gluon.rsc.GluonConstants.MODE_START; |
| 37 | import static org.onosproject.gluon.rsc.GluonConstants.MODE_STOP; |
| 38 | import static org.onosproject.gluon.rsc.GluonConstants.NO_SERVER_AVAIL; |
| 39 | import static org.onosproject.gluon.rsc.GluonConstants.NO_SERVER_AVAIL_ON_PORT; |
| 40 | import static org.onosproject.gluon.rsc.GluonConstants.PROTON_KEY_SUPPORT; |
| 41 | import static org.onosproject.gluon.rsc.GluonConstants.WRONG_INPUT; |
| 42 | import static org.onosproject.gluon.rsc.GluonConstants.WRONG_INPUT_TYPE; |
| 43 | import static org.onosproject.gluon.rsc.GluonConstants.WRONG_IP_FORMAT; |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * To monitor Gluon etcd server. |
| 48 | */ |
| 49 | @Command(scope = "onos", name = "gluon", |
| 50 | description = "Support for reading Gluon data via etcd client") |
| 51 | public class GluonServerCommand extends AbstractShellCommand { |
| 52 | |
| 53 | @Option(name = "-m", aliases = "--mode", |
| 54 | description = "Gluon server monitoring mode: start; stop", |
| 55 | required = false, multiValued = false) |
| 56 | String mode = MODE_START; |
| 57 | |
| 58 | @Option(name = "-i", aliases = "--server-ip", |
| 59 | description = "Gluon server ip address", |
| 60 | required = true, multiValued = false) |
| 61 | String ipAddress = null; |
| 62 | |
| 63 | @Option(name = "-p", aliases = "--port", description = "Gluon server port", |
| 64 | required = false, multiValued = false) |
| 65 | String port = GLUON_DEFAULT_PORT; |
| 66 | |
| 67 | @Option(name = "-k", aliases = "--key", |
| 68 | description = "Proton key : net-l3vpn", |
| 69 | required = false, multiValued = false) |
| 70 | String protonKey = KEY_TYPE; |
| 71 | |
| 72 | public String version = null; |
| 73 | |
| 74 | @Override |
| 75 | public void execute() { |
| 76 | try { |
| 77 | if (ipAddress != null && isValidIP(ipAddress) && isValidPort(port) |
| 78 | && isValidMode(mode) && isValidProtonKey(protonKey) |
| 79 | && isSeverReachable()) { |
| 80 | String url = GLUON_HTTP + ipAddress + ":" + port; |
| 81 | if (isEtcdSeverAvailable()) { |
| 82 | //Gets gluon server running version |
| 83 | version = gluonServerVersion(); |
| 84 | createServer(url, protonKey, mode, version); |
| 85 | } else { |
| 86 | log.info(NO_SERVER_AVAIL_ON_PORT); |
| 87 | return; |
| 88 | } |
| 89 | } else { |
| 90 | log.info(WRONG_INPUT); |
| 91 | } |
| 92 | } catch (Exception e) { |
| 93 | print(null, e.getMessage()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns boolean if given IP format is valid. |
| 99 | * |
| 100 | * @param ipAddr Ip Address |
| 101 | * @return boolean |
| 102 | */ |
| 103 | public boolean isValidIP(String ipAddr) { |
| 104 | boolean isIPaddrValid; |
| 105 | Pattern pattern = Pattern.compile("^(\\d{1,3})\\" + |
| 106 | ".(\\d{1,3})\\" + |
| 107 | ".(\\d{1,3})\\.(\\d{1,3})$"); |
| 108 | Matcher matcher = pattern.matcher(ipAddr); |
| 109 | if (matcher.find()) { |
| 110 | isIPaddrValid = true; |
| 111 | } else { |
| 112 | print(WRONG_IP_FORMAT); |
| 113 | isIPaddrValid = false; |
| 114 | } |
| 115 | return isIPaddrValid; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns boolean if given port value is valid. |
| 120 | * |
| 121 | * @param portValue port number |
| 122 | * @return boolean |
| 123 | */ |
| 124 | public boolean isValidPort(String portValue) { |
| 125 | boolean isPortValid = false; |
| 126 | try { |
| 127 | Integer portNum = Integer.parseInt(portValue); |
| 128 | if (portNum >= 0 && portNum <= 65535) { |
| 129 | isPortValid = true; |
| 130 | } else { |
| 131 | print(INVALID_RANGE); |
| 132 | isPortValid = false; |
| 133 | } |
| 134 | } catch (NumberFormatException nfe) { |
| 135 | print(WRONG_INPUT_TYPE); |
| 136 | } |
| 137 | return isPortValid; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns boolean if given mode is valid. |
| 142 | * |
| 143 | * @param mode server mode |
| 144 | * @return boolean |
| 145 | */ |
| 146 | public boolean isValidMode(String mode) { |
| 147 | boolean isValidMode; |
| 148 | if (mode.equalsIgnoreCase(MODE_START) || |
| 149 | mode.equalsIgnoreCase(MODE_STOP)) { |
| 150 | isValidMode = true; |
| 151 | } else { |
| 152 | print(INVALID_MODE); |
| 153 | isValidMode = false; |
| 154 | } |
| 155 | return isValidMode; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns boolean if given mode is valid. |
| 160 | * |
| 161 | * @param key key |
| 162 | * @return boolean |
| 163 | */ |
| 164 | public boolean isValidProtonKey(String key) { |
| 165 | boolean isValidProtonKey = true; |
| 166 | if (!KEY_TYPE.equalsIgnoreCase(key)) { |
| 167 | print(PROTON_KEY_SUPPORT); |
| 168 | isValidProtonKey = false; |
| 169 | } |
| 170 | return isValidProtonKey; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns version of gluon server. |
| 175 | * |
| 176 | * @return String |
| 177 | */ |
| 178 | |
| 179 | public String gluonServerVersion() { |
| 180 | String serverUrl = GLUON_HTTP + this.ipAddress + ":" + |
| 181 | this.port + "/version"; |
| 182 | GluonServer gluonServer = new GluonServer(); |
| 183 | String gluonversion = gluonServer.getGluonServerVersion(serverUrl); |
| 184 | String[] versionArray = gluonversion.split("\\."); |
| 185 | version = versionArray[0]; |
| 186 | return version; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns reachability of Gluon server. |
| 191 | * |
| 192 | * @return isSeverReachable |
| 193 | */ |
| 194 | public boolean isSeverReachable() { |
| 195 | boolean isSeverReachable = false; |
| 196 | try { |
| 197 | InetAddress inet = InetAddress.getByName(ipAddress); |
| 198 | if (inet.isReachable(5000)) { |
| 199 | isSeverReachable = true; |
| 200 | } else { |
| 201 | isSeverReachable = false; |
| 202 | print(NO_SERVER_AVAIL); |
| 203 | } |
| 204 | } catch (IOException e) { |
| 205 | isSeverReachable = false; |
| 206 | log.error("Check server process is failed with {} ", |
| 207 | e.getMessage()); |
| 208 | } |
| 209 | return isSeverReachable; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Returns availability of Gluon server. |
| 214 | * |
| 215 | * @return isServerAvailable |
| 216 | */ |
| 217 | public boolean isEtcdSeverAvailable() { |
| 218 | String serverUrl = GLUON_HTTP + ipAddress + ":" + port; |
| 219 | boolean isServerAvailable; |
| 220 | try { |
| 221 | URL url = new URL(serverUrl); |
| 222 | URLConnection connection = url.openConnection(); |
| 223 | connection.connect(); |
| 224 | isServerAvailable = true; |
| 225 | } catch (IOException e) { |
| 226 | print(NO_SERVER_AVAIL_ON_PORT); |
| 227 | isServerAvailable = false; |
| 228 | } |
| 229 | return isServerAvailable; |
| 230 | } |
| 231 | } |