Removing blueprints-processor
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / database / HtMapper.java
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.impl.database;
19
20 import com.fasterxml.jackson.annotation.JsonInclude.Include;
21 import com.fasterxml.jackson.core.JsonParseException;
22 import com.fasterxml.jackson.databind.JsonMappingException;
23 import java.io.IOException;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * @author Herbert
30  *
31  */
32 public class HtMapper<T> {
33
34     private static final Logger log = LoggerFactory.getLogger(HtMapper.class);
35
36     private final Class<? extends T> clazz;
37
38     private final JsonMapperBase objectMapperRead;
39     private final JsonMapperBase objectMapperWrite;
40
41     private int mappingFailures;
42
43     public HtMapper(Class<? extends T> clazz) {
44
45         this.mappingFailures = 0;
46         this.clazz = clazz;
47
48         this.objectMapperRead = new JsonMapperBase();
49         this.objectMapperWrite = this.objectMapperRead;
50     }
51
52     public Class<? extends T> getClazz() {
53         return clazz;
54     }
55
56     public int getMappingFailures() {
57         return mappingFailures;
58     }
59
60     public String objectToJson(T object) {
61         return objectMapperWrite.objectToJson(object);
62     }
63
64     /**
65      * Do the mapping from Json to class Block further mapping if there is are to
66      * many failures
67      *
68      * @param json String with Objects JSON representation
69      * @return The Object
70      */
71     public @Nullable T getObjectFromJson(byte[] json) {
72
73         if (json == null) {
74             return null;
75         } else if (mappingFailures < 10) {
76             try {
77                 T object = objectMapperRead.readValue(json, clazz);
78                 return object;
79             } catch (JsonParseException e) {
80                 mappingFailures++;
81                 log.warn(e.toString());
82             } catch (JsonMappingException e) {
83                 mappingFailures++;
84                 log.warn(e.toString());
85             } catch (IOException e) {
86                 mappingFailures++;
87                 log.warn(e.toString());
88             } catch (Exception e) {
89                 mappingFailures++;
90                 log.warn(e.toString());
91             }
92         }
93         log.warn("Problems parsing : {} {}", clazz, json);
94         return null;
95     }
96
97     /**
98      * Do the mapping from Json to class Block further mapping if there is are to
99      * many failures
100      *
101      * @param json String with Objects JSON representation
102      * @return The Object
103      */
104     public @Nullable T getObjectFromJson(String json) {
105
106         if (json == null) {
107             return null;
108         } else if (mappingFailures < 10) {
109             try {
110                 T object = objectMapperRead.readValue(json, clazz);
111                 return object;
112             } catch (JsonParseException e) {
113                 mappingFailures++;
114                 log.warn(e.toString());
115             } catch (JsonMappingException e) {
116                 mappingFailures++;
117                 log.warn(e.toString());
118             } catch (IOException e) {
119                 mappingFailures++;
120                 log.warn(e.toString());
121             } catch (Exception e) {
122                 mappingFailures++;
123                 log.warn(e.toString());
124             }
125         }
126         log.warn("Problems parsing : {} {}", clazz, json);
127         return null;
128     }
129
130     public void setSerializationInclusion(Include incl) {
131         this.objectMapperRead.setSerializationInclusion(incl);
132
133     }
134     public void resetSerializationInclusion() {
135         this.objectMapperRead.setSerializationInclusion(Include.USE_DEFAULTS);
136
137
138     }
139
140 }