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.onap.so.client.aai.AAICommonObjectMapperProvider;
30 import org.onap.so.jsonpath.JsonPathUtil;
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.fasterxml.jackson.databind.ObjectMapper;
35 public class AAIResultWrapper implements Serializable {
37 private static final long serialVersionUID = 5895841925807816737L;
38 private final Optional<String> jsonBody;
39 private final ObjectMapper mapper;
40 public AAIResultWrapper(String json) {
41 this.jsonBody = Optional.ofNullable(json);
42 this.mapper = new AAICommonObjectMapperProvider().getMapper();
45 public Optional<Relationships> getRelationships() {
46 final String path = "$.relationship-list";
47 if (!jsonBody.isPresent()) {
48 return Optional.empty();
50 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody.get(), path);
51 if (result.isPresent()) {
52 return Optional.of(new Relationships(result.get()));
54 return Optional.empty();
58 public String getJson() {
59 return jsonBody.orElse("{}");
62 public Map<String, Object> asMap() {
63 if (!this.jsonBody.isPresent()) {
64 return new HashMap<>();
67 return mapper.readValue(this.jsonBody.get(), new TypeReference<Map<String, Object>>(){});
68 } catch (IOException e) {
69 return new HashMap<>();
73 public <T> Optional<T> asBean(Class<T> clazz) {
74 if (!this.jsonBody.isPresent()) {
75 return Optional.empty();
78 return Optional.of(mapper.readValue(this.jsonBody.get(), clazz));
79 } catch (IOException e) {
80 return Optional.empty();
84 public boolean isEmpty() {
85 return !this.jsonBody.isPresent();
88 public String toString() {
89 return this.getJson();