Merge of new rebased code
[appc.git] / appc-asdc-listener / appc-asdc-listener-bundle / src / main / java / org / openecomp / appc / sdc / listener / AsdcListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.sdc.listener;
23 import org.openecomp.sdc.impl.DistributionClientFactory;
24 import org.openecomp.sdc.utils.DistributionActionResultEnum;
25
26 import java.net.URL;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Properties;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.TimeUnit;
32
33 import org.openecomp.appc.configuration.Configuration;
34 import org.openecomp.appc.configuration.ConfigurationFactory;
35 import org.openecomp.sdc.api.IDistributionClient;
36 import org.openecomp.sdc.api.results.IDistributionClientResult;
37 import org.openecomp.sdc.impl.DistributionClientFactory;
38 import org.openecomp.sdc.utils.DistributionActionResultEnum;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41
42 public class AsdcListener {
43
44     /**
45      * The bundle context
46      */
47     private IDistributionClient client;
48     private AsdcCallback callback;
49     private AsdcConfig config;
50     private CountDownLatch latch;
51
52
53     private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcListener.class);
54
55     @SuppressWarnings("unused")
56     public void start() throws Exception {
57         logger.info("Starting bundle ASDC Listener");
58         Configuration configuration = ConfigurationFactory.getConfiguration();
59         Properties props = configuration.getProperties();
60
61         config = new AsdcConfig(props);
62
63         client = DistributionClientFactory.createDistributionClient();
64         callback = new AsdcCallback(config.getStoreOpURI(), client);
65
66         latch = new CountDownLatch(1);
67
68         new Thread(new Runnable() {
69             @Override
70             public void run() {
71                 initialRegistration(config);
72
73                 IDistributionClientResult result = client.init(config, callback);
74
75                 if (result.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
76                     client.start();
77                 } else {
78                     logger.error(String.format("Could not register ASDC client. %s - %s", result.getDistributionActionResult(),
79                                     result.getDistributionMessageResult()));
80                 }
81
82                 latch.countDown();
83             }
84         }).start();
85     }
86
87     @SuppressWarnings("unused")
88     public void stop() throws InterruptedException {
89         logger.info("Stopping ASDC Listener");
90         latch.await(10, TimeUnit.SECONDS);
91
92         if (callback != null) {
93             callback.stop();
94         }
95         if (client != null) {
96             client.stop();
97
98         }
99         logger.info("ASDC Listener stopped successfully");
100     }
101
102     private boolean initialRegistration(AsdcConfig config) {
103         try {
104             final String jsonTemplate = "{\"consumerName\": \"%s\",\"consumerSalt\": \"%s\",\"consumerPassword\":\"%s\"}";
105             String saltedPassStr = org.openecomp.tlv.sdc.security.Passwords.hashPassword(config.getPassword());
106             if (saltedPassStr == null || !saltedPassStr.contains(":")) {
107                 return false;
108             }
109
110             String[] saltedPass = saltedPassStr.split(":");
111             String json = String.format(jsonTemplate, config.getUser(), saltedPass[0], saltedPass[1]);
112
113             Map<String, String> headers = new HashMap<>();
114             // TODO - Replace the header below to sdc's requirements. What should the new value be
115             headers.put("USER_ID", "test");
116
117             // TODO - How to format the url. Always same endpoint or ports?
118             String host = config.getAsdcAddress();
119             URL url = new URL(
120                     String.format("http%s://%s/sdc2/rest/v1/consumers", host.contains("443") ? "s" : "", host));
121
122             logger.info(String.format("Attempting to register user %s on %s with salted pass of %s", config.getUser(),
123                     url, saltedPass[1]));
124
125             ProviderResponse result = ProviderOperations.post(url, json, headers);
126             return result.getStatus() == 200;
127         } catch (Exception e) {
128             logger.error("Error performing initial registration with ASDC server. User may not be able to connect", e);
129             return false;
130         }
131     }
132 }