45621f09a6e4695492dd86aef218c097f802241b
[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.apache.log4j.Logger;
30 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
31 import org.onap.so.jsonpath.JsonPathUtil;
32
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 public class AAIResultWrapper implements Serializable {
38
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);
43         
44         public AAIResultWrapper(String json) {
45                 this.jsonBody = json;
46                 this.mapper = new AAICommonObjectMapperProvider().getMapper();
47         }
48         
49         public AAIResultWrapper(Object aaiObject) {
50                 this.mapper = new AAICommonObjectMapperProvider().getMapper();
51                 this.jsonBody = mapObjectToString(aaiObject);
52         }
53         
54         protected String mapObjectToString(Object aaiObject) {
55                 try {
56                         return mapper.writeValueAsString(aaiObject);
57                 } catch (JsonProcessingException e) {
58                         logger.warn("could not parse object into json - defaulting to {}");
59                         return "{}";
60                 }
61         }
62         public Optional<Relationships> getRelationships() {
63                 final String path = "$.relationship-list";
64                 if (isEmpty()) {
65                         return Optional.empty();
66                 }
67                 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
68                 if (result.isPresent()) {
69                         return Optional.of(new Relationships(result.get()));
70                 } else {
71                         return Optional.empty();
72                 }
73         }
74         
75         public String getJson() {
76                 if(jsonBody == null) {
77                         return "{}";
78                 } else {
79                         return jsonBody;
80                 }
81         }
82         
83         public Map<String, Object> asMap() {
84                 if (isEmpty()) {
85                         return new HashMap<>();
86                 }
87                 try {
88                         return mapper.readValue(this.jsonBody, new TypeReference<Map<String, Object>>(){});
89                 } catch (IOException e) {
90                         return new HashMap<>();
91                 }
92         }
93         
94         public <T> Optional<T> asBean(Class<T> clazz) {
95                 if (isEmpty()) {
96                         return Optional.empty();
97                 }
98                 try {
99                         return Optional.of(mapper.readValue(this.jsonBody, clazz));
100                 } catch (IOException e) {
101                         return Optional.empty();
102                 }
103         }
104         
105         public boolean isEmpty() {
106                 return jsonBody == null;
107         }
108         @Override
109         public String toString() {
110                 return this.getJson();
111         }
112
113 }