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