Open Kilda Java Documentation
CacheWarmingService.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.wfm.topology.cache.service;
17 
29 
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.commons.lang3.tuple.MutablePair;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Objects;
38 import java.util.stream.Collectors;
39 
40 public class CacheWarmingService {
41 
42  private static final Logger LOGGER = LoggerFactory.getLogger(CacheWarmingService.class);
43  private final List<MutablePair<Flow, CachedFlowState>> predefinedFlows = new ArrayList<>();
44 
45  private NetworkCache networkCache;
46 
47  public CacheWarmingService(NetworkCache networkCache) {
48  this.networkCache = networkCache;
49  }
50 
57  public List<PortInfoData> getPortsForDiscovering(SwitchId switchId) {
58  List<IslInfoData> links = getActiveLinks(switchId);
59  return links.stream()
61  .flatMap(List::stream)
62  .map(isl -> new PortInfoData(isl.getSwitchId(), isl.getPortNo(), PortChangeType.CACHED))
63  .collect(Collectors.toList());
64  }
65 
71  private List<IslInfoData> getActiveLinks(SwitchId switchId) {
72  return networkCache.getIslsBySwitch(switchId).stream()
73  .filter(isl -> isLinkAvailable(isl.getPath(), switchId))
74  .collect(Collectors.toList());
75  }
76 
83  private boolean isLinkAvailable(List<PathNode> nodes, SwitchId currentSwitchId) {
84  return nodes.stream()
85  .allMatch(node -> networkCache.getSwitch(node.getSwitchId()).getState().isActive()
86  || StringUtils.equals(currentSwitchId.toString(), node.getSwitchId().toString()));
87  }
88 
95  public List<CommandData> getFlowCommands(SwitchId switchId) {
96  List<CommandData> result = new ArrayList<>();
97  predefinedFlows.stream()
98  .filter(flow -> switchId.equals(flow.getLeft().getSourceSwitch()))
99  .forEach(pair -> {
100  CommandData command = getFlowCommandIfNeeded(pair.getLeft());
101  if (Objects.nonNull(command)) {
102  result.add(command);
103  }
104  });
105 
106  return result;
107  }
108 
109  private CommandData getFlowCommandIfNeeded(Flow flow) {
110  CommandData request = null;
111 
112  if (flow.getState() == FlowState.CACHED && isFlowSwitchesUp(flow)) {
113  request = new FlowCreateRequest(flow);
114  LOGGER.info("Created flow create request for flowId {}", flow.getFlowId());
115  predefinedFlows.stream()
116  .filter(item -> item.getLeft().equals(flow))
117  .findFirst()
118  .ifPresent(pair -> pair.setRight(CachedFlowState.CREATED));
119  }
120 
121  return request;
122  }
123 
124  private boolean isFlowSwitchesUp(Flow flow) {
125  return Objects.nonNull(flow) && flow.getFlowPath().getPath()
126  .stream()
127  .allMatch(node ->
128  networkCache.getSwitch(node.getSwitchId()).getState().isActive());
129  }
130 
136  if (Objects.nonNull(flows.getLeft())) {
137  predefinedFlows.add(new MutablePair<>(flows.getLeft(), CachedFlowState.CACHED));
138  }
139  if (Objects.nonNull(flows.getRight())) {
140  predefinedFlows.add(new MutablePair<>(flows.getRight(), CachedFlowState.CACHED));
141  }
142  }
143 
144  private enum CachedFlowState {
145  CACHED, CREATED
146  }
147 }
List< PortInfoData > getPortsForDiscovering(SwitchId switchId)
Definition: nodes.py:1
def command(payload, fields)
Definition: share.py:102
list result
Definition: plan-d.py:72