ActivitySpec - Correcting logger messages
[sdc.git] / common / openecomp-common-configuration-management / openecomp-configuration-management-cli / src / main / java / org / openecomp / config / cli / app / Configuration.java
1 package org.openecomp.config.cli.app;
2
3 import org.openecomp.config.api.ConfigurationChangeListener;
4 import org.openecomp.config.api.ConfigurationManager;
5
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.management.JMX;
10 import javax.management.MBeanServerConnection;
11 import javax.management.ObjectName;
12 import javax.management.remote.JMXConnector;
13 import javax.management.remote.JMXConnectorFactory;
14 import javax.management.remote.JMXServiceURL;
15
16 /**
17  * The type Configuration.
18  */
19 public class Configuration {
20
21   /**
22    * The entry point of application.
23    *
24    * @param args the input arguments
25    * @throws Exception the exception
26    */
27   public static void main(String[] args) throws Exception {
28
29     String host = getValueFor("host", args);
30     String port = getValueFor("port", args);
31
32     if (host == null) {
33       host = "127.0.0.1";
34     }
35     if (port == null) {
36       port = "9999";
37     }
38     JMXServiceURL url =
39         new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi");
40     Map<String, String[]> env = new HashMap<>();
41     //populate dummy value. need to be changed as per impl.
42     String[] credentials = {"principal", "password"};
43     env.put(JMXConnector.CREDENTIALS, credentials);
44
45     try (JMXConnector jmxc = JMXConnectorFactory.connect(url, env)) {
46
47       boolean isUpdate = isKeyPresent("update", args);
48
49       Map<String, Object> input = new HashMap<>();
50       input.put("ImplClass", isUpdate ? "org.openecomp.config.type.ConfigurationUpdate"
51           : "org.openecomp.config.type.ConfigurationQuery");
52       input.put("externalLookup", isKeyPresent("lookup", args));
53       input.put("nodeOverride", isKeyPresent("nodeOverride", args));
54       input.put("nodeSpecific", isKeyPresent("nodeSpecific", args));
55       input.put("fallback", isKeyPresent("fallback", args));
56       input.put("latest", isKeyPresent("latest", args));
57       input.put("tenant", getValueFor("tenant", args));
58       input.put("namespace", getValueFor("namespace", args));
59       input.put("key", getValueFor("key", args));
60       input.put("value", getValueFor("value", args));
61
62
63       if (!isKeyPresent("list", args) && getValueFor("key", args) == null) {
64         throw new RuntimeException("Key is missing.");
65       }
66       if (isKeyPresent("update", args) && getValueFor("value", args) == null) {
67         throw new RuntimeException("Value is missing.");
68       }
69
70
71       MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
72       ObjectName mbeanName = new ObjectName("org.openecomp.jmx:name=SystemConfig");
73       org.openecomp.config.api.ConfigurationManager conf =
74           JMX.newMBeanProxy(mbsc, mbeanName, org.openecomp.config.api.ConfigurationManager.class,
75               true);
76
77       boolean isGet = isKeyPresent("get", args);
78       boolean isList = isKeyPresent("list", args);
79       if (isGet) {
80         System.out.println(conf.getConfigurationValue(input));
81       } else if (isList) {
82         Map<String, String> map = conf.listConfiguration(input);
83         for (Map.Entry<String, String> entry : map.entrySet()) {
84           System.out.println(entry.getKey() + " : " + entry.getValue());
85         }
86       } else if (isUpdate) {
87         conf.updateConfigurationValue(input);
88       }
89     }
90   }
91
92   private static boolean isSwitch(String key) {
93     return key.startsWith("-");
94   }
95
96   private static String getValueFor(String key, String[] args) {
97     for (int i = 0; i < args.length; i++) {
98       if (isSwitch(args[i])) {
99         String node = args[i].substring(1);
100         if (node.equalsIgnoreCase(key) && i < args.length - 1) {
101           return args[i + 1].trim().length() == 0 ? null : args[i + 1].trim();
102         }
103       }
104     }
105     return null;
106   }
107
108   private static boolean isKeyPresent(String key, String[] args) {
109     for (int i = 0; i < args.length; i++) {
110       if (isSwitch(args[i])) {
111         String node = args[i].substring(1);
112         if (node.equalsIgnoreCase(key)) {
113           return true;
114         }
115       }
116     }
117     return false;
118   }
119 }