Change nexus values to properties
[appc.git] / app-c / appc / 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
24 import java.net.URL;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30
31 import org.openecomp.appc.configuration.Configuration;
32 import org.openecomp.appc.configuration.ConfigurationFactory;
33 import org.openecomp.sdc.api.IDistributionClient;
34 import org.openecomp.sdc.api.results.IDistributionClientResult;
35 import org.openecomp.sdc.impl.DistributionClientFactory;
36 import org.openecomp.sdc.utils.DistributionActionResultEnum;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40
41 public class AsdcListener {
42
43     /**
44      * The bundle context
45      */
46     private IDistributionClient client;
47     private AsdcCallback callback;
48     private AsdcConfig config;
49     private CountDownLatch latch;
50
51
52     private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcListener.class);
53
54     @SuppressWarnings("unused")
55     public void start() throws Exception {
56         logger.info("Starting bundle ASDC Listener");
57         Configuration configuration = ConfigurationFactory.getConfiguration();
58         Properties props = configuration.getProperties();
59
60         config = new AsdcConfig(props);
61
62         client = DistributionClientFactory.createDistributionClient();
63         callback = new AsdcCallback(config.getStoreOpURI(), client);
64
65         latch = new CountDownLatch(1);
66
67         new Thread(new Runnable() {
68             @Override
69             public void run() {
70                 initialRegistration(config);
71
72                 IDistributionClientResult result = client.init(config, callback);
73
74                 if (result.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
75                     client.start();
76                 } else {
77                     logger.error(String.format("Could not register ASDC client. %s - %s", result.getDistributionActionResult(),
78                                     result.getDistributionMessageResult()));
79                 }
80
81                 latch.countDown();
82             }
83         }).start();
84     }
85
86     @SuppressWarnings("unused")
87     public void stop() throws InterruptedException {
88         logger.info("Stopping ASDC Listener");
89         latch.await(10, TimeUnit.SECONDS);
90
91         if (callback != null) {
92             callback.stop();
93         }
94         if (client != null) {
95             client.stop();
96
97         }
98         logger.info("ASDC Listener stopped successfully");
99     }
100
101     private boolean initialRegistration(AsdcConfig config) {
102         try {
103             final String jsonTemplate = "{\"consumerName\": \"%s\",\"consumerSalt\": \"%s\",\"consumerPassword\":\"%s\"}";
104             String saltedPassStr = org.openecomp.tlv.sdc.security.Passwords.hashPassword(config.getPassword());
105             if (saltedPassStr == null || !saltedPassStr.contains(":")) {
106                 return false;
107             }
108
109             String[] saltedPass = saltedPassStr.split(":");
110             String json = String.format(jsonTemplate, config.getUser(), saltedPass[0], saltedPass[1]);
111
112             Map<String, String> headers = new HashMap<>();
113             // TODO - Replace the header below to sdc's requirements. What should the new value be
114             headers.put("HTTP_CSP_ID", "test");
115
116             // TODO - How to format the url. Always same endpoint or ports?
117             String host = config.getAsdcAddress();
118             URL url = new URL(
119                     String.format("http%s://%s/sdc2/rest/v1/consumers", host.contains("443") ? "s" : "", host));
120
121             logger.info(String.format("Attempting to register user %s on %s with salted pass of %s", config.getUser(),
122                     url, saltedPass[1]));
123
124             ProviderResponse result = ProviderOperations.post(url, json, headers);
125             return result.getStatus() == 200;
126         } catch (Exception e) {
127             logger.error("Error performing initial registration with ASDC server. User may not be able to connect", e);
128             return false;
129         }
130     }
131 }