blob: e5fdab716b2b415d1b4545c14f8ce81c1a9ea16b [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.Option;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Mohammad Shahid0cf9c0e2017-08-09 15:58:19 +053021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.gluon.rsc.GluonServer;
23
24import java.io.IOException;
25import java.net.InetAddress;
26import java.net.URL;
27import java.net.URLConnection;
28import java.util.regex.Matcher;
29import java.util.regex.Pattern;
30
31import static org.onosproject.gluon.manager.GluonManager.createServer;
32import static org.onosproject.gluon.rsc.GluonConstants.GLUON_DEFAULT_PORT;
33import static org.onosproject.gluon.rsc.GluonConstants.GLUON_HTTP;
34import static org.onosproject.gluon.rsc.GluonConstants.INVALID_MODE;
35import static org.onosproject.gluon.rsc.GluonConstants.INVALID_RANGE;
36import static org.onosproject.gluon.rsc.GluonConstants.KEY_TYPE;
37import static org.onosproject.gluon.rsc.GluonConstants.MODE_START;
38import static org.onosproject.gluon.rsc.GluonConstants.MODE_STOP;
39import static org.onosproject.gluon.rsc.GluonConstants.NO_SERVER_AVAIL;
40import static org.onosproject.gluon.rsc.GluonConstants.NO_SERVER_AVAIL_ON_PORT;
41import static org.onosproject.gluon.rsc.GluonConstants.PROTON_KEY_SUPPORT;
42import static org.onosproject.gluon.rsc.GluonConstants.WRONG_INPUT;
43import static org.onosproject.gluon.rsc.GluonConstants.WRONG_INPUT_TYPE;
44import static org.onosproject.gluon.rsc.GluonConstants.WRONG_IP_FORMAT;
45
46
47/**
48 * To monitor Gluon etcd server.
49 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050@Service
Mohammad Shahid0cf9c0e2017-08-09 15:58:19 +053051@Command(scope = "onos", name = "gluon",
52 description = "Support for reading Gluon data via etcd client")
53public 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 Milkey86ad7bb2018-09-27 12:32:28 -070077 protected void doExecute() {
Mohammad Shahid0cf9c0e2017-08-09 15:58:19 +053078 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}