2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.common.util;
23 import org.apache.commons.codec.binary.Base64;
24 import org.apache.commons.io.FileUtils;
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
26 import org.openecomp.sdc.common.api.Constants;
27 import org.openecomp.sdc.common.log.api.ILogConfiguration;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.regex.Pattern;
38 public class GeneralUtility {
40 public static boolean generateTextFile(String fileName, String fileData) {
41 boolean isSuccessfull = true;
43 FileUtils.writeStringToFile(new File(fileName), fileData);
44 } catch (IOException e) {
45 isSuccessfull = false;
51 * Use with care, usage is not advised!!!
52 * The method only checks if String does not contain special characters + length divided by 4 with no remainder.
53 * The methods contained in other common libraries do the same.
55 public static boolean isBase64Encoded(byte[] data) {
56 return Base64.isBase64(data);
60 *Use with care, usage is not advised!!!
61 * The method only checks if String does not contain special characters + length divided by 4 with no remainder.
62 * The methods contained in other common libraries do the same.
64 public static boolean isBase64Encoded(String str) {
65 boolean isEncoded = false;
67 // checks if the string was properly padded to the
68 isEncoded = ((str.length() % 4 == 0) && (Pattern.matches("\\A[a-zA-Z0-9/+]+={0,2}\\z", str)));
70 // If no exception is caught, then it is possibly a base64
72 byte[] data = Base64.decodeBase64(str);
75 } catch (Exception e) {
76 // If exception is caught, then it is not a base64 encoded string
83 * Checks whether the passed string exceeds a limit of number of characters.
87 * @return the result of comparison, or false if str is null.
89 public static boolean isExceedingLimit(String str, int limit) {
93 return str.length() > limit;
97 * Checks the passed string list whether the cumulative length of strings and delimiters between them exceeds a limit of number of characters. For example for list ("one","two","three") with delimiter "," the length of list is calculated
102 * @param delimiterLength
103 * - 0 if there is no delimeter.
104 * @return the result of comparison, or false if strList is null.
106 public static boolean isExceedingLimit(List<String> strList, int limit, int delimiterLength) {
107 if (strList == null || strList.isEmpty()) {
111 int size = strList.size();
112 for (int i = 0; i < size - 1; i++) {
113 String str = strList.get(i);
117 sum += delimiterLength;
119 String str = strList.get(size - 1);
127 * Return the extension as the substring from the last dot. For input "kuku.txt", "txt" will be returned. If no dot is found or input is null, empty string is returned.
132 public static String getFilenameExtension(String fileName) {
133 String res = Constants.EMPTY_STRING;
134 if (fileName != null) {
135 int indexOf = fileName.lastIndexOf('.');
136 if (indexOf != -1 && indexOf < (fileName.length() - 1)) {
137 res = fileName.substring(indexOf + 1);
143 public static String calculateMD5Base64EncodedByByteArray(byte[] payload) {
144 String decodedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(payload);
145 byte[] encodeMd5 = Base64.encodeBase64(decodedMd5.getBytes());
146 return new String(encodeMd5);
155 public static String calculateMD5Base64EncodedByString(String data) {
156 String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
158 // encode base-64 result
159 byte[] encodeBase64 = Base64.encodeBase64(calculatedMd5.getBytes());
160 return new String(encodeBase64);
167 * @return String is null or Empty
169 public static boolean isEmptyString(String str) {
170 return str == null || str.trim().isEmpty();
173 public static String getEcompRequestId() {
174 return MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
180 public static <T extends ICategorizedElement> Map<String,Map<String,List<T>>> getCategorizedComponents(List<T> components) {
182 Map<String, Map<String, List<T>>> categorizedMap = new HashMap<>();
183 components.forEach(component -> categorizeComponent(component, categorizedMap));
184 return categorizedMap;
187 private static <T extends ICategorizedElement> void categorizeComponent(T component, Map<String, Map<String, List<T>>> categorizedMap) {
188 if(component.getComponentTypeAsString().equals("SERVICE")){
189 categorizeService(component, categorizedMap);
190 }else if(component.getComponentTypeAsString().equals("RESOURCE")){
191 categorizeResource(component, categorizedMap);
195 private static <T extends ICategorizedElement> void categorizeResource(T component, Map<String, Map<String, List<T>>> categorizedMap) {
196 String category = component.getCategoryName();
197 String subCategory = component.getSubcategoryName();
198 putComponentToMap(component, categorizedMap, category, subCategory);
201 private static <T extends ICategorizedElement> void categorizeService(T component, Map<String, Map<String, List<T>>> categorizedMap) {
202 String category = "Generic";
203 String subCategory = "Generic";
204 putComponentToMap(component, categorizedMap, category, subCategory);
207 private static <T extends ICategorizedElement> void putComponentToMap(T component, Map<String, Map<String, List<T>>> categorizedMap,
208 String category, String subCategory) {
210 Map<String, List<T>> categoryMap = categorizedMap.computeIfAbsent(category, k -> new HashMap<>());
211 List<T> subCategoryList = categoryMap.computeIfAbsent(subCategory, k -> new ArrayList<>());
212 subCategoryList.add(component);