Open Kilda Java Documentation
IPortImpl.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.simulator.classes;
17 
25 
26 import org.projectfloodlight.openflow.types.DatapathId;
27 
28 import java.io.IOException;
29 import java.time.Instant;
30 import java.util.Random;
31 import java.util.UUID;
32 
33 public class IPortImpl implements IPort {
34  private static final int MAX_SMALL = 50;
35  private static final int MAX_LARGE = 10000;
36 
37  private Random rand = new Random();
38  private int number;
39  private long rxPackets = 0;
40  private long txPackets = 0;
41  private long rxBytes = 0;
42  private long txBytes = 0;
43  private long rxDropped = 0;
44  private long txDropped = 0;
45  private long rxErrors = 0;
46  private long txErrors = 0;
47  private long rxFrameErr = 0;
48  private long rxOverErr = 0;
49  private long rxCrcErr = 0;
50  private long collisions = 0;
51  private boolean isActive = true;
52  private boolean isForwarding = true;
53  private int latency = 0;
54  private DatapathId peerSwitch;
55  private int peerPortNum = -1;
56  private ISwitchImpl sw;
57 
58  public IPortImpl(ISwitchImpl sw, PortStateType state, int portNumber) throws SimulatorException {
59  setSw(sw);
60  if (state == PortStateType.UP) {
61  enable();
62  } else if (state == PortStateType.DOWN) {
63  disable();
64  } else {
65  throw new SimulatorException(String.format("Unknown port state %s", state.toString()));
66  }
67  number = portNumber;
68  }
69 
75  public InfoMessage makePorChangetMessage() throws IOException {
77 
79  new SwitchId(sw.getDpid().toString()),
80  number,
81  type
82  );
83  return new InfoMessage(data, Instant.now().toEpochMilli(), UUID.randomUUID().toString(), null);
84  }
85 
86  @Override
87  public void enable() {
88  this.isActive = true;
89  this.isForwarding = true;
90  }
91 
92  @Override
93  public void disable() {
94  this.isActive = false;
95  this.isForwarding = false;
96  }
97 
98  @Override
99  public void block() {
100  this.isForwarding = false;
101  }
102 
103  @Override
104  public void unblock() {
105  this.isForwarding = true;
106  }
107 
108  public void modPort(PortModMessage message) {
109  isActive = message.isActive();
110  isForwarding = message.isForwarding();
111  }
112 
113  @Override
114  public boolean isActive() {
115  return isActive;
116  }
117 
118  @Override
119  public boolean isForwarding() {
120  return isForwarding;
121  }
122 
123  @Override
124  public int getNumber() {
125  return number;
126  }
127 
128  @Override
129  public void setLatency(int latency) {
130  this.latency = latency;
131  }
132 
133  @Override
134  public int getLatency() {
135  return latency;
136  }
137 
138  @Override
139  public String getPeerSwitch() {
140  return peerSwitch.toString();
141  }
142 
143  @Override
144  public void setPeerSwitch(String peerSwitch) {
145  this.peerSwitch = DatapathId.of(peerSwitch);
146  }
147 
148  @Override
149  public void setPeerPortNum(int peerPortNum) {
150  this.peerPortNum = peerPortNum;
151  }
152 
153  @Override
154  public int getPeerPortNum() {
155  return peerPortNum;
156  }
157 
158  @Override
159  public void setIsl(DatapathId peerSwitch, int peerPortNum) {
160  this.peerSwitch = peerSwitch;
161  this.peerPortNum = peerPortNum;
162  }
163 
164  public ISwitchImpl getSw() {
165  return sw;
166  }
167 
168  public void setSw(ISwitchImpl sw) {
169  this.sw = sw;
170  }
171 
172  @Override
173  public boolean isActiveIsl() {
174  return (peerSwitch != null && peerPortNum >= 0 && isActive && isForwarding);
175  }
176 
177  @Override
179  if (isForwarding && isActive) {
180  this.rxPackets += rand.nextInt(MAX_LARGE);
181  this.txPackets += rand.nextInt(MAX_LARGE);
182  this.rxBytes += rand.nextInt(MAX_LARGE);
183  this.txBytes += rand.nextInt(MAX_LARGE);
184  this.rxDropped += rand.nextInt(MAX_SMALL);
185  this.txDropped += rand.nextInt(MAX_SMALL);
186  this.rxErrors += rand.nextInt(MAX_SMALL);
187  this.txErrors += rand.nextInt(MAX_SMALL);
188  this.rxFrameErr += rand.nextInt(MAX_SMALL);
189  this.rxOverErr += rand.nextInt(MAX_SMALL);
190  this.rxCrcErr += rand.nextInt(MAX_SMALL);
191  this.collisions += rand.nextInt(MAX_SMALL);
192  }
193 
194  return new PortStatsEntry(this.number, this.rxPackets, this.txPackets, this.rxBytes, this.txBytes,
195  this.rxDropped, this.txDropped, this.rxErrors, this.txErrors, this.rxFrameErr, this.rxOverErr,
196  this.rxCrcErr, this.collisions);
197  }
198 }
void setPeerSwitch(String peerSwitch)
Definition: IPortImpl.java:144
IPortImpl(ISwitchImpl sw, PortStateType state, int portNumber)
Definition: IPortImpl.java:58
void modPort(PortModMessage message)
Definition: IPortImpl.java:108
void setIsl(DatapathId peerSwitch, int peerPortNum)
Definition: IPortImpl.java:159