2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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=========================================================
 
  22 package org.openecomp.appc.sdc.listener;
 
  25 import java.util.HashMap;
 
  27 import java.util.Properties;
 
  28 import java.util.concurrent.CountDownLatch;
 
  29 import java.util.concurrent.TimeUnit;
 
  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;
 
  41 public class AsdcListener {
 
  46     private IDistributionClient client;
 
  47     private AsdcCallback callback;
 
  48     private AsdcConfig config;
 
  49     private CountDownLatch latch;
 
  52     private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcListener.class);
 
  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();
 
  60         config = new AsdcConfig(props);
 
  62         client = DistributionClientFactory.createDistributionClient();
 
  63         callback = new AsdcCallback(config.getStoreOpURI(), client);
 
  65         latch = new CountDownLatch(1);
 
  67         new Thread(new Runnable() {
 
  70                 initialRegistration(config);
 
  72                 IDistributionClientResult result = client.init(config, callback);
 
  74                 if (result.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
 
  77                     logger.error(String.format("Could not register ASDC client. %s - %s", result.getDistributionActionResult(),
 
  78                                     result.getDistributionMessageResult()));
 
  86     @SuppressWarnings("unused")
 
  87     public void stop() throws InterruptedException {
 
  88         logger.info("Stopping ASDC Listener");
 
  89         latch.await(10, TimeUnit.SECONDS);
 
  91         if (callback != null) {
 
  98         logger.info("ASDC Listener stopped successfully");
 
 101     private boolean initialRegistration(AsdcConfig config) {
 
 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(":")) {
 
 109             String[] saltedPass = saltedPassStr.split(":");
 
 110             String json = String.format(jsonTemplate, config.getUser(), saltedPass[0], saltedPass[1]);
 
 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");
 
 116             // TODO - How to format the url. Always same endpoint or ports?
 
 117             String host = config.getAsdcAddress();
 
 119                     String.format("http%s://%s/sdc2/rest/v1/consumers", host.contains("443") ? "s" : "", host));
 
 121             logger.info(String.format("Attempting to register user %s on %s with salted pass of %s", config.getUser(),
 
 122                     url, saltedPass[1]));
 
 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);