Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
| 16 | package org.onosproject.isis.io.isispacket.pdu; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.base.Objects; |
| 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onosproject.isis.controller.IsisInterfaceState; |
| 23 | import org.onosproject.isis.io.isispacket.IsisHeader; |
| 24 | import org.onosproject.isis.io.isispacket.tlv.AdjacencyStateTlv; |
| 25 | import org.onosproject.isis.io.isispacket.tlv.AreaAddressTlv; |
| 26 | import org.onosproject.isis.io.isispacket.tlv.IpInterfaceAddressTlv; |
| 27 | import org.onosproject.isis.io.isispacket.tlv.IsisNeighborTlv; |
| 28 | import org.onosproject.isis.io.isispacket.tlv.IsisTlv; |
| 29 | |
| 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Representation of ISIS hello PDU. |
| 35 | */ |
| 36 | public abstract class HelloPdu extends IsisHeader { |
| 37 | |
| 38 | protected List<IsisTlv> variableLengths = new ArrayList<>(); |
| 39 | private byte circuitType; |
| 40 | private String sourceId; |
| 41 | private int holdingTime; |
| 42 | private int pduLength; |
| 43 | |
| 44 | public void addTlv(IsisTlv isisTlv) { |
| 45 | variableLengths.add(isisTlv); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Returns the variable lengths. |
| 50 | * |
| 51 | * @return variable lengths |
| 52 | */ |
| 53 | public List<IsisTlv> tlvs() { |
| 54 | return variableLengths; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the list of area addresses. |
| 59 | * |
| 60 | * @return areaAddresses area addresses |
| 61 | */ |
| 62 | public List<String> areaAddress() { |
| 63 | List<String> areaAddresses = null; |
| 64 | for (IsisTlv tlv : tlvs()) { |
| 65 | if (tlv instanceof AreaAddressTlv) { |
| 66 | areaAddresses = ((AreaAddressTlv) tlv).areaAddress(); |
| 67 | } |
| 68 | } |
| 69 | return areaAddresses; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the list of interface IP addresses. |
| 74 | * |
| 75 | * @return interfaceIpAddresses list of interface IP addresses |
| 76 | */ |
| 77 | public List<Ip4Address> interfaceIpAddresses() { |
| 78 | List<Ip4Address> interfaceIpAddresses = null; |
| 79 | for (IsisTlv tlv : tlvs()) { |
| 80 | if (tlv instanceof IpInterfaceAddressTlv) { |
| 81 | interfaceIpAddresses = ((IpInterfaceAddressTlv) tlv).interfaceAddress(); |
| 82 | } |
| 83 | } |
| 84 | return interfaceIpAddresses; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the list of neighbor list. |
| 89 | * |
| 90 | * @return macAddresses list of neighbor MAC address |
| 91 | */ |
| 92 | public List<MacAddress> neighborList() { |
| 93 | List<MacAddress> macAddresses = null; |
| 94 | for (IsisTlv tlv : tlvs()) { |
| 95 | if (tlv instanceof IsisNeighborTlv) { |
| 96 | macAddresses = ((IsisNeighborTlv) tlv).neighbor(); |
| 97 | } |
| 98 | } |
| 99 | return macAddresses; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the adjacency state. |
| 104 | * |
| 105 | * @return interfaceState adjacency state |
| 106 | */ |
| 107 | public IsisInterfaceState adjacencyState() { |
| 108 | IsisInterfaceState interfaceState = null; |
| 109 | for (IsisTlv tlv : tlvs()) { |
| 110 | if (tlv instanceof AdjacencyStateTlv) { |
| 111 | interfaceState = IsisInterfaceState.get(((AdjacencyStateTlv) tlv).adjacencyType()); |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | return interfaceState; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the source ID. |
| 120 | * |
| 121 | * @return sourceId source ID |
| 122 | */ |
| 123 | public String sourceId() { |
| 124 | return sourceId; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Sets source ID. |
| 129 | * |
| 130 | * @param sourceId source ID |
| 131 | */ |
| 132 | public void setSourceId(String sourceId) { |
| 133 | this.sourceId = sourceId; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns the PDU length. |
| 138 | * |
| 139 | * @return pduLength PDU length |
| 140 | */ |
| 141 | public int pduLength() { |
| 142 | return pduLength; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Sets the PDU length. |
| 147 | * |
| 148 | * @param pduLength PDU lenght |
| 149 | */ |
| 150 | public void setPduLength(int pduLength) { |
| 151 | this.pduLength = pduLength; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns the holding time. |
| 156 | * |
| 157 | * @return holdingTime holding time |
| 158 | */ |
| 159 | public int holdingTime() { |
| 160 | return holdingTime; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets the holding time. |
| 165 | * |
| 166 | * @param holdingTime holding time |
| 167 | */ |
| 168 | public void setHoldingTime(int holdingTime) { |
| 169 | this.holdingTime = holdingTime; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Returns the circuit type. |
| 174 | * |
| 175 | * @return circuitType circuit type |
| 176 | */ |
| 177 | public byte circuitType() { |
| 178 | return circuitType; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Sets the circuit type. |
| 183 | * |
| 184 | * @param circuitType circuit type |
| 185 | */ |
| 186 | public void setCircuitType(byte circuitType) { |
| 187 | this.circuitType = circuitType; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public String toString() { |
| 192 | return MoreObjects.toStringHelper(getClass()) |
| 193 | .omitNullValues() |
| 194 | .add("circuitType", circuitType) |
| 195 | .add("sourceId", sourceId) |
| 196 | .add("holdingTime", holdingTime) |
| 197 | .add("pduLength", pduLength) |
| 198 | .toString(); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public boolean equals(Object o) { |
| 203 | if (this == o) { |
| 204 | return true; |
| 205 | } |
| 206 | if (o == null || getClass() != o.getClass()) { |
| 207 | return false; |
| 208 | } |
| 209 | HelloPdu that = (HelloPdu) o; |
| 210 | return Objects.equal(circuitType, that.circuitType) && |
| 211 | Objects.equal(sourceId, that.sourceId) && |
| 212 | Objects.equal(holdingTime, that.holdingTime) && |
| 213 | Objects.equal(pduLength, that.pduLength); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public int hashCode() { |
| 218 | return Objects.hashCode(circuitType, sourceId, holdingTime, pduLength); |
| 219 | } |
| 220 | } |