blob: 9f0d94a6748c6d8e9ddbc720d7dcf9b0f2f71ca1 [file] [log] [blame]
Shashikanth VH3fe5f232016-02-09 11:36:15 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Shashikanth VH3fe5f232016-02-09 11:36:15 +05303 *
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.Arrays;
19
20import org.onosproject.bgpio.util.Constants;
21import org.jboss.netty.buffer.ChannelBuffer;
22import org.onosproject.bgpio.exceptions.BgpParseException;
23
24import com.google.common.base.MoreObjects;
25
26/**
27 * Provides implementation of BGP flow specification action.
28 */
29public class BgpFsActionTrafficAction implements BgpValueType {
30
31 public static final short TYPE = Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION;
32 private byte[] bitMask;
Shashikanth VH03205d22016-02-19 22:48:08 +053033 public static final byte BIT_MASK_LEN = 6;
Shashikanth VH3fe5f232016-02-09 11:36:15 +053034
35 /**
36 * Constructor to initialize the value.
37 *
38 * @param bitMask traffic action bit mask
39 */
40 public BgpFsActionTrafficAction(byte[] bitMask) {
41 this.bitMask = Arrays.copyOf(bitMask, bitMask.length);
42 }
43
44 @Override
45 public short getType() {
46 return this.TYPE;
47 }
48
49 @Override
50 public int hashCode() {
51 return Arrays.hashCode(bitMask);
52 }
53
54 @Override
55 public boolean equals(Object obj) {
56 if (this == obj) {
57 return true;
58 }
59 if (obj instanceof BgpFsActionTrafficAction) {
60 BgpFsActionTrafficAction other = (BgpFsActionTrafficAction) obj;
61 return Arrays.equals(this.bitMask, other.bitMask);
62 }
63 return false;
64 }
65
66 @Override
67 public int write(ChannelBuffer cb) {
68 int iLenStartIndex = cb.writerIndex();
69
70 cb.writeShort(TYPE);
71
72 cb.writeBytes(bitMask);
73
74 return cb.writerIndex() - iLenStartIndex;
75 }
76
77 /**
78 * Reads the channel buffer and returns object.
79 *
80 * @param cb channelBuffer
Shashikanth VH3fe5f232016-02-09 11:36:15 +053081 * @return object of flow spec action traffic rate
82 * @throws BgpParseException while parsing BgpFsActionTrafficAction
83 */
Shashikanth VH826990b2016-02-11 16:52:46 +053084 public static BgpFsActionTrafficAction read(ChannelBuffer cb) throws BgpParseException {
85 byte[] bitMask;
Shashikanth VH826990b2016-02-11 16:52:46 +053086
Shashikanth VH03205d22016-02-19 22:48:08 +053087 bitMask = cb.readBytes(BIT_MASK_LEN).array();
Shashikanth VH826990b2016-02-11 16:52:46 +053088 return new BgpFsActionTrafficAction(bitMask);
Shashikanth VH3fe5f232016-02-09 11:36:15 +053089 }
90
91 @Override
92 public String toString() {
93 return MoreObjects.toStringHelper(getClass())
94 .add("TYPE", TYPE)
95 .add("bitMask", bitMask).toString();
96 }
97
98 @Override
99 public int compareTo(Object o) {
100 // TODO Auto-generated method stub
101 return 0;
102 }
103}