2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.sdc.listener;
26 import org.openecomp.sdc.impl.DistributionClientFactory;
27 import org.openecomp.sdc.utils.DistributionActionResultEnum;
30 import java.util.HashMap;
32 import java.util.Properties;
33 import java.util.concurrent.CountDownLatch;
34 import java.util.concurrent.TimeUnit;
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;
45 public class AsdcListener {
50 private IDistributionClient client;
51 private AsdcCallback callback;
52 private AsdcConfig config;
53 private CountDownLatch latch;
56 private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcListener.class);
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();
64 config = new AsdcConfig(props);
66 client = DistributionClientFactory.createDistributionClient();
67 callback = new AsdcCallback(config.getStoreOpURI(), client);
69 latch = new CountDownLatch(1);
71 new Thread(new Runnable() {
74 initialRegistration(config);
76 IDistributionClientResult result = client.init(config, callback);
78 if (result.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
81 logger.error(String.format("Could not register ASDC client. %s - %s", result.getDistributionActionResult(),
82 result.getDistributionMessageResult()));
90 @SuppressWarnings("unused")
91 public void stop() throws InterruptedException {
92 logger.info("Stopping ASDC Listener");
93 latch.await(10, TimeUnit.SECONDS);
95 if (callback != null) {
102 logger.info("ASDC Listener stopped successfully");
105 private boolean initialRegistration(AsdcConfig config) {
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(":")) {
113 String[] saltedPass = saltedPassStr.split(":");
114 String json = String.format(jsonTemplate, config.getUser(), saltedPass[0], saltedPass[1]);
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");
120 // TODO - How to format the url. Always same endpoint or ports?
121 String host = config.getAsdcAddress();
123 String.format("http%s://%s/sdc2/rest/v1/consumers", host.contains("443") ? "s" : "", host));
125 logger.info(String.format("Attempting to register user %s on %s with salted pass of %s", config.getUser(),
126 url, saltedPass[1]));
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);