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