1 package org.openkilda.integration.service;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.http.HttpMethod;
8 import org.springframework.stereotype.Service;
9 import org.springframework.util.StringUtils;
10 import org.springframework.web.util.UriComponentsBuilder;
12 import com.fasterxml.jackson.core.JsonProcessingException;
13 import com.fasterxml.jackson.databind.ObjectMapper;
15 import java.io.InputStream;
16 import java.io.UnsupportedEncodingException;
18 import java.net.URLEncoder;
19 import java.util.HashMap;
20 import java.util.List;
23 import org.apache.http.HttpResponse;
64 ObjectMapper objectMapper;
73 HttpResponse response = restClientManager.invoke(applicationProperties.getSwitches(),
74 HttpMethod.GET,
"",
"", applicationService.getAuthHeader());
76 List<SwitchInfo> switchesResponse =
77 restClientManager.getResponseList(response,
SwitchInfo.class);
78 return getSwitchInfoSetName(switchesResponse);
89 private List<SwitchInfo> getSwitchInfoSetName(List<SwitchInfo> switches) {
91 LOGGER.info(
"Inside getSwitchInfoSetName : Start");
92 if (switches != null && !StringUtils.isEmpty(switches)) {
111 if (csNames != null && !StringUtils.isEmpty(csNames) && csNames.size() > 0) {
112 if (csNames.containsKey(switchId.toLowerCase())
113 || csNames.containsKey(switchId.toUpperCase())) {
117 return csNames.get(switchId);
136 return islLinkConverter.toIslLinksInfo(links, islCostMap());
145 HttpResponse response = restClientManager.invoke(applicationProperties.getLinks(),
146 HttpMethod.GET,
"",
"", applicationService.getAuthHeader());
148 List<IslLink> links = restClientManager.getResponseList(response,
IslLink.class);
154 private Map<String,String> islCostMap(){
156 Map<String,String> islCostMap =
new HashMap<>();
157 if (linkProps != null) {
159 linkProps.forEach(linkProp -> {
161 linkProp.getSrc_switch() +
"-" + linkProp.getSrc_port() +
"-" 162 + linkProp.getDst_switch() +
"-" + linkProp.getDst_port();
163 String
value = linkProp.getProperty(
"cost");
164 islCostMap.put(key,
value);
179 UriComponentsBuilder builder =
180 UriComponentsBuilder.fromHttpUrl(applicationProperties.getLinkProps());
181 builder = setLinkProps(keys, builder);
182 String fullUri = builder.build().toUriString();
183 HttpResponse response = restClientManager.invoke(fullUri, HttpMethod.GET,
"",
"",
184 applicationService.getAuthHeader());
187 List<LinkProps> linkPropsResponses =
188 restClientManager.getResponseList(response,
LinkProps.class);
190 return linkPropsResponses;
194 LOGGER.error(
"Exception in getIslLinkProps " + exception.getMessage(), exception);
206 @SuppressWarnings(
"unchecked")
208 Map<String, String> csNames =
new HashMap<String, String>();
210 InputStream inputStream = null;
213 inputStream =
new URL(applicationProperties.getSwitchDataFilePath()).openStream();
214 if (inputStream != null) {
217 if (
data != null && !StringUtils.isEmpty(
data)) {
221 }
catch (Exception ex) {
223 "Inside getCustomSwitchNameFromFile unable to find switch file path Exception :",
239 HttpResponse response = restClientManager.invoke(applicationProperties.getLinkProps(),
240 HttpMethod.PUT, objectMapper.writeValueAsString(keys),
"application/json",
241 applicationService.getAuthHeader());
243 }
catch (Exception e) {
244 LOGGER.error(
"Inside updateIslLinkProps Exception :", e);
256 private UriComponentsBuilder setLinkProps(
LinkProps keys, UriComponentsBuilder builder) {
260 builder.queryParam(
"src_switch",
263 builder.queryParam(
"src_port", URLEncoder.encode(keys.
getSrc_port(),
"UTF-8"));
265 builder.queryParam(
"dst_switch",
268 builder.queryParam(
"dst_port", URLEncoder.encode(keys.
getDst_port(),
"UTF-8"));
270 }
catch (UnsupportedEncodingException e) {
271 throw new ContentNotFoundException();
285 HttpResponse response =
287 .invoke(applicationProperties.getSwitchRules().replace(
"{switch_id}",
288 switchId), HttpMethod.GET,
"",
"",
289 applicationService.getAuthHeader());
291 }
catch (Exception e) {
292 LOGGER.error(
"Inside updateIslLinkProps Exception :", e);
307 HttpResponse response = restClientManager.invoke(
308 applicationProperties.getConfigSwitchPort().replace(
"{switch_id}", switchId).replace(
"{port_no}",
310 HttpMethod.PUT, objectMapper.writeValueAsString(configuration),
"application/json",
311 applicationService.getAuthHeader());
313 return restClientManager.getResponse(response,
ConfiguredPort.class);
315 }
catch (JsonProcessingException e) {
316 LOGGER.error(
"Error occurred while converting configration to string. Exception: " + e.getMessage(), e);
String customSwitchName(Map< String, String > csNames, String switchId)
List< IslLinkInfo > getIslLinks()
static boolean isValidResponse(final HttpResponse response)
String getSwitchRules(String switchId)
static boolean isEmpty(final Collection<?> collection)
Map< String, String > getCustomSwitchNameFromFile()
static boolean chkStringIsNotEmpty(String value)
ConfiguredPort configurePort(String switchId, String port, PortConfiguration configuration)
static String toString(final InputStream inputStream)
static< T > T toObject(final String data, final Class< T > objClass)
List< LinkProps > getIslLinkProps(LinkProps keys)
String updateIslLinkProps(List< LinkProps > keys)
List< SwitchInfo > getSwitches()
List< IslLink > getIslLinkPortsInfo()