From 280f8015d06af1f41a3ef12e8300801c7a5e0d54 Mon Sep 17 00:00:00 2001 From: AviZi Date: Fri, 9 Jun 2017 02:39:56 +0300 Subject: [SDC-29] Amdocs OnBoard 1707 initial commit. Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370 Signed-off-by: AviZi --- .../openecomp/core/utilities/CommonMethods.java | 178 ++++++++++++++++----- .../core/utilities/file/FileContentHandler.java | 5 + .../openecomp/core/utilities/file/FileUtils.java | 32 +++- .../utilities/json/JsonSchemaDataGenerator.java | 31 ++-- .../openecomp/core/utilities/json/JsonUtil.java | 35 ++-- .../openecomp/core/utilities/yaml/YamlUtil.java | 77 ++++----- 6 files changed, 234 insertions(+), 124 deletions(-) (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org') diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java index 343636bd15..81a82c6298 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java @@ -21,6 +21,7 @@ package org.openecomp.core.utilities; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.collections4.MapUtils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -36,8 +37,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.UUID; @@ -45,14 +48,12 @@ import java.util.UUID; * This class provides auxiliary static methods. */ public class CommonMethods { - //private static final Logger logger = LoggerFactory.getLogger(CommonMethods.class); - - private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); private static final char[] CHARS = new char[]{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + private static final char[] hexArray = "0123456789ABCDEF".toCharArray(); /** * Private default constructor to prevent instantiation of the class objects. @@ -73,8 +74,8 @@ public class CommonMethods { ObjectOutputStream ds = new ObjectOutputStream(byteArray); ds.writeObject(object); ds.close(); - } catch (IOException e0) { - throw new RuntimeException(e0); + } catch (IOException exception) { + throw new RuntimeException(exception); } return byteArray.toByteArray(); @@ -93,8 +94,8 @@ public class CommonMethods { ObjectInputStream stream = new ObjectInputStream(new ByteArrayInputStream(bytes)); obj = (Serializable) stream.readObject(); stream.close(); - } catch (IOException | ClassNotFoundException e0) { - throw new RuntimeException(e0); + } catch (IOException | ClassNotFoundException exception) { + throw new RuntimeException(exception); } return obj; @@ -185,8 +186,8 @@ public class CommonMethods { /** * Converts the array with Long elements to the array with long (primitive type). * - * @param array input array with Long elements. - * @return array with the same elements converted to the long type (primitive). + * @param array input array with Long elements + * @return array with the same elements converted to the long type (primitive) */ public static long[] toPrimitive(Long[] array) { if (array == null) { @@ -255,12 +256,12 @@ public class CommonMethods { * empty. * * @param the type parameter - * @param left Elements of this array will be copied to positions from 0 to - * left.length - 1 in the target array. - * @param right Elements of this array will be copied to positions from - * left.length to left.length + right.length - * @return A newly allocate Java array that accommodates elements of source (left/right) - arraysor one of source arrays if another is empty, null - otherwise. + * @param left Elements of this array will be copied to positions from 0 to left.length - + * 1 in the target array. + * @param right Elements of this array will be copied to positions from left.length to + * left.length + right.length + * @return A newly allocate Java array that accommodates elements of source (left/right) arrays + orone of source arrays if another is empty, null - otherwise. */ @SuppressWarnings("unchecked") public static T[] concat(T[] left, T[] right) { @@ -288,25 +289,25 @@ public class CommonMethods { * * @param the type parameter * @param the type parameter - * @param b0 An object instance to be casted to the specified Java type. + * @param b1 An object instance to be casted to the specified Java type. * @param cls Target Java type. * @return Object instance safely casted to the requested Java type. - * @throws ClassCastException In case which is the given object is not instance of the - * specified Java type. + * @throws ClassCastException In case which is the given object is not instance of the specified + * Java type. */ @SuppressWarnings("unchecked") - public static D cast(B b0, Class cls) { - D d0 = null; - if (b0 != null) { - if (!cls.isInstance(b0)) { + public static D cast(B b1, Class cls) { + D d1 = null; + if (b1 != null) { + if (!cls.isInstance(b1)) { throw new ClassCastException(String - .format("Failed to cast from '%s' to '%s'", b0.getClass().getName(), cls.getName())); + .format("Failed to cast from '%s' to '%s'", b1.getClass().getName(), cls.getName())); } else { - d0 = (D) b0; + d1 = (D) b1; } } - return d0; + return d1; } // cast /** @@ -349,8 +350,8 @@ public class CommonMethods { Class impl = (Class) temp; return newInstance(impl); - } catch (ClassNotFoundException e0) { - throw new IllegalArgumentException(e0); + } catch (ClassNotFoundException exception) { + throw new IllegalArgumentException(exception); } } @@ -364,10 +365,10 @@ public class CommonMethods { public static T newInstance(Class cls) { try { return cls.newInstance(); - } catch (InstantiationException e0) { - throw new RuntimeException(e0); - } catch (IllegalAccessException e0) { - throw new RuntimeException(e0); + } catch (InstantiationException exception) { + throw new RuntimeException(exception); + } catch (IllegalAccessException exception) { + throw new RuntimeException(exception); } } @@ -388,15 +389,15 @@ public class CommonMethods { /** * Gets stack trace. * - * @param t0 the t 0 + * @param throwable the throwable * @return the stack trace */ - public static String getStackTrace(Throwable t0) { - if (null == t0) { + public static String getStackTrace(Throwable throwable) { + if (null == throwable) { return ""; } StringWriter sw = new StringWriter(); - t0.printStackTrace(new PrintWriter(sw)); + throwable.printStackTrace(new PrintWriter(sw)); return sw.toString(); } @@ -416,8 +417,8 @@ public class CommonMethods { String str = sw.toString(); try { sw.close(); - } catch (IOException e0) { - System.err.println(e0); + } catch (IOException exception) { + System.err.println(exception); } return str; @@ -530,10 +531,10 @@ public class CommonMethods { public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int j = 0; j < bytes.length; j++) { - int v0 = bytes[j] & 0xFF; - int x0 = j << 1; - hexChars[x0] = hexArray[v0 >>> 4]; - hexChars[x0 + 1] = hexArray[v0 & 0x0F]; + int var = bytes[j] & 0xFF; + int x1 = j << 1; + hexChars[x1] = hexArray[var >>> 4]; + hexChars[x1 + 1] = hexArray[var & 0x0F]; } return new String(hexChars); } @@ -543,13 +544,106 @@ public class CommonMethods { * * @param the class of the objects in the set * @param element the single element to be contained in the returned Set - * @return an immutable set containing only the specified object.The returned set is serializable. + * @return an immutable set containing only the specified object. The returned set is + serializable. */ public static Set toSingleElementSet(T element) { return Collections.singleton(element); } + /** + * Merge lists of map list. + * + * @param the type parameter + * @param the type parameter + * @param target the target + * @param source the source + * @return the list + */ + public static List> mergeListsOfMap(List> target, + List> source) { + List> retList = new ArrayList<>(); + if (Objects.nonNull(target)) { + retList.addAll(target); + } + + if (Objects.nonNull(source)) { + for (Map sourceMap : source) { + for (Map.Entry entry : sourceMap.entrySet()) { + mergeEntryInList(entry.getKey(), entry.getValue(), retList); + } + } + } + return retList; + } + + /** + * Merge lists list. + * + * @param the type parameter + * @param target the target + * @param source the source + * @return the list + */ + public static List mergeLists(List target, List source) { + List retList = new ArrayList<>(); + + if (Objects.nonNull(source)) { + retList.addAll(source); + } + if (Objects.nonNull(target)) { + retList.addAll(target); + } + + return retList; + } + + /** + * Merge entry in list. + * + * @param the type parameter + * @param the type parameter + * @param key the key + * @param value the value + * @param target the target + */ + public static void mergeEntryInList(T key, S value, List> target) { + boolean found = false; + for (Map map : target) { + if (map.containsKey(key)) { + map.put(key, value); + found = true; + } + } + + if (!found) { + Map newMap = new HashMap<>(); + newMap.put(key, value); + target.add(newMap); + } + } + + + /** + * Merge maps map. + * + * @param the type parameter + * @param the type parameter + * @param target the target + * @param source the source + * @return the map + */ + public static Map mergeMaps(Map target, Map source) { + Map retMap = new HashMap<>(); + if (MapUtils.isNotEmpty(source)) { + retMap.putAll(source); + } + if (MapUtils.isNotEmpty(target)) { + retMap.putAll(target); + } + return retMap; + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java index 67a79875d4..c828505932 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileContentHandler.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Set; public class FileContentHandler { + private Map files = new HashMap<>(); /** @@ -57,6 +58,10 @@ public class FileContentHandler { files.put(fileName, FileUtils.toByteArray(is)); } + public Map getFiles() { + return files; + } + public void setFiles(FileContentHandler extFiles) { extFiles.getFileList().stream() .forEach(fileName -> this.addFile(fileName, extFiles.getFileContent(fileName))); diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java index 72fa9ac7b8..664ccb68c6 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java @@ -20,14 +20,14 @@ package org.openecomp.core.utilities.file; +import org.apache.commons.collections4.CollectionUtils; import org.openecomp.core.utilities.json.JsonUtil; import org.openecomp.core.utilities.yaml.YamlUtil; +import org.openecomp.sdc.logging.types.LoggerConstants; +import org.openecomp.sdc.logging.types.LoggerErrorDescription; +import org.slf4j.MDC; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.URL; import java.util.ArrayList; import java.util.Enumeration; @@ -58,6 +58,23 @@ public class FileUtils { return is; } + /** + * Gets file input stream. + * + * @param urlFile the url file + * @return the file input stream + */ + public static InputStream getFileInputStream(URL urlFile) { + InputStream is; + try { + assert urlFile != null; + is = urlFile.openStream(); + } catch (IOException exception) { + throw new RuntimeException(exception); + } + return is; + } + /** * Gets file input streams. * @@ -134,9 +151,9 @@ public class FileUtils { * @return the input stream */ public static InputStream loadFileToInputStream(String fileName) { - URL urlFile = FileUtils.class.getClassLoader().getResource(fileName); + URL urlFile = Thread.currentThread().getContextClassLoader().getResource(fileName); try { - Enumeration en = FileUtils.class.getClassLoader().getResources(fileName); + Enumeration en = Thread.currentThread().getContextClassLoader().getResources(fileName); while (en.hasMoreElements()) { urlFile = en.nextElement(); } @@ -248,6 +265,7 @@ public class FileUtils { return mapFileContent; } + /** * The enum File extension. */ diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonSchemaDataGenerator.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonSchemaDataGenerator.java index 8968eeb323..84e4f6d7c9 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonSchemaDataGenerator.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonSchemaDataGenerator.java @@ -22,22 +22,17 @@ package org.openecomp.core.utilities.json; import org.json.JSONException; import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; import java.util.HashMap; import java.util.Map; -/** - * The type Json schema data generator. - */ public class JsonSchemaDataGenerator { private static final String ROOT = "root"; - private static final Logger logger = LoggerFactory.getLogger(JsonSchemaDataGenerator.class); - /** - * The Include defaults. - */ + private static final Logger logger = + (Logger) LoggerFactory.getLogger(JsonSchemaDataGenerator.class); boolean includeDefaults = true; private JSONObject root; private Map referencesData; @@ -54,11 +49,6 @@ public class JsonSchemaDataGenerator { root = new JSONObject(jsonSchema); } - /** - * Sets include defaults. - * - * @param includeDefaults the include defaults - */ public void setIncludeDefaults(boolean includeDefaults) { this.includeDefaults = includeDefaults; } @@ -66,15 +56,16 @@ public class JsonSchemaDataGenerator { /** * Generates json data that conform to the schema according to turned on flags. * - * @return json that conform to the schema. + * @return json that conform to the schema */ public String generateData() { referencesData = new HashMap<>(); JSONObject data = new JSONObject(); generateData(ROOT, root, - data); // "root" is dummy name to represent the top level object (which, as apposed to - // inner objects, doesn't have a name in the schema) + data); + // "root" is dummy name to represent the top level object + // (which, as apposed to inner objects, doesn't have a name in the schema) return data.has(ROOT) ? data.get(ROOT).toString() : data.toString(); } @@ -161,12 +152,12 @@ public class JsonSchemaDataGenerator { default: break; } - } catch (JSONException e0) { + } catch (JSONException exception) { Object defaultValue = property.get(JsonSchemaKeyword.DEFAULT); logger.error(String.format( "Invalid schema: '%s' property type is '%s' but it has a default value which is not: %s.", - propertyName, propertyType, defaultValue), e0); - throw e0; + propertyName, propertyType, defaultValue), exception); + throw exception; } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java index 6ae3677a8d..87c75fce62 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/json/JsonUtil.java @@ -20,14 +20,13 @@ package org.openecomp.core.utilities.json; - import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonIOException; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; -import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections4.CollectionUtils; import org.everit.json.schema.EnumSchema; import org.everit.json.schema.Schema; import org.everit.json.schema.ValidationException; @@ -82,17 +81,17 @@ public class JsonUtil { * @return the t */ public static T json2Object(String json, Class classOfT) { - T type; + T typ; try { try (Reader br = new StringReader(json)) { - type = new Gson().fromJson(br, classOfT); - } catch (IOException e0) { - throw e0; + typ = new Gson().fromJson(br, classOfT); + } catch (IOException exception) { + throw exception; } - } catch (JsonIOException | JsonSyntaxException | IOException e0) { - throw new RuntimeException(e0); + } catch (JsonIOException | JsonSyntaxException | IOException exception) { + throw new RuntimeException(exception); } - return type; + return typ; } /** @@ -109,8 +108,8 @@ public class JsonUtil { try (Reader br = new BufferedReader(new InputStreamReader(is))) { type = new Gson().fromJson(br, classOfT); } - } catch (JsonIOException | JsonSyntaxException | IOException e0) { - throw new RuntimeException(e0); + } catch (JsonIOException | JsonSyntaxException | IOException exception) { + throw new RuntimeException(exception); } finally { if (is != null) { try { @@ -153,16 +152,16 @@ public class JsonUtil { .collect(Collectors.toList()); } - private static String mapValidationExceptionToMessage(ValidationException e0) { - if (e0.getViolatedSchema() instanceof EnumSchema) { - return mapEnumViolationToMessage(e0); + private static String mapValidationExceptionToMessage(ValidationException exception) { + if (exception.getViolatedSchema() instanceof EnumSchema) { + return mapEnumViolationToMessage(exception); } - return e0.getMessage(); + return exception.getMessage(); } - private static String mapEnumViolationToMessage(ValidationException e1) { - Set possibleValues = ((EnumSchema) e1.getViolatedSchema()).getPossibleValues(); - return e1.getMessage().replaceFirst("enum value", possibleValues.size() == 1 + private static String mapEnumViolationToMessage(ValidationException exception) { + Set possibleValues = ((EnumSchema) exception.getViolatedSchema()).getPossibleValues(); + return exception.getMessage().replaceFirst("enum value", possibleValues.size() == 1 ? String.format("value. %s is the only possible value for this field", possibleValues.iterator().next()) : String.format("value. Possible values: %s", CommonMethods diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/yaml/YamlUtil.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/yaml/YamlUtil.java index 56261f08de..d38bf484e4 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/yaml/YamlUtil.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/yaml/YamlUtil.java @@ -20,9 +20,9 @@ package org.openecomp.core.utilities.yaml; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.core.utilities.CommonMethods; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.Yaml; @@ -45,19 +45,20 @@ import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; + /** * The type Yaml util. */ public class YamlUtil { - private static Logger logger = LoggerFactory.getLogger(YamlUtil.class); + private static Logger logger = (Logger) LoggerFactory.getLogger(YamlUtil.class); /** * Yaml to object t. * * @param the type parameter * @param yamlContent the yaml content - * @param typClass the typ class + * @param typClass the t class * @return the t */ public T yamlToObject(String yamlContent, Class typClass) { @@ -76,7 +77,7 @@ public class YamlUtil { * * @param the type parameter * @param yamlContent the yaml content - * @param typClass the typ class + * @param typClass the t class * @return the t */ public T yamlToObject(InputStream yamlContent, Class typClass) { @@ -102,7 +103,7 @@ public class YamlUtil { yamlContent.close(); } } catch (IOException ignore) { - //nothing to dd + //do nothing } } } @@ -111,8 +112,8 @@ public class YamlUtil { /** * Gets constructor. * - * @param the type parameter - * @param typClass the typ class + * @param the type parameter + * @param typClass the t class * @return the constructor */ public Constructor getConstructor(Class typClass) { @@ -128,6 +129,7 @@ public class YamlUtil { return new MyPropertyUtils(); } + /** * Yaml to map map. * @@ -145,7 +147,7 @@ public class YamlUtil { * Object to yaml string. * * @param the type parameter - * @param obj the obj + * @param obj the obj * @return the string */ public String objectToYaml(Object obj) { @@ -191,6 +193,16 @@ public class YamlUtil { private class CustomRepresenter extends Representer { + @Override + protected MappingNode representJavaBean(Set properties, Object javaBean) { + //remove the bean type from the output yaml (!! ...) + if (!classTags.containsKey(javaBean.getClass())) { + addClassTag(javaBean.getClass(), Tag.MAP); + } + + return super.representJavaBean(properties, javaBean); + } + @Override protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) { @@ -205,16 +217,6 @@ public class YamlUtil { : defaultNode; } } - - @Override - protected MappingNode representJavaBean(Set properties, Object javaBean) { - //remove the bean type from the output yaml (!! ...) - if (!classTags.containsKey(javaBean.getClass())) { - addClassTag(javaBean.getClass(), Tag.MAP); - } - - return super.representJavaBean(properties, javaBean); - } } @@ -222,6 +224,14 @@ public class YamlUtil { * The type My property utils. */ public class MyPropertyUtils extends PropertyUtils { + //Unsorted properties + @Override + protected Set createPropertySet(Class type, BeanAccess bnAccess) + throws IntrospectionException { + return new LinkedHashSet(getPropertiesMap(type, + BeanAccess.FIELD).values()); + } + @Override public Property getProperty(Class type, String name) throws IntrospectionException { if (name.equals("default")) { @@ -230,14 +240,6 @@ public class YamlUtil { return super.getProperty(type, name); } - //Unsorted properties - @Override - protected Set createPropertySet(Class type, BeanAccess beanAccess) - throws IntrospectionException { - return new LinkedHashSet(getPropertiesMap(type, - BeanAccess.FIELD).values()); - } - } /** @@ -254,16 +256,6 @@ public class YamlUtil { super(theRoot); } - @Override - protected Map constructMapping(MappingNode node) { - try { - return super.constructMapping(node); - } catch (IllegalStateException exception) { - throw new ParserException("while parsing MappingNode", node.getStartMark(), - exception.getMessage(), node.getEndMark()); - } - } - @Override protected Map createDefaultMap() { final Map delegate = super.createDefaultMap(); @@ -282,6 +274,17 @@ public class YamlUtil { } }; } + + @Override + protected Map constructMapping(MappingNode node) { + try { + return super.constructMapping(node); + } catch (IllegalStateException exception) { + throw new ParserException("while parsing MappingNode", + node.getStartMark(), exception.getMessage(), + node.getEndMark()); + } + } } } -- cgit 1.2.3-korg