blob: e311fce1d5abfb24dec61f5174109036c9b07241 [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 link local (9).
29 */
30public class OpaqueLsa9 extends OpaqueLsaHeader {
31
32 /*
33 0 1 2 3
34 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
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 | LS age | Options | 9, 10 or 11 |
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 | Opaque Type | Opaque ID |
39 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 | Advertising Router |
41 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42 | LS Sequence Number |
43 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 | LS checksum | Length |
45 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 | |
47 + +
48 | Opaque Information |
49 + +
50 | ... |
51 */
52 private byte[] opaqueInfo = null;
53
54 /**
55 * Creates an instance of Opaque type 9 LSA.
56 *
57 * @param lsaHeader LSA header instance
58 */
59 public OpaqueLsa9(OpaqueLsaHeader lsaHeader) {
60 populateHeader(lsaHeader);
61 }
62
63 /**
64 * Reads from channel buffer and populate instance.
65 *
66 * @param channelBuffer channelBuffer instance
67 */
68 public void readFrom(ChannelBuffer channelBuffer) {
69 int length = channelBuffer.readableBytes();
70 opaqueInfo = new byte[length];
71 channelBuffer.readBytes(opaqueInfo, 0, length);
72 }
73
74 /**
75 * Returns instance as bytes.
76 *
77 * @return instance as bytes
78 */
79 public byte[] asBytes() {
80 byte[] lsaMessage = null;
81
82 byte[] lsaHeader = getOpaqueLsaHeaderAsByteArray();
83 byte[] lsaBody = getLsaBodyAsByteArray();
84 lsaMessage = Bytes.concat(lsaHeader, lsaBody);
85
86 return lsaMessage;
87 }
88
89 /**
90 * Gets the LSA body.
91 *
92 * @return the LSA body
93 */
94 public byte[] getLsaBodyAsByteArray() {
95 return opaqueInfo;
96
97 }
98
99 @Override
100 public OspfLsaType getOspfLsaType() {
101 return OspfLsaType.LINK_LOCAL_OPAQUE_LSA;
102 }
103
104 @Override
105 public boolean equals(Object o) {
106 if (this == o) {
107 return true;
108 }
109 if (o == null || getClass() != o.getClass()) {
110 return false;
111 }
112 OpaqueLsa9 that = (OpaqueLsa9) o;
kdarapue4da4a02017-01-04 15:00:14 +0530113 return Arrays.equals(opaqueInfo, that.opaqueInfo);
Mohamed Rahila3b9e992016-02-16 20:26:49 +0530114 }
115
116 @Override
117 public int hashCode() {
kdarapuf0da43d2017-01-04 16:31:45 +0530118 return Arrays.hashCode(opaqueInfo);
Mohamed Rahila3b9e992016-02-16 20:26:49 +0530119 }
120
121 @Override
122 public String toString() {
123 return MoreObjects.toStringHelper(getClass())
124 .omitNullValues()
125 .add("opaqueInfo", opaqueInfo)
126 .toString();
127 }
128}