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.base.database;
20 import java.io.IOException;
21 import javax.annotation.Nullable;
23 import org.elasticsearch.common.bytes.BytesReference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
34 public class HtMapper<T> {
36 private static final Logger log = LoggerFactory.getLogger(HtDatabaseClientAbstract.class);
38 private final Class<? extends T> clazz;
40 private final JsonMapperBase objectMapperRead;
41 private final JsonMapperBase objectMapperWrite;
43 private int mappingFailures;
46 public HtMapper(Class<? extends T> clazz) {
48 this.mappingFailures = 0;
51 this.objectMapperRead = new JsonMapperBase();
52 this.objectMapperWrite = this.objectMapperRead;
55 public Class<? extends T> getClazz() {
59 public int getMappingFailures() {
60 return mappingFailures;
63 public String objectToJson( T object ) {
64 return objectMapperWrite.objectToJson(object);
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
73 public @Nullable T getObjectFromJson(byte[] json) {
77 } else if (mappingFailures < 10) {
79 T object = objectMapperRead.readValue(json, clazz);
81 } catch (JsonParseException e) {
83 log.warn(e.toString());
84 } catch (JsonMappingException e) {
86 log.warn(e.toString());
87 } catch (IOException e) {
89 log.warn(e.toString());
90 } catch (Exception e) {
92 log.warn(e.toString());
95 log.warn("Problems parsing : {} {}", clazz, json);
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
105 public @Nullable T getObjectFromJson(BytesReference json) {
107 return json == null ? null : getObjectFromJson(json.toBytes());