b088244b100e90e049ceac20d447e5166990e76e
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.sdc.listener;
24 import org.openecomp.sdc.impl.DistributionClientFactory;
25 import org.openecomp.sdc.utils.DistributionActionResultEnum;
26
27 import java.net.URL;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.TimeUnit;
33
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;
42
43 public class AsdcListener {
44
45     /**
46      * The bundle context
47      */
48     private IDistributionClient client;
49     private AsdcCallback callback;
50     private AsdcConfig config;
51     private CountDownLatch latch;
52
53
54     private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcListener.class);
55
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();
61
62         config = new AsdcConfig(props);
63
64         client = DistributionClientFactory.createDistributionClient();
65         callback = new AsdcCallback(config.getStoreOpURI(), client);
66
67         latch = new CountDownLatch(1);
68
69         new Thread(new Runnable() {
70             @Override
71             public void run() {
72                 initialRegistration(config);
73
74                 IDistributionClientResult result = client.init(config, callback);
75
76                 if (result.getDistributionActionResult() == DistributionActionResultEnum.SUCCESS) {
77                     client.start();
78                 } else {
79                     logger.error(String.format("Could not register ASDC client. %s - %s", result.getDistributionActionResult(),
80                                     result.getDistributionMessageResult()));
81                 }
82
83                 latch.countDown();
84             }
85         }).start();
86     }
87
88     @SuppressWarnings("unused")
89     public void stop() throws InterruptedException {
90         logger.info("Stopping ASDC Listener");
91         latch.await(10, TimeUnit.SECONDS);
92
93         if (callback != null) {
94             callback.stop();
95         }
96         if (client != null) {
97             client.stop();
98
99         }
100         logger.info("ASDC Listener stopped successfully");
101     }
102
103     private boolean initialRegistration(AsdcConfig config) {
104         try {
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(":")) {
108                 return false;
109             }
110
111             String[] saltedPass = saltedPassStr.split(":");
112             String json = String.format(jsonTemplate, config.getUser(), saltedPass[0], saltedPass[1]);
113
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");
117
118             // TODO - How to format the url. Always same endpoint or ports?
119             String host = config.getAsdcAddress();
120             URL url = new URL(
121                     String.format("http%s://%s/sdc2/rest/v1/consumers", host.contains("443") ? "s" : "", host));
122
123             logger.info(String.format("Attempting to register user %s on %s with salted pass of %s", config.getUser(),
124                     url, saltedPass[1]));
125
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);
130             return false;
131         }
132     }
133 }