Open Kilda Java Documentation
LinkServiceImpl.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.service.impl;
17 
40 
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.stereotype.Service;
46 
47 import java.util.ArrayList;
48 import java.util.List;
49 import java.util.stream.Collectors;
50 
51 @Service
52 public class LinkServiceImpl implements LinkService {
53 
54  private static final Logger logger = LoggerFactory.getLogger(LinkServiceImpl.class);
55 
56  @Autowired
57  private CorrelationIdFactory idFactory;
58 
59  @Autowired
60  private LinkMapper linkMapper;
61 
62  @Autowired
63  private LinkPropsMapper linkPropsMapper;
64 
65  @Value("#{kafkaTopicsConfig.getTopoEngTopic()}")
66  private String topologyEngineTopic;
67 
71  @Value("#{kafkaTopicsConfig.getTopoNbTopic()}")
72  private String nbworkerTopic;
73 
74  @Autowired
75  private MessageConsumer messageConsumer;
76  @Autowired
77  private MessageProducer messageProducer;
78 
79  @Autowired
80  private ResponseCollector<IslInfoData> linksCollector;
81  @Autowired
82  private ResponseCollector<LinkPropsResponse> teLinksCollector;
83  @Autowired
84  private ResponseCollector<LinkPropsData> linksPropsCollector;
85 
86  @Override
87  public List<LinkDto> getLinks() {
88  final String correlationId = RequestCorrelationId.getId();
89  logger.debug("Get links request received");
90  CommandMessage request = new CommandMessage(new GetLinksRequest(), System.currentTimeMillis(), correlationId);
91  messageProducer.send(nbworkerTopic, request);
92  List<IslInfoData> links = linksCollector.getResult(correlationId);
93 
94  return links.stream()
95  .map(linkMapper::toLinkDto)
96  .collect(Collectors.toList());
97  }
98 
99  @Override
100  public List<LinkPropsDto> getLinkProps(SwitchId srcSwitch, Integer srcPort, SwitchId dstSwitch, Integer dstPort) {
101  final String correlationId = RequestCorrelationId.getId();
102  logger.debug("Get link properties request received");
103  LinkPropsGet request = new LinkPropsGet(new NetworkEndpointMask(srcSwitch, srcPort),
104  new NetworkEndpointMask(dstSwitch, dstPort));
105  CommandMessage message = new CommandMessage(request, System.currentTimeMillis(), correlationId);
106  messageProducer.send(nbworkerTopic, message);
107  List<LinkPropsData> links = linksPropsCollector.getResult(correlationId);
108 
109  logger.debug("Found link props items: {}", links.size());
110  return links.stream()
111  .map(linkPropsMapper::toDto)
112  .collect(Collectors.toList());
113  }
114 
115  @Override
116  public BatchResults setLinkProps(List<LinkPropsDto> linkPropsList) {
117  logger.debug("Link props \"SET\" request received (consists of {} records)", linkPropsList.size());
118 
119  ArrayList<String> pendingRequest = new ArrayList<>(linkPropsList.size());
120  for (LinkPropsDto requestItem : linkPropsList) {
121  LinkPropsPut teRequest = new LinkPropsPut(linkPropsMapper.toLinkProps(requestItem));
122  String requestId = idFactory.produceChained(RequestCorrelationId.getId());
123  CommandMessage message = new CommandMessage(teRequest, System.currentTimeMillis(), requestId);
124  messageProducer.send(topologyEngineTopic, message);
125 
126  pendingRequest.add(requestId);
127  }
128 
129  int successCount = 0;
130  ArrayList<String> errors = new ArrayList<>(pendingRequest.size());
131  for (String requestId : pendingRequest) {
132  InfoMessage message = (InfoMessage) messageConsumer.poll(requestId);
133  LinkPropsResponse response = (LinkPropsResponse) message.getData();
134 
135  if (response.isSuccess()) {
136  successCount += 1;
137  } else {
138  errors.add(response.getError());
139  }
140  }
141 
142  return new BatchResults(linkPropsList.size() - successCount, successCount, errors);
143  }
144 
145  @Override
146  public BatchResults delLinkProps(List<LinkPropsDto> linkPropsList) {
147  ArrayList<String> pendingChains = new ArrayList<>();
148  for (LinkPropsDto requestItem : linkPropsList) {
149  LinkPropsDrop teRequest = new LinkPropsDrop(linkPropsMapper.toLinkPropsMask(requestItem));
150  String requestId = idFactory.produceChained(RequestCorrelationId.getId());
151  CommandMessage message = new CommandMessage(teRequest, System.currentTimeMillis(), requestId);
152  messageProducer.send(topologyEngineTopic, message);
153 
154  pendingChains.add(requestId);
155  }
156 
157  int successCount = 0;
158  ArrayList<String> errors = new ArrayList<>();
159  for (String requestId : pendingChains) {
160  List<LinkPropsResponse> responseBatch = teLinksCollector.getResult(requestId);
161  for (LinkPropsResponse response : responseBatch) {
162  if (response.isSuccess()) {
163  successCount += 1;
164  } else {
165  errors.add(response.getError());
166  }
167  }
168  }
169 
170  return new BatchResults(errors.size(), successCount, errors);
171  }
172 }