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.aaiclient.client.graphinventory.entities;
25 import java.io.IOException;
26 import java.io.Serializable;
27 import java.util.HashMap;
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;
37 public abstract class GraphInventoryResultWrapper<R extends GraphInventoryRelationships<?, ?, ?>>
38 implements Serializable {
40 private static final long serialVersionUID = 5895841925807816727L;
41 protected final String jsonBody;
42 protected final ObjectMapper mapper;
43 private final transient Logger logger;
45 protected GraphInventoryResultWrapper(String json, Logger logger) {
47 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
51 protected GraphInventoryResultWrapper(Object aaiObject, Logger logger) {
52 this.mapper = new GraphInventoryCommonObjectMapperProvider().getMapper();
53 this.jsonBody = mapObjectToString(aaiObject);
57 protected String mapObjectToString(Object aaiObject) {
59 return mapper.writeValueAsString(aaiObject);
60 } catch (JsonProcessingException e) {
61 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();
79 protected abstract R createRelationships(String json);
81 public String getJson() {
82 if (jsonBody == null) {
89 public Map<String, Object> asMap() {
91 return asBean(new TypeReference<Map<String, Object>>() {}).orElse(new HashMap<>());
94 public <T> Optional<T> asBean(Class<T> clazz) {
96 return Optional.empty();
99 return Optional.of(mapper.readValue(this.jsonBody, clazz));
100 } catch (IOException e) {
101 return Optional.empty();
105 public <T> Optional<T> asBean(TypeReference<T> reference) {
107 return Optional.empty();
110 return Optional.of(mapper.readValue(this.jsonBody, reference));
111 } catch (IOException e) {
112 return Optional.empty();
116 public boolean isEmpty() {
117 return jsonBody == null;
121 public String toString() {
122 return this.getJson();