080ae5494418b6747eea0dc907932afd675b9d56
[sdc.git] / common / openecomp-sdc-artifact-generator-lib / openecomp-sdc-artifact-generator-api / src / main / java / org / openecomp / sdc / generator / data / GeneratorUtil.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.generator.data;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
26 import org.apache.commons.codec.digest.DigestUtils;
27
28 import java.io.IOException;
29 import java.util.Base64;
30
31 /**
32  * Utility method class for artifact generation.
33  */
34 public class GeneratorUtil {
35   /**
36    * Translate tosca yaml into the provided model class.
37    *
38    * @param tosca    Tosca file content
39    * @param classOfT Model class for the translated object
40    * @param <T>      Template parameter for the return object
41    * @return Object model for the provided tosca yaml file
42    */
43   public static <T> T translateTosca(String tosca, Class<T> classOfT) throws IOException {
44     T tos;
45     //changing file
46     ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); // jackson data-bind
47     mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
48     tos = mapper.readValue(tosca, classOfT);
49     return tos;
50   }
51
52   /**
53    * Decodes Base64 encode byte array input.
54    *
55    * @param input Base64 encoded byte array
56    * @return Decoded byte array
57    */
58   public static byte[] decoder(byte[] input) {
59     if (input != null) {
60       byte[] output = Base64.getDecoder().decode(input);
61       return output;
62     }
63     return null;
64   }
65
66   /**
67    * Encode a byte array input using Base64 encoding.
68    *
69    * @param input Input byte array to be encoded
70    * @return Base64 encoded byte array
71    */
72   public static byte[] encode(byte[] input) {
73     if (input != null) {
74       byte[] output = Base64.getEncoder().encode(input);
75       return output;
76     }
77     return null;
78   }
79
80   /**
81    * Calculate the checksum for a given input.
82    *
83    * @param input Byte array for which the checksum has to be calculated
84    * @return Calculated checksum of the input byte array
85    */
86   public static String checkSum(byte[] input) {
87     String checksum = null;
88     if (input != null) {
89       checksum = (DigestUtils.md5Hex(input)).toUpperCase();
90     }
91     return checksum;
92   }
93
94   /**
95    * Check if string is empty or null.
96    *
97    * @param input Input String
98    * @return true if string is empty/null and false otherwise
99    */
100   public static boolean isEmpty(String input) {
101     return input == null || input.length() == 0;
102   }
103
104 }