Open Kilda Java Documentation
DiscoveryLink.java
Go to the documentation of this file.
1 /* Copyright 2017 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.messaging.model;
17 
18 import com.fasterxml.jackson.annotation.JsonCreator;
19 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
20 import com.fasterxml.jackson.annotation.JsonInclude;
21 import com.fasterxml.jackson.annotation.JsonProperty;
22 import lombok.Getter;
23 import lombok.ToString;
24 
25 import java.io.Serializable;
26 import java.util.Objects;
27 
28 @JsonIgnoreProperties(ignoreUnknown = true)
29 @JsonInclude(JsonInclude.Include.NON_NULL)
30 @Getter
31 @ToString
32 public class DiscoveryLink implements Serializable {
33 
37  public static final int ENDLESS_ATTEMPTS = -1;
38 
39  @JsonProperty("source")
40  private NetworkEndpoint source;
41 
42  @JsonProperty("destination")
43  private NetworkEndpoint destination;
44 
48  @JsonProperty("attempts")
49  private int attempts;
50 
54  @JsonProperty("ack_attempts")
55  private int ackAttempts;
56 
57  @JsonProperty("time_counter")
58  private int timeCounter;
59 
60  @JsonProperty("check_interval")
61  private int checkInterval;
62 
66  @JsonProperty("consecutive_failure")
67  private int consecutiveFailure;
68 
69  @JsonProperty("consecutive_success")
70  private int consecutiveSuccess;
71 
72  @JsonProperty("consecutive_failure_limit")
73  private int consecutiveFailureLimit;
74 
75  @JsonProperty("active")
76  private boolean active;
77 
81  public DiscoveryLink(SwitchId srcSwitch, int srcPort, int checkInterval, int consecutiveFailureLimit) {
82  this.source = new NetworkEndpoint(srcSwitch, srcPort);
83  this.destination = null;
84  this.timeCounter = 0;
85  this.checkInterval = checkInterval;
86  this.consecutiveFailureLimit = consecutiveFailureLimit;
87  this.consecutiveFailure = 0;
88  this.consecutiveSuccess = 0;
89  }
90 
94  public DiscoveryLink(SwitchId srcSwitch, int srcPort, SwitchId dstSwitch, int dstPort,
95  int checkInterval, int consecutiveFailureLimit, boolean active) {
96  this.source = new NetworkEndpoint(srcSwitch, srcPort);
97  this.destination = new NetworkEndpoint(dstSwitch, dstPort);
98  this.timeCounter = 0;
99  this.checkInterval = checkInterval;
100  this.consecutiveFailureLimit = consecutiveFailureLimit;
101  this.consecutiveFailure = 0;
102  this.consecutiveSuccess = 0;
103  this.active = active;
104  }
105 
109  @JsonCreator
110  public DiscoveryLink(@JsonProperty("source") final NetworkEndpoint source,
111  @JsonProperty("destination") final NetworkEndpoint destination,
112  @JsonProperty("attempts") final int attempts,
113  @JsonProperty("ack_attempts") final int ackAttempts,
114  @JsonProperty("time_counter") final int timeCounter,
115  @JsonProperty("check_interval") final int checkInterval,
116  @JsonProperty("consecutive_failure") final int consecutiveFailure,
117  @JsonProperty("consecutive_success") final int consecutiveSuccess,
118  @JsonProperty("consecutive_failure_limit") final int consecutiveFailureLimit,
119  @JsonProperty("active") final boolean active) {
120  this.source = source;
121  this.destination = destination;
122  this.attempts = attempts;
123  this.ackAttempts = ackAttempts;
124  this.timeCounter = timeCounter;
125  this.checkInterval = checkInterval;
126  this.consecutiveFailureLimit = consecutiveFailureLimit;
127  this.consecutiveFailure = consecutiveFailure;
128  this.consecutiveSuccess = consecutiveSuccess;
129  this.active = active;
130  }
131 
135  public void activate(NetworkEndpoint destination) {
136  this.destination = destination;
137  this.active = true;
138  }
139 
144  public void deactivate() {
145  this.active = false;
146  this.consecutiveFailure = 0;
147  this.consecutiveSuccess = 0;
148  }
149 
155  public void renew() {
156  attempts = 0;
157  ackAttempts = 0;
158  timeCounter = 0;
159  }
160 
165  public boolean isNewAttemptAllowed() {
166  if (consecutiveFailureLimit == ENDLESS_ATTEMPTS) { // never gonna give a link up.
167  return true;
168  }
169  return consecutiveFailure < consecutiveFailureLimit;
170  }
171 
172  public void clearConsecutiveFailure() {
173  consecutiveFailure = 0;
174  }
175 
176  public void clearConsecutiveSuccess() {
177  consecutiveSuccess = 0;
178  }
179 
180  public void fail() {
181  consecutiveFailure++;
182  active = false;
183  }
184 
185  public void success() {
186  consecutiveSuccess++;
187  }
188 
189  public void tick() {
190  timeCounter++;
191  }
192 
193  public void resetTickCounter() {
194  timeCounter = 0;
195  }
196 
201  public boolean isAttemptsLimitExceeded(int attemptsLimit) {
202  return attempts > attemptsLimit;
203  }
204 
208  public void incAttempts() {
209  attempts++;
210  }
211 
216  public boolean isAckAttemptsLimitExceeded(int attemptsLimit) {
217  return ackAttempts > attemptsLimit;
218  }
219 
223  public void incAcknowledgedAttempts() {
224  ackAttempts++;
225  }
226 
227  public boolean timeToCheck() {
228  return timeCounter >= checkInterval;
229  }
230 
237  public boolean isDestinationChanged(SwitchId dstSwitch, int dstPort) {
238  // check if the link was previously not discovered
239  if (this.destination == null) {
240  return false;
241  }
242 
243  return !Objects.equals(this.destination, new NetworkEndpoint(dstSwitch, dstPort));
244  }
245 
246  @Override
247  public boolean equals(Object o) {
248  if (this == o) {
249  return true;
250  }
251  if (!(o instanceof DiscoveryLink)) {
252  return false;
253  }
254  DiscoveryLink that = (DiscoveryLink) o;
255  return Objects.equals(getSource(), that.getSource())
256  && Objects.equals(getDestination(), that.getDestination());
257  }
258 
259  @Override
260  public int hashCode() {
261  return Objects.hash(getSource(), getDestination());
262  }
263 
264 }