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 java.nio.file.Files;
22 import java.nio.file.Paths;
23 import java.util.List;
25 import javax.annotation.Nullable;
27 import org.elasticsearch.common.bytes.BytesReference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.fasterxml.jackson.core.JsonParseException;
32 import com.fasterxml.jackson.databind.JsonMappingException;
33 import com.fasterxml.jackson.databind.JsonNode;
39 public class HtMapper<T> {
41 private static final Logger log = LoggerFactory.getLogger(HtDatabaseClientAbstract.class);
43 private final Class<? extends T> clazz;
45 private final JsonMapperBase objectMapperRead;
46 private final JsonMapperBase objectMapperWrite;
48 private int mappingFailures;
51 public HtMapper(Class<? extends T> clazz) {
53 this.mappingFailures = 0;
56 this.objectMapperRead = new JsonMapperBase();
57 this.objectMapperWrite = this.objectMapperRead;
60 public Class<? extends T> getClazz() {
64 public int getMappingFailures() {
65 return mappingFailures;
68 public String objectToJson( T object ) {
69 return objectMapperWrite.objectToJson(object);
72 public String objectListToJson( List<T> objectList ) {
73 return objectMapperWrite.objectListToJson( objectList );
76 public T readValue( JsonNode node ) {
79 T object = objectMapperRead.readValue(node.traverse(), 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());
94 log.warn("Can not parse: {} {} ", clazz, node);
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
105 public @Nullable T getObjectFromJson(byte[] json) {
109 else if (mappingFailures < 10) {
111 T object = objectMapperRead.readValue(json, clazz);
113 } catch (JsonParseException e) {
115 log.warn(e.toString());
116 } catch (JsonMappingException e) {
118 log.warn(e.toString());
119 } catch (IOException e) {
121 log.warn(e.toString());
122 } catch (Exception e) {
124 log.warn(e.toString());
127 log.warn("Problems parsing : {} {}", clazz, json);
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
137 public @Nullable T getObjectFromJson(BytesReference json) {
139 return json == null ? null : getObjectFromJson(json.toBytes());
144 * Read json from File.
145 * @param fileName File with JSON text
146 * @return Object Object
148 public T readJsonObjectFromFile( String fileName ) {
149 byte[] content = null;
150 log.debug("Filename readJsonObjectFromFile: {}",fileName);
153 content = Files.readAllBytes(Paths.get(fileName));
154 } catch (IOException e1) {
155 log.warn("IO Problem: {}", e1.getMessage());
158 if (content != null) {
159 return getObjectFromJson(content);