2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.sdc.listener;
24 import org.openecomp.sdc.impl.DistributionClientFactory;
25 import org.openecomp.sdc.utils.DistributionActionResultEnum;
28 import java.util.HashMap;
30 import java.util.Properties;
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.TimeUnit;
34 import org.openecomp.appc.configuration.Configuration;
35 import org.openecomp.appc.configuration.ConfigurationFactory;
36 import org.openecomp.sdc.api.IDistributionClient;
37 import org.openecomp.sdc.api.results.IDistributionClientResult;
38 import org.openecomp.sdc.impl.DistributionClientFactory;
39 import org.openecomp.sdc.utils.DistributionActionResultEnum;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
43 public class AsdcListener {
48 private IDistributionClient client;
49 private AsdcCallback callback;
50 private AsdcConfig config;
51 private CountDownLatch latch;
54 private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcListener.class);
56 @SuppressWarnings("unused")
57 public void start() throws Exception {
58 logger.info("Starting bundle ASDC Listener");
59 Configuration configuration = ConfigurationFactory.getConfiguration();
60 Properties props = configuration.getProperties();
62 config = new AsdcConfig(props);
64 client = DistributionClientFactory.createDistributionClient();
65 callback = new AsdcCallback(config.getStoreOpURI(), client);
67 latch = new CountDownLatch(1);
69 new Thread(new Runnable() {
72 initialRegistration(config);
74 IDistributionClientResult result = client.init(config, callback);
76 if (result.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
79 logger.error(String.format("Could not register ASDC client. %s - %s", result.getDistributionActionResult(),
80 result.getDistributionMessageResult()));
88 @SuppressWarnings("unused")
89 public void stop() throws InterruptedException {
90 logger.info("Stopping ASDC Listener");
91 latch.await(10, TimeUnit.SECONDS);
93 if (callback != null) {
100 logger.info("ASDC Listener stopped successfully");
103 private boolean initialRegistration(AsdcConfig config) {
105 final String jsonTemplate = "{\"consumerName\": \"%s\",\"consumerSalt\": \"%s\",\"consumerPassword\":\"%s\"}";
106 String saltedPassStr = org.openecomp.tlv.sdc.security.Passwords.hashPassword(config.getPassword());
107 if (saltedPassStr == null || !saltedPassStr.contains(":")) {
111 String[] saltedPass = saltedPassStr.split(":");
112 String json = String.format(jsonTemplate, config.getUser(), saltedPass[0], saltedPass[1]);
114 Map<String, String> headers = new HashMap<>();
115 // TODO - Replace the header below to sdc's requirements. What should the new value be
116 headers.put("USER_ID", "test");
118 // TODO - How to format the url. Always same endpoint or ports?
119 String host = config.getAsdcAddress();
121 String.format("http%s://%s/sdc2/rest/v1/consumers", host.contains("443") ? "s" : "", host));
123 logger.info(String.format("Attempting to register user %s on %s with salted pass of %s", config.getUser(),
124 url, saltedPass[1]));
126 ProviderResponse result = ProviderOperations.post(url, json, headers);
127 return result.getStatus() == 200;
128 } catch (Exception e) {
129 logger.error("Error performing initial registration with ASDC server. User may not be able to connect", e);