aboutsummaryrefslogtreecommitdiffstats
path: root/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcListener.java
blob: 2d61927499dc7b1db8b03ae319a1ce973a2d9fa5 (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
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * 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.openecomp.appc.sdc.listener;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.openecomp.appc.configuration.Configuration;
import org.openecomp.appc.configuration.ConfigurationFactory;
import org.openecomp.sdc.api.IDistributionClient;
import org.openecomp.sdc.api.results.IDistributionClientResult;
import org.openecomp.sdc.impl.DistributionClientFactory;
import org.openecomp.sdc.utils.DistributionActionResultEnum;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;


public class AsdcListener {

    /**
     * The bundle context
     */
    private IDistributionClient client;
    private AsdcCallback callback;
    private AsdcConfig config;
    private CountDownLatch latch;


    private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcListener.class);

    @SuppressWarnings("unused")
    public void start() throws Exception {
        logger.info("Starting bundle ASDC Listener");
        Configuration configuration = ConfigurationFactory.getConfiguration();
        Properties props = configuration.getProperties();

        config = new AsdcConfig(props);

        client = DistributionClientFactory.createDistributionClient();
        callback = new AsdcCallback(config.getStoreOpURI(), client);

        latch = new CountDownLatch(1);

        new Thread(new Runnable() {
            @Override
            public void run() {
                initialRegistration(config);

                IDistributionClientResult result = client.init(config, callback);

                if (result.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
                    client.start();
                } else {
                    logger.error(String.format("Could not register ASDC client. %s - %s", result.getDistributionActionResult(),
                                    result.getDistributionMessageResult()));
                }

                latch.countDown();
            }
        }).start();
    }

    @SuppressWarnings("unused")
    public void stop() throws InterruptedException {
        logger.info("Stopping ASDC Listener");
        latch.await(10, TimeUnit.SECONDS);

        if (callback != null) {
            callback.stop();
        }
        if (client != null) {
            client.stop();

        }
        logger.info("ASDC Listener stopped successfully");
    }

    private boolean initialRegistration(AsdcConfig config) {
        try {
            final String jsonTemplate = "{\"consumerName\": \"%s\",\"consumerSalt\": \"%s\",\"consumerPassword\":\"%s\"}";
            String saltedPassStr = org.openecomp.tlv.sdc.security.Passwords.hashPassword(config.getPassword());
            if (saltedPassStr == null || !saltedPassStr.contains(":")) {
                return false;
            }

            String[] saltedPass = saltedPassStr.split(":");
            String json = String.format(jsonTemplate, config.getUser(), saltedPass[0], saltedPass[1]);

            Map<String, String> headers = new HashMap<>();
            // TODO - Replace the header below to sdc's requirements. What should the new value be
            headers.put("HTTP_CSP_ID", "test");

            // TODO - How to format the url. Always same endpoint or ports?
            String host = config.getAsdcAddress();
            URL url = new URL(
                    String.format("http%s://%s/sdc2/rest/v1/consumers", host.contains("443") ? "s" : "", host));

            logger.info(String.format("Attempting to register user %s on %s with salted pass of %s", config.getUser(),
                    url, saltedPass[1]));

            ProviderResponse result = ProviderOperations.post(url, json, headers);
            return result.getStatus() == 200;
        } catch (Exception e) {
            logger.error("Error performing initial registration with ASDC server. User may not be able to connect", e);
            return false;
        }
    }
}