9e6572388b91698e232b5bb446b7cd0dd90ccbc7
[so.git] /
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.aaiclient.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 import org.onap.aaiclient.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
31 import org.onap.so.jsonpath.JsonPathUtil;
32 import org.slf4j.Logger;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 public abstract class GraphInventoryResultWrapper<R extends GraphInventoryRelationships<?, ?, ?>>
38         implements Serializable {
39
40     private static final long serialVersionUID = 5895841925807816727L;
41     protected final String jsonBody;
42     protected final ObjectMapper mapper;
43     private final transient Logger logger;
44
45     protected GraphInventoryResultWrapper(String json, Logger logger) {
46         this.jsonBody = json;
47         this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
48         this.logger = logger;
49     }
50
51     protected GraphInventoryResultWrapper(Object aaiObject, Logger logger) {
52         this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
53         this.jsonBody = mapObjectToString(aaiObject);
54         this.logger = logger;
55     }
56
57     protected String mapObjectToString(Object aaiObject) {
58         try {
59             return mapper.writeValueAsString(aaiObject);
60         } catch (JsonProcessingException e) {
61             logger.warn("could not parse object into json - defaulting to empty object");
62             return "{}";
63         }
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
79     protected abstract R createRelationships(String json);
80
81     public String getJson() {
82         if (jsonBody == null) {
83             return "{}";
84         } else {
85             return jsonBody;
86         }
87     }
88
89     public Map<String, Object> asMap() {
90
91         return asBean(new TypeReference<Map<String, Object>>() {}).orElse(new HashMap<>());
92     }
93
94     public <T> Optional<T> asBean(Class<T> clazz) {
95         if (isEmpty()) {
96             return Optional.empty();
97         }
98         try {
99             return Optional.of(mapper.readValue(this.jsonBody, clazz));
100         } catch (IOException e) {
101             return Optional.empty();
102         }
103     }
104
105     public <T> Optional<T> asBean(TypeReference<T> reference) {
106         if (isEmpty()) {
107             return Optional.empty();
108         }
109         try {
110             return Optional.of(mapper.readValue(this.jsonBody, reference));
111         } catch (IOException e) {
112             return Optional.empty();
113         }
114     }
115
116     public boolean isEmpty() {
117         return jsonBody == null;
118     }
119
120     @Override
121     public String toString() {
122         return this.getJson();
123     }
124
125 }