c0b29e46c270c20f4c1f0ba38818410a12d344b9
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / entities / GraphInventoryResultWrapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client.graphinventory.entities;
24
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Optional;
30
31 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
32 import org.onap.so.jsonpath.JsonPathUtil;
33 import org.slf4j.Logger;
34
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39 public abstract class GraphInventoryResultWrapper<R extends GraphInventoryRelationships<?, ?, ?>> implements Serializable {
40
41         private static final long serialVersionUID = 5895841925807816727L;
42         protected final String jsonBody;
43         protected final ObjectMapper mapper;
44         private final transient Logger logger;
45         
46         protected GraphInventoryResultWrapper(String json, Logger logger) {
47                 this.jsonBody = json;
48                 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
49                 this.logger = logger;
50         }
51         
52         protected GraphInventoryResultWrapper(Object aaiObject, Logger logger) {
53                 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
54                 this.jsonBody = mapObjectToString(aaiObject);
55                 this.logger = logger;
56         }
57         
58         protected String mapObjectToString(Object aaiObject) {
59                 try {
60                         return mapper.writeValueAsString(aaiObject);
61                 } catch (JsonProcessingException e) {
62                         logger.warn("could not parse object into json - defaulting to empty object");
63                         return "{}";
64                 }
65         }
66         public Optional<R> getRelationships() {
67                 final String path = "$.relationship-list";
68                 if (isEmpty()) {
69                         return Optional.empty();
70                 }
71                 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
72                 if (result.isPresent()) {
73                         return Optional.of(createRelationships(result.get()));
74                 } else {
75                         return Optional.empty();
76                 }
77         }
78         protected abstract R createRelationships(String json);
79         
80         public String getJson() {
81                 if(jsonBody == null) {
82                         return "{}";
83                 } else {
84                         return jsonBody;
85                 }
86         }
87         
88         public Map<String, Object> asMap() {
89                 
90                 return asBean(new TypeReference<Map<String, Object>>(){}).orElse(new HashMap<>());
91         }
92         
93         public <T> Optional<T> asBean(Class<T> clazz) {
94                 if (isEmpty()) {
95                         return Optional.empty();
96                 }
97                 try {
98                         return Optional.of(mapper.readValue(this.jsonBody, clazz));
99                 } catch (IOException e) {
100                         return Optional.empty();
101                 }
102         }
103         
104         public <T> Optional<T> asBean(TypeReference<T> reference) {
105                 if (isEmpty()) {
106                         return Optional.empty();
107                 }
108                 try {
109                         return Optional.of(mapper.readValue(this.jsonBody, reference));
110                 } catch (IOException e) {
111                         return Optional.empty();
112                 }
113         }
114         
115         public boolean isEmpty() {
116                 return jsonBody == null;
117         }
118         @Override
119         public String toString() {
120                 return this.getJson();
121         }
122
123 }