64d3fa02a125b0513ca1632ad7b4e456c35e908a
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * 
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database;
19
20 import java.io.IOException;
21 import java.nio.file.Files;
22 import java.nio.file.Paths;
23 import java.util.List;
24
25 import javax.annotation.Nullable;
26
27 import org.elasticsearch.common.bytes.BytesReference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.fasterxml.jackson.core.JsonParseException;
32 import com.fasterxml.jackson.databind.JsonMappingException;
33 import com.fasterxml.jackson.databind.JsonNode;
34
35 /**
36  * @author Herbert
37  *
38  */
39 public class HtMapper<T> {
40
41     private static final Logger log = LoggerFactory.getLogger(HtDatabaseClientAbstract.class);
42
43     private final Class<? extends T> clazz;
44
45     private final JsonMapperBase objectMapperRead;
46     private final JsonMapperBase objectMapperWrite;
47
48     private int mappingFailures;
49
50
51     public HtMapper(Class<? extends T> clazz) {
52
53         this.mappingFailures = 0;
54         this.clazz = clazz;
55
56         this.objectMapperRead = new JsonMapperBase();
57         this.objectMapperWrite = this.objectMapperRead;
58     }
59
60     public Class<? extends T> getClazz() {
61         return clazz;
62     }
63
64     public int getMappingFailures() {
65         return mappingFailures;
66     }
67
68     public String objectToJson( T object ) {
69         return objectMapperWrite.objectToJson(object);
70     }
71
72     public String objectListToJson( List<T> objectList ) {
73         return objectMapperWrite.objectListToJson( objectList );
74     }
75
76     public T readValue( JsonNode node ) {
77
78         try {
79             T object = objectMapperRead.readValue(node.traverse(), clazz);
80             return object;
81         } catch (JsonParseException e) {
82             mappingFailures++;
83             log.warn(e.toString());
84         } catch (JsonMappingException e) {
85             mappingFailures++;
86             log.warn(e.toString());
87         } catch (IOException e) {
88             mappingFailures++;
89             log.warn(e.toString());
90         } catch (Exception e) {
91             mappingFailures++;
92             log.warn(e.toString());
93         }
94         log.warn("Can not parse: {} {} ", clazz, node);
95         return null;
96
97     }
98
99     /**
100      * Do the mapping from Json to class
101      * Block further mapping if there is are to many failures
102      * @param json String with Objects JSON representation
103      * @return The Object
104      */
105     public @Nullable T getObjectFromJson(byte[] json) {
106
107         if (json == null)
108                 return null;
109         else if (mappingFailures < 10) {
110             try {
111                 T object = objectMapperRead.readValue(json, clazz);
112                 return object;
113             } catch (JsonParseException e) {
114                 mappingFailures++;
115                 log.warn(e.toString());
116             } catch (JsonMappingException e) {
117                 mappingFailures++;
118                 log.warn(e.toString());
119             } catch (IOException e) {
120                 mappingFailures++;
121                 log.warn(e.toString());
122             } catch (Exception e) {
123                 mappingFailures++;
124                 log.warn(e.toString());
125             }
126         }
127         log.warn("Problems parsing : {} {}", clazz, json);
128         return null;
129     }
130
131     /**
132      * Do the mapping from Json to class
133      * Block further mapping if there is are to many failures
134      * @param json Byte array with JSON Object representation
135      * @return The Object
136      */
137     public @Nullable T getObjectFromJson(BytesReference json) {
138
139         return json == null ? null : getObjectFromJson(json.toBytes());
140
141     }
142
143     /**
144      * Read json from File.
145      * @param fileName File with JSON text
146      * @return Object Object
147      */
148     public T readJsonObjectFromFile( String fileName ) {
149         byte[] content = null;
150         log.debug("Filename readJsonObjectFromFile: {}",fileName);
151
152         try {
153             content = Files.readAllBytes(Paths.get(fileName));
154         } catch (IOException e1) {
155             log.warn("IO Problem: {}", e1.getMessage());
156         }
157
158         if (content != null) {
159             return getObjectFromJson(content);
160         } else {
161             return null;
162         }
163     }
164
165
166 }