blob: 6da7e54b34100eec8646ded6b56f1a1c98941995 [file] [log] [blame]
Shashikanth VH60e73982016-02-10 11:25:34 +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 VH60e73982016-02-10 11:25:34 +053019import java.util.List;
20import java.util.Objects;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.onosproject.bgpio.exceptions.BgpParseException;
24import org.onosproject.bgpio.util.Constants;
25
26import com.google.common.base.MoreObjects;
27
28/**
29 * Provides implementation of BGP flow specification component.
30 */
31public class BgpFsIpProtocol implements BgpValueType {
32 public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_IP_PROTO;
33 private List<BgpFsOperatorValue> operatorValue;
34
35 /**
36 * Constructor to initialize the value.
37 *
38 * @param operatorValue list of operator and value
39 */
40 public BgpFsIpProtocol(List<BgpFsOperatorValue> operatorValue) {
41 this.operatorValue = operatorValue;
42 }
43
44 /**
45 * Returns flow type operator and value.
46 *
47 * @return flow type value
48 */
49 public List<BgpFsOperatorValue> operatorValue() {
50 return operatorValue;
51 }
52
53
54 @Override
55 public short getType() {
56 return this.FLOW_SPEC_TYPE;
57 }
58
59 @Override
60 public int hashCode() {
61 return Objects.hash(operatorValue);
62 }
63
64 @Override
65 public boolean equals(Object obj) {
66 if (this == obj) {
67 return true;
68 }
69 if (obj instanceof BgpFsIpProtocol) {
70 BgpFsIpProtocol other = (BgpFsIpProtocol) obj;
71 return Objects.equals(this.operatorValue, other.operatorValue);
72 }
73 return false;
74 }
75
76 @Override
77 public int write(ChannelBuffer cb) {
78 int iLenStartIndex = cb.writerIndex();
79 cb.writeByte(FLOW_SPEC_TYPE);
80
81 for (BgpFsOperatorValue fsOperVal : operatorValue) {
82 cb.writeByte(fsOperVal.option());
83 cb.writeBytes(fsOperVal.value());
84 }
85
86 return cb.writerIndex() - iLenStartIndex;
87 }
88
89 /**
90 * Reads the channel buffer and returns object.
91 *
92 * @param cb channelBuffer
Shashikanth VH60e73982016-02-10 11:25:34 +053093 * @return object of flow spec IP protocol
94 * @throws BgpParseException while parsing BgpFsIpProtocol
95 */
Shashikanth VHc53a4132016-02-11 18:33:34 +053096 public static BgpFsIpProtocol read(ChannelBuffer cb) throws BgpParseException {
97 List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
98 byte option;
99 byte proto;
100
101 do {
102 option = (byte) cb.readByte();
103 proto = cb.readByte();
104 operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) proto}));
105 } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
106
107 return new BgpFsIpProtocol(operatorValue);
Shashikanth VH60e73982016-02-10 11:25:34 +0530108 }
109
110 @Override
111 public String toString() {
112 return MoreObjects.toStringHelper(getClass())
113 .add("FLOW_SPEC_TYPE", FLOW_SPEC_TYPE)
114 .add("operatorValue", operatorValue).toString();
115 }
116
117 @Override
118 public int compareTo(Object o) {
119 // TODO Auto-generated method stub
120 return 0;
121 }
122}