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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database;
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;
32 public class HtMapper<T> {
34 private static final Logger log = LoggerFactory.getLogger(HtMapper.class);
36 private final Class<? extends T> clazz;
38 private final JsonMapperBase objectMapperRead;
39 private final JsonMapperBase objectMapperWrite;
41 private int mappingFailures;
43 public HtMapper(Class<? extends T> clazz) {
45 this.mappingFailures = 0;
48 this.objectMapperRead = new JsonMapperBase();
49 this.objectMapperWrite = this.objectMapperRead;
52 public Class<? extends T> getClazz() {
56 public int getMappingFailures() {
57 return mappingFailures;
60 public String objectToJson(T object) {
61 return objectMapperWrite.objectToJson(object);
65 * Do the mapping from Json to class Block further mapping if there is are to
68 * @param json String with Objects JSON representation
71 public @Nullable T getObjectFromJson(byte[] json) {
75 } else if (mappingFailures < 10) {
77 T object = objectMapperRead.readValue(json, clazz);
79 } catch (JsonParseException e) {
81 log.warn(e.toString());
82 } catch (JsonMappingException e) {
84 log.warn(e.toString());
85 } catch (IOException e) {
87 log.warn(e.toString());
88 } catch (Exception e) {
90 log.warn(e.toString());
93 log.warn("Problems parsing : {} {}", clazz, json);
98 * Do the mapping from Json to class Block further mapping if there is are to
101 * @param json String with Objects JSON representation
104 public @Nullable T getObjectFromJson(String json) {
108 } else if (mappingFailures < 10) {
110 T object = objectMapperRead.readValue(json, clazz);
112 } catch (JsonParseException e) {
114 log.warn(e.toString());
115 } catch (JsonMappingException e) {
117 log.warn(e.toString());
118 } catch (IOException e) {
120 log.warn(e.toString());
121 } catch (Exception e) {
123 log.warn(e.toString());
126 log.warn("Problems parsing : {} {}", clazz, json);
130 public void setSerializationInclusion(Include incl) {
131 this.objectMapperRead.setSerializationInclusion(incl);
134 public void resetSerializationInclusion() {
135 this.objectMapperRead.setSerializationInclusion(Include.USE_DEFAULTS);