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