Merge "made max retries configurable via mso config"
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / entities / GraphInventoryResultWrapper.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.graphinventory.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.client.aai.entities.Relationships;
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 GraphInventoryResultWrapper implements Serializable {
40
41         private static final long serialVersionUID = 5895841925807816727L;
42         protected final String jsonBody;
43         protected final ObjectMapper mapper;
44         private final transient Logger logger;
45         
46         protected GraphInventoryResultWrapper(String json, Logger logger) {
47                 this.jsonBody = json;
48                 this.mapper = new AAICommonObjectMapperProvider().getMapper();
49                 this.logger = logger;
50         }
51         
52         protected GraphInventoryResultWrapper(Object aaiObject, Logger logger) {
53                 this.mapper = new AAICommonObjectMapperProvider().getMapper();
54                 this.jsonBody = mapObjectToString(aaiObject);
55                 this.logger = logger;
56         }
57         
58         protected String mapObjectToString(Object aaiObject) {
59                 try {
60                         return mapper.writeValueAsString(aaiObject);
61                 } catch (JsonProcessingException e) {
62                         logger.warn("could not parse object into json - defaulting to {}");
63                         return "{}";
64                 }
65         }
66         public Optional<Relationships> getRelationships() {
67                 final String path = "$.relationship-list";
68                 if (isEmpty()) {
69                         return Optional.empty();
70                 }
71                 Optional<String> result = JsonPathUtil.getInstance().locateResult(jsonBody, path);
72                 if (result.isPresent()) {
73                         return Optional.of(new Relationships(result.get()));
74                 } else {
75                         return Optional.empty();
76                 }
77         }
78         
79         public String getJson() {
80                 if(jsonBody == null) {
81                         return "{}";
82                 } else {
83                         return jsonBody;
84                 }
85         }
86         
87         public Map<String, Object> asMap() {
88                 if (isEmpty()) {
89                         return new HashMap<>();
90                 }
91                 try {
92                         return mapper.readValue(this.jsonBody, new TypeReference<Map<String, Object>>(){});
93                 } catch (IOException e) {
94                         return new HashMap<>();
95                 }
96         }
97         
98         public <T> Optional<T> asBean(Class<T> clazz) {
99                 if (isEmpty()) {
100                         return Optional.empty();
101                 }
102                 try {
103                         return Optional.of(mapper.readValue(this.jsonBody, clazz));
104                 } catch (IOException e) {
105                         return Optional.empty();
106                 }
107         }
108         
109         public boolean isEmpty() {
110                 return jsonBody == null;
111         }
112         @Override
113         public String toString() {
114                 return this.getJson();
115         }
116
117 }