blob: 0780be06475e90ea13d59661e250f41e61d031bc [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 link local (9).
27 */
28public class OpaqueLsa9 extends OpaqueLsaHeader {
29
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 9 LSA.
54 *
55 * @param lsaHeader LSA header instance
56 */
57 public OpaqueLsa9(OpaqueLsaHeader lsaHeader) {
58 populateHeader(lsaHeader);
59 }
60
61 /**
62 * Reads from channel buffer and populate instance.
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 * Gets the LSA body.
89 *
90 * @return the LSA body
91 */
92 public byte[] getLsaBodyAsByteArray() {
93 return opaqueInfo;
94
95 }
96
97 @Override
98 public OspfLsaType getOspfLsaType() {
99 return OspfLsaType.LINK_LOCAL_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 OpaqueLsa9 that = (OpaqueLsa9) o;
111 return Objects.equal(opaqueInfo, that.opaqueInfo);
112 }
113
114 @Override
115 public int hashCode() {
116 return Objects.hashCode(opaqueInfo);
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(getClass())
122 .omitNullValues()
123 .add("opaqueInfo", opaqueInfo)
124 .toString();
125 }
126}