Open Kilda Java Documentation
SwitchServiceImpl.java
Go to the documentation of this file.
1 /* Copyright 2017 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 
18 import static java.lang.String.format;
19 import static java.util.Collections.emptyList;
20 
59 
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.beans.factory.annotation.Value;
64 import org.springframework.stereotype.Service;
65 import org.springframework.util.CollectionUtils;
66 
67 import java.util.ArrayList;
68 import java.util.List;
69 import java.util.stream.Collectors;
70 
71 @Service
72 public class SwitchServiceImpl implements SwitchService {
73 
74  private static final Logger LOGGER = LoggerFactory.getLogger(SwitchServiceImpl.class);
75 
76  @Value("#{kafkaTopicsConfig.getTopoEngTopic()}")
77  private String topoEngTopic;
78 
79  @Autowired
80  private MessageProducer messageProducer;
81 
82  @Autowired
83  private MessageConsumer<Message> messageConsumer;
84 
85  @Autowired
86  private ResponseCollector<SwitchInfoData> switchesCollector;
87 
88  @Autowired
89  private SwitchMapper switchMapper;
90 
91  @Value("#{kafkaTopicsConfig.getSpeakerTopic()}")
92  private String floodlightTopic;
93 
94  @Value("#{kafkaTopicsConfig.getNorthboundTopic()}")
95  private String northboundTopic;
96 
97  @Value("#{kafkaTopicsConfig.getTopoNbTopic()}")
98  private String nbworkerTopic;
99 
103  @Override
104  public List<SwitchDto> getSwitches() {
105  final String correlationId = RequestCorrelationId.getId();
106  LOGGER.debug("Get switch request received");
107  CommandMessage request = new CommandMessage(new GetSwitchesRequest(), System.currentTimeMillis(),
108  correlationId);
109  messageProducer.send(nbworkerTopic, request);
110 
111  List<SwitchInfoData> switches = switchesCollector.getResult(correlationId);
112  return switches.stream()
113  .map(switchMapper::toSwitchDto)
114  .collect(Collectors.toList());
115  }
116 
120  @Override
121  public SwitchFlowEntries getRules(SwitchId switchId, Long cookie, String correlationId) {
122  DumpRulesRequest request = new DumpRulesRequest(switchId);
123  CommandWithReplyToMessage commandMessage = new CommandWithReplyToMessage(request, System.currentTimeMillis(),
124  correlationId, Destination.CONTROLLER, northboundTopic);
125  messageProducer.send(floodlightTopic, commandMessage);
126  Message message = messageConsumer.poll(correlationId);
127  SwitchFlowEntries response = (SwitchFlowEntries) validateInfoMessage(commandMessage, message, correlationId);
128 
129  if (cookie > 0L) {
130  List<FlowEntry> matchedFlows = new ArrayList<>();
131  for (FlowEntry entry : response.getFlowEntries()) {
132  if (cookie.equals(entry.getCookie())) {
133  matchedFlows.add(entry);
134  }
135  }
136  response = new SwitchFlowEntries(response.getSwitchId(), matchedFlows);
137  }
138  return response;
139  }
140 
141  @Override
142  public SwitchFlowEntries getRules(SwitchId switchId, Long cookie) {
143  return getRules(switchId, cookie, RequestCorrelationId.getId());
144  }
145 
146  @Override
147  public List<Long> deleteRules(SwitchId switchId, DeleteRulesAction deleteAction) {
148  final String correlationId = RequestCorrelationId.getId();
149  LOGGER.debug("Delete switch rules request received: deleteAction={}", deleteAction);
150 
151  SwitchRulesDeleteRequest data = new SwitchRulesDeleteRequest(switchId, deleteAction, null);
152  CommandMessage request = new CommandWithReplyToMessage(data, System.currentTimeMillis(), correlationId,
153  Destination.CONTROLLER, northboundTopic);
154  messageProducer.send(floodlightTopic, request);
155 
156  Message message = messageConsumer.poll(correlationId);
157  SwitchRulesResponse response = (SwitchRulesResponse) validateInfoMessage(request, message, correlationId);
158  return response.getRuleIds();
159  }
160 
161  @Override
162  public List<Long> deleteRules(SwitchId switchId, DeleteRulesCriteria criteria) {
163  final String correlationId = RequestCorrelationId.getId();
164  LOGGER.debug("Delete switch rules request received: criteria={}", criteria);
165 
166  SwitchRulesDeleteRequest data = new SwitchRulesDeleteRequest(switchId, null, criteria);
167  CommandMessage request = new CommandWithReplyToMessage(data, System.currentTimeMillis(), correlationId,
168  Destination.CONTROLLER, northboundTopic);
169  messageProducer.send(floodlightTopic, request);
170 
171  Message message = messageConsumer.poll(correlationId);
172  SwitchRulesResponse response = (SwitchRulesResponse) validateInfoMessage(request, message, correlationId);
173  return response.getRuleIds();
174  }
175 
176 
180  @Override
181  public List<Long> installRules(SwitchId switchId, InstallRulesAction installAction) {
182  final String correlationId = RequestCorrelationId.getId();
183  LOGGER.debug("Install switch rules request received");
184 
185  SwitchRulesInstallRequest data = new SwitchRulesInstallRequest(switchId, installAction);
186  CommandMessage request = new CommandWithReplyToMessage(data, System.currentTimeMillis(), correlationId,
187  Destination.CONTROLLER, northboundTopic);
188  messageProducer.send(floodlightTopic, request);
189 
190  Message message = messageConsumer.poll(correlationId);
191  SwitchRulesResponse response = (SwitchRulesResponse) validateInfoMessage(request, message, correlationId);
192  return response.getRuleIds();
193  }
194 
195 
199  @Override
201  final String correlationId = RequestCorrelationId.getId();
202  LOGGER.debug("Set/Get switch connect mode request received: mode = {}", mode);
203 
205  CommandMessage request = new CommandWithReplyToMessage(data, System.currentTimeMillis(), correlationId,
206  Destination.CONTROLLER, northboundTopic);
207  messageProducer.send(floodlightTopic, request);
208 
209  Message message = messageConsumer.poll(correlationId);
210  ConnectModeResponse response = (ConnectModeResponse) validateInfoMessage(request, message, correlationId);
211  return response.getMode();
212  }
213 
214  @Override
216  final String correlationId = RequestCorrelationId.getId();
217 
218  CommandWithReplyToMessage validateCommandMessage = new CommandWithReplyToMessage(
219  new SwitchRulesValidateRequest(switchId),
220  System.currentTimeMillis(), correlationId, Destination.TOPOLOGY_ENGINE, northboundTopic);
221  messageProducer.send(topoEngTopic, validateCommandMessage);
222 
223  Message validateResponseMessage = messageConsumer.poll(correlationId);
224  SyncRulesResponse validateResponse = (SyncRulesResponse) validateInfoMessage(validateCommandMessage,
225  validateResponseMessage, correlationId);
226 
227  return switchMapper.toRulesValidationResult(validateResponse);
228  }
229 
230  @Override
231  public RulesSyncResult syncRules(SwitchId switchId) {
232  RulesValidationResult validationResult = validateRules(switchId);
233  List<Long> missingRules = validationResult.getMissingRules();
234 
235  if (CollectionUtils.isEmpty(missingRules)) {
236  return switchMapper.toRulesSyncResult(validationResult, emptyList());
237  }
238 
239  LOGGER.debug("The validation result for switch {}: missing rules = {}", switchId, missingRules);
240 
241  // Synchronize the missing rules
242  String syncCorrelationId = format("%s-sync", RequestCorrelationId.getId());
243  CommandWithReplyToMessage syncCommandMessage = new CommandWithReplyToMessage(
244  new SwitchRulesSyncRequest(switchId, missingRules),
245  System.currentTimeMillis(), syncCorrelationId, Destination.TOPOLOGY_ENGINE, northboundTopic);
246  messageProducer.send(topoEngTopic, syncCommandMessage);
247 
248  Message syncResponseMessage = messageConsumer.poll(syncCorrelationId);
249  SyncRulesResponse syncResponse = (SyncRulesResponse) validateInfoMessage(syncCommandMessage,
250  syncResponseMessage, syncCorrelationId);
251 
252  return switchMapper.toRulesSyncResult(validationResult, syncResponse.getInstalledRules());
253  }
254 
255  @Override
256  public DeleteMeterResult deleteMeter(SwitchId switchId, long meterId) {
257  String requestId = RequestCorrelationId.getId();
259  new DeleteMeterRequest(switchId, meterId),
260  System.currentTimeMillis(), requestId, Destination.TOPOLOGY_ENGINE, northboundTopic);
261  messageProducer.send(floodlightTopic, deleteCommand);
262 
263  Message response = messageConsumer.poll(requestId);
264  DeleteMeterResponse result = (DeleteMeterResponse) validateInfoMessage(deleteCommand, response, requestId);
265  return new DeleteMeterResult(result.isDeleted());
266  }
267 
271  @Override
273  String correlationId = RequestCorrelationId.getId();
274 
275  PortConfigurationRequest request = new PortConfigurationRequest(switchId,
276  port, toPortAdminDown(config.getStatus()));
277  CommandWithReplyToMessage updateStatusCommand = new CommandWithReplyToMessage(
278  request, System.currentTimeMillis(), correlationId,
279  Destination.CONTROLLER, northboundTopic);
280  messageProducer.send(floodlightTopic, updateStatusCommand);
281 
282  Message response = messageConsumer.poll(correlationId);
284  updateStatusCommand, response, correlationId);
285 
286  return new PortDto(switchPortResponse.getSwitchId().toString(), switchPortResponse.getPortNo());
287  }
288 
289  private Boolean toPortAdminDown(PortStatus status) {
290  if (status == null) {
291  return null;
292  }
293 
294  boolean adminDownState;
295  switch (status) {
296  case UP:
297  adminDownState = false;
298  break;
299  case DOWN:
300  adminDownState = true;
301  break;
302  default:
303  throw new IllegalArgumentException(String.format(
304  "Unsupported enum %s value: %s", PortStatus.class.getName(), status));
305  }
306  return adminDownState;
307  }
308 }
PortDto configurePort(SwitchId switchId, int port, PortConfigurationPayload config)
SwitchFlowEntries getRules(SwitchId switchId, Long cookie)
Definition: FlowEntry.java:29
SwitchFlowEntries getRules(SwitchId switchId, Long cookie, String correlationId)
DeleteMeterResult deleteMeter(SwitchId switchId, long meterId)
def status()
Definition: rest.py:593
RulesValidationResult validateRules(SwitchId switchId)
List< Long > deleteRules(SwitchId switchId, DeleteRulesAction deleteAction)
default InfoData validateInfoMessage(final Message requestMessage, final Message responseMessage, final String correlationId)
list result
Definition: plan-d.py:72
ConnectModeRequest.Mode connectMode(ConnectModeRequest.Mode mode)
List< Long > deleteRules(SwitchId switchId, DeleteRulesCriteria criteria)
List< Long > installRules(SwitchId switchId, InstallRulesAction installAction)