Open Kilda Java Documentation
SwitchIntegrationService.java
Go to the documentation of this file.
1 package org.openkilda.integration.service;
2 
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 
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;
11 
12 import com.fasterxml.jackson.core.JsonProcessingException;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 
15 import java.io.InputStream;
16 import java.io.UnsupportedEncodingException;
17 import java.net.URL;
18 import java.net.URLEncoder;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 
23 import org.apache.http.HttpResponse;
40 
46 @Service
48 
49  private static final Logger LOGGER = LoggerFactory.getLogger(SwitchIntegrationService.class);
50 
51  @Autowired
52  private RestClientManager restClientManager;
53 
54  @Autowired
55  private ApplicationProperties applicationProperties;
56 
57  @Autowired
58  private ApplicationService applicationService;
59 
60  @Autowired
61  private IslLinkConverter islLinkConverter;
62 
63  @Autowired
64  ObjectMapper objectMapper;
65 
72  public List<SwitchInfo> getSwitches() {
73  HttpResponse response = restClientManager.invoke(applicationProperties.getSwitches(),
74  HttpMethod.GET, "", "", applicationService.getAuthHeader());
75  if (RestClientManager.isValidResponse(response)) {
76  List<SwitchInfo> switchesResponse =
77  restClientManager.getResponseList(response, SwitchInfo.class);
78  return getSwitchInfoSetName(switchesResponse);
79  }
80  return null;
81  }
82 
89  private List<SwitchInfo> getSwitchInfoSetName(List<SwitchInfo> switches) {
90 
91  LOGGER.info("Inside getSwitchInfoSetName : Start");
92  if (switches != null && !StringUtils.isEmpty(switches)) {
93 
94  Map<String, String> csNames = getCustomSwitchNameFromFile();
95 
96  for (SwitchInfo switchInfo : switches) {
97  switchInfo.setName(customSwitchName(csNames, switchInfo.getSwitchId()));
98  }
99  }
100  return switches;
101  }
102 
110  public String customSwitchName(Map<String, String> csNames, String switchId) {
111  if (csNames != null && !StringUtils.isEmpty(csNames) && csNames.size() > 0) {
112  if (csNames.containsKey(switchId.toLowerCase())
113  || csNames.containsKey(switchId.toUpperCase())) {
114  if (!IoUtil.chkStringIsNotEmpty(csNames.get(switchId))) {
115  return switchId;
116  } else {
117  return csNames.get(switchId);
118  }
119  } else {
120  return switchId;
121  }
122  } else
123  return switchId;
124  }
125 
131  public List<IslLinkInfo> getIslLinks() {
132  List<IslLink> links = getIslLinkPortsInfo();
133  if (CollectionUtil.isEmpty(links)) {
134  throw new ContentNotFoundException();
135  }
136  return islLinkConverter.toIslLinksInfo(links, islCostMap());
137  }
138 
144  public List<IslLink> getIslLinkPortsInfo() {
145  HttpResponse response = restClientManager.invoke(applicationProperties.getLinks(),
146  HttpMethod.GET, "", "", applicationService.getAuthHeader());
147  if (RestClientManager.isValidResponse(response)) {
148  List<IslLink> links = restClientManager.getResponseList(response, IslLink.class);
149  return links;
150  }
151  return null;
152  }
153 
154  private Map<String,String> islCostMap(){
155  List<LinkProps> linkProps = getIslLinkProps(null);
156  Map<String,String> islCostMap = new HashMap<>();
157  if (linkProps != null) {
158 
159  linkProps.forEach(linkProp -> {
160  String key =
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);
165  });
166  }
167 
168  return islCostMap;
169 
170  }
171 
172 
178  public List<LinkProps> getIslLinkProps(LinkProps keys) {
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());
185  try {
186  if (RestClientManager.isValidResponse(response)) {
187  List<LinkProps> linkPropsResponses =
188  restClientManager.getResponseList(response, LinkProps.class);
189  if (!CollectionUtil.isEmpty(linkPropsResponses)) {
190  return linkPropsResponses;
191  }
192  }
193  } catch (InvalidResponseException exception) {
194  LOGGER.error("Exception in getIslLinkProps " + exception.getMessage(), exception);
195  return null;
196  }
197  return null;
198  }
199 
200 
206  @SuppressWarnings("unchecked")
207  public Map<String, String> getCustomSwitchNameFromFile() {
208  Map<String, String> csNames = new HashMap<String, String>();
209 
210  InputStream inputStream = null;
211  String data = null;
212  try {
213  inputStream = new URL(applicationProperties.getSwitchDataFilePath()).openStream();
214  if (inputStream != null) {
215  data = IoUtil.toString(inputStream);
216 
217  if (data != null && !StringUtils.isEmpty(data)) {
218  csNames = JsonUtil.toObject(data, HashMap.class);
219  }
220  }
221  } catch (Exception ex) {
222  LOGGER.error(
223  "Inside getCustomSwitchNameFromFile unable to find switch file path Exception :",
224  ex);
225  }
226  return csNames;
227 
228  }
229 
237  public String updateIslLinkProps(List<LinkProps> keys) {
238  try {
239  HttpResponse response = restClientManager.invoke(applicationProperties.getLinkProps(),
240  HttpMethod.PUT, objectMapper.writeValueAsString(keys), "application/json",
241  applicationService.getAuthHeader());
242  return IoUtil.toString(response.getEntity().getContent());
243  } catch (Exception e) {
244  LOGGER.error("Inside updateIslLinkProps Exception :", e);
245  throw new IntegrationException(e);
246  }
247  }
248 
256  private UriComponentsBuilder setLinkProps(LinkProps keys, UriComponentsBuilder builder) {
257  try {
258  if (keys != null) {
259  if (!keys.getSrc_switch().isEmpty())
260  builder.queryParam("src_switch",
261  URLEncoder.encode(keys.getSrc_switch(), "UTF-8"));
262  if (!keys.getSrc_port().isEmpty())
263  builder.queryParam("src_port", URLEncoder.encode(keys.getSrc_port(), "UTF-8"));
264  if (!keys.getDst_switch().isEmpty())
265  builder.queryParam("dst_switch",
266  URLEncoder.encode(keys.getDst_switch(), "UTF-8"));
267  if (!keys.getDst_port().isEmpty())
268  builder.queryParam("dst_port", URLEncoder.encode(keys.getDst_port(), "UTF-8"));
269  }
270  } catch (UnsupportedEncodingException e) {
271  throw new ContentNotFoundException();
272  }
273  return builder;
274  }
275 
282  public String getSwitchRules(String switchId) {
283 
284  try {
285  HttpResponse response =
286  restClientManager
287  .invoke(applicationProperties.getSwitchRules().replace("{switch_id}",
288  switchId), HttpMethod.GET, "", "",
289  applicationService.getAuthHeader());
290  return IoUtil.toString(response.getEntity().getContent());
291  } catch (Exception e) {
292  LOGGER.error("Inside updateIslLinkProps Exception :", e);
293  throw new IntegrationException(e);
294  }
295  }
296 
305  public ConfiguredPort configurePort(String switchId, String port, PortConfiguration configuration) {
306  try {
307  HttpResponse response = restClientManager.invoke(
308  applicationProperties.getConfigSwitchPort().replace("{switch_id}", switchId).replace("{port_no}",
309  port),
310  HttpMethod.PUT, objectMapper.writeValueAsString(configuration), "application/json",
311  applicationService.getAuthHeader());
312  if (RestClientManager.isValidResponse(response)) {
313  return restClientManager.getResponse(response, ConfiguredPort.class);
314  }
315  } catch (JsonProcessingException e) {
316  LOGGER.error("Error occurred while converting configration to string. Exception: " + e.getMessage(), e);
317  throw new IntegrationException(e);
318  }
319  return null;
320  }
321 }
String customSwitchName(Map< String, String > csNames, String switchId)
value
Definition: nodes.py:62
static boolean isValidResponse(final HttpResponse response)
static boolean isEmpty(final Collection<?> collection)
static boolean chkStringIsNotEmpty(String value)
Definition: IoUtil.java:64
ConfiguredPort configurePort(String switchId, String port, PortConfiguration configuration)
static String toString(final InputStream inputStream)
Definition: IoUtil.java:29
static< T > T toObject(final String data, final Class< T > objClass)
Definition: JsonUtil.java:29