Open Kilda Java Documentation
VerificationData.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.floodlight.model.flow;
17 
21 
22 import com.auth0.jwt.JWTCreator;
23 import com.auth0.jwt.interfaces.DecodedJWT;
24 import org.apache.commons.lang3.builder.EqualsBuilder;
25 import org.apache.commons.lang3.builder.HashCodeBuilder;
26 import org.projectfloodlight.openflow.types.DatapathId;
27 
28 import java.util.UUID;
29 
30 public class VerificationData {
31  private static String JWT_KEY_PREFIX = "flow.verification.";
32 
33  private long sendTime = 0;
34  private long recvTime = 0;
35  private long senderLatency = 0;
36 
37  private final DatapathId source;
38  private final DatapathId dest;
39  private final UUID packetId;
40 
44  public static VerificationData of(DecodedJWT token) throws CorruptedNetworkDataException {
45  long recvTime = System.currentTimeMillis();
46 
48  try {
49  DatapathId source = DatapathId.of(token.getClaim(makeJwtKey("source")).asLong());
50  DatapathId dest = DatapathId.of(token.getClaim(makeJwtKey("dest")).asLong());
51  UUID packetId = UUID.fromString(token.getClaim(makeJwtKey("id")).asString());
52 
53  data = new VerificationData(source, dest, packetId);
54  data.setSenderLatency(token.getClaim(makeJwtKey("senderLatency")).asLong());
55  data.setSendTime(token.getClaim(makeJwtKey("time")).asLong());
56  data.setRecvTime(recvTime);
57  } catch (NullPointerException e) {
59  String.format("Corrupted flow verification package (%s)", token));
60  }
61 
62  return data;
63  }
64 
68  public static VerificationData of(UniFlowVerificationRequest verificationRequest) {
69  DatapathId source = DatapathId.of(verificationRequest.getSourceSwitchId().toLong());
70  DatapathId dest = DatapathId.of(verificationRequest.getDestSwitchId().toLong());
71  return new VerificationData(source, dest, verificationRequest.getPacketId());
72  }
73 
74  public VerificationData(DatapathId source, DatapathId dest, UUID packetId) {
75  this.source = source;
76  this.dest = dest;
77  this.packetId = packetId;
78  }
79 
83  public JWTCreator.Builder toJwt(JWTCreator.Builder token) {
84  token.withClaim(makeJwtKey("source"), source.getLong());
85  token.withClaim(makeJwtKey("dest"), dest.getLong());
86  token.withClaim(makeJwtKey("id"), packetId.toString());
87 
88  token.withClaim(makeJwtKey("senderLatency"), getSenderLatency());
89  sendTime = System.currentTimeMillis();
90  token.withClaim(makeJwtKey("time"), sendTime);
91 
92  return token;
93  }
94 
98  public VerificationMeasures produceMeasurements(long recipientLatency) {
99  long latency = getRecvTime() - getSendTime() - getSenderLatency() - recipientLatency;
100  if (latency < 0) {
101  latency = -1;
102  }
103  return new VerificationMeasures(latency, getSenderLatency(), recipientLatency);
104  }
105 
106  public long getSendTime() {
107  return sendTime;
108  }
109 
110  private void setSendTime(long sendTime) {
111  this.sendTime = sendTime;
112  }
113 
114  public long getRecvTime() {
115  return recvTime;
116  }
117 
118  private void setRecvTime(long recvTime) {
119  this.recvTime = recvTime;
120  }
121 
122  public long getSenderLatency() {
123  return senderLatency;
124  }
125 
126  public void setSenderLatency(long senderLatency) {
127  this.senderLatency = senderLatency;
128  }
129 
130  public DatapathId getSource() {
131  return source;
132  }
133 
134  public DatapathId getDest() {
135  return dest;
136  }
137 
138  public UUID getPacketId() {
139  return packetId;
140  }
141 
142  @Override
143  public boolean equals(Object o) {
144  if (this == o) {
145  return true;
146  }
147  if (o == null || getClass() != o.getClass()) {
148  return false;
149  }
150 
152 
153  return new EqualsBuilder()
154  .append(source, that.source)
155  .append(dest, that.dest)
156  .append(packetId, that.packetId)
157  .isEquals();
158  }
159 
160  @Override
161  public int hashCode() {
162  return new HashCodeBuilder(17, 37)
163  .append(source)
164  .append(dest)
165  .append(packetId)
166  .toHashCode();
167  }
168 
169  private static String makeJwtKey(String name) {
170  return JWT_KEY_PREFIX + name;
171  }
172 }
static VerificationData of(DecodedJWT token)
name
Definition: setup.py:24
static VerificationData of(UniFlowVerificationRequest verificationRequest)
VerificationData(DatapathId source, DatapathId dest, UUID packetId)
VerificationMeasures produceMeasurements(long recipientLatency)
JWTCreator.Builder toJwt(JWTCreator.Builder token)