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