Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 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 | |
Shashikanth VH | c53a413 | 2016-02-11 18:33:34 +0530 | [diff] [blame] | 18 | import java.util.LinkedList; |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 19 | import java.util.Objects; |
| 20 | import java.util.List; |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 23 | import org.onosproject.bgpio.util.Constants; |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | |
| 26 | /** |
| 27 | * Provides implementation of BGP flow specification component. |
| 28 | */ |
| 29 | public class BgpFsDscpValue implements BgpValueType { |
| 30 | |
| 31 | public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_DSCP; |
| 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 BgpFsDscpValue(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 BgpFsDscpValue) { |
| 59 | BgpFsDscpValue other = (BgpFsDscpValue) 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 | |
| 69 | cb.writeByte(FLOW_SPEC_TYPE); |
| 70 | |
| 71 | for (BgpFsOperatorValue fsOperVal : operatorValue) { |
| 72 | cb.writeByte(fsOperVal.option()); |
| 73 | cb.writeBytes(fsOperVal.value()); |
| 74 | } |
| 75 | |
| 76 | return cb.writerIndex() - iLenStartIndex; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Reads the channel buffer and returns object. |
| 81 | * |
| 82 | * @param cb channelBuffer |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 83 | * @return object of flow spec DSCP value |
| 84 | * @throws BgpParseException while parsing BgpFsDscpValue |
| 85 | */ |
Shashikanth VH | c53a413 | 2016-02-11 18:33:34 +0530 | [diff] [blame] | 86 | public static BgpFsDscpValue read(ChannelBuffer cb) throws BgpParseException { |
| 87 | List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 88 | byte option; |
| 89 | byte dscpValue; |
| 90 | |
| 91 | do { |
| 92 | option = (byte) cb.readByte(); |
| 93 | dscpValue = cb.readByte(); |
| 94 | operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) dscpValue})); |
| 95 | } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); |
| 96 | return new BgpFsDscpValue(operatorValue); |
Shashikanth VH | 42b4cf3 | 2016-02-09 15:16:25 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return MoreObjects.toStringHelper(getClass()) |
| 102 | .add("FLOW_SPEC_TYPE", FLOW_SPEC_TYPE) |
| 103 | .add("operatorValue", operatorValue).toString(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int compareTo(Object o) { |
| 108 | // TODO Auto-generated method stub |
| 109 | return 0; |
| 110 | } |
| 111 | } |