2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.aai.entities;
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.HashMap;
27 import java.util.Optional;
29 import org.apache.log4j.Logger;
30 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
31 import org.onap.so.jsonpath.JsonPathUtil;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
37 public class AAIResultWrapper implements Serializable {
39 private static final long serialVersionUID = 5895841925807816737L;
40 private final String jsonBody;
41 private final ObjectMapper mapper;
42 private final transient Logger logger = Logger.getLogger(AAIResultWrapper.class);
44 public AAIResultWrapper(String json) {
46 this.mapper = new AAICommonObjectMapperProvider().getMapper();
49 public AAIResultWrapper(Object aaiObject) {
50 this.mapper = new AAICommonObjectMapperProvider().getMapper();
51 this.jsonBody = mapObjectToString(aaiObject);
54 protected String mapObjectToString(Object aaiObject) {
56 return mapper.writeValueAsString(aaiObject);
57 } catch (JsonProcessingException e) {
58 logger.warn("could not parse object into json - defaulting to {}");
62 public Optional<Relationships> getRelationships() {
63 final String path = "$.relationship-list";
65 return Optional.empty();
67 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
68 if (result.isPresent()) {
69 return Optional.of(new Relationships(result.get()));
71 return Optional.empty();
75 public String getJson() {
76 if(jsonBody == null) {
83 public Map<String, Object> asMap() {
85 return new HashMap<>();
88 return mapper.readValue(this.jsonBody, new TypeReference<Map<String, Object>>(){});
89 } catch (IOException e) {
90 return 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 boolean isEmpty() {
106 return jsonBody == null;
109 public String toString() {
110 return this.getJson();