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