push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-core / src / main / java / org / openecomp / sdc / validation / utils / ValidationConfigurationManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.validation.utils;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.core.utilities.CommonMethods;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.core.utilities.json.JsonUtil;
27 import org.openecomp.core.validation.interfaces.Validator;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.io.InputStream;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35
36 public class ValidationConfigurationManager {
37
38   private static final String VALIDATION_CONFIGURATION = "validationConfiguration.json";
39   private static final List<Validator> validators = new ArrayList<>();
40   private static Logger logger = LoggerFactory.getLogger(ValidationConfigurationManager.class);
41
42   /**
43    * Init validators list.
44    *
45    * @return the list
46    */
47   public static List<Validator> initValidators() {
48     synchronized (validators) {
49       if (CollectionUtils.isEmpty(validators)) {
50         InputStream validationConfigurationJson =
51             FileUtils.getFileInputStream(VALIDATION_CONFIGURATION);
52         ValidationConfiguration validationConfiguration =
53             JsonUtil.json2Object(validationConfigurationJson, ValidationConfiguration.class);
54         List<ValidatorConfiguration> conf = validationConfiguration.getValidatorConfigurationList();
55         conf.stream().filter(ValidatorConfiguration::isEnableInd).forEachOrdered(
56             validatorConfiguration -> validators.add(validatorInit(validatorConfiguration)));
57       }
58     }
59     return validators;
60   }
61
62   private static Validator validatorInit(ValidatorConfiguration validatorConf) {
63     Validator validator = null;
64     try {
65       validator =
66           CommonMethods.newInstance(validatorConf.getImplementationClass(), Validator.class);
67     } catch (IllegalArgumentException iae) {
68       logger.error("Validator:" + validatorConf.getName() + " Class:"
69           + validatorConf.getImplementationClass() + " failed in initialization. error:"
70           + iae.toString() + " trace:" + Arrays.toString(iae.getStackTrace()));
71     }
72     return validator;
73   }
74 }