ae3210733900ed8c66d6c1636eb95c181654934d
[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.aaiclient.client.graphinventory.GraphInventoryObjectName;
32 import org.onap.so.jsonpath.JsonPathUtil;
33 import org.slf4j.Logger;
34 import com.fasterxml.jackson.core.JsonProcessingException;
35 import com.fasterxml.jackson.core.type.TypeReference;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 public abstract class GraphInventoryResultWrapper<R extends GraphInventoryRelationships<?, ?, ?>>
39         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
67     public boolean hasRelationshipsTo(GraphInventoryObjectName name) {
68         Optional<R> rOpt = this.getRelationships();
69         if (rOpt.isPresent()) {
70             return rOpt.get().getRelatedLinks(name).size() > 0;
71         } else {
72             return false;
73         }
74     }
75
76     public Optional<R> getRelationships() {
77         final String path = "$.relationship-list";
78         if (isEmpty()) {
79             return Optional.empty();
80         }
81         Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
82         if (result.isPresent()) {
83             return Optional.of(createRelationships(result.get()));
84         } else {
85             return Optional.empty();
86         }
87     }
88
89     protected abstract R createRelationships(String json);
90
91     public String getJson() {
92         if (jsonBody == null) {
93             return "{}";
94         } else {
95             return jsonBody;
96         }
97     }
98
99     public Map<String, Object> asMap() {
100
101         return asBean(new TypeReference<Map<String, Object>>() {}).orElse(new HashMap<>());
102     }
103
104     public <T> Optional<T> asBean(Class<T> clazz) {
105         if (isEmpty()) {
106             return Optional.empty();
107         }
108         try {
109             return Optional.of(mapper.readValue(this.jsonBody, clazz));
110         } catch (IOException e) {
111             return Optional.empty();
112         }
113     }
114
115     public <T> Optional<T> asBean(TypeReference<T> reference) {
116         if (isEmpty()) {
117             return Optional.empty();
118         }
119         try {
120             return Optional.of(mapper.readValue(this.jsonBody, reference));
121         } catch (IOException e) {
122             return Optional.empty();
123         }
124     }
125
126     public boolean isEmpty() {
127         return jsonBody == null;
128     }
129
130     @Override
131     public String toString() {
132         return this.getJson();
133     }
134
135 }