Shashikanth VH | 3fe5f23 | 2016-02-09 11:36:15 +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 | 3fe5f23 | 2016-02-09 11:36:15 +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 | |
| 18 | import java.util.Arrays; |
| 19 | |
| 20 | import org.onosproject.bgpio.util.Constants; |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 23 | |
| 24 | import com.google.common.base.MoreObjects; |
| 25 | |
| 26 | /** |
| 27 | * Provides implementation of BGP flow specification action. |
| 28 | */ |
| 29 | public class BgpFsActionReDirect implements BgpValueType { |
| 30 | |
| 31 | public static final short TYPE = Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT; |
| 32 | private byte[] routeTarget; |
Shashikanth VH | 03205d2 | 2016-02-19 22:48:08 +0530 | [diff] [blame] | 33 | public static final byte ROUTE_TARGET_LEN = 6; |
Shashikanth VH | 3fe5f23 | 2016-02-09 11:36:15 +0530 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Constructor to initialize the value. |
| 37 | * |
| 38 | * @param routeTarget route target |
| 39 | */ |
| 40 | public BgpFsActionReDirect(byte[] routeTarget) { |
| 41 | this.routeTarget = Arrays.copyOf(routeTarget, routeTarget.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(routeTarget); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean equals(Object obj) { |
| 56 | if (this == obj) { |
| 57 | return true; |
| 58 | } |
| 59 | if (obj instanceof BgpFsActionReDirect) { |
| 60 | BgpFsActionReDirect other = (BgpFsActionReDirect) obj; |
| 61 | return Arrays.equals(this.routeTarget, other.routeTarget); |
| 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(routeTarget); |
| 73 | |
| 74 | return cb.writerIndex() - iLenStartIndex; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Reads the channel buffer and returns object. |
| 79 | * |
| 80 | * @param cb channelBuffer |
Shashikanth VH | 3fe5f23 | 2016-02-09 11:36:15 +0530 | [diff] [blame] | 81 | * @return object of flow spec action redirect |
| 82 | * @throws BgpParseException while parsing BgpFsActionReDirect |
| 83 | */ |
Shashikanth VH | 826990b | 2016-02-11 16:52:46 +0530 | [diff] [blame] | 84 | public static BgpFsActionReDirect read(ChannelBuffer cb) throws BgpParseException { |
| 85 | byte[] routeTarget; |
Shashikanth VH | 826990b | 2016-02-11 16:52:46 +0530 | [diff] [blame] | 86 | |
Shashikanth VH | 03205d2 | 2016-02-19 22:48:08 +0530 | [diff] [blame] | 87 | routeTarget = cb.readBytes(ROUTE_TARGET_LEN).array(); |
Shashikanth VH | 826990b | 2016-02-11 16:52:46 +0530 | [diff] [blame] | 88 | return new BgpFsActionReDirect(routeTarget); |
Shashikanth VH | 3fe5f23 | 2016-02-09 11:36:15 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String toString() { |
| 93 | return MoreObjects.toStringHelper(getClass()) |
| 94 | .add("TYPE", TYPE) |
| 95 | .add("routeTarget", routeTarget).toString(); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public int compareTo(Object o) { |
| 100 | // TODO Auto-generated method stub |
| 101 | return 0; |
| 102 | } |
| 103 | } |