diff options
author | Michael Lando <ml636r@att.com> | 2017-02-19 12:35:04 +0200 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2017-02-19 12:35:04 +0200 |
commit | f5f13c4f6b6fe3b4d98e349dfd7db59339803436 (patch) | |
tree | 72caffc93fab394ffa3b761505775331f1c559b9 /openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types | |
parent | 451a3400b76511393c62a444f588a4ed15f4a549 (diff) |
push addional code
Change-Id: Ia427bb3460cda3a896f8faced2de69eaf3807b74
Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types')
4 files changed, 341 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/FileValidationContext.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/FileValidationContext.java new file mode 100644 index 0000000000..81e8931dab --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/FileValidationContext.java @@ -0,0 +1,53 @@ +/*- + * ============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.core.validation.types; + + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +public class FileValidationContext { + private String fileName; + private MessageContainer messageContainer = new MessageContainer(); + private byte[] content; + + public FileValidationContext(String fileName, byte[] fileContent) { + this.fileName = fileName; + this.content = fileContent; + } + + + MessageContainer getMessageContainer() { + return this.messageContainer; + } + + public InputStream getContent() { + return new ByteArrayInputStream(content); + } + + public String getFileName() { + return this.fileName; + } + + public boolean isEmpty() { + return content == null || content.length == 0; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/GlobalValidationContext.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/GlobalValidationContext.java new file mode 100644 index 0000000000..e6917e84f6 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/GlobalValidationContext.java @@ -0,0 +1,142 @@ +/*- + * ============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.core.validation.types; + +import org.apache.commons.collections4.CollectionUtils; +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.core.validation.interfaces.Validator; +import org.openecomp.sdc.common.utils.AsdcCommon; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.InputStream; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiPredicate; +import java.util.stream.Collectors; + +public class GlobalValidationContext { + + private static Logger logger = LoggerFactory.getLogger(Validator.class); + private Map<String, FileValidationContext> fileContextMap = new HashMap<>(); + private Map<String, MessageContainer> messageContainerMap = new HashMap<>(); + + /** + * Add message. + * + * @param fileName the file name + * @param level the level + * @param message the message + */ + public void addMessage(String fileName, ErrorLevel level, String message) { + + printLog(fileName, message, level); + + if (fileContextMap.containsKey(fileName)) { + fileContextMap.get(fileName).getMessageContainer().getMessageBuilder().setMessage(message) + .setLevel(level).create(); + } else { + if (CommonMethods.isEmpty(fileName)) { + fileName = AsdcCommon.UPLOAD_FILE; + } + MessageContainer messageContainer; + synchronized (this) { + messageContainer = messageContainerMap.get(fileName); + if (messageContainer == null) { + messageContainer = new MessageContainer(); + messageContainerMap.put(fileName, messageContainer); + } + } + messageContainer.getMessageBuilder().setMessage(message).setLevel(level).create(); + } + } + + /** + * Gets file content. + * + * @param fileName the file name + * @return the file content + */ + public InputStream getFileContent(String fileName) { + FileValidationContext fileContext = fileContextMap.get(fileName); + if (fileContext == null || fileContext.isEmpty()) { + return null; + } + return fileContext.getContent(); + } + + public void addFileContext(String fileName, byte[] fileContent) { + fileContextMap.put(fileName, new FileValidationContext(fileName, fileContent)); + } + + /** + * Gets context message containers. + * + * @return the context message containers + */ + public Map<String, MessageContainer> getContextMessageContainers() { + + Map<String, MessageContainer> contextMessageContainer = new HashMap<>(); + fileContextMap.entrySet().stream().filter(entry -> CollectionUtils + .isNotEmpty(entry.getValue().getMessageContainer().getErrorMessageList())).forEach( + entry -> contextMessageContainer.put( + entry.getKey(), entry.getValue().getMessageContainer())); + messageContainerMap.entrySet().stream() + .filter(entry -> CollectionUtils.isNotEmpty(entry.getValue().getErrorMessageList())) + .forEach(entry -> contextMessageContainer.put(entry.getKey(), entry.getValue())); + return contextMessageContainer; + } + + public Map<String, FileValidationContext> getFileContextMap() { + return fileContextMap; + } + + private void printLog(String fileName, String message, ErrorLevel level) { + + String messageToPrint = message + " in file[" + fileName + "]"; + + switch (level) { + case ERROR: + logger.error(messageToPrint); + break; + case WARNING: + logger.warn(messageToPrint); + break; + case INFO: + logger.info(messageToPrint); + break; + default: + break; + } + } + + public Collection<String> files(BiPredicate<String, GlobalValidationContext> func) { + return fileContextMap.keySet().stream().filter(t -> func.test(t, this)) + .collect(Collectors.toList()); + } + + public Collection<String> getFiles() { + return this.getFileContextMap().keySet(); + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/MessageContainer.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/MessageContainer.java new file mode 100644 index 0000000000..9d37bade67 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/MessageContainer.java @@ -0,0 +1,82 @@ +/*- + * ============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.core.validation.types; + +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Predicate; + +public class MessageContainer { + + private List<ErrorMessage> errorMessageList = new ArrayList<>(); + + public List<ErrorMessage> getErrorMessageList() { + return errorMessageList; + } + + public MessageBuilder getMessageBuilder() { + return new MessageBuilder(); + } + + /** + * Gets error message list by level. + * + * @param level the level + * @return the error message list by level + */ + public List<ErrorMessage> getErrorMessageListByLevel(ErrorLevel level) { + + List<ErrorMessage> errors = new ArrayList<>(); + errorMessageList.stream().filter(new Predicate<ErrorMessage>() { + @Override + public boolean test(ErrorMessage errorMessage) { + return errorMessage.getLevel().equals(level); + } + }).forEach(errorMessage -> errors.add(errorMessage)); + return errors; + } + + public class MessageBuilder { + + String message; + ErrorLevel level; + + MessageBuilder setMessage(String message) { + this.message = message; + return this; + } + + MessageBuilder setLevel(ErrorLevel level) { + this.level = level; + return this; + } + + void create() { + ErrorMessage errorMessage = new ErrorMessage(level, message); + if (!errorMessageList.contains(errorMessage)) { + errorMessageList.add(errorMessage); + } + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/MessageContainerUtil.java b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/MessageContainerUtil.java new file mode 100644 index 0000000000..c678bb5014 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-validation-lib/openecomp-sdc-validation-api/src/main/java/org/openecomp/core/validation/types/MessageContainerUtil.java @@ -0,0 +1,64 @@ +/*- + * ============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.core.validation.types; + +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.datatypes.error.ErrorMessage; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MessageContainerUtil { + + /** + * Gets message by level. + * + * @param level the level + * @param messages the messages + * @return the message by level + */ + public static Map<String, + List<ErrorMessage>> getMessageByLevel( + ErrorLevel level,Map<String,List<ErrorMessage>> messages) { + if (messages == null) { + return null; + } + Map<String, List<ErrorMessage>> filteredMessages = new HashMap<>(); + messages.entrySet().stream().forEach( + entry -> entry.getValue().stream().filter(message -> message.getLevel().equals(level)) + .forEach(message -> addMessage(entry.getKey(), message, filteredMessages + ))); + return filteredMessages; + } + + private static void addMessage(String fileName, ErrorMessage message, + Map<String, List<ErrorMessage>> messages) { + List<ErrorMessage> messageList = messages.get(fileName); + if (messageList == null) { + messageList = new ArrayList<>(); + messages.put(fileName, messageList); + } + messageList.add(message); + } +} |