blob: da37bb190b8de2266378582c49c505020d87aafd [file] [log] [blame]
Chidambar babu86b3b1a2016-02-16 17:39:52 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Chidambar babu86b3b1a2016-02-16 17:39:52 +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;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Objects;
20import com.google.common.primitives.Bytes;
21import org.onosproject.ospf.protocol.util.OspfUtil;
22
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * Defines the Opaque LSA header, fields and the methods to access them.
28 */
29public class OpaqueLsaHeader extends LsaHeader {
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 Opaque LSA header format
45 REFERENCE : RFC 5250
46 */
47 private int opaqueId;
48 private int opaqueType;
49
50 /**
51 * Populates the header from the lsaHeader instance.
52 *
53 * @param lsaHeader lsa header instance.
54 */
55 public void populateHeader(OpaqueLsaHeader lsaHeader) {
56 //assign all the header values
57 this.setAge(lsaHeader.age());
58 this.setOptions(lsaHeader.options());
59 this.setLsType(lsaHeader.lsType());
60 this.setLinkStateId(lsaHeader.linkStateId());
61 this.setAdvertisingRouter(lsaHeader.advertisingRouter());
62 this.setLsSequenceNo(lsaHeader.lsSequenceNo());
63 this.setLsCheckSum(lsaHeader.lsCheckSum());
64 this.setLsPacketLen(lsaHeader.lsPacketLen());
65 this.setOpaqueId(lsaHeader.opaqueId());
66 this.setOpaqueType(lsaHeader.opaqueType());
67 }
68
69 /**
70 * Gets the opaque id.
71 *
72 * @return opaque id
73 */
74 public int opaqueId() {
75 return opaqueId;
76 }
77
78 /**
79 * Sets the opaque id.
80 *
81 * @param opaqueId opaque id
82 */
83 public void setOpaqueId(int opaqueId) {
84 this.opaqueId = opaqueId;
85 }
86
87 /**
88 * Gets opaque type.
89 *
90 * @return opaque type
91 */
92 public int opaqueType() {
93 return opaqueType;
94 }
95
96 /**
97 * Sets opaque type.
98 *
99 * @param opaqueType opaque type
100 */
101 public void setOpaqueType(int opaqueType) {
102 this.opaqueType = opaqueType;
103 }
104
105 /**
106 * Gets header as byte array.
107 *
108 * @return header as byte array
109 */
110 public byte[] getOpaqueLsaHeaderAsByteArray() {
111 List<Byte> headerLst = new ArrayList<>();
112 try {
113 headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.age())));
114 headerLst.add((byte) this.options());
115 headerLst.add((byte) this.lsType());
116 headerLst.add((byte) this.opaqueType());
117 headerLst.addAll(Bytes.asList(OspfUtil.convertToThreeBytes(this.opaqueId())));
118 headerLst.addAll(Bytes.asList(this.advertisingRouter().toOctets()));
119 headerLst.addAll(Bytes.asList(OspfUtil.convertToFourBytes(this.lsSequenceNo())));
120 headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsCheckSum())));
121 headerLst.addAll(Bytes.asList(OspfUtil.convertToTwoBytes(this.lsPacketLen())));
122 } catch (Exception e) {
123 log.debug("Error::getLsaHeaderAsByteArray {}", e.getMessage());
124 return Bytes.toArray(headerLst);
125 }
126 return Bytes.toArray(headerLst);
127 }
128
129 @Override
130 public boolean equals(Object o) {
131 if (this == o) {
132 return true;
133 }
134 if (o == null || getClass() != o.getClass()) {
135 return false;
136 }
137 OpaqueLsaHeader that = (OpaqueLsaHeader) o;
138 return Objects.equal(opaqueId, that.opaqueId) &&
139 Objects.equal(opaqueType, that.opaqueType) &&
140 Objects.equal(age(), that.age()) &&
141 Objects.equal(options(), that.options()) &&
142 Objects.equal(lsType(), that.lsType()) &&
143 Objects.equal(lsSequenceNo(), that.lsSequenceNo()) &&
144 Objects.equal(lsCheckSum(), that.lsCheckSum()) &&
145 Objects.equal(lsPacketLen(), that.lsPacketLen()) &&
146 Objects.equal(linkStateId(), that.linkStateId()) &&
147 Objects.equal(advertisingRouter(), that.advertisingRouter());
148 }
149
150 @Override
151 public int hashCode() {
152 return Objects.hashCode(opaqueId, opaqueType);
153 }
154
155 @Override
156 public String toString() {
157 return MoreObjects.toStringHelper(getClass())
158 .omitNullValues()
159 .add("opaqueId", this.opaqueId)
160 .add("opaqueType", opaqueType)
161 .toString();
162 }
163}