2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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============================================
20 * ===================================================================
24 package org.onap.clamp.clds.config.sdc;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.JsonObject;
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;
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;
42 * This class maps the SDC config JSON for one controller.
44 public class SdcSingleControllerConfiguration implements IConfiguration {
46 private static final EELFLogger logger = EELFManager.getInstance()
47 .getLogger(SdcSingleControllerConfiguration.class);
49 * The sdc Controller name corresponding.
51 private String sdcControllerName;
53 * The root of the JSON.
55 private JsonObject 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;
72 * Supported artifact types.
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 TOSCA_CSAR, VF_MODULES_METADATA
86 public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST = Collections
87 .unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));
90 * This constructor builds a SdcSingleControllerConfiguration from the
95 * @param controllerName
96 * The controller name that must appear in the JSON
98 public SdcSingleControllerConfiguration(JsonObject jsonNode, String controllerName) {
99 jsonRootNode = jsonNode;
100 setSdcControllerName(controllerName);
101 testAllRequiredParameters();
104 public String getSdcControllerName() {
105 return sdcControllerName;
109 * Sets SDC controller name.
111 * @param controllerName SDC controller name
113 public void setSdcControllerName(String controllerName) {
114 this.sdcControllerName = controllerName;
115 errorMessageKeyNotFound = " parameter cannot be found in config file for controller name" + sdcControllerName;
116 testAllRequiredParameters();
119 private String getStringConfig(String key) {
120 if (jsonRootNode != null && jsonRootNode.get(key) != null) {
121 String config = jsonRootNode.get(key).getAsString();
122 return config.isEmpty() ? null : config;
127 private Integer getIntConfig(String key) {
128 if (jsonRootNode != null && jsonRootNode.get(key) != null) {
129 return jsonRootNode.get(key).getAsInt();
135 private String getEncryptedStringConfig(String key) throws GeneralSecurityException, DecoderException {
136 if (jsonRootNode != null && jsonRootNode.get(key) != null) {
137 return jsonRootNode.get(key).getAsString().isEmpty() ? null
138 : CryptoUtils.decrypt(jsonRootNode.get(key).getAsString());
144 public java.lang.Boolean isUseHttpsWithDmaap() {
149 public String getConsumerGroup() {
150 if (jsonRootNode != null && jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
151 String config = jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME).getAsString();
152 return "NULL".equals(config) || config.isEmpty() ? null : config;
158 public String getConsumerID() {
159 return getStringConfig(CONSUMER_ID_ATTRIBUTE_NAME);
163 public String getEnvironmentName() {
164 return getStringConfig(ENVIRONMENT_NAME_ATTRIBUTE_NAME);
168 public String getPassword() {
170 return getEncryptedStringConfig(SDC_KEY_ATTRIBUTE_NAME);
171 } catch (GeneralSecurityException | DecoderException e) {
172 logger.error("Unable to decrypt the SDC password", e);
178 public int getPollingInterval() {
179 return getIntConfig(POLLING_INTERVAL_ATTRIBUTE_NAME);
183 public List<String> getRelevantArtifactTypes() {
184 // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because
185 // the ASDC Client could try to modify it !!!
186 return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
190 public String getUser() {
191 return getStringConfig(USER_ATTRIBUTE_NAME);
195 public String getAsdcAddress() {
196 return getStringConfig(SDC_ADDRESS_ATTRIBUTE_NAME);
200 public int getPollingTimeout() {
201 return getIntConfig(POLLING_TIMEOUT_ATTRIBUTE_NAME);
205 public boolean activateServerTLSAuth() {
206 if (jsonRootNode != null && jsonRootNode.get(ACTIVATE_SERVER_TLS_AUTH) != null &&
207 jsonRootNode.get(ACTIVATE_SERVER_TLS_AUTH).isJsonPrimitive()) {
208 return jsonRootNode.get(ACTIVATE_SERVER_TLS_AUTH).getAsBoolean();
215 public String getKeyStorePassword() {
217 return getEncryptedStringConfig(KEY_STORE_KEY);
218 } catch (GeneralSecurityException | DecoderException e) {
219 logger.error("Unable to decrypt the SDC password", e);
225 public String getKeyStorePath() {
226 return getStringConfig(KEY_STORE_PATH);
230 * This method can be used to validate all required parameters are well
233 public void testAllRequiredParameters() {
234 // Special case for this attribute that can be null from
236 if (jsonRootNode == null) {
237 throw new SdcParametersException("Json is null for controller " + this.getSdcControllerName());
239 if (this.getConsumerGroup() == null && (jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME) == null
240 || !"NULL".equals(jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME).getAsString()))) {
241 throw new SdcParametersException(CONSUMER_GROUP_ATTRIBUTE_NAME + errorMessageKeyNotFound);
243 if (this.getConsumerID() == null || this.getConsumerID().isEmpty()) {
244 throw new SdcParametersException(CONSUMER_ID_ATTRIBUTE_NAME + errorMessageKeyNotFound);
246 if (this.getEnvironmentName() == null || this.getEnvironmentName().isEmpty()) {
247 throw new SdcParametersException(ENVIRONMENT_NAME_ATTRIBUTE_NAME + errorMessageKeyNotFound);
249 if (this.getAsdcAddress() == null || this.getAsdcAddress().isEmpty()) {
250 throw new SdcParametersException(SDC_ADDRESS_ATTRIBUTE_NAME + errorMessageKeyNotFound);
252 if (this.getMsgBusAddress() == null || this.getMsgBusAddress().isEmpty()) {
253 throw new SdcParametersException(MESSAGE_BUS_ADDRESSES + errorMessageKeyNotFound);
255 if (this.getPassword() == null || this.getPassword().isEmpty()) {
256 throw new SdcParametersException(SDC_KEY_ATTRIBUTE_NAME + errorMessageKeyNotFound);
258 if (this.getPollingInterval() == 0) {
259 throw new SdcParametersException(POLLING_INTERVAL_ATTRIBUTE_NAME + errorMessageKeyNotFound);
261 if (this.getPollingTimeout() == 0) {
262 throw new SdcParametersException(POLLING_TIMEOUT_ATTRIBUTE_NAME + errorMessageKeyNotFound);
264 if (this.getRelevantArtifactTypes() == null || this.getRelevantArtifactTypes().isEmpty()) {
265 throw new SdcParametersException(RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME + errorMessageKeyNotFound);
267 if (this.getUser() == null || this.getUser().isEmpty()) {
268 throw new SdcParametersException(USER_ATTRIBUTE_NAME + errorMessageKeyNotFound);
273 * The flag allows the client to receive metadata for all resources of the
274 * service regardless of the artifacts associated to them. Setting the flag
275 * to false will preserve legacy behavior.
278 public boolean isFilterInEmptyResources() {
283 public List<String> getMsgBusAddress() {
284 List<String> addressesList = new ArrayList<>();
285 if (jsonRootNode != null && jsonRootNode.get(MESSAGE_BUS_ADDRESSES) != null) {
286 jsonRootNode.get(MESSAGE_BUS_ADDRESSES).getAsJsonArray().forEach(k -> addressesList.add(k.getAsString()));
287 return addressesList;
289 return addressesList;