blob: c97c4d06713cac6f9ba9a3fe2169312d051e589b [file] [log] [blame]
Yafit Hadarf8caac02015-08-25 10:21:44 +03001/*
2 * Copyright 2015, ECI Telecom, Inc.
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 */
16package org.projectfloodlight.openflow.types;
17
18
19import java.util.List;
20
21import org.jboss.netty.buffer.ChannelBuffer;
22
23import com.google.common.collect.ComparisonChain;
24import com.google.common.primitives.UnsignedBytes;
25import com.google.common.hash.PrimitiveSink;
26
27import org.projectfloodlight.openflow.exceptions.OFParseError;
28import org.projectfloodlight.openflow.protocol.*;
29import com.google.common.hash.Funnel;
30import org.projectfloodlight.openflow.util.*;
31import java.util.Arrays;
32
33public class OduSignalID implements OFValueType<OduSignalID> {
34
35 // version: 1.3
36 final static byte WIRE_VERSION = 4;
37 final static int MINIMUM_LENGTH = 4;
38
39 private final static int DEFAULT_TPN = 0x0;
40 private final static int DEFAULT_TSLEN = 80;
41 private final static byte[] DEFAULT_TSMAP = new byte[10];
42
43 // OF message fields
44 private final int tpn;
45 private final int tslen;
46 private final byte[] tsmap;
47
48 // Immutable default instance
49 public final static OduSignalID DEFAULT = new OduSignalID(
50 DEFAULT_TPN, DEFAULT_TSLEN, DEFAULT_TSMAP
51 );
52
53 // package private constructor - used by readers, builders, and factory
54 public OduSignalID(int tpn, int tslen, byte[] tsmap) {
55 this.tpn = tpn;
56 this.tslen = tslen;
57 this.tsmap = tsmap;
58 }
59
60 public int getTpn() {
61 return tpn;
62 }
63
64 public int getTslen() {
65 return tslen;
66 }
67
68 public byte[] getTsmap() {
69 return tsmap;
70 }
71
72
73 @Override
74 public int getLength() {
75 return MINIMUM_LENGTH + 12; // tslen == 80
76 }
77
78 public void writeTo(ChannelBuffer c) {
79 c.writeShort(tpn);
80 c.writeShort(tslen);
81 c.writeBytes(tsmap); // 10 bytes
82 c.writeZero(2); // write bytes for add padding alignment (the size of bytes in tsmap must be divided in 4)
83 }
84
85 public static OduSignalID readFrom(ChannelBuffer c) {
86 int tpn = U16.f(c.readShort());
87 int tslen = U16.f(c.readShort());
88 byte[] tsmap = null;
89 tsmap =ChannelUtils.readBytes(c, 10);
90 ChannelUtils.readBytes(c, 2); // skip padding
91 OduSignalID oduSigId = new OduSignalID(
92 tpn,
93 tslen,
94 tsmap
95 );
96 return oduSigId;
97 }
98
99
100 public void putTo(PrimitiveSink sink) {
101 FUNNEL.funnel(this, sink);
102 }
103
104 final static OduSignalIDFunnel FUNNEL = new OduSignalIDFunnel();
105 static class OduSignalIDFunnel implements Funnel< OduSignalID> {
106 private static final long serialVersionUID = 1L;
107 @Override
108 public void funnel( OduSignalID message, PrimitiveSink sink) {
109 sink.putInt(message.tpn);
110 sink.putInt(message.tslen);
111 sink.putBytes(message.tsmap);
112 }
113 }
114
115 @Override
116 public String toString() {
117 StringBuilder b = new StringBuilder(" OduSignalID(");
118 b.append("tpn=").append(tpn);
119 b.append(", ");
120 b.append("tslen=").append(tslen);
121 b.append(", ");
122 b.append("tsmap=").append(Arrays.toString(tsmap));
123 b.append(")");
124 return b.toString();
125 }
126
127 @Override
128 public boolean equals(Object obj) {
129 if (this == obj)
130 return true;
131 if (obj == null)
132 return false;
133 if (getClass() != obj.getClass())
134 return false;
135 OduSignalID other = (OduSignalID) obj;
136
137 if( tpn != other.tpn)
138 return false;
139 if( tslen != other.tslen)
140 return false;
141 if (!Arrays.equals(tsmap, other.tsmap))
142 return false;
143 return true;
144 }
145
146 @Override
147 public int hashCode() {
148 final int prime = 31;
149 int result = 1;
150
151 result = prime * result + tpn;
152 result = prime * result + tslen;
153 result = prime * result + Arrays.hashCode(tsmap);
154 return result;
155 }
156
157 @Override
158 public OduSignalID applyMask(OduSignalID mask) {
159 byte[] maskTsmap = null;
160 if (this.tsmap!=null && this.tsmap.length > 0) {
161 maskTsmap = new byte[this.tsmap.length];
162 int i = 0;
163 for (byte b : this.tsmap){
164 maskTsmap[i] =(byte) (b & mask.tsmap[i++]);
165 }
166 }
167 return new OduSignalID(this.tpn & mask.tpn,
168 (short) (this.tslen & mask
169 .tslen),
170 maskTsmap);
171 }
172
173 @Override
174 public int compareTo(OduSignalID o) {
175 return ComparisonChain.start()
176 .compare(tpn,o.tpn)
177 .compare(tslen,o.tslen)
178 .compare(tsmap,o.tsmap, UnsignedBytes.lexicographicalComparator())
179 .result();
180 }
181
182}