blob: 9aef15e682628be91538d54c041e86825a35a4bb [file] [log] [blame]
Shashikanth VH6fe08ff2016-02-08 16:18:10 +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 VH6fe08ff2016-02-08 16:18:10 +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 BgpFsPortNum implements BgpValueType {
30 public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_PORT;
31 private List<BgpFsOperatorValue> operatorValue;
32
33 /**
34 * Constructor to initialize the value.
35 *
36 * @param operatorValue list of operator and value
37 */
38 public BgpFsPortNum(List<BgpFsOperatorValue> operatorValue) {
39 this.operatorValue = operatorValue;
40 }
41
42 @Override
43 public short getType() {
44 return this.FLOW_SPEC_TYPE;
45 }
46
47 @Override
48 public int hashCode() {
49 return Objects.hash(operatorValue);
50 }
51
52 @Override
53 public boolean equals(Object obj) {
54 if (this == obj) {
55 return true;
56 }
57 if (obj instanceof BgpFsPortNum) {
58 BgpFsPortNum other = (BgpFsPortNum) obj;
59 return this.operatorValue.equals(other.operatorValue);
60 }
61 return false;
62 }
63
64 @Override
65 public int write(ChannelBuffer cb) {
66 int iLenStartIndex = cb.writerIndex();
67
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 VH6fe08ff2016-02-08 16:18:10 +053082 * @return object of flow spec port number
83 * @throws BgpParseException while parsing BgpFsPortNum
84 */
Shashikanth VHc53a4132016-02-11 18:33:34 +053085 public static BgpFsPortNum read(ChannelBuffer cb) throws BgpParseException {
86 List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
87 byte option;
88 short port;
89
90 do {
91 option = (byte) cb.readByte();
92 int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
93 if ((1 << len) == 1) {
94 port = cb.readByte();
95 operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
96 } else {
97 port = cb.readShort();
98 operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
99 }
100
101
102 } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
103
104 return new BgpFsPortNum(operatorValue);
Shashikanth VH6fe08ff2016-02-08 16:18:10 +0530105 }
106
107 @Override
108 public String toString() {
109 return MoreObjects.toStringHelper(getClass())
110 .add("FLOW_SPEC_TYPE", FLOW_SPEC_TYPE)
111 .add("operatorValue", operatorValue).toString();
112 }
113
114 @Override
115 public int compareTo(Object o) {
116 // TODO Auto-generated method stub
117 return 0;
118 }
119}