Open Kilda Java Documentation
PathComputerFlowFetcher.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Telstra Open Source
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.openkilda.wfm.share.utils;
18 
22 
23 import lombok.Getter;
24 import org.neo4j.cypher.InvalidArgumentException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.Map;
32 
34  private static final Logger log = LoggerFactory.getLogger(PathComputerFlowFetcher.class);
35 
36  @Getter
37  private final Collection<BidirectionalFlow> flows = new ArrayList<>();
38 
39  public PathComputerFlowFetcher(PathComputer pathComputer) {
40  for (FlowCollector collector : fetchFlows(pathComputer)) {
41  try {
42  flows.add(collector.make());
43  } catch (InvalidArgumentException e) {
44  log.error(
45  "Invalid flow pairing {}: {}",
46  collector.anyDefined().getFlowId(),
47  e.toString());
48  }
49 
50  }
51  }
52 
53  private Collection<FlowCollector> fetchFlows(PathComputer pathComputer) {
54  Map<String, FlowCollector> flowPairsMap = new HashMap<>();
55  for (Flow flow : pathComputer.getAllFlows()) {
56  if (!flowPairsMap.containsKey(flow.getFlowId())) {
57  flowPairsMap.put(flow.getFlowId(), new FlowCollector());
58  }
59 
60  FlowCollector pair = flowPairsMap.get(flow.getFlowId());
61  try {
62  pair.add(flow);
63  } catch (IllegalArgumentException e) {
64  log.error("Invalid half-flow {}: {}", flow.getFlowId(), e.toString());
65  }
66  }
67 
68  return flowPairsMap.values();
69  }
70 }