Open Kilda Java Documentation
FlowValidator.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.flow.validation;
17 
18 import static java.lang.String.format;
19 
22 
23 import com.google.common.annotations.VisibleForTesting;
24 
25 import java.util.Optional;
26 import java.util.Set;
27 
31 public class FlowValidator {
32 
33  private final FlowCache flowCache;
34 
35  public FlowValidator(FlowCache flowCache) {
36  this.flowCache = flowCache;
37  }
38 
45  public void validate(Flow flow) throws FlowValidationException {
46  checkBandwidth(flow);
47  checkFlowForEndpointConflicts(flow);
48  }
49 
50  @VisibleForTesting
51  void checkBandwidth(Flow flow) throws FlowValidationException {
52  if (flow.getBandwidth() < 0) {
53  throw new FlowValidationException(
54  format("The flow '%s' has invalid bandwidth %d provided.",
55  flow.getFlowId(),
56  flow.getBandwidth()));
57  }
58  }
59 
66  @VisibleForTesting
67  void checkFlowForEndpointConflicts(Flow requestedFlow) throws FlowValidationException {
68  // Check the source
69  Set<Flow> conflictsOnSource;
70  if (requestedFlow.getSourceVlan() == 0) {
71  conflictsOnSource = flowCache.getFlowsForEndpoint(
72  requestedFlow.getSourceSwitch(),
73  requestedFlow.getSourcePort());
74  } else {
75  conflictsOnSource = flowCache.getFlowsForEndpoint(
76  requestedFlow.getSourceSwitch(),
77  requestedFlow.getSourcePort(),
78  requestedFlow.getSourceVlan());
79  }
80 
81  Optional<Flow> conflictedFlow = conflictsOnSource.stream()
82  .filter(flow -> !flow.getFlowId().equals(requestedFlow.getFlowId()))
83  .findAny();
84  if (conflictedFlow.isPresent()) {
85  throw new FlowValidationException(
86  format("The port %d on the switch '%s' has already occupied by the flow '%s'.",
87  requestedFlow.getSourcePort(),
88  requestedFlow.getSourceSwitch(),
89  conflictedFlow.get().getFlowId()));
90  }
91 
92  // Check the destination
93  Set<Flow> conflictsOnDest;
94  if (requestedFlow.getDestinationVlan() == 0) {
95  conflictsOnDest = flowCache.getFlowsForEndpoint(
96  requestedFlow.getDestinationSwitch(),
97  requestedFlow.getDestinationPort());
98  } else {
99  conflictsOnDest = flowCache.getFlowsForEndpoint(
100  requestedFlow.getDestinationSwitch(),
101  requestedFlow.getDestinationPort(),
102  requestedFlow.getDestinationVlan());
103  }
104 
105  conflictedFlow = conflictsOnDest.stream()
106  .filter(flow -> !flow.getFlowId().equals(requestedFlow.getFlowId()))
107  .findAny();
108  if (conflictedFlow.isPresent()) {
109  throw new FlowValidationException(
110  format("The port %d on the switch '%s' has already occupied by the flow '%s'.",
111  requestedFlow.getDestinationPort(),
112  requestedFlow.getDestinationSwitch(),
113  conflictedFlow.get().getFlowId()));
114  }
115  }
116 }
Set< Flow > getFlowsForEndpoint(SwitchId switchId, int port)
Definition: FlowCache.java:500