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