Open Kilda Java Documentation
FlowCollector.java
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 public class FlowCollector {
26  private String flowId;
27  private Flow forward = null;
28  private Flow reverse = null;
29 
30  public void add(Flow flow) {
31  Flow current;
32 
33  boolean isForward = flow.isForward();
34  if (isForward) {
35  current = this.forward;
36  } else {
37  current = reverse;
38  }
39 
40  if (current != null) {
41  throw new IllegalArgumentException(
42  String.format("The flow(%s) for %s is already set",
43  flowId, isForward ? "FORWARD" : "REVERSE"));
44  }
45 
46  if (isForward) {
47  forward = flow;
48  } else {
49  reverse = flow;
50  }
51 
52  flowId = flow.getFlowId();
53  }
54 
55  public Flow anyDefined() {
56  if (forward != null) {
57  return forward;
58  }
59  if (reverse != null) {
60  return reverse;
61  }
62 
63  // FIXME(surabujin): make custom exception class
64  throw new IllegalArgumentException("No one half-flow pieces defined");
65  }
66 
68  List<String> missing = new ArrayList<>(2);
69  if (forward == null) {
70  missing.add("FORWARD is missing");
71  }
72  if (reverse == null) {
73  missing.add("REVERSE is missing");
74  }
75  if (0 < missing.size()) {
76  throw new IllegalArgumentException(
77  String.format(
78  "Flow pair is incomplete: %s",
79  String.join(" and ", missing))
80  );
81  }
82 
83  return new BidirectionalFlow(forward, reverse);
84  }
85 }