Merge "Reorder modifiers"
[so.git] / common / src / main / java / org / openecomp / mso / 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.openecomp.mso.client.aai.entities;
22
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Optional;
27
28 import org.openecomp.mso.client.aai.AAICommonObjectMapperProvider;
29 import org.openecomp.mso.jsonpath.JsonPathUtil;
30
31 import com.fasterxml.jackson.core.type.TypeReference;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33
34 public class AAIResultWrapper {
35
36         private final String jsonBody;
37         private final ObjectMapper mapper;
38         public AAIResultWrapper(String json) {
39                 this.jsonBody = json;
40                 this.mapper = new AAICommonObjectMapperProvider().getMapper();
41         }
42         
43         public Optional<Relationships> getRelationships() {
44                 final String path = "$.relationship-list";
45                 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
46                 if (result.isPresent()) {
47                         return Optional.of(new Relationships(result.get()));
48                 } else {
49                         return Optional.empty();
50                 }
51         }
52         
53         public String getJson() {
54                 return jsonBody;
55         }
56         
57         public Map<String, Object> asMap() {
58                 try {
59                         return mapper.readValue(this.jsonBody, new TypeReference<Map<String, Object>>(){});
60                 } catch (IOException e) {
61                         return new HashMap<>();
62                 }
63         }
64         
65         public <T> Optional<T> asBean(Class<T> clazz) {
66                 try {
67                         return Optional.of(mapper.readValue(this.jsonBody, clazz));
68                 } catch (IOException e) {
69                         return Optional.empty();
70                 }
71         }
72         
73         @Override
74         public String toString() {
75                 return this.getJson();
76         }
77
78 }