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