aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/util/AAIPrimaryHost.java
blob: 80fbbd3ab13ce0d18758f28b667eb13ec7185b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.aai.util;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.UUID;

import org.openecomp.aai.logging.AAILogger;
import org.openecomp.aai.logging.LogLine;

import com.att.eelf.configuration.EELFLogger;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;


/**
 * This class provides Logger methods for the AAI application 
 */

public class AAIPrimaryHost {
	
		protected static AAILogger aaiLogger = new AAILogger(AAIPrimaryHost.class.getName());
		protected LogLine logline = new LogLine();
		public EELFLogger logger;
		
		private final String isReachable = new String("isReachable");
		private final String binPing = new String("binPing");
		private final String netcat = new String("netcat");
		private final String echo = new String("echo");
		private final String DEFAULT_CHECK = new String("aai.primary.filetransfer.");
		private String transId = UUID.randomUUID().toString();
		private String fromAppId = "AAI-INIT";
		

		/**
		 * Instantiates a new AAI primary host.
		 *
		 * @param transId the trans id
		 * @param fromAppId the from app id
		 */
		public AAIPrimaryHost(String transId, String fromAppId) {
			this.fromAppId = fromAppId;
			this.transId = transId;
		}

		/**
		 * Do command.
		 *
		 * @param command the command
		 * @return the int
		 * @throws Exception the exception
		 */
		public int doCommand(List<String> command)  
				  throws Exception 
		{ 
			String s = null; 
				      
			ProcessBuilder pb = new ProcessBuilder(command); 
			Process process = pb.start(); 
				  
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); 
			BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
				  
				    // read the output from the command 
			//System.out.println("Here is the standard output of the command:\n"); 
			while ((s = stdInput.readLine()) != null) 
			{ 
				      //System.out.println(s); 
				aaiLogger.debug(logline, "stdout "+ s );
			} 
				  
				    // read any errors from the attempted command 
			System.out.println("Here is the standard error of the command (if any):\n"); 
			while ((s = stdError.readLine()) != null) 
			{ 
				      //System.out.println(s); 
				aaiLogger.debug(logline, "stderrbbbbbb "+ s );
			} 
			return process.waitFor();
		} 
				    


		/**
		 * Method amIPrimary.
		 * @return boolean
		 */
		
		public boolean amIPrimary() 
		{
			return amIPrimary( DEFAULT_CHECK );
		}
		
		/**
		 * Am I primary using echo.
		 *
		 * @param hostname the hostname
		 * @param aaiPrimaryCheck the aai primary check
		 * @param aaiServerList the aai server list
		 * @return true, if successful
		 */
		public boolean amIPrimaryUsingEcho( String hostname, String aaiPrimaryCheck, String aaiServerList) {
			
			String methodName = "amIPrimaryUsingEcho";
			
			logline.init("aaigen", transId, fromAppId, methodName);
			
			StringTokenizer st = new StringTokenizer( aaiServerList, "|" );
        	String host;
        	String msg = null;
        	while ( st.hasMoreTokens() ) {
        		host = st.nextToken();
        				
				String portWithEndpoint = aaiPrimaryCheck.substring(aaiPrimaryCheck.indexOf(":"));
				//System.out.println( "using portEndpoint: " + portWithEndpoint);
				Client client = null;
				
				try {
					client = HttpsAuthClient.getTwoWaySSLClient();
				}
				catch (KeyManagementException kme){
					msg = "KeyManagementException in REST call to echo: " + kme.toString();
				} catch (Exception e) {
					msg = " Exception in REST call to echo: " + e.toString();
				}
				
				if ( msg != null ) {
		    		logline.add( "problem using echo " + msg + " no primary found ",  aaiServerList );
		    		aaiLogger.info(logline, false, "AAI_7401");
					return false;
				}
				String resource = "https://" + host + portWithEndpoint;
				WebResource webResource = client
				   .resource(resource);
			

				
				try {
					ClientResponse response = webResource.accept("application/json")
					         .header("X-TransactionId", transId)
					         .header("X-FromAppId",  "PrimaryCheck")
			                   .get(ClientResponse.class);
			
					String output = response.getEntity(String.class);
				
					if (response.getStatus() != 200) {
						aaiLogger.debug(logline, "echo status " + response.getStatus() + " echo response  "+ output );
						logline.add( "unexpected status ", response.getStatus() );
						aaiLogger.info(logline, false, "AAI_7402");
					} else {
						aaiLogger.debug(logline, "echo response from server  "+ output );
						if ( host.contains(hostname)) {
							return true;
						}
						return false;
					}
				} catch ( Exception e) {
					logline.add( "exception checking primary ", resource );
					aaiLogger.info(logline, false, "AAI_4000", e);
				}
			}
    		logline.add( "no primary found ",  aaiServerList );
    		aaiLogger.info(logline, false, "AAI_4000");
			return false;
		}
		
		/**
		 * Am I primary.
		 *
		 * @param checkName the check name
		 * @return true, if successful
		 */
		public boolean amIPrimary(String checkName) 
		{
			String methodName = "amIPrimary";
			
			logline.init("aaigen", transId, fromAppId, methodName);
			String aaiServerList = null;
			String aaiPrimaryCheck = null;
			String aaiPingTimeout = null;
			String aaiPingCount = null;
			
			int timeout = -1;
			
			String msg = null;
			
			try {
				
				aaiServerList = AAIConfig.get(checkName + "serverlist");
				//aaiPrimaryCheck = isReachable;
				aaiPrimaryCheck = AAIConfig.get(checkName + "primarycheck");
				//String aaiPingTimeout = "5000";
				aaiPingTimeout = AAIConfig.get(checkName + "pingtimeout");
				timeout = (new Integer(aaiPingTimeout)).intValue();
				//aaiPingCount = "5";
				aaiPingCount = AAIConfig.get(checkName + "pingcount");
			} catch ( Exception e ) {
				msg = "missing aaiconfig.properties for primary check, no check is done";
        		logline.add( "unable to check primary ", msg );
        		aaiLogger.info(logline, true, "0");
        		return true;
			}
			
			aaiLogger.debug(logline, "checking primary  "+ aaiPrimaryCheck + " on " + aaiServerList + " timeout " + timeout );
			
			InetAddress ip;
	    	String hostname = null;
	    	
        	try {
				ip = InetAddress.getLocalHost();
	        	if ( ip != null ) {
	        		hostname = ip.getHostName();
	        		if ( hostname != null ) {
	        			if  ( !( aaiServerList.contains(hostname) ) )
	        				msg = "host name not found in server list " + hostname;
	        		} else
	        			msg = "InetAddress returned null hostname";
	        	} 
			} catch (UnknownHostException e) {
				
				e.printStackTrace();
				msg = "InetAddress getLocalHost exception " + e.getMessage();
        	} 
        	if ( ( msg != null ) && !aaiPrimaryCheck.startsWith( "echo1") ) { //for unit testing
        		logline.add( "unable to check primary ", msg );
        		aaiLogger.info(logline, true, "0");
        		return true;
        	}
        	
        	if (aaiPrimaryCheck.startsWith(echo)) {
        		return amIPrimaryUsingEcho( hostname, aaiPrimaryCheck, aaiServerList );
        	}
        	StringTokenizer st = new StringTokenizer( aaiServerList, "|" );
        	String host;
        	while ( st.hasMoreTokens() ) {
        		host = st.nextToken();
        		try {
        			
        			if (  aaiPrimaryCheck.equals(isReachable)) {
        				ip = InetAddress.getByName(host);
        				if ( ip == null ) {
        	        		logline.add( "getByName failed ", host );
        	        		aaiLogger.info(logline, true, "0");
        	        		return true;
        				}
        				if  ( ip.isReachable(timeout) ) {
        					aaiLogger.debug(logline, "primary is  " + host );
        					if ( host.contains(hostname)) {
        		        		aaiLogger.info(logline, true, "0");
        						return true;
        					}
                    		aaiLogger.info(logline, false, "AAI_4000");
        					return false;
        				} else {
        					aaiLogger.debug(logline, "isReachable false for  "+ host );
        				}
        			} else if (  aaiPrimaryCheck.equals(binPing)) {
        			    List<String> commands = new ArrayList<String>(); 
        			    commands.add("/bin/ping"); 
        			    commands.add("-c"); 
        			    commands.add(aaiPingTimeout); 
        			    commands.add(host); 
        			    int pingResult = doCommand(commands);
        			    if ( pingResult == 0 ) {
        			    	if ( host.contains(hostname)) {
        		        		aaiLogger.info(logline, true, "0");
        			    		return true;
        			    	}
                    		aaiLogger.info(logline, false, "AAI_4000");
        					return false;	
        			    }else {
        					aaiLogger.debug(logline, "pingResult " + pingResult + "for "+ host );
        				}
        			} else if (  aaiPrimaryCheck.equals(netcat)) {
        			    List<String> commands = new ArrayList<String>(); 
        			    commands.add("/usr/bin/nc"); 
        			    commands.add("-w"); 
        			    commands.add(aaiPingCount); // seconds
        			    commands.add(host); 
        			    commands.add("22"); // SCP port
        			    int pingResult = doCommand(commands);
        			    if ( pingResult == 0 ) {
        			    	if ( host.contains(hostname)) {
        			    		aaiLogger.info(logline, true, "0");
        			    		return true;
        			    	}
                    		aaiLogger.info(logline, false, "AAI_4000");
        					return false;	
        			    }else {
        					aaiLogger.debug(logline, "netcat " + pingResult + "for "+ host );
        				}
        			}
        		
        		} catch ( Exception e) {
        			e.printStackTrace();
        			msg = "processing serverList for host " + host + " exception " + e.getMessage();
        			logline.add( "no server found ",  msg );
            		aaiLogger.info(logline, false, "AAI_4000");
            		return false;
        		}
        	}
    		logline.add( "no primary found ",  aaiServerList );
    		aaiLogger.info(logline, false, "AAI_4000");
        	return false;
		}
		
		/**
		 * Which is primary.
		 *
		 * @return the string
		 */
		public String whichIsPrimary() 
		{
			return whichIsPrimary( DEFAULT_CHECK );
		}
		
		/**
		 * Which is primary using echo.
		 *
		 * @param aaiPrimaryCheck the aai primary check
		 * @param aaiServerList the aai server list
		 * @return the string
		 */
		public String whichIsPrimaryUsingEcho( String aaiPrimaryCheck, String aaiServerList) {
			
			String methodName = "whichIsPrimaryUsingEcho";
			
			logline.init("aaigen", transId, fromAppId, methodName);
			
			StringTokenizer st = new StringTokenizer( aaiServerList, "|" );
        	String host;
        	String msg = null;
        	while ( st.hasMoreTokens() ) {
        		host = st.nextToken();
        				
				String portWithEndpoint = aaiPrimaryCheck.substring(aaiPrimaryCheck.indexOf(":"));
				//System.out.println( "using portEndpoint: " + portWithEndpoint);
				Client client = null;
				
				try {
					client = HttpsAuthClient.getTwoWaySSLClient();
				}
				catch (KeyManagementException kme){
					msg = "KeyManagementException in REST call to echo: " + kme.toString();
				} catch (Exception e) {
					msg = " Exception in REST call to echo: " + e.toString();
				}
				
				if ( msg != null ) {
		    		logline.add( "problem using echo " + msg + " no primary found ",  aaiServerList );
		    		aaiLogger.info(logline, false, "AAI_7401");
					return null;
				}
				String resource = "https://" + host + portWithEndpoint;
				WebResource webResource = client
				   .resource(resource);

				try {
					ClientResponse response = webResource.accept("application/json")
					         .header("X-TransactionId", transId)
					         .header("X-FromAppId",  "PrimaryCheck")
			                   .get(ClientResponse.class);
					String output = response.getEntity(String.class);
					if (response.getStatus() != 200) {
						aaiLogger.debug(logline, "echo status " + response.getStatus() + " echo response  "+ output );
						logline.add( "unexpected status ", response.getStatus() );
						aaiLogger.info(logline, false, "AAI_7402");
					} else {
						return host;
					}
				} catch ( Exception e ) {
					logline.add( "exception checking primary ", resource );
					aaiLogger.info(logline, false, "AAI_4000", e);
				}
			}
    		logline.add( "no primary found ",  aaiServerList );
    		aaiLogger.info(logline, false, "AAI_4000");
			return null;
		}
		
		/**
		 * Which is primary.
		 *
		 * @param checkName the check name
		 * @return the string
		 */
		public String whichIsPrimary( String checkName )
		{
			String methodName = "whichIsPrimary";
			
			logline.init("aaigen", transId, fromAppId, methodName);
			String aaiServerList = null;
			String aaiPrimaryCheck = null;
			String aaiPingTimeout = null;
			String aaiPingCount = null;
			
			int timeout = -1;
			
			String msg = null;
			
			try {
				
				aaiServerList = AAIConfig.get(checkName + "serverlist");
				aaiPrimaryCheck = AAIConfig.get(checkName + "primarycheck");

			} catch ( Exception e ) {
				msg = "missing aaiconfig.properties to find primary, returning";
	    		logline.add( "unable to find primary ", msg );
	    		aaiLogger.info(logline, true, "0");
	    		return null;
			}
			
			aaiLogger.debug(logline, "finding primary  "+ aaiPrimaryCheck + " on " + aaiServerList + " timeout " + timeout );
				
	    	return whichIsPrimaryUsingEcho( aaiPrimaryCheck, aaiServerList );
		}
}