summaryrefslogtreecommitdiffstats
path: root/dblib/provider/src/main/java/org/onap/ccsdk/sli/core/dblib/EncShellCommand.java
blob: eaa5700c985c4b2943a3c50bfc349a831e7cac37 (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
package org.onap.ccsdk.sli.core.dblib;

import java.lang.reflect.Method;

/**
 * https://karaf.apache.org/manual/latest-2.x/developers-guide/extending-console.html
 * https://github.com/apache/karaf/tree/master/shell/console/src/main/java/org/apache/felix/gogo/commands
 */
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.apache.karaf.shell.console.OsgiCommandSupport;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Command(scope = "dblib", name = "encode", description="Says hello")
public class EncShellCommand extends OsgiCommandSupport {
    private static Logger LOGGER = LoggerFactory.getLogger(EncShellCommand.class);

    @Argument(index = 0, name = "arg", description = "The command argument", required = true, multiValued = false)
    String arg = null;

    @Override
    protected Object doExecute() throws Exception {
        System.out.println(String.format("Original value: %s", arg));
        System.out.println(String.format("Encrypted value: %s", encrypt(arg)));
        return null;
    }

    private String encrypt(String value) {
        try {
            BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();

            ServiceReference sref = bctx.getServiceReference("org.opendaylight.aaa.encrypt.AAAEncryptionService");
            Object encrSvc = bctx.getService(sref);

            Method gs2Method = encrSvc.getClass().getMethod("encrypt", new Class[] { "".getClass() });
            Object unmasked = gs2Method.invoke(encrSvc, new Object[] { value });
            return String.format("ENC:%s", unmasked.toString());

        } catch (Exception exc) {
            LOGGER.error("Failure", exc);
            return value;
        }
    }
}