blob: 93d024b5fb09b07d9d533bd83c58212af2245806 [file] [log] [blame]
Shashikanth VH3fe5f232016-02-09 11:36:15 +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
18import java.util.Objects;
19import org.onosproject.bgpio.util.Constants;
20import org.jboss.netty.buffer.ChannelBuffer;
21import org.onosproject.bgpio.exceptions.BgpParseException;
22import com.google.common.base.MoreObjects;
23
24/**
25 * Provides implementation of BGP flow specification action.
26 */
27public class BgpFsActionTrafficRate implements BgpValueType {
28
29 public static final short TYPE = Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE;
30 private short asn;
31 private float rate;
32
33 /**
34 * Constructor to initialize the value.
35 *
36 * @param asn autonomous system number
37 * @param rate traffic rate
38 */
39 public BgpFsActionTrafficRate(short asn, float rate) {
40 this.asn = asn;
41 this.rate = rate;
42 }
43
44 @Override
45 public short getType() {
46 return this.TYPE;
47 }
48
49 @Override
50 public int hashCode() {
51 return Objects.hash(asn, rate);
52 }
53
54 @Override
55 public boolean equals(Object obj) {
56 if (this == obj) {
57 return true;
58 }
59 if (obj instanceof BgpFsActionTrafficRate) {
60 BgpFsActionTrafficRate other = (BgpFsActionTrafficRate) obj;
61 return Objects.equals(this.asn, other.asn) && Objects.equals(this.rate, other.rate);
62 }
63 return false;
64 }
65
66 @Override
67 public int write(ChannelBuffer cb) {
68
69 int iLenStartIndex = cb.writerIndex();
70 cb.writeShort(TYPE);
71
72 cb.writeShort(asn);
73
74 cb.writeFloat(rate);
75
76 return cb.writerIndex() - iLenStartIndex;
77 }
78
79 /**
80 * Reads the channel buffer and returns object.
81 *
82 * @param cb channelBuffer
Shashikanth VH3fe5f232016-02-09 11:36:15 +053083 * @return object of flow spec action traffic rate
84 * @throws BgpParseException while parsing BgpFsActionTrafficRate
85 */
Shashikanth VH826990b2016-02-11 16:52:46 +053086 public static BgpFsActionTrafficRate read(ChannelBuffer cb) throws BgpParseException {
87 short asn;
88 float rate;
Shashikanth VH826990b2016-02-11 16:52:46 +053089
Shashikanth VH03205d22016-02-19 22:48:08 +053090 asn = cb.readShort();
91 rate = cb.readFloat();
Shashikanth VH826990b2016-02-11 16:52:46 +053092 return new BgpFsActionTrafficRate(asn, rate);
Shashikanth VH3fe5f232016-02-09 11:36:15 +053093 }
94
95 @Override
96 public String toString() {
97 return MoreObjects.toStringHelper(getClass())
98 .add("TYPE", TYPE)
99 .add("asn", asn)
100 .add("rate", rate).toString();
101 }
102
103 @Override
104 public int compareTo(Object o) {
105 // TODO Auto-generated method stub
106 return 0;
107 }
108}