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