summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/config/sdc/SdcSingleControllerConfiguration.java
blob: ca756aa09cb1d68ec93851c707a486a96c437fcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 * 
 */

package org.onap.clamp.clds.config.sdc;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;

import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.codec.DecoderException;
import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
import org.onap.clamp.clds.util.CryptoUtils;
import org.onap.sdc.api.consumer.IConfiguration;

/**
 * This class maps the SDC config JSON for one controller.
 */
public class SdcSingleControllerConfiguration implements IConfiguration {

    private static final EELFLogger logger = EELFManager.getInstance()
            .getLogger(SdcSingleControllerConfiguration.class);
    /**
     * The sdc Controller name corresponding.
     */
    private String sdcControllerName;
    /**
     * The root of the JSON.
     */
    private JsonNode jsonRootNode;
    // All keys that can be present in the JSON
    public static final String CONSUMER_GROUP_ATTRIBUTE_NAME = "consumerGroup";
    public static final String CONSUMER_ID_ATTRIBUTE_NAME = "consumerId";
    public static final String ENVIRONMENT_NAME_ATTRIBUTE_NAME = "environmentName";
    public static final String SDC_KEY_ATTRIBUTE_NAME = "password";
    public static final String POLLING_INTERVAL_ATTRIBUTE_NAME = "pollingInterval";
    public static final String RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME = "relevantArtifactTypes";
    public static final String USER_ATTRIBUTE_NAME = "user";
    public static final String SDC_ADDRESS_ATTRIBUTE_NAME = "sdcAddress";
    public static final String POLLING_TIMEOUT_ATTRIBUTE_NAME = "pollingTimeout";
    public static final String ACTIVATE_SERVER_TLS_AUTH = "activateServerTLSAuth";
    public static final String KEY_STORE_KEY = "keyStorePassword";
    public static final String KEY_STORE_PATH = "keyStorePath";
    public static final String MESSAGE_BUS_ADDRESSES = "messageBusAddresses";
    private String errorMessageKeyNotFound;
    /**
     * Supported artifact types.
     */
    public static final String HEAT = "HEAT";
    public static final String HEAT_ARTIFACT = "HEAT_ARTIFACT";
    public static final String HEAT_ENV = "HEAT_ENV";
    public static final String HEAT_NESTED = "HEAT_NESTED";
    public static final String HEAT_NET = "HEAT_NET";
    public static final String HEAT_VOL = "HEAT_VOL";
    public static final String OTHER = "OTHER";
    public static final String TOSCA_CSAR = "TOSCA_CSAR";
    public static final String VF_MODULES_METADATA = "VF_MODULES_METADATA";
    private static final String[] SUPPORTED_ARTIFACT_TYPES = {
            TOSCA_CSAR, VF_MODULES_METADATA
    };
    public static final List<String> SUPPORTED_ARTIFACT_TYPES_LIST = Collections
            .unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));

    /**
     * This constructor builds a SdcSingleControllerConfiguration from the
     * corresponding json.
     * 
     * @param jsonRootNode
     *            The JSON node
     * @param controllerName
     *            The controller name that must appear in the JSON
     */
    public SdcSingleControllerConfiguration(JsonNode jsonNode, String controllerName) {
        jsonRootNode = jsonNode;
        setSdcControllerName(controllerName);
        testAllRequiredParameters();
    }

    public String getSdcControllerName() {
        return sdcControllerName;
    }

    public void setSdcControllerName(String controllerName) {
        this.sdcControllerName = controllerName;
        errorMessageKeyNotFound = " parameter cannot be found in config file for controller name" + sdcControllerName;
        testAllRequiredParameters();
    }

    private String getStringConfig(String key) {
        if (jsonRootNode != null && jsonRootNode.get(key) != null) {
            String config = jsonRootNode.get(key).asText();
            return config.isEmpty() ? null : config;
        }
        return null;
    }

    private Integer getIntConfig(String key) {
        if (jsonRootNode != null && jsonRootNode.get(key) != null) {
            return jsonRootNode.get(key).asInt();
        } else {
            return 0;
        }
    }

    private String getEncryptedStringConfig(String key) throws GeneralSecurityException, DecoderException {
        if (jsonRootNode != null && jsonRootNode.get(key) != null) {
            return jsonRootNode.get(key).asText().isEmpty() ? null
                    : CryptoUtils.decrypt(jsonRootNode.get(key).asText());
        }
        return null;
    }

    @Override
    public java.lang.Boolean isUseHttpsWithDmaap() {
        return false;
    }

    @Override
    public String getConsumerGroup() {
        if (jsonRootNode != null && jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
            String config = jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME).asText();
            return "NULL".equals(config) || config.isEmpty() ? null : config;
        }
        return null;
    }

    @Override
    public String getConsumerID() {
        return getStringConfig(CONSUMER_ID_ATTRIBUTE_NAME);
    }

    @Override
    public String getEnvironmentName() {
        return getStringConfig(ENVIRONMENT_NAME_ATTRIBUTE_NAME);
    }

    @Override
    public String getPassword() {
        try {
            return getEncryptedStringConfig(SDC_KEY_ATTRIBUTE_NAME);
        } catch (GeneralSecurityException | DecoderException e) {
            logger.error("Unable to decrypt the SDC password", e);
            return null;
        }
    }

    @Override
    public int getPollingInterval() {
        return getIntConfig(POLLING_INTERVAL_ATTRIBUTE_NAME);
    }

    @Override
    public List<String> getRelevantArtifactTypes() {
        // DO not return the Static List SUPPORTED_ARTIFACT_TYPES_LIST because
        // the ASDC Client could try to modify it !!!
        return Arrays.asList(SUPPORTED_ARTIFACT_TYPES);
    }

    @Override
    public String getUser() {
        return getStringConfig(USER_ATTRIBUTE_NAME);
    }

    @Override
    public String getAsdcAddress() {
        return getStringConfig(SDC_ADDRESS_ATTRIBUTE_NAME);
    }

    @Override
    public int getPollingTimeout() {
        return getIntConfig(POLLING_TIMEOUT_ATTRIBUTE_NAME);
    }

    @Override
    public boolean activateServerTLSAuth() {
        if (jsonRootNode != null && jsonRootNode.get(ACTIVATE_SERVER_TLS_AUTH) != null) {
            return jsonRootNode.get(ACTIVATE_SERVER_TLS_AUTH).asBoolean(false);
        } else {
            return false;
        }
    }

    @Override
    public String getKeyStorePassword() {
        try {
            return getEncryptedStringConfig(KEY_STORE_KEY);
        } catch (GeneralSecurityException | DecoderException e) {
            logger.error("Unable to decrypt the SDC password", e);
            return null;
        }
    }

    @Override
    public String getKeyStorePath() {
        return getStringConfig(KEY_STORE_PATH);
    }

    /**
     * This method can be used to validate all required parameters are well
     * there.
     */
    public void testAllRequiredParameters() {
        // Special case for this attribute that can be null from
        // getConsumerGroup
        if (jsonRootNode == null) {
            throw new SdcParametersException("Json is null for controller " + this.getSdcControllerName());
        }
        if (this.getConsumerGroup() == null && (jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME) == null
                || !"NULL".equals(jsonRootNode.get(CONSUMER_GROUP_ATTRIBUTE_NAME).asText()))) {
            throw new SdcParametersException(CONSUMER_GROUP_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getConsumerID() == null || this.getConsumerID().isEmpty()) {
            throw new SdcParametersException(CONSUMER_ID_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getEnvironmentName() == null || this.getEnvironmentName().isEmpty()) {
            throw new SdcParametersException(ENVIRONMENT_NAME_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getAsdcAddress() == null || this.getAsdcAddress().isEmpty()) {
            throw new SdcParametersException(SDC_ADDRESS_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getMsgBusAddress() == null || this.getMsgBusAddress().isEmpty()) {
            throw new SdcParametersException(MESSAGE_BUS_ADDRESSES + errorMessageKeyNotFound);
        }
        if (this.getPassword() == null || this.getPassword().isEmpty()) {
            throw new SdcParametersException(SDC_KEY_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getPollingInterval() == 0) {
            throw new SdcParametersException(POLLING_INTERVAL_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getPollingTimeout() == 0) {
            throw new SdcParametersException(POLLING_TIMEOUT_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getRelevantArtifactTypes() == null || this.getRelevantArtifactTypes().isEmpty()) {
            throw new SdcParametersException(RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
        if (this.getUser() == null || this.getUser().isEmpty()) {
            throw new SdcParametersException(USER_ATTRIBUTE_NAME + errorMessageKeyNotFound);
        }
    }

    /**
     * The flag allows the client to receive metadata for all resources of the
     * service regardless of the artifacts associated to them. Setting the flag
     * to false will preserve legacy behavior.
     */
    @Override
    public boolean isFilterInEmptyResources() {
        return false;
    }

    @Override
    public List<String> getMsgBusAddress() {
        List<String> addressesList = new ArrayList<>();
        if (jsonRootNode != null && jsonRootNode.get(MESSAGE_BUS_ADDRESSES) != null) {
            jsonRootNode.get(MESSAGE_BUS_ADDRESSES).forEach(k -> addressesList.add(k.asText()));
            return addressesList;
        } else {
            return addressesList;
        }
    }
}