9d8ddc3ae93d14f24d94ef8d66c105831a6adf2a
[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 javax.annotation.Nullable;
22
23 import org.elasticsearch.common.bytes.BytesReference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29
30 /**
31  * @author Herbert
32  *
33  */
34 public class HtMapper<T> {
35
36     private static final Logger log = LoggerFactory.getLogger(HtDatabaseClientAbstract.class);
37
38     private final Class<? extends T> clazz;
39
40     private final JsonMapperBase objectMapperRead;
41     private final JsonMapperBase objectMapperWrite;
42
43     private int mappingFailures;
44
45
46     public HtMapper(Class<? extends T> clazz) {
47
48         this.mappingFailures = 0;
49         this.clazz = clazz;
50
51         this.objectMapperRead = new JsonMapperBase();
52         this.objectMapperWrite = this.objectMapperRead;
53     }
54
55     public Class<? extends T> getClazz() {
56         return clazz;
57     }
58
59     public int getMappingFailures() {
60         return mappingFailures;
61     }
62
63     public String objectToJson( T object ) {
64         return objectMapperWrite.objectToJson(object);
65     }
66
67     /**
68      * Do the mapping from Json to class
69      * Block further mapping if there is are to many failures
70      * @param json String with Objects JSON representation
71      * @return The Object
72      */
73     public @Nullable T getObjectFromJson(byte[] json) {
74
75         if (json == null) {
76             return null;
77         } else if (mappingFailures < 10) {
78             try {
79                 T object = objectMapperRead.readValue(json, 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         }
95         log.warn("Problems parsing : {} {}", clazz, json);
96         return null;
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 Byte array with JSON Object representation
103      * @return The Object
104      */
105     public @Nullable T getObjectFromJson(BytesReference json) {
106
107         return json == null ? null : getObjectFromJson(json.toBytes());
108
109     }
110
111 }