|
| 1 | +/* |
| 2 | + Copyright 2017 Ericsson AB. |
| 3 | + For a full list of individual contributors, please see the commit history. |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +*/ |
| 14 | +package com.ericsson.ei.erqueryservice; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import javax.annotation.PostConstruct; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import org.springframework.beans.factory.annotation.Value; |
| 27 | +import org.springframework.boot.web.client.RestTemplateBuilder; |
| 28 | +import org.springframework.http.HttpEntity; |
| 29 | +import org.springframework.http.HttpHeaders; |
| 30 | +import org.springframework.http.HttpMethod; |
| 31 | +import org.springframework.http.MediaType; |
| 32 | +import org.springframework.http.ResponseEntity; |
| 33 | +import org.springframework.stereotype.Component; |
| 34 | +import org.springframework.util.LinkedMultiValueMap; |
| 35 | +import org.springframework.web.client.RestOperations; |
| 36 | +import org.springframework.web.util.UriComponentsBuilder; |
| 37 | + |
| 38 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 39 | +import com.fasterxml.jackson.databind.JsonNode; |
| 40 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 41 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 42 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author evasiba |
| 46 | + * |
| 47 | + */ |
| 48 | +@Component |
| 49 | +public class ERQueryService { |
| 50 | + |
| 51 | + static Logger log = (Logger) LoggerFactory.getLogger(ERQueryService.class); |
| 52 | + |
| 53 | + private RestOperations rest; |
| 54 | + |
| 55 | + public final static int DOWNSTREAM = 0; |
| 56 | + public final static int UPSTREAM = 1; |
| 57 | + public final static int DOWNANDUPSTREAM = 2; |
| 58 | + |
| 59 | + @Value("${er.url}") |
| 60 | + private String url; |
| 61 | + |
| 62 | + public String getUrl() { |
| 63 | + return url; |
| 64 | + } |
| 65 | + |
| 66 | + public ERQueryService(RestTemplateBuilder builder) { |
| 67 | + rest = builder.build(); |
| 68 | + } |
| 69 | + |
| 70 | + public void setRest(RestOperations rest) { |
| 71 | + this.rest = rest; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * This method only extracts the event information from ER2.0 based on the |
| 76 | + * eventID. |
| 77 | + * |
| 78 | + * @param eventId |
| 79 | + * @return ResponseEntity |
| 80 | + */ |
| 81 | + public ResponseEntity getEventDataById(String eventId) { |
| 82 | + String erUrl = url.trim() + "{id}"; |
| 83 | + log.info("The url is : " + erUrl); |
| 84 | + Map<String, String> params = new HashMap<String, String>(); |
| 85 | + params.put("id", eventId); |
| 86 | + ResponseEntity<String> response = null; |
| 87 | + log.info("The ID parameter is set"); |
| 88 | + try { |
| 89 | + response = rest.getForEntity(erUrl, String.class, params); |
| 90 | + log.info("The response is : " + response.toString()); |
| 91 | + } catch (Exception e) { |
| 92 | + log.error(e.getMessage(), e); |
| 93 | + } |
| 94 | + return response; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * This method is used to fetch only the upstream or downstream or both |
| 99 | + * event information from ER2.0 based on the eventID and searchAction |
| 100 | + * conditions. |
| 101 | + * |
| 102 | + * @param eventId |
| 103 | + * @param searchAction |
| 104 | + * @param limitParam |
| 105 | + * @param levelsParam |
| 106 | + * @param tree |
| 107 | + * @return ResponseEntity |
| 108 | + */ |
| 109 | + |
| 110 | + public ResponseEntity getEventStreamDataById(String eventId, int searchAction, int limitParam, |
| 111 | + int levelsParam, boolean tree) { |
| 112 | + |
| 113 | + String erUrl = url.trim() + eventId; |
| 114 | + log.info("The url is : " + erUrl); |
| 115 | + |
| 116 | + // Request Body parameters |
| 117 | + JsonNode uriParams = getSearchParameters(searchAction); |
| 118 | + |
| 119 | + // Add query parameter |
| 120 | + UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(erUrl).queryParam("limit", limitParam) |
| 121 | + .queryParam("levels", levelsParam).queryParam("tree", tree); |
| 122 | + |
| 123 | + HttpHeaders headers = new HttpHeaders(); |
| 124 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 125 | + |
| 126 | + HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity(uriParams, headers); |
| 127 | + log.info("The request is : " + builder.buildAndExpand(uriParams).toUri().toString()); |
| 128 | + |
| 129 | + ResponseEntity response = rest.exchange(builder.buildAndExpand(uriParams).toUri(), HttpMethod.POST, |
| 130 | + requestEntity, JsonNode.class); |
| 131 | + return response; |
| 132 | + } |
| 133 | + |
| 134 | + /** Generates the json object used as body for downstream/upstream |
| 135 | + * query requests |
| 136 | + * @param searchAction - one of DOWNSTREAM, UPSTREAM or DOWNANDUPSTREAM |
| 137 | + * @return |
| 138 | + */ |
| 139 | + public JsonNode getSearchParameters(int searchAction) { |
| 140 | + JsonNode uriParams = null; |
| 141 | + ObjectMapper objectmapper = new ObjectMapper(); |
| 142 | + |
| 143 | + String[] linkTypes = {"ALL"}; |
| 144 | + |
| 145 | + try { |
| 146 | + uriParams = objectmapper.readTree("{}"); |
| 147 | + if (searchAction == DOWNSTREAM) { |
| 148 | + putSearchParameter(uriParams, "dlt", linkTypes); |
| 149 | + } else if (searchAction == UPSTREAM) { |
| 150 | + putSearchParameter(uriParams, "ult", linkTypes); |
| 151 | + } else if (searchAction == DOWNANDUPSTREAM) { |
| 152 | + putSearchParameter(uriParams, "dlt", linkTypes); |
| 153 | + putSearchParameter(uriParams, "ult", linkTypes); |
| 154 | + } |
| 155 | + } catch (Exception e) { |
| 156 | + log.error(e.getMessage(), e); |
| 157 | + } |
| 158 | + return uriParams; |
| 159 | + } |
| 160 | + |
| 161 | + /** Create an array node with link types for upstream or downstream query |
| 162 | + * @param params |
| 163 | + * @param actionString |
| 164 | + * @param linkTypes |
| 165 | + */ |
| 166 | + public void putSearchParameter(JsonNode params, String actionString, String[] linkTypes) { |
| 167 | + ArrayNode node =((ObjectNode) params).putArray(actionString); |
| 168 | + for (String string : linkTypes) { |
| 169 | + node.add(string); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @PostConstruct |
| 174 | + public void init() { |
| 175 | + log.debug("The url parameter is : " + url); |
| 176 | + } |
| 177 | +} |
0 commit comments