2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.client.graphinventory.entities;
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.util.HashMap;
29 import java.util.Optional;
31 import org.onap.so.client.graphinventory.GraphInventoryCommonObjectMapperProvider;
32 import org.onap.so.jsonpath.JsonPathUtil;
33 import org.slf4j.Logger;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.core.type.TypeReference;
37 import com.fasterxml.jackson.databind.ObjectMapper;
39 public abstract class GraphInventoryResultWrapper<R extends GraphInventoryRelationships<?, ?, ?>> implements Serializable {
41 private static final long serialVersionUID = 5895841925807816727L;
42 protected final String jsonBody;
43 protected final ObjectMapper mapper;
44 private final transient Logger logger;
46 protected GraphInventoryResultWrapper(String json, Logger logger) {
48 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
52 protected GraphInventoryResultWrapper(Object aaiObject, Logger logger) {
53 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
54 this.jsonBody = mapObjectToString(aaiObject);
58 protected String mapObjectToString(Object aaiObject) {
60 return mapper.writeValueAsString(aaiObject);
61 } catch (JsonProcessingException e) {
62 logger.warn("could not parse object into json - defaulting to empty object");
66 public Optional<R> getRelationships() {
67 final String path = "$.relationship-list";
69 return Optional.empty();
71 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
72 if (result.isPresent()) {
73 return Optional.of(createRelationships(result.get()));
75 return Optional.empty();
78 protected abstract R createRelationships(String json);
80 public String getJson() {
81 if(jsonBody == null) {
88 public Map<String, Object> asMap() {
90 return asBean(new TypeReference<Map<String, Object>>(){}).orElse(new HashMap<>());
93 public <T> Optional<T> asBean(Class<T> clazz) {
95 return Optional.empty();
98 return Optional.of(mapper.readValue(this.jsonBody, clazz));
99 } catch (IOException e) {
100 return Optional.empty();
104 public <T> Optional<T> asBean(TypeReference<T> reference) {
106 return Optional.empty();
109 return Optional.of(mapper.readValue(this.jsonBody, reference));
110 } catch (IOException e) {
111 return Optional.empty();
115 public boolean isEmpty() {
116 return jsonBody == null;
119 public String toString() {
120 return this.getJson();