Shashikanth VH | 3fe5f23 | 2016-02-09 11:36:15 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.bgpio.types; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | import org.onosproject.bgpio.util.Constants; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; |
| 21 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 22 | import com.google.common.base.MoreObjects; |
| 23 | |
| 24 | /** |
| 25 | * Provides implementation of BGP flow specification action. |
| 26 | */ |
| 27 | public 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 VH | 3fe5f23 | 2016-02-09 11:36:15 +0530 | [diff] [blame] | 83 | * @return object of flow spec action traffic rate |
| 84 | * @throws BgpParseException while parsing BgpFsActionTrafficRate |
| 85 | */ |
Shashikanth VH | 826990b | 2016-02-11 16:52:46 +0530 | [diff] [blame] | 86 | public static BgpFsActionTrafficRate read(ChannelBuffer cb) throws BgpParseException { |
| 87 | short asn; |
| 88 | float rate; |
Shashikanth VH | 826990b | 2016-02-11 16:52:46 +0530 | [diff] [blame] | 89 | |
Shashikanth VH | 03205d2 | 2016-02-19 22:48:08 +0530 | [diff] [blame] | 90 | asn = cb.readShort(); |
| 91 | rate = cb.readFloat(); |
Shashikanth VH | 826990b | 2016-02-11 16:52:46 +0530 | [diff] [blame] | 92 | return new BgpFsActionTrafficRate(asn, rate); |
Shashikanth VH | 3fe5f23 | 2016-02-09 11:36:15 +0530 | [diff] [blame] | 93 | } |
| 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 | } |