Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +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.ospf.protocol.lsa.types; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 19 | import com.google.common.base.Objects; |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 20 | import com.google.common.primitives.Bytes; |
kdarapu | e4da4a0 | 2017-01-04 15:00:14 +0530 | [diff] [blame^] | 21 | |
| 22 | import java.util.Arrays; |
| 23 | |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 24 | import org.jboss.netty.buffer.ChannelBuffer; |
| 25 | import org.onosproject.ospf.controller.OspfLsaType; |
| 26 | import org.onosproject.ospf.protocol.lsa.OpaqueLsaHeader; |
| 27 | |
| 28 | /** |
| 29 | * Representation of an Opaque LSA of type link local (9). |
| 30 | */ |
| 31 | public class OpaqueLsa9 extends OpaqueLsaHeader { |
| 32 | |
| 33 | /* |
| 34 | 0 1 2 3 |
| 35 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 36 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 37 | | LS age | Options | 9, 10 or 11 | |
| 38 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 39 | | Opaque Type | Opaque ID | |
| 40 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 41 | | Advertising Router | |
| 42 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 43 | | LS Sequence Number | |
| 44 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 45 | | LS checksum | Length | |
| 46 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 47 | | | |
| 48 | + + |
| 49 | | Opaque Information | |
| 50 | + + |
| 51 | | ... | |
| 52 | */ |
| 53 | private byte[] opaqueInfo = null; |
| 54 | |
| 55 | /** |
| 56 | * Creates an instance of Opaque type 9 LSA. |
| 57 | * |
| 58 | * @param lsaHeader LSA header instance |
| 59 | */ |
| 60 | public OpaqueLsa9(OpaqueLsaHeader lsaHeader) { |
| 61 | populateHeader(lsaHeader); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Reads from channel buffer and populate instance. |
| 66 | * |
| 67 | * @param channelBuffer channelBuffer instance |
| 68 | */ |
| 69 | public void readFrom(ChannelBuffer channelBuffer) { |
| 70 | int length = channelBuffer.readableBytes(); |
| 71 | opaqueInfo = new byte[length]; |
| 72 | channelBuffer.readBytes(opaqueInfo, 0, length); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns instance as bytes. |
| 77 | * |
| 78 | * @return instance as bytes |
| 79 | */ |
| 80 | public byte[] asBytes() { |
| 81 | byte[] lsaMessage = null; |
| 82 | |
| 83 | byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray(); |
| 84 | byte[] lsaBody = getLsaBodyAsByteArray(); |
| 85 | lsaMessage = Bytes.concat(lsaHeader, lsaBody); |
| 86 | |
| 87 | return lsaMessage; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets the LSA body. |
| 92 | * |
| 93 | * @return the LSA body |
| 94 | */ |
| 95 | public byte[] getLsaBodyAsByteArray() { |
| 96 | return opaqueInfo; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public OspfLsaType getOspfLsaType() { |
| 102 | return OspfLsaType.LINK_LOCAL_OPAQUE_LSA; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean equals(Object o) { |
| 107 | if (this == o) { |
| 108 | return true; |
| 109 | } |
| 110 | if (o == null || getClass() != o.getClass()) { |
| 111 | return false; |
| 112 | } |
| 113 | OpaqueLsa9 that = (OpaqueLsa9) o; |
kdarapu | e4da4a0 | 2017-01-04 15:00:14 +0530 | [diff] [blame^] | 114 | return Arrays.equals(opaqueInfo, that.opaqueInfo); |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public int hashCode() { |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 119 | return Objects.hashCode(opaqueInfo); |
Mohamed Rahil | a3b9e99 | 2016-02-16 20:26:49 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String toString() { |
| 124 | return MoreObjects.toStringHelper(getClass()) |
| 125 | .omitNullValues() |
| 126 | .add("opaqueInfo", opaqueInfo) |
| 127 | .toString(); |
| 128 | } |
| 129 | } |