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