Merge "Created Ref Data tbl for controllerSelection"
[so.git] / common / src / main / java / org / onap / so / client / aai / entities / AAIResultWrapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.aai.entities;
22
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Optional;
28
29 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
30 import org.onap.so.jsonpath.JsonPathUtil;
31
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34
35 public class AAIResultWrapper implements Serializable {
36
37         private static final long serialVersionUID = 5895841925807816737L;
38         private final String jsonBody;
39         private final ObjectMapper mapper;
40         public AAIResultWrapper(String json) {
41                 this.jsonBody = json;
42                 this.mapper = new AAICommonObjectMapperProvider().getMapper();
43         }
44         
45         public Optional<Relationships> getRelationships() {
46                 final String path = "$.relationship-list";
47                 if (isEmpty()) {
48                         return Optional.empty();
49                 }
50                 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
51                 if (result.isPresent()) {
52                         return Optional.of(new Relationships(result.get()));
53                 } else {
54                         return Optional.empty();
55                 }
56         }
57         
58         public String getJson() {
59                 if(jsonBody == null) {
60                         return "{}";
61                 } else {
62                         return jsonBody;
63                 }
64         }
65         
66         public Map<String, Object> asMap() {
67                 if (isEmpty()) {
68                         return new HashMap<>();
69                 }
70                 try {
71                         return mapper.readValue(this.jsonBody, new TypeReference<Map<String, Object>>(){});
72                 } catch (IOException e) {
73                         return new HashMap<>();
74                 }
75         }
76         
77         public <T> Optional<T> asBean(Class<T> clazz) {
78                 if (isEmpty()) {
79                         return Optional.empty();
80                 }
81                 try {
82                         return Optional.of(mapper.readValue(this.jsonBody, clazz));
83                 } catch (IOException e) {
84                         return Optional.empty();
85                 }
86         }
87         
88         public boolean isEmpty() {
89                 return jsonBody == null;
90         }
91         @Override
92         public String toString() {
93                 return this.getJson();
94         }
95
96 }