aboutsummaryrefslogtreecommitdiffstats
path: root/asdc-controller/src/main/java/org/openecomp/mso/asdc/client/ASDCConfiguration.java
blob: 61ca698678d13d9d31ac79d4a3df52a0eb2d00a8 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 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.openecomp.mso.asdc.client;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;

import org.codehaus.jackson.JsonNode;

import org.openecomp.sdc.api.consumer.IConfiguration;
import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
import org.openecomp.mso.properties.MsoJsonProperties;
import org.openecomp.mso.properties.MsoPropertiesException;
import org.openecomp.mso.properties.MsoPropertiesFactory;

public class ASDCConfiguration implements IConfiguration {

    private MsoPropertiesFactory msoPropertiesFactory;

    // SHell command to obtain the same encryption, 128 bits key, key must be HEX
    // echo -n "This is a test string" | openssl aes-128-ecb -e -K 546573746F736973546573746F736973 -nosalt | xxd

    private String configKey;

    private MsoJsonProperties msoProperties;

    private String asdcControllerName;

    public static final String MSO_PROP_ASDC = "MSO_PROP_ASDC";
    public static final String PARAMETER_PATTERN = "asdc-connections";

    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 PASSWORD_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 ASDC_ADDRESS_ATTRIBUTE_NAME = "asdcAddress";
    public static final String POLLING_TIMEOUT_ATTRIBUTE_NAME = "pollingTimeout";
    public static final String ACTIVATE_SERVER_TLS_AUTH = "activateServerTLSAuth";
    public static final String KEY_STORE_PASSWORD = "keyStorePassword";
    public static final String KEY_STORE_PATH = "keyStorePath";

    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 VF_MODULES_METADATA="VF_MODULES_METADATA";


    private static final String[] SUPPORTED_ARTIFACT_TYPES = {HEAT,
    		HEAT_ARTIFACT,
    		HEAT_ENV,
    		HEAT_NESTED,
    		HEAT_NET,
    		HEAT_VOL,
    		OTHER,
    		VF_MODULES_METADATA};

    public static final List<String>  SUPPORTED_ARTIFACT_TYPES_LIST =  Collections.unmodifiableList(Arrays.asList(SUPPORTED_ARTIFACT_TYPES));

    /**
     * Default constructor, the mso.properties is searched in the classpath (for testing)
     * Or in /etc/ecomp/mso/config/mso.properties
     *
     * @param controllerName The controllerName of the config JSON tree
     * @throws ASDCParametersException in case of issues with the parameters
     * @throws IOException If the key file has not been loaded properly
     */
    public ASDCConfiguration (String controllerName) throws ASDCParametersException, IOException {

        Properties keyProp = new Properties ();
        this.asdcControllerName = controllerName;

        keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("config-key.properties"));
        configKey = (String) keyProp.get ("asdc.config.key");

        // This structure contains static values initialized by servlet initializer
        this.msoPropertiesFactory = new MsoPropertiesFactory ();

        refreshASDCConfig ();

    }

    public String getAsdcControllerName () {
        return asdcControllerName;
    }

    private JsonNode getASDCControllerConfigJsonNode () {
        if (this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
            return this.msoProperties.getJsonRootNode ().get (PARAMETER_PATTERN).get (this.asdcControllerName);
        } else {
            return null;
        }

    }

    /**
     * This method reload the config if needed.
     *
     * @return true if config has been reloaded, false otherwise
     * @throws ASDCParametersException In case the parameters validation fails
     * @throws MsoPropertiesException
     */
    public boolean refreshASDCConfig () throws ASDCParametersException {

        try {
            if (msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties)) {
                msoProperties = msoPropertiesFactory.getMsoJsonProperties (MSO_PROP_ASDC);

                this.testAllParameters ();
                return true;
            } else {
                return false;
            }
        } catch (MsoPropertiesException e) {
            throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be reloaded",
                                               e);
        }
    }

    /**
     * This method is useful to check whether a mso properties config has been changed.
     *
     * @return true is a new config is availabe, false otherwise
     * @throws ASDCParametersException
     * @throws MsoPropertiesException
     */
    public boolean hasASDCConfigChanged () throws ASDCParametersException {
        try {
            return msoPropertiesFactory.propertiesHaveChanged (MSO_PROP_ASDC, msoProperties);
        } catch (MsoPropertiesException e) {
            throw new ASDCParametersException ("mso.asdc.json not initialized properly, ASDC config cannot be read", e);
        }
    }

    @Override
    public String getConsumerGroup () {
        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME) != null) {
            String config = masterConfigNode.get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ();

            if ("NULL".equals (config) || config.isEmpty ()) {
                return null;
            } else {
                return config;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getConsumerID () {

        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME) != null) {
            String config = masterConfigNode.get (CONSUMER_ID_ATTRIBUTE_NAME).asText ();

            if (config.isEmpty ()) {
                return null;
            } else {
                return config;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getEnvironmentName () {
        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME) != null) {
            String config = masterConfigNode.get (ENVIRONMENT_NAME_ATTRIBUTE_NAME).asText ();

            if (config.isEmpty ()) {
                return null;
            } else {
                return config;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getPassword () {
        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME) != null) {
            String config = this.msoProperties.getEncryptedProperty (masterConfigNode.get (PASSWORD_ATTRIBUTE_NAME),
                                                                     null,
                                                                     this.configKey);

            if (config.isEmpty ()) {
                return null;
            } else {
                return config;
            }
        } else {
            return null;
        }
    }

    @Override
    public int getPollingInterval () {
        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME) != null) {
            return masterConfigNode.get (POLLING_INTERVAL_ATTRIBUTE_NAME).asInt ();
        } else {
            return 0;
        }
    }

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

    @Override
    public String getUser () {
        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (USER_ATTRIBUTE_NAME) != null) {
            String config = masterConfigNode.get (USER_ATTRIBUTE_NAME).asText ();

            if (config.isEmpty ()) {
                return null;
            } else {
                return config;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getAsdcAddress () {
        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME) != null) {
            String config = masterConfigNode.get (ASDC_ADDRESS_ATTRIBUTE_NAME).asText ();

            if (config.isEmpty ()) {
                return null;
            } else {
                return config;
            }
        } else {
            return null;
        }
    }

    @Override
    public int getPollingTimeout () {
        JsonNode masterConfigNode = getASDCControllerConfigJsonNode ();
        if (masterConfigNode != null && masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME) != null) {
            return masterConfigNode.get (POLLING_TIMEOUT_ATTRIBUTE_NAME).asInt ();
        } else {
            return 0;
        }
    }

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

	@Override
	public String getKeyStorePassword() {
		JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
		if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PASSWORD) != null) {
			String config = this.msoProperties.getEncryptedProperty(masterConfigNode.get(KEY_STORE_PASSWORD), null,
					this.configKey);

			if (config.isEmpty()) {
				return null;
			} else {
				return config;
			}
		} else {
			return null;
		}
	}

	@Override
	public String getKeyStorePath() {
		JsonNode masterConfigNode = getASDCControllerConfigJsonNode();
		if (masterConfigNode != null && masterConfigNode.get(KEY_STORE_PATH) != null) {
			String config = masterConfigNode.get(KEY_STORE_PATH).asText();

			if (config.isEmpty()) {
				return null;
			} else {
				return config;
			}
		} else {
			return null;
		}
	}

    public void testAllParameters () throws ASDCParametersException {

        // Special case for this attribute that can be null from getConsumerGroup
        if (this.getConsumerGroup () == null
            && (getASDCControllerConfigJsonNode () == null
                || !"NULL".equals (getASDCControllerConfigJsonNode ().get (CONSUMER_GROUP_ATTRIBUTE_NAME).asText ()))) {
            throw new ASDCParametersException (CONSUMER_GROUP_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getConsumerID () == null || this.getConsumerID ().isEmpty ()) {
            throw new ASDCParametersException (CONSUMER_ID_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getEnvironmentName () == null || this.getEnvironmentName ().isEmpty ()) {
            throw new ASDCParametersException (ENVIRONMENT_NAME_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getAsdcAddress () == null || this.getAsdcAddress ().isEmpty ()) {
            throw new ASDCParametersException (ASDC_ADDRESS_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getPassword () == null || this.getPassword ().isEmpty ()) {
            throw new ASDCParametersException (PASSWORD_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getPollingInterval () == 0) {
            throw new ASDCParametersException (POLLING_INTERVAL_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getPollingTimeout () == 0) {
            throw new ASDCParametersException (POLLING_TIMEOUT_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getRelevantArtifactTypes () == null || this.getRelevantArtifactTypes ().isEmpty ()) {
            throw new ASDCParametersException (RELEVANT_ARTIFACT_TYPES_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }

        if (this.getUser () == null || this.getUser ().isEmpty ()) {
            throw new ASDCParametersException (USER_ATTRIBUTE_NAME
                                               + " parameter cannot be found in config mso.properties");
        }
    }

    /**
     * This method triggers the MsoPropertiesFactory to get the ASDC config from the cache and extracts all controllers
     * defined.
     *
     * @return A list of controller Names defined in the cache config
     * @throws ASDCParametersException In cas of issues with the cache
     */
    public static List <String> getAllDefinedControllers () throws ASDCParametersException {

        MsoJsonProperties msoProp;
        try {
            List <String> result = new ArrayList <String> ();
            msoProp = new MsoPropertiesFactory ().getMsoJsonProperties (MSO_PROP_ASDC);

            if (msoProp.getJsonRootNode ().get (PARAMETER_PATTERN) != null) {
                Iterator <Entry <String, JsonNode>> it = msoProp.getJsonRootNode ()
                                                                .get (PARAMETER_PATTERN)
                                                                .getFields ();

                Entry <String, JsonNode> entry;
                while (it.hasNext ()) {
                    entry = it.next ();
                    result.add (entry.getKey ());

                }
            }
            return result;
        } catch (MsoPropertiesException e) {
            throw new ASDCParametersException ("Unable to get the JSON Properties in cache:" + MSO_PROP_ASDC, e);
        }

    }

    /**
     * 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.
     */
    public boolean isFilterInEmptyResources() {
 	   return true;
    }

}