summaryrefslogtreecommitdiffstats
path: root/site-manager/src/main/java/org/onap/policy/common/sitemanager/Main.java
blob: 876e6e06fddbed345df9511970d1095e13fa133d (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*-
 * ============LICENSE_START=======================================================
 * site-manager
 * ================================================================================
 * 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.onap.policy.common.sitemanager;

/*
 * Site Manager argument list:
 *
 * none - dump help information
 * show - dump information about all nodes 
 *		([site, nodetype, resourceName],
 *			adminState, opState, availStatus, standbyStatus)
 *			The first 3 determine the sort order.
 * setAdminState [ -s <site> | -r <resourceName> ] <new-state>
 * lock [ -s <site> | -r <resourceName> ]
 * unlock [ -s <site> | -r <resourceName> ]
 */

import java.io.File;
import java.io.FileInputStream;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.Persistence;
import javax.management.JMX;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.onap.policy.common.im.jpa.ResourceRegistrationEntity;
import org.onap.policy.common.im.jpa.StateManagementEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.onap.policy.common.im.jmx.ComponentAdminMBean;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 * This class contains the main entry point for Site Manager.
 */
public class Main
{
	private static final Logger logger = LoggerFactory.getLogger(Main.class.getName());
	
  // table mapping 'resourceName' to 'StateManagmentEntity'
  static HashMap<String, StateManagementEntity> stateManagementTable =
	new HashMap<String, StateManagementEntity>();

  // table mapping 'resourceName' to 'StateManagmentEntity'
  static HashMap<String, ResourceRegistrationEntity> resourceRegistrationTable =
	new HashMap<String, ResourceRegistrationEntity>();

  /**
   * Print out help information regarding command arguments.
   */
  private static void help()
  {
	System.out.print
	  ("Usage:\n"
	   + "    siteManager show [ -s <site> | -r <resourceName> ] :\n"
	   + "        display node information\n"
	   + "    siteManager setAdminState { -s <site> | -r <resourceName> }"
	   + " <new-state> :\n"
	   + "        update admin state on selected nodes\n"
	   + "    siteManager lock { -s <site> | -r <resourceName> } :\n"
	   + "        lock selected nodes\n"
	   + "    siteManager unlock { -s <site> | -r <resourceName> } :\n"
	   + "        unlock selected nodes\n");
  }

  /**
   * Print out help information regarding the properties file.
   *
   * @param propertiesFileName the path to the properties file
   */
  private static void helpProperties(String propertiesFileName)
  {
	if (propertiesFileName == null)
	  {
		// file name not specified (missing system property)
		System.out.print
		  ("'siteManager' needs to be passed the system property\n"
		   + "'siteManager.properties', which is the file name of a\n"
		   + "properties file containing database access information\n\n");
	  }
	else
	  {
		File file = new File(propertiesFileName);
		if (!file.exists())
		  {
			// file name specified, but does not exist
			System.out.print
			  ("Properties file '" + file.getAbsolutePath()
			   + "' does not exist.\n\n");
		  }
		else
		  {
			// file name specified and does exist -- presumably, the
			// problem is with one or more properties
			System.out.print
			  ("One or more missing properties in\n'" + file.getAbsolutePath()
			   + "'.\n\n");
		  }
	  }

	System.out.print
	  ("The following properties need to be specified:\n\n"
	   + "    javax.persistence.jdbc.driver -"
	   + " typically 'org.mariadb.jdbc.Driver'\n"
	   + "    javax.persistence.jdbc.url - URL referring to the database,\n"
	   + "        which typically has the form:"
	   + " 'jdbc:mariadb://<host>:<port>/<db>'\n"
	   + "        ('<db>' is probably 'xacml' in this case)\n"
	   + "    javax.persistence.jdbc.user - the user id for accessing the"
	   + " database\n"
	   + "    javax.persistence.jdbc.password - password for accessing the"
	   + " database\n");
  }

  /**
   * This is the main entry point
   *
   * @param args these are command-line arguments to 'siteManager'
   */
  public static void main(String... args)
  {
	Options options = new Options();
	options.addOption("s", true, "specify site");
	options.addOption("r", true, "specify resource name");
	options.addOption("h", false, "display help");
	options.addOption("?", false, "display help");

	// parse options
	CommandLineParser parser = new DefaultParser();
	CommandLine cmd = null;

	try
	  {
		cmd = parser.parse(options, args);
	  }
	catch (ParseException e)
	  {
		System.out.println(e.getMessage());
		help();
		System.exit(1);
	  }

	if (cmd == null || cmd.getOptionValue('h') != null || cmd.getOptionValue('?') != null)
	  {
		help();
		System.exit(0);
	  }

	// fetch options, and remaining arguments
	String sOption = cmd.getOptionValue('s');
	String rOption = cmd.getOptionValue('r');
	List<String> argList = cmd.getArgList();

	// a number of commands require either the '-r' option or '-s' option
	boolean optionLetterSpecified = (rOption != null || sOption != null);

	// used to accumulate any error messages that are generated
	StringBuilder error = new StringBuilder();

	// first non-option argument
	String arg0 = null;

	if (argList.size() == 0)
	  {
		error.append("No command specified\n");
	  }
	else
	  {
		arg0 = argList.get(0);
		if ("show".equalsIgnoreCase(arg0))
		  {
			// show [ -s <site> | -r <resourceName> ]
			if (argList.size() != 1)
			  {
				error.append("show: Extra arguments\n");
			  }
		  }
		else if ("setAdminState".equalsIgnoreCase(arg0))
		  {
			// setAdminState { -s <site> | -r <resourceName> } <new-state>
			switch (argList.size())
			  {
			  case 1:
				error.append("setAdminState: Missing <new-state> value\n");
				break;
			  case 2:
				// this is expected
				break;
			  default:
				error.append("setAdminState: Extra arguments\n");
				break;
			  }
			if (!optionLetterSpecified)
			  {
				error.append
				  ("setAdminState: Either '-s' or '-r' option is needed\n");
			  }
		  }
		else if ("lock".equalsIgnoreCase(arg0))
		  {
			// lock { -s <site> | -r <resourceName> }
			if (argList.size() != 1)
			  {
				error.append("lock: Extra arguments\n");
			  }
			if (!optionLetterSpecified)
			  {
				error.append("lock: Either '-s' or '-r' option is needed\n");
			  }
		  }
		else if ("unlock".equalsIgnoreCase(arg0))
		  {
			// unlock { -s <site> | -r <resourceName> }
			if (argList.size() != 1)
			  {
				error.append("unlock: Extra arguments\n");
			  }
			if (!optionLetterSpecified)
			  {
				error.append("unlock: Either '-s' or '-r' option is needed\n");
			  }
		  }
		else
		  {
			error.append(arg0).append(": Unknown command\n");
		  }
	  }
	if (sOption != null && rOption != null)
	  {
		error
		  .append(arg0)
		  .append(":  'r' and 's' options are mutually exclusive\n");
	  }
	if (error.length() != 0)
	  {
		// if any errors have occurred, dump out the error string,
		// help information, and exit
		System.out.println(error.toString());
		help();
		System.exit(2);
	  }

	// read in properties used to access the database
	String propertiesFileName = System.getProperty("siteManager.properties");
	File propertiesFile = null;

	if (propertiesFileName == null
		|| !(propertiesFile = new File(propertiesFileName)).exists())
	  {
		helpProperties(propertiesFileName);
		System.exit(3);
	  }
	FileInputStream fis = null;
	Properties properties = new Properties();
	try
	  {
		fis = new FileInputStream(propertiesFile);
		properties.load(fis);
	  }
	catch (Exception e)
	  {
		System.out.println("Exception loading properties: " + e);
		helpProperties(propertiesFileName);
		System.exit(3);
	  }
	finally
	  {
		try
		  {
			fis.close();
		  }
		catch (Exception e)
		  {
			System.err.println(e);
		  }
	  }

	// verify that we have all of the properties needed
	if (properties.getProperty("javax.persistence.jdbc.driver") == null
		|| properties.getProperty("javax.persistence.jdbc.url") == null
		|| properties.getProperty("javax.persistence.jdbc.user") == null
		|| properties.getProperty("javax.persistence.jdbc.password") == null)
	  {
		// one or more missing properties
		helpProperties(propertiesFileName);
		System.exit(3);
	  }

	// access database through 'EntityManager'
	EntityManagerFactory emf =
	  Persistence.createEntityManagerFactory("operationalPU", properties);
	EntityManager em = emf.createEntityManager();

	// sQuery - used for StateManagementEntity table
	// rQuery - used for ResourceRegistrationEntity table
	Query sQuery, rQuery;

	if (rOption != null)
	  {
		// 'resourceName' specified -- both queries are limited to this
		// resource
		sQuery = em.createQuery("SELECT s FROM StateManagementEntity s"
								+ " WHERE s.resourceName = :resourceName")
		  .setParameter("resourceName", rOption);
		rQuery = em.createQuery("SELECT r FROM ResourceRegistrationEntity r"
								+ " WHERE r.resourceName = :resourceName")
		  .setParameter("resourceName", rOption);
	  }
	else if (sOption != null)
	  {
		// 'site' is specified -- 'ResourceRegistrationEntity' has a 'site'
		// field, but 'StateManagementEntity' does not
		sQuery = em.createQuery("SELECT s FROM StateManagementEntity s");
		rQuery = em.createQuery("SELECT r FROM ResourceRegistrationEntity r"
								+ " WHERE r.site = :site")
		  .setParameter("site", sOption);
	  }
	else
	  {
		// query all entries
		sQuery = em.createQuery("SELECT s FROM StateManagementEntity s");
		rQuery = em.createQuery("SELECT r FROM ResourceRegistrationEntity r");
	  }

	// perform 'StateManagementEntity' query, and place matching entries
	// in 'stateManagementTable'
	for (Object o : sQuery.getResultList())
	  {
		if (o instanceof StateManagementEntity)
		  {
			StateManagementEntity s = (StateManagementEntity) o;
			stateManagementTable.put(s.getResourceName(), s);
		  }
	  }

	// perform 'ResourceRegistrationQuery', and place matching entries
	// in 'resourceRegistrationTable' ONLY if there is also an associated
	// 'stateManagementTable' entry
	for (Object o : rQuery.getResultList())
	  {
		if (o instanceof ResourceRegistrationEntity)
		  {
			ResourceRegistrationEntity r = (ResourceRegistrationEntity) o;
			String resourceName = r.getResourceName();
			if (stateManagementTable.get(resourceName) != null)
			  {
				// only include entries that have a corresponding
				// state table entry -- silently ignore the rest
				resourceRegistrationTable.put(resourceName, r);
			  }
		  }
	  }

	if (resourceRegistrationTable.size() == 0)
	  {
		System.out.println(arg0 + ": No matching entries");
		System.exit(4);
	  }

	if ("setAdminState".equalsIgnoreCase(arg0))
	  {
		// update admin state on all of the nodes
		String adminState = argList.get(1);
		EntityTransaction et = em.getTransaction();
		et.begin();
		try
		  {
			// iterate over all matching 'ResourceRegistrationEntity' instances
			for (ResourceRegistrationEntity r :
				   resourceRegistrationTable.values())
			  {
				// we know the corresponding 'StateManagementEntity' exists --
				// 'ResourceRegistrationEntity' entries without a matching
				// 'StateManagementEntity' entry were not placed in the table
				StateManagementEntity s =
				  stateManagementTable.get(r.getResourceName());

				// update the admin state, and save the changes
				s.setAdminState(adminState);
				em.persist(s);
			  }
		  }
		finally
		  {
			// do the commit
			em.flush();
			et.commit();
		  }
	  }
	else if ("lock".equalsIgnoreCase(arg0) || "unlock".equalsIgnoreCase(arg0))
	  {
		// these use the JMX interface
		for (ResourceRegistrationEntity r :
			   resourceRegistrationTable.values())
		  {
			// lock or unlock the entity
			jmxOp(arg0, r);

			// change should be reflected in 'adminState'
			em.refresh(stateManagementTable.get(r.getResourceName()));
		  }
	  }

	// free connection to the database
	em.close();

	// display all entries
	display();
  }

  /**
   * Process a 'lock' or 'unlock' operation on a single
   * 'ResourceRegistrationEntity'
   *
   * @param arg0 this is the string "lock" or "unlock"
   * @param r this is the ResourceRegistrationEntity to lock or unlock
   */
  static void jmxOp(String arg0, ResourceRegistrationEntity r)
  {
	String resourceName = r.getResourceName();
	String jmxUrl = r.getResourceUrl();
	if (jmxUrl == null)
	  {
		System.out.println(arg0 + ": no resource URL for '"
						   + resourceName + "'");
		return;
	  }

	JMXConnector connector = null;
	try
	  {
		connector = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl));
		ComponentAdminMBean admin = JMX.newMXBeanProxy
		  (connector.getMBeanServerConnection(),
		   new ObjectName("ONAP_POLICY_COMP:name=" + resourceName),
		   ComponentAdminMBean.class);

		if ("lock".equals(arg0))
		  {
			admin.lock();
		  }
		else
		  {
			admin.unlock();
		  }
	  }
	catch (Exception e)
	  {
		// e.printStackTrace();
		System.out.println(arg0 + " failed for '" + resourceName + "': " + e);
	  }
	finally
	  {
		if (connector != null)
		  {
			try
			  {
				connector.close();
			  }
			catch (Exception e)
			  {
				System.err.println(e);
			  }
		  }
	  }
  }

  /**
   * Compare two strings, either of which may be null
   *
   * @param s1 the first string
   * @param s2 the second string
   * @return a negative value if s1<s2, 0 if they are equal,
   *	and positive if s1>s2
   */
  static private int stringCompare(String s1, String s2)
  {
	return ((s1 == null) ?
			(s2 == null ? 0 : -1) :
			(s2 == null ? 1 : s1.compareTo(s2)));
  }

  /**
   * Update an array of 'length' fields using an array of Strings, any of
   * which may be 'null'. This method is used to determine the field width
   * of each column in a tabular dump.
   *
   * @param current this is an array of length 7, containing the current
   *	maximum lengths of each column in the tabular dump
   * @param s this is an array of length 7, containing the current String
   *	entry for each column
   */
  static private void updateLengths(int[] current, String[] s)
  {
	for (int i = 0 ; i < 7 ; i += 1)
	  {
		String str = s[i];
		int newLength = (str == null ? 4 : str.length());
		if (current[i] < newLength)
		  {
			// this column needs to be expanded
			current[i] = newLength;
		  }
	  }
  }

  /**
   * Ordered display -- dump out all of the entries, in 
   */
  static void display()
  {
	TreeSet<String[]> treeset = new TreeSet<String[]>
	  (new Comparator<String[]>()
	  {
		public int compare(String[] r1, String[] r2)
		  {
			int rval = 0;

			// the first 3 columns are 'Site', 'NodeType', and 'ResourceName',
			// and are used to sort the entries
			for (int i = 0 ; i < 3 ; i += 1)
			  {
				if ((rval = stringCompare(r1[i], r2[i])) != 0)
				  break;
			  }
			return(rval);
		  }
	  });

	String[] labels = new String[]
	  {"Site", "NodeType", "ResourceName",
	   "AdminState", "OpState", "AvailStatus", "StandbyStatus"};
	String[] underlines = new String[]
	  {"----", "--------", "------------",
	   "----------", "-------", "-----------", "-------------"};

	// each column needs to be at least wide enough to fit the column label
	int lengths[] = new int[7];
	updateLengths(lengths, labels);

	// Go through the 'resourceRegistrationTable', and generate the
	// associated table row. Maximum column widths are updated, and the
	// entry is inserted into tree, which has the effect of sorting the
	// entries.
	for (ResourceRegistrationEntity r : resourceRegistrationTable.values())
	  {
		StateManagementEntity s =
		  stateManagementTable.get(r.getResourceName());

		// these are the entries to be displayed for this row
		String[] values = new String[]
		  {
			r.getSite(), r.getNodeType(), r.getResourceName(),
			s.getAdminState(), s.getOpState(),
			s.getAvailStatus(), s.getStandbyStatus()
		  };

		treeset.add(values);
		updateLengths(lengths, values);
	  }

	// generate format string
	StringBuilder sb = new StringBuilder();
	for (int i = 0 ; i < 7 ; i += 1)
	  {
		sb.append('%').append(i+1).append("$-")
		  .append(lengths[i]).append("s ");
	  }
	sb.setCharAt(sb.length()-1, '\n');
	String formatString = sb.toString();

	// display column headers
	logger.info(formatString, (Object[])labels);
	System.out.printf(formatString, (Object[])underlines);

	// display all of the rows
	for (String[] values : treeset)
	  {
		System.out.printf(formatString, (Object[])values);
	  }
  }
}