Open Kilda Java Documentation
FlowTrafficExamBuilder.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.atdd.staging.helpers;
17 
30 
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Optional;
34 
35 public class FlowTrafficExamBuilder {
36 
37  private final TraffExamService traffExam;
38 
39  private Map<NetworkEndpoint, TraffGen> endpointToTraffGen = new HashMap<>();
40 
42  this.traffExam = traffExam;
43 
44  for (TraffGen traffGen : topology.getActiveTraffGens()) {
45  NetworkEndpoint endpoint = new NetworkEndpoint(
46  traffGen.getSwitchConnected().getDpId(), traffGen.getSwitchPort());
47  endpointToTraffGen.put(endpoint, traffGen);
48  }
49  }
50 
56  Optional<TraffGen> source = Optional.ofNullable(
57  endpointToTraffGen.get(makeComparableEndpoint(flow.getSource())));
58  Optional<TraffGen> dest = Optional.ofNullable(
59  endpointToTraffGen.get(makeComparableEndpoint(flow.getDestination())));
60 
61  checkIsFlowApplicable(flow, source.isPresent(), dest.isPresent());
62 
63  //noinspection ConstantConditions
64  Host sourceHost = traffExam.hostByName(source.get().getName());
65  //noinspection ConstantConditions
66  Host destHost = traffExam.hostByName(dest.get().getName());
67 
68  // burst value is hardcoded into floddlight-modules as 1000 kbit/sec, so to overcome this burst we need at least
69  // 1024 * 1024 / 8 / 1500 = 87.3...
70  Exam forward = Exam.builder()
71  .flow(flow)
72  .source(sourceHost)
73  .sourceVlan(new Vlan(flow.getSource().getVlanId()))
74  .dest(destHost)
75  .destVlan(new Vlan(flow.getDestination().getVlanId()))
76  .bandwidthLimit(new Bandwidth(bandwidth))
77  .burstPkt(100)
78  .build();
79  Exam reverse = Exam.builder()
80  .flow(flow)
81  .source(destHost)
82  .sourceVlan(new Vlan(flow.getDestination().getVlanId()))
83  .dest(sourceHost)
84  .destVlan(new Vlan(flow.getSource().getVlanId()))
85  .bandwidthLimit(new Bandwidth(bandwidth))
86  .burstPkt(100)
87  .build();
88 
89  return new FlowBidirectionalExam(forward, reverse);
90  }
91 
95  public Exam buildExam(FlowPayload flow, int bandwidth) throws FlowNotApplicableException {
96  Optional<TraffGen> source = Optional.ofNullable(
97  endpointToTraffGen.get(makeComparableEndpoint(flow.getSource())));
98  Optional<TraffGen> dest = Optional.ofNullable(
99  endpointToTraffGen.get(makeComparableEndpoint(flow.getDestination())));
100 
101  checkIsFlowApplicable(flow, source.isPresent(), dest.isPresent());
102 
103  //noinspection ConstantConditions
104  Host sourceHost = traffExam.hostByName(source.get().getName());
105  //noinspection ConstantConditions
106  Host destHost = traffExam.hostByName(dest.get().getName());
107 
108  // burst value is hardcoded into floddlight-modules as 1000 kbit/sec, so to overcome this burst we need at least
109  // 1024 * 1024 / 8 / 1500 = 87.3...
110  return Exam.builder()
111  .flow(flow)
112  .source(sourceHost)
113  .sourceVlan(new Vlan(flow.getSource().getVlanId()))
114  .dest(destHost)
115  .destVlan(new Vlan(flow.getDestination().getVlanId()))
116  .bandwidthLimit(new Bandwidth(bandwidth))
117  .burstPkt(100)
118  .build();
119  }
120 
121  private void checkIsFlowApplicable(FlowPayload flow, boolean sourceApplicable, boolean destApplicable)
123  String message;
124 
125  if (!sourceApplicable && !destApplicable) {
126  message = "source endpoint and destination endpoint are";
127  } else if (!sourceApplicable) {
128  message = "source endpoint is";
129  } else if (!destApplicable) {
130  message = "dest endpoint is";
131  } else {
132  message = null;
133  }
134 
135  if (message != null) {
136  throw new FlowNotApplicableException(String.format(
137  "Flow's %s %s not applicable for traffic examination.", flow.getId(), message));
138  }
139  }
140 
141  private NetworkEndpoint makeComparableEndpoint(FlowEndpointPayload flowEndpoint) {
142  return new NetworkEndpoint(flowEndpoint);
143  }
144 }
FlowTrafficExamBuilder(TopologyDefinition topology, TraffExamService traffExam)
FlowBidirectionalExam buildBidirectionalExam(FlowPayload flow, int bandwidth)
source
Definition: nodes.py:53