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