Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / base / database / HtMapper.java
1 /*********************************************************************************
2  *  Copyright © 2015, highstreet technologies GmbH
3  *  All rights reserved!
4  *
5  *  http://www.highstreet-technologies.com/
6  *
7  *  The reproduction, transmission or use of this document or its contents is not
8  *  permitted without express written authority. Offenders will be liable for
9  *  damages. All rights, including rights created by patent grant or registration
10  *  of a utility model or design, are reserved. Technical modifications possible.
11  *  Technical specifications and features are binding only insofar as they are
12  *  specifically and expressly agreed upon in a written contract.
13  *
14  *  @author: Martin Skorupski [martin@skorupski.de]
15  *********************************************************************************/
16 package org.opendaylight.mwtn.base.database;
17
18 import java.io.IOException;
19 import java.nio.file.Files;
20 import java.nio.file.Paths;
21 import java.util.List;
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 import com.fasterxml.jackson.databind.JsonNode;
30
31 /**
32  * @author Herbert
33  *
34  */
35 public class HtMapper<T> {
36
37     private static final Logger log = LoggerFactory.getLogger(HtDatabaseClientAbstract.class);
38
39     private final Class<? extends T> clazz;
40
41     private final JsonMapperBase objectMapperRead;
42     private final JsonMapperBase objectMapperWrite;
43
44     private int mappingFailures;
45
46
47     public HtMapper(Class<? extends T> clazz) {
48
49         this.mappingFailures = 0;
50         this.clazz = clazz;
51
52         this.objectMapperRead = new JsonMapperBase();
53         this.objectMapperWrite = this.objectMapperRead;
54     }
55
56     public Class<? extends T> getClazz() {
57         return clazz;
58     }
59
60     public int getMappingFailures() {
61         return mappingFailures;
62     }
63
64     public String objectToJson( T object ) {
65         return objectMapperWrite.objectToJson(object);
66     }
67
68     public String objectListToJson( List<T> objectList ) {
69         return objectMapperWrite.objectListToJson( objectList );
70     }
71
72     public T readValue( JsonNode node ) {
73
74         try {
75             T object = objectMapperRead.readValue(node.traverse(), clazz);
76             return object;
77         } catch (JsonParseException e) {
78             mappingFailures++;
79             log.warn(e.toString());
80         } catch (JsonMappingException e) {
81             mappingFailures++;
82             log.warn(e.toString());
83         } catch (IOException e) {
84             mappingFailures++;
85             log.warn(e.toString());
86         } catch (Exception e) {
87             mappingFailures++;
88             log.warn(e.toString());
89         }
90         log.warn("Can not parse: {} {} ", clazz, node);
91         return null;
92
93     }
94
95     /**
96      * Do the mapping from Json to class
97      * Block further mapping if there is are to many failures
98      * @param json String with Objects JSON representation
99      * @return The Object
100      */
101     public T getObjectFromJson(byte[] json) {
102
103         if (json != null &&    mappingFailures < 10) {
104             try {
105                 T object = objectMapperRead.readValue(json, clazz);
106                 return object;
107             } catch (JsonParseException e) {
108                 mappingFailures++;
109                 log.warn(e.toString());
110             } catch (JsonMappingException e) {
111                 mappingFailures++;
112                 log.warn(e.toString());
113             } catch (IOException e) {
114                 mappingFailures++;
115                 log.warn(e.toString());
116             } catch (Exception e) {
117                 mappingFailures++;
118                 log.warn(e.toString());
119             }
120         }
121         log.warn("Problems parsing : {} {}", clazz, json);
122         return null;
123     }
124
125     /**
126      * Do the mapping from Json to class
127      * Block further mapping if there is are to many failures
128      * @param json Byte array with JSON Object representation
129      * @return The Object
130      */
131     public T getObjectFromJson(BytesReference json) {
132
133         return getObjectFromJson(json.toBytes());
134
135     }
136
137     /**
138      * Read json from File.
139      * @param fileName File with JSON text
140      * @return Object Object
141      */
142     public T readJsonObjectFromFile( String fileName ) {
143         byte[] content = null;
144         log.debug("Filename readJsonObjectFromFile: {}",fileName);
145
146         try {
147             content = Files.readAllBytes(Paths.get(fileName));
148         } catch (IOException e1) {
149             log.warn("IO Problem: {}", e1.getMessage());
150         }
151
152         if (content != null) {
153             return getObjectFromJson(content);
154         } else {
155             return null;
156         }
157     }
158
159
160 }