blob: 3ad87a33554181768e3718f19b53d0d862c28a52 [file] [log] [blame]
Shashikanth VH42b4cf32016-02-09 15:16:25 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.bgpio.types;
17
Shashikanth VHc53a4132016-02-11 18:33:34 +053018import java.util.LinkedList;
Shashikanth VH42b4cf32016-02-09 15:16:25 +053019import java.util.Objects;
20import java.util.List;
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.bgpio.exceptions.BgpParseException;
23import org.onosproject.bgpio.util.Constants;
24import com.google.common.base.MoreObjects;
25
26/**
27 * Provides implementation of BGP flow specification component.
28 */
29public class BgpFsDestinationPortNum implements BgpValueType {
30
31 public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_DST_PORT;
32 private List<BgpFsOperatorValue> operatorValue;
33
34 /**
35 * Constructor to initialize the value.
36 *
37 * @param operatorValue list of operator and value
38 */
39 public BgpFsDestinationPortNum(List<BgpFsOperatorValue> operatorValue) {
40 this.operatorValue = operatorValue;
41 }
42
43 @Override
44 public short getType() {
45 return this.FLOW_SPEC_TYPE;
46 }
47
48 @Override
49 public int hashCode() {
50 return Objects.hash(operatorValue);
51 }
52
53 @Override
54 public boolean equals(Object obj) {
55 if (this == obj) {
56 return true;
57 }
58 if (obj instanceof BgpFsDestinationPortNum) {
59 BgpFsDestinationPortNum other = (BgpFsDestinationPortNum) obj;
60 return this.operatorValue.equals(other.operatorValue);
61 }
62 return false;
63 }
64
65 @Override
66 public int write(ChannelBuffer cb) {
67 int iLenStartIndex = cb.writerIndex();
68 cb.writeByte(FLOW_SPEC_TYPE);
69
70 for (BgpFsOperatorValue fsOperVal : operatorValue) {
71 cb.writeByte(fsOperVal.option());
72 cb.writeBytes(fsOperVal.value());
73 }
74
75 return cb.writerIndex() - iLenStartIndex;
76 }
77
78 /**
79 * Reads the channel buffer and returns object.
80 *
81 * @param cb channelBuffer
Shashikanth VH42b4cf32016-02-09 15:16:25 +053082 * @return object of flow spec destination port number
83 * @throws BgpParseException while parsing BgpFsDestinationPortNum
84 */
Shashikanth VHc53a4132016-02-11 18:33:34 +053085 public static BgpFsDestinationPortNum read(ChannelBuffer cb) throws BgpParseException {
86 List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
87 byte option;
88 short port;
89
90 /*
91 0 1 2 3 4 5 6 7
92 +---+---+---+---+---+---+---+----+
93 | e | a | len | 0 |lt |gt |eq |
94 +---+---+---+---+---+---+---+----+
95 */
96
97 do {
98 option = (byte) cb.readByte();
99 int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
100 if ((1 << len) == 1) {
101 port = cb.readByte();
102 operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
103 } else {
104 port = cb.readShort();
105 operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
106 }
107 } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
108
109 return new BgpFsDestinationPortNum(operatorValue);
Shashikanth VH42b4cf32016-02-09 15:16:25 +0530110 }
111
112 @Override
113 public String toString() {
114 return MoreObjects.toStringHelper(getClass())
115 .add("FLOW_SPEC_TYPE", FLOW_SPEC_TYPE)
116 .add("operatorValue", operatorValue).toString();
117 }
118
119 @Override
120 public int compareTo(Object o) {
121 // TODO Auto-generated method stub
122 return 0;
123 }
124}