summaryrefslogtreecommitdiffstats
path: root/sms-client/java/src/main/example/SmsClientExample.java
blob: ca38adb26db7da6f78f53e5964fb8ec16ec33440 (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
/*
 * Copyright 2018 Intel Corporation, Inc
 *
 * 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.
 */

import java.io.FileInputStream;
import java.lang.Boolean;
import java.lang.Integer;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
import java.security.KeyStore;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import java.util.HashMap;
import java.util.Map;
import org.onap.aaf.sms.SmsClient;
import org.onap.aaf.sms.SmsResponse;

/*
 * Sample application demonstrating various operations related
 * Secret Management Service's APIs
 */

public class SmsClientExample {
    public static void main(String[] args) throws Exception {
        // Set up the Sun PKCS 11 provider
        Provider p = Security.getProvider("SunPKCS11-pkcs11Test");
        if (p==null) {
            throw new RuntimeException("could not get security provider");
        }

        // Load the key store
        char[] pin = "45789654".toCharArray();
        KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
        keyStore.load(null, pin);

        // Load the CA certificate
        FileInputStream tst = new FileInputStream("/ca.jks");
        KeyStore trustStore = KeyStore.getInstance("JKS");
        trustStore.load(tst, pin);

        KeyManagerFactory keyManagerFactory =
             KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        //Add to keystore to key manager
        keyManagerFactory.init(keyStore, pin);

        TrustManagerFactory trustManagerFactory =
             TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        //Create the context
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keyManagerFactory.getKeyManagers(),
             trustManagerFactory.getTrustManagers(), new SecureRandom());
        //Create a socket factory
        SSLSocketFactory ssf = context.getSocketFactory();
        SmsClient sms = new SmsClient("onap.mydomain.com", 10443, ssf);
        SmsResponse resp1 = sms.createDomain("onap.new.test.sms0");
        if ( resp1.getSuccess() ) {
            System.out.println(resp1.getResponse());
            System.out.println(resp1.getResponseCode());
        }
        Map<String, Object> m1 = new HashMap<String, Object>();
        m1.put("passwd", "gax6ChD0yft");
        SmsResponse resp2 = sms.storeSecret("onap.new.test.sms0", "testsec",  m1);
        if ( resp2.getSuccess() ) {
            System.out.println(resp2.getResponse());
            System.out.println(resp2.getResponseCode());
        }
        Map<String, Object> m2 = new HashMap<String, Object>();
        m2.put("username", "dbuser");
        m2.put("isadmin", new Boolean(true));
        m2.put("age", new Integer(40));
        m2.put("secretkey", "asjdhkuhioeukadfjsadnfkjhsdukfhaskdjhfasdf");
        m2.put("token", "2139084553458973452349230849234234908234342");
        SmsResponse resp3 = sms.storeSecret("onap.new.test.sms0","credentials", m2);
        if ( resp3.getSuccess() ) {
            System.out.println(resp3.getResponse());
            System.out.println(resp3.getResponseCode());
        }
        SmsResponse resp4 = sms.getSecretNames("onap.new.test.sms0");
        if ( resp4.getSuccess() ) {
            System.out.println(resp4.getResponse());
            System.out.println(resp4.getResponseCode());
        }
        SmsResponse resp5= sms.getSecret("onap.new.test.sms0", "testsec");
        if ( resp5.getSuccess() ) {
            System.out.println(resp5.getResponse());
            System.out.println(resp5.getResponseCode());
        }
        SmsResponse resp6= sms.getSecret("onap.new.test.sms0", "credentials");
        if ( resp6.getSuccess() ) {
            Boolean b = (Boolean)resp6.getResponse().get("isadmin");
            if ( b )
                System.out.println("Age=" + (Integer)resp6.getResponse().get("age"));
            System.out.println(resp6.getResponse());
            System.out.println(resp6.getResponseCode());
        }
        SmsResponse resp7=sms.deleteDomain("onap.new.test.sms0");
        if ( resp7.getSuccess() ) {
            System.out.println(resp7.getResponse());
            System.out.println(resp7.getResponseCode());
        }
    }
}