Remove ECOMP in headers
[clamp.git] / src / main / java / org / onap / clamp / clds / config / sdc / SdcSingleControllerConfiguration.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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  * ===================================================================
21  * 
22  */
23
24 package org.onap.clamp.clds.config.sdc;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29
30 import java.security.GeneralSecurityException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.List;
35
36 import org.apache.commons.codec.DecoderException;
37 import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
38 import org.onap.clamp.clds.util.CryptoUtils;
39 import org.onap.sdc.api.consumer.IConfiguration;
40
41 /**
42  * This class maps the SDC config JSON for one controller.
43  */
44 public class SdcSingleControllerConfiguration implements IConfiguration {
45
46     private static final EELFLogger logger = EELFManager.getInstance()
47             .getLogger(SdcSingleControllerConfiguration.class);
48     /**
49      * The sdc Controller name corresponding.
50      */
51     private String sdcControllerName;
52     /**
53      * The root of the JSON.
54      */
55     private JsonNode jsonRootNode;
56     // All keys that can be present in the JSON
57     public static final String CONSUMER_GROUP_ATTRIBUTE_NAME = "consumerGroup";
58     public static final String CONSUMER_ID_ATTRIBUTE_NAME = "consumerId";
59     public static final String ENVIRONMENT_NAME_ATTRIBUTE_NAME = "environmentName";
60     public static final String SDC_KEY_ATTRIBUTE_NAME = "password";
61     public static final String POLLING_INTERVAL_ATTRIBUTE_NAME = "pollingInterval";
62     public static final String RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME = "relevantArtifactTypes";
63     public static final String USER_ATTRIBUTE_NAME = "user";
64     public static final String SDC_ADDRESS_ATTRIBUTE_NAME = "sdcAddress";
65     public static final String POLLING_TIMEOUT_ATTRIBUTE_NAME = "pollingTimeout";
66     public static final String ACTIVATE_SERVER_TLS_AUTH = "activateServerTLSAuth";
67     public static final String KEY_STORE_KEY = "keyStorePassword";
68     public static final String KEY_STORE_PATH = "keyStorePath";
69     public static final String MESSAGE_BUS_ADDRESSES = "messageBusAddresses";
70     private String errorMessageKeyNotFound;
71     /**
72      * Supported artifact types.
73      */
74     public static final String HEAT = "HEAT";
75     public static final String HEAT_ARTIFACT = "HEAT_ARTIFACT";
76     public static final String HEAT_ENV = "HEAT_ENV";
77     public static final String HEAT_NESTED = "HEAT_NESTED";
78     public static final String HEAT_NET = "HEAT_NET";
79     public static final String HEAT_VOL = "HEAT_VOL";
80     public static final String OTHER = "OTHER";
81     public static final String TOSCA_CSAR = "TOSCA_CSAR";
82     public static final String VF_MODULES_METADATA = "VF_MODULES_METADATA";
83     private static final String[] SUPPORTED_ARTIFACT_TYPES = {
84             HEAT, HEAT_ARTIFACT, HEAT_ENV, HEAT_NESTED, HEAT_NET, HEAT_VOL, OTHER, TOSCA_CSAR, VF_MODULES_METADATA
85     };
86     public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST = Collections
87             .unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
88
89     /**
90      * This constructor builds a SdcSingleControllerConfiguration from the
91      * corresponding json.
92      * 
93      * @param jsonRootNode
94      *            The JSON node
95      * @param controllerName
96      *            The controller name that must appear in the JSON
97      */
98     public SdcSingleControllerConfiguration(JsonNode jsonNode, String controllerName) {
99         jsonRootNode = jsonNode;
100         setSdcControllerName(controllerName);
101         testAllRequiredParameters();
102     }
103
104     public String getSdcControllerName() {
105         return sdcControllerName;
106     }
107
108     public void setSdcControllerName(String controllerName) {
109         this.sdcControllerName = controllerName;
110         errorMessageKeyNotFound = " parameter cannot be found in config file for controller name" + sdcControllerName;
111         testAllRequiredParameters();
112     }
113
114     private String getStringConfig(String key) {
115         if (jsonRootNode != null && jsonRootNode.get(key) != null) {
116             String config = jsonRootNode.get(key).asText();
117             return config.isEmpty() ? null : config;
118         }
119         return null;
120     }
121
122     private Integer getIntConfig(String key) {
123         if (jsonRootNode != null && jsonRootNode.get(key) != null) {
124             return jsonRootNode.get(key).asInt();
125         } else {
126             return 0;
127         }
128     }
129
130     private String getEncryptedStringConfig(String key) throws GeneralSecurityException, DecoderException {
131         if (jsonRootNode != null && jsonRootNode.get(key) != null) {
132             return jsonRootNode.get(key).asText().isEmpty() ? null
133                     : CryptoUtils.decrypt(jsonRootNode.get(key).asText());
134         }
135         return null;
136     }
137
138     @Override
139     public java.lang.Boolean isUseHttpsWithDmaap() {
140         return false;
141     }
142
143     @Override
144     public String getConsumerGroup() {
145         if (jsonRootNode != null && jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
146             String config = jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME).asText();
147             return "NULL".equals(config) || config.isEmpty() ? null : config;
148         }
149         return null;
150     }
151
152     @Override
153     public String getConsumerID() {
154         return getStringConfig(CONSUMER_ID_ATTRIBUTE_NAME);
155     }
156
157     @Override
158     public String getEnvironmentName() {
159         return getStringConfig(ENVIRONMENT_NAME_ATTRIBUTE_NAME);
160     }
161
162     @Override
163     public String getPassword() {
164         try {
165             return getEncryptedStringConfig(SDC_KEY_ATTRIBUTE_NAME);
166         } catch (GeneralSecurityException | DecoderException e) {
167             logger.error("Unable to decrypt the SDC password", e);
168             return null;
169         }
170     }
171
172     @Override
173     public int getPollingInterval() {
174         return getIntConfig(POLLING_INTERVAL_ATTRIBUTE_NAME);
175     }
176
177     @Override
178     public List<String> getRelevantArtifactTypes() {
179         // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because
180         // the ASDC Client could try to modify it !!!
181         return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
182     }
183
184     @Override
185     public String getUser() {
186         return getStringConfig(USER_ATTRIBUTE_NAME);
187     }
188
189     @Override
190     public String getAsdcAddress() {
191         return getStringConfig(SDC_ADDRESS_ATTRIBUTE_NAME);
192     }
193
194     @Override
195     public int getPollingTimeout() {
196         return getIntConfig(POLLING_TIMEOUT_ATTRIBUTE_NAME);
197     }
198
199     @Override
200     public boolean activateServerTLSAuth() {
201         if (jsonRootNode != null && jsonRootNode.get(ACTIVATE_SERVER_TLS_AUTH) != null) {
202             return jsonRootNode.get(ACTIVATE_SERVER_TLS_AUTH).asBoolean(false);
203         } else {
204             return false;
205         }
206     }
207
208     @Override
209     public String getKeyStorePassword() {
210         try {
211             return getEncryptedStringConfig(KEY_STORE_KEY);
212         } catch (GeneralSecurityException | DecoderException e) {
213             logger.error("Unable to decrypt the SDC password", e);
214             return null;
215         }
216     }
217
218     @Override
219     public String getKeyStorePath() {
220         return getStringConfig(KEY_STORE_PATH);
221     }
222
223     /**
224      * This method can be used to validate all required parameters are well
225      * there.
226      */
227     public void testAllRequiredParameters() {
228         // Special case for this attribute that can be null from
229         // getConsumerGroup
230         if (jsonRootNode == null) {
231             throw new SdcParametersException("Json is null for controller " + this.getSdcControllerName());
232         }
233         if (this.getConsumerGroup() == null && (jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME) == null
234                 || !"NULL".equals(jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME).asText()))) {
235             throw new SdcParametersException(CONSUMER_GROUP_ATTRIBUTE_NAME + errorMessageKeyNotFound);
236         }
237         if (this.getConsumerID() == null || this.getConsumerID().isEmpty()) {
238             throw new SdcParametersException(CONSUMER_ID_ATTRIBUTE_NAME + errorMessageKeyNotFound);
239         }
240         if (this.getEnvironmentName() == null || this.getEnvironmentName().isEmpty()) {
241             throw new SdcParametersException(ENVIRONMENT_NAME_ATTRIBUTE_NAME + errorMessageKeyNotFound);
242         }
243         if (this.getAsdcAddress() == null || this.getAsdcAddress().isEmpty()) {
244             throw new SdcParametersException(SDC_ADDRESS_ATTRIBUTE_NAME + errorMessageKeyNotFound);
245         }
246         if (this.getMsgBusAddress() == null || this.getMsgBusAddress().isEmpty()) {
247             throw new SdcParametersException(MESSAGE_BUS_ADDRESSES + errorMessageKeyNotFound);
248         }
249         if (this.getPassword() == null || this.getPassword().isEmpty()) {
250             throw new SdcParametersException(SDC_KEY_ATTRIBUTE_NAME + errorMessageKeyNotFound);
251         }
252         if (this.getPollingInterval() == 0) {
253             throw new SdcParametersException(POLLING_INTERVAL_ATTRIBUTE_NAME + errorMessageKeyNotFound);
254         }
255         if (this.getPollingTimeout() == 0) {
256             throw new SdcParametersException(POLLING_TIMEOUT_ATTRIBUTE_NAME + errorMessageKeyNotFound);
257         }
258         if (this.getRelevantArtifactTypes() == null || this.getRelevantArtifactTypes().isEmpty()) {
259             throw new SdcParametersException(RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME + errorMessageKeyNotFound);
260         }
261         if (this.getUser() == null || this.getUser().isEmpty()) {
262             throw new SdcParametersException(USER_ATTRIBUTE_NAME + errorMessageKeyNotFound);
263         }
264     }
265
266     /**
267      * The flag allows the client to receive metadata for all resources of the
268      * service regardless of the artifacts associated to them. Setting the flag
269      * to false will preserve legacy behavior.
270      */
271     @Override
272     public boolean isFilterInEmptyResources() {
273         return false;
274     }
275
276     @Override
277     public List<String> getMsgBusAddress() {
278         List<String> addressesList = new ArrayList<>();
279         if (jsonRootNode != null && jsonRootNode.get(MESSAGE_BUS_ADDRESSES) != null) {
280             jsonRootNode.get(MESSAGE_BUS_ADDRESSES).forEach(k -> addressesList.add(k.asText()));
281             return addressesList;
282         } else {
283             return addressesList;
284         }
285     }
286 }