diff options
author | AviZi <avi.ziv@amdocs.com> | 2017-06-09 02:39:56 +0300 |
---|---|---|
committer | AviZi <avi.ziv@amdocs.com> | 2017-06-09 02:39:56 +0300 |
commit | 280f8015d06af1f41a3ef12e8300801c7a5e0d54 (patch) | |
tree | 9c1d3978c04cd28068f02073038c936bb49ca9e0 /openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src | |
parent | fd3821dad11780d33c5373d74c957c442489945e (diff) |
[SDC-29] Amdocs OnBoard 1707 initial commit.
Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370
Signed-off-by: AviZi <avi.ziv@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src')
9 files changed, 213 insertions, 242 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/impl/ValidationManagerImpl.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/impl/ValidationManagerImpl.java index 64c409b463..acb4eed448 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/impl/ValidationManagerImpl.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/impl/ValidationManagerImpl.java @@ -21,28 +21,40 @@ package org.openecomp.sdc.validation.impl; import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.core.validation.api.ValidationManager; -import org.openecomp.core.validation.interfaces.Validator; +import org.openecomp.sdc.validation.Validator; import org.openecomp.core.validation.types.GlobalValidationContext; import org.openecomp.core.validation.types.MessageContainer; import org.openecomp.sdc.datatypes.error.ErrorMessage; -import org.openecomp.sdc.validation.utils.ValidationConfigurationManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.validation.services.ValidationFactory; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; public class ValidationManagerImpl implements ValidationManager { - private static Logger logger = LoggerFactory.getLogger(ValidationManagerImpl.class); + private static Logger logger = (Logger) LoggerFactory.getLogger(ValidationManagerImpl.class); + private GlobalValidationContext globalContext; private List<Validator> validators; public ValidationManagerImpl() { globalContext = new GlobalValidationContext(); - validators = ValidationConfigurationManager.initValidators(); + validators = ValidationFactory.getValidators(); + } + + @Override + public Map<String, List<ErrorMessage>> validate() { + for (Validator validator : validators) { + if(Objects.nonNull(validator)) { + validator.validate(globalContext); + } + } + return convertMessageContainsToErrorMessage(globalContext.getContextMessageContainers()); } @Override @@ -51,11 +63,8 @@ public class ValidationManagerImpl implements ValidationManager { } @Override - public Map<String, List<ErrorMessage>> validate() { - for (Validator validator : validators) { - validator.validate(globalContext); - } - return convertMessageContainsToErrorMessage(globalContext.getContextMessageContainers()); + public void updateGlobalContext(GlobalValidationContext globalContext) { + this.globalContext = globalContext; } private Map<String, List<ErrorMessage>> convertMessageContainsToErrorMessage( @@ -66,4 +75,5 @@ public class ValidationManagerImpl implements ValidationManager { .forEach(entry -> errors.put(entry.getKey(), entry.getValue().getErrorMessageList())); return errors; } + } diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/services/ValidationFactory.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/services/ValidationFactory.java new file mode 100644 index 0000000000..b290e7cda9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/services/ValidationFactory.java @@ -0,0 +1,58 @@ +package org.openecomp.sdc.validation.services; + +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.config.api.Configuration; +import org.openecomp.config.api.ConfigurationManager; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.validation.Validator; +import org.openecomp.sdc.validation.type.ConfigConstants; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class ValidationFactory { + private static final List<Validator> validators = new ArrayList<>(); + private static Map<String, ImplementationConfiguration> validationImplMap; + private static Logger logger = + (Logger) LoggerFactory.getLogger(ValidationFactory.class); + private static File file; + + + static { + Configuration config = ConfigurationManager.lookup(); + validationImplMap = config.populateMap(ConfigConstants.Namespace, + ConfigConstants.Validator_Impl_Key, ImplementationConfiguration.class); + validationImplMap.putAll(config.populateMap(ConfigConstants.Mandatory_Namespace, + ConfigConstants.Validator_Impl_Key, ImplementationConfiguration.class)); + } + + /** + * Initialize a validator. + */ + public static List<Validator> getValidators() { + synchronized (validators) { + if (CollectionUtils.isEmpty(validators)) { + validationImplMap.values().stream() + .filter(implementationConfiguration -> implementationConfiguration + .isEnable()).forEachOrdered(implementationConfiguration -> validators.add + (cerateValidatorImpl(implementationConfiguration))); + } + } + return validators; + } + + private static Validator cerateValidatorImpl(ImplementationConfiguration validatorConf) { + Validator validator = null; + validator = + CommonMethods.newInstance(validatorConf.getImplementationClass(), Validator.class); + validator.init(validatorConf.getProperties()); + + logger.debug("created validator" + validatorConf.getImplementationClass()); + return validator; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationConfiguration.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationConfiguration.java deleted file mode 100644 index 1a74e1cc34..0000000000 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationConfiguration.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * 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.sdc.validation.utils; - -import java.util.Collections; -import java.util.List; - -public class ValidationConfiguration { - private List<ValidatorConfiguration> validatorConfigurationList; - - public List<ValidatorConfiguration> getValidatorConfigurationList() { - return Collections.unmodifiableList(validatorConfigurationList); - } - - public void setValidatorConfigurationList( - List<ValidatorConfiguration> validatorConfigurationList) { - this.validatorConfigurationList = validatorConfigurationList; - } -} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationConfigurationManager.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationConfigurationManager.java deleted file mode 100644 index cc921ce985..0000000000 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationConfigurationManager.java +++ /dev/null @@ -1,74 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * 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.sdc.validation.utils; - -import org.apache.commons.collections4.CollectionUtils; -import org.openecomp.core.utilities.CommonMethods; -import org.openecomp.core.utilities.file.FileUtils; -import org.openecomp.core.utilities.json.JsonUtil; -import org.openecomp.core.validation.interfaces.Validator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class ValidationConfigurationManager { - - private static final String VALIDATION_CONFIGURATION = "validationConfiguration.json"; - private static final List<Validator> validators = new ArrayList<>(); - private static Logger logger = LoggerFactory.getLogger(ValidationConfigurationManager.class); - - /** - * Init validators list. - * - * @return the list - */ - public static List<Validator> initValidators() { - synchronized (validators) { - if (CollectionUtils.isEmpty(validators)) { - InputStream validationConfigurationJson = - FileUtils.getFileInputStream(VALIDATION_CONFIGURATION); - ValidationConfiguration validationConfiguration = - JsonUtil.json2Object(validationConfigurationJson, ValidationConfiguration.class); - List<ValidatorConfiguration> conf = validationConfiguration.getValidatorConfigurationList(); - conf.stream().filter(ValidatorConfiguration::isEnableInd).forEachOrdered( - validatorConfiguration -> validators.add(validatorInit(validatorConfiguration))); - } - } - return validators; - } - - private static Validator validatorInit(ValidatorConfiguration validatorConf) { - Validator validator = null; - try { - validator = - CommonMethods.newInstance(validatorConf.getImplementationClass(), Validator.class); - } catch (IllegalArgumentException iae) { - logger.error("Validator:" + validatorConf.getName() + " Class:" - + validatorConf.getImplementationClass() + " failed in initialization. error:" - + iae.toString() + " trace:" + Arrays.toString(iae.getStackTrace())); - } - return validator; - } -} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationManagerUtil.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationManagerUtil.java deleted file mode 100644 index 960bdc2165..0000000000 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidationManagerUtil.java +++ /dev/null @@ -1,65 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * 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.sdc.validation.utils; - -import org.openecomp.core.utilities.file.FileContentHandler; -import org.openecomp.core.utilities.file.FileUtils; -import org.openecomp.core.validation.api.ValidationManager; -import org.openecomp.core.validation.errors.Messages; -import org.openecomp.core.validation.factory.ValidationManagerFactory; -import org.openecomp.sdc.common.utils.AsdcCommon; -import org.openecomp.sdc.datatypes.error.ErrorLevel; -import org.openecomp.sdc.datatypes.error.ErrorMessage; - -import java.io.InputStream; -import java.util.List; -import java.util.Map; - -public class ValidationManagerUtil { - - /** - * Handle missing manifest. - * - * @param fileContentMap the file content map - * @param errors the errors - */ - public static void handleMissingManifest(FileContentHandler fileContentMap, - Map<String, List<ErrorMessage>> errors) { - InputStream manifest = fileContentMap.getFileContent(AsdcCommon.MANIFEST_NAME); - if (manifest == null) { - ErrorMessage.ErrorMessageUtil.addMessage(AsdcCommon.MANIFEST_NAME, errors) - .add(new ErrorMessage(ErrorLevel.ERROR, Messages.MANIFEST_NOT_EXIST.getErrorMessage())); - } - } - - /** - * Init validation manager validation manager. - * - * @param fileContentMap the file content map - * @return the validation manager - */ - public static ValidationManager initValidationManager(FileContentHandler fileContentMap) { - ValidationManager validationManager = ValidationManagerFactory.getInstance().createInterface(); - fileContentMap.getFileList().stream().forEach(fileName -> validationManager - .addFile(fileName, FileUtils.toByteArray(fileContentMap.getFileContent(fileName)))); - return validationManager; - } -} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidatorConfiguration.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidatorConfiguration.java deleted file mode 100644 index 12feb641ae..0000000000 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/java/org/openecomp/sdc/validation/utils/ValidatorConfiguration.java +++ /dev/null @@ -1,51 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * 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.sdc.validation.utils; - -public class ValidatorConfiguration { - private String name; - private boolean enableInd = true; - private String implementationClass; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public boolean isEnableInd() { - return enableInd; - } - - public void setEnableInd(boolean enableInd) { - this.enableInd = enableInd; - } - - public String getImplementationClass() { - return implementationClass; - } - - public void setImplementationClass(String implementationClass) { - this.implementationClass = implementationClass; - } -} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/resources/config-mandatory-validaton.json b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/resources/config-mandatory-validaton.json new file mode 100644 index 0000000000..e221467baf --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/resources/config-mandatory-validaton.json @@ -0,0 +1,20 @@ +{ + "validatorImplementations": { + "yamlValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.YamlValidator" + }, + "heatValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.HeatValidator" + }, + "manifestValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.ManifestValidator" + } + }, + + "_config":{ + "namespace": "mandatoryValidation" + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/resources/config-validaton.json b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/resources/config-validaton.json new file mode 100644 index 0000000000..08c42880b2 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/main/resources/config-validaton.json @@ -0,0 +1,87 @@ +{ + "validatorImplementations": { + "sharedResourceGuideLineValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.SharedResourceGuideLineValidator" + }, + "forbiddenResourceGuideLineValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.ForbiddenResourceGuideLineValidator", + "properties": { + "forbiddenResourceTypes": { + "OS::Neutron::FloatingIP": { + "enable": true + } + } + } + }, + "contrailValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.ContrailValidator" + }, + "resourceHeatValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.HeatResourceValidator", + "properties": { + "resourceBaseValidator": { + "OS::Nova::Server": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.heatresource.NovaServerResourceValidator" + }, + "OS::Nova::ServerGroup": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.heatresource.NovaServerGroupResourceValidator" + }, + "OS::Heat::ResourceGroup": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.heatresource.ResourceGroupResourceValidator" + }, + "OS::Neutron::Port": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.heatresource.NeutronPortResourceValidator" + }, + "OS::Contrail::NetworkPolicy": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.heatresource.ContrailNetworkPolicyResourceValidator" + }, + "OS::Neutron::SecurityGroup":{ + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.heatresource.NeutronSecurityGroupResourceValidator" + }, + "nestedResource": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.heatresource.NestedResourceValidator" + } + } + } + }, + "namingConventionGuideLineValidator": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.NamingConventionGuideLineValidator", + "properties": { + "resourceBaseValidator": { + "OS::Nova::Server": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.namingconvention.NovaServerNamingConventionGuideLineValidator" + }, + "OS::Neutron::Port": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.namingconvention.NeutronPortNamingConventionValidator" + }, + "OS::Contrail::ServiceTemplate": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.namingconvention.ContrailServiceTemplateNamingConventionValidator" + }, + "OS::Contrail::ServiceInstance": { + "enable": true, + "implementationClass": "org.openecomp.sdc.validation.impl.validators.namingconvention.ContrailServiceInstanceNamingConventionValidator" + } + } + } + } + }, + + "_config":{ + "namespace": "validation" + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java index 6a9f8df12d..8bb95afacd 100644 --- a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-core/src/test/java/org/openecomp/sdc/heat/services/tree/HeatTreeManagerTest.java @@ -1,8 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * 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.sdc.heat.services.tree; -import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; +import org.openecomp.config.ConfigurationUtils; import org.openecomp.core.utilities.file.FileContentHandler; import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree; import org.testng.Assert; import org.testng.annotations.Test; @@ -10,6 +31,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + public class HeatTreeManagerTest { @@ -34,7 +59,7 @@ public class HeatTreeManagerTest { heatTreeManager.createTree(); HeatStructureTree tree = heatTreeManager.getTree(); Assert.assertNotNull(tree); - Assert.assertEquals(tree.getHEAT().size(), 2); + Assert.assertEquals(tree.getHeat().size(), 2); } private byte[] getFileContent(File file) { @@ -46,6 +71,4 @@ public class HeatTreeManagerTest { return new byte[0]; } - - } |