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.util; |
| 17 | |
| 18 | import org.onlab.packet.Ip4Address; |
| 19 | import org.onlab.packet.MacAddress; |
| 20 | import org.onosproject.isis.controller.IsisInterface; |
| 21 | import org.onosproject.isis.controller.IsisNeighbor; |
| 22 | import org.onosproject.isis.controller.IsisNetworkType; |
| 23 | import org.onosproject.isis.controller.IsisPduType; |
| 24 | import org.onosproject.isis.io.isispacket.IsisHeader; |
| 25 | import org.onosproject.isis.io.isispacket.pdu.AttachedToOtherAreas; |
| 26 | import org.onosproject.isis.io.isispacket.pdu.LsPdu; |
| 27 | import org.onosproject.isis.io.isispacket.tlv.AreaAddressTlv; |
| 28 | import org.onosproject.isis.io.isispacket.tlv.HostNameTlv; |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame^] | 29 | import org.onosproject.isis.io.isispacket.tlv.IpExtendedReachabilityTlv; |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 30 | import org.onosproject.isis.io.isispacket.tlv.IpInterfaceAddressTlv; |
| 31 | import org.onosproject.isis.io.isispacket.tlv.IpInternalReachabilityTlv; |
| 32 | import org.onosproject.isis.io.isispacket.tlv.IsReachabilityTlv; |
| 33 | import org.onosproject.isis.io.isispacket.tlv.MetricOfInternalReachability; |
| 34 | import org.onosproject.isis.io.isispacket.tlv.MetricsOfReachability; |
| 35 | import org.onosproject.isis.io.isispacket.tlv.ProtocolSupportedTlv; |
| 36 | import org.onosproject.isis.io.isispacket.tlv.TlvHeader; |
| 37 | import org.onosproject.isis.io.isispacket.tlv.TlvType; |
| 38 | |
| 39 | import java.util.List; |
| 40 | |
| 41 | /** |
| 42 | * Representation of link state PDU generator. |
| 43 | */ |
| 44 | public class LspGenerator { |
| 45 | |
| 46 | public LsPdu getLsp(IsisInterface isisInterface, String lspId, IsisPduType isisPduType, |
| 47 | List<Ip4Address> allConfiguredInterfaceIps) { |
| 48 | IsisHeader header = getHeader(isisPduType); |
| 49 | LsPdu lsp = new LsPdu(header); |
| 50 | |
| 51 | lsp.setPduLength(0); |
| 52 | lsp.setRemainingLifeTime(IsisConstants.LSPMAXAGE); |
| 53 | lsp.setLspId(lspId); |
| 54 | lsp.setSequenceNumber(isisInterface.isisLsdb().lsSequenceNumber(isisPduType)); |
| 55 | lsp.setCheckSum(0); |
| 56 | if (isisPduType == IsisPduType.L1LSPDU) { |
| 57 | lsp.setTypeBlock((byte) 1); |
| 58 | lsp.setIntermediateSystemType((byte) 1); |
| 59 | } else if (isisPduType == IsisPduType.L2LSPDU) { |
| 60 | lsp.setTypeBlock((byte) 3); |
| 61 | lsp.setIntermediateSystemType((byte) 3); |
| 62 | } |
| 63 | lsp.setAttachedToOtherAreas(AttachedToOtherAreas.NONE); |
| 64 | lsp.setPartitionRepair(false); |
| 65 | lsp.setLspDbol(false); |
| 66 | |
| 67 | TlvHeader tlvHeader = new TlvHeader(); |
| 68 | tlvHeader.setTlvType(TlvType.AREAADDRESS.value()); |
| 69 | tlvHeader.setTlvLength(0); |
| 70 | AreaAddressTlv areaAddressTlv = new AreaAddressTlv(tlvHeader); |
| 71 | areaAddressTlv.addAddress(isisInterface.areaAddress()); |
| 72 | lsp.addTlv(areaAddressTlv); |
| 73 | |
| 74 | tlvHeader.setTlvType(TlvType.PROTOCOLSUPPORTED.value()); |
| 75 | tlvHeader.setTlvLength(0); |
| 76 | ProtocolSupportedTlv protocolSupportedTlv = new ProtocolSupportedTlv(tlvHeader); |
| 77 | protocolSupportedTlv.addProtocolSupported((byte) IsisConstants.PROTOCOLSUPPORTED); |
| 78 | lsp.addTlv(protocolSupportedTlv); |
| 79 | |
| 80 | tlvHeader.setTlvType(TlvType.IPINTERFACEADDRESS.value()); |
| 81 | tlvHeader.setTlvLength(0); |
| 82 | IpInterfaceAddressTlv ipInterfaceAddressTlv = new IpInterfaceAddressTlv(tlvHeader); |
| 83 | for (Ip4Address ipaddress : allConfiguredInterfaceIps) { |
| 84 | ipInterfaceAddressTlv.addInterfaceAddres(ipaddress); |
| 85 | } |
| 86 | lsp.addTlv(ipInterfaceAddressTlv); |
| 87 | |
| 88 | tlvHeader.setTlvType(TlvType.HOSTNAME.value()); |
| 89 | tlvHeader.setTlvLength(0); |
| 90 | HostNameTlv hostNameTlv = new HostNameTlv(tlvHeader); |
| 91 | hostNameTlv.setHostName(isisInterface.intermediateSystemName()); |
| 92 | lsp.addTlv(hostNameTlv); |
| 93 | |
| 94 | tlvHeader.setTlvType(TlvType.ISREACHABILITY.value()); |
| 95 | tlvHeader.setTlvLength(0); |
| 96 | IsReachabilityTlv isReachabilityTlv = new IsReachabilityTlv(tlvHeader); |
| 97 | isReachabilityTlv.setReserved(0); |
| 98 | MetricsOfReachability metricsOfReachability = new MetricsOfReachability(); |
| 99 | metricsOfReachability.setDefaultMetric((byte) 10); |
| 100 | metricsOfReachability.setDefaultIsInternal(true); |
| 101 | metricsOfReachability.setDelayMetric((byte) 10); |
| 102 | metricsOfReachability.setDelayIsInternal(true); |
| 103 | metricsOfReachability.setDelayMetricSupported(true); |
| 104 | metricsOfReachability.setExpenseMetric((byte) 10); |
| 105 | metricsOfReachability.setExpenseIsInternal(true); |
| 106 | metricsOfReachability.setExpenseMetricSupported(true); |
| 107 | metricsOfReachability.setErrorMetric((byte) 10); |
| 108 | metricsOfReachability.setErrorIsInternal(true); |
| 109 | metricsOfReachability.setErrorMetricSupported(true); |
| 110 | if (isisInterface.networkType() == IsisNetworkType.BROADCAST) { |
| 111 | if (isisPduType == IsisPduType.L1LSPDU) { |
| 112 | metricsOfReachability.setNeighborId(isisInterface.l1LanId()); |
| 113 | } else if (isisPduType == IsisPduType.L2LSPDU) { |
| 114 | metricsOfReachability.setNeighborId(isisInterface.l2LanId()); |
| 115 | } |
| 116 | } else if (isisInterface.networkType() == IsisNetworkType.P2P) { |
| 117 | MacAddress neighborMac = isisInterface.neighbors().iterator().next(); |
| 118 | IsisNeighbor neighbor = isisInterface.lookup(neighborMac); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame^] | 119 | metricsOfReachability.setNeighborId(neighbor.neighborSystemId() + ".00"); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | isReachabilityTlv.addMeticsOfReachability(metricsOfReachability); |
| 123 | lsp.addTlv(isReachabilityTlv); |
| 124 | |
| 125 | tlvHeader.setTlvType(TlvType.IPINTERNALREACHABILITY.value()); |
| 126 | tlvHeader.setTlvLength(0); |
| 127 | IpInternalReachabilityTlv ipInterReacTlv = new IpInternalReachabilityTlv(tlvHeader); |
| 128 | MetricOfInternalReachability metricOfIntRea = new MetricOfInternalReachability(); |
| 129 | metricOfIntRea.setDefaultMetric((byte) 10); |
| 130 | metricOfIntRea.setDefaultIsInternal(true); |
| 131 | metricOfIntRea.setDefaultDistributionDown(true); |
| 132 | metricOfIntRea.setDelayMetric((byte) 0); |
| 133 | metricOfIntRea.setDelayMetricSupported(false); |
| 134 | metricOfIntRea.setDelayIsInternal(true); |
| 135 | metricOfIntRea.setExpenseMetric((byte) 0); |
| 136 | metricOfIntRea.setExpenseMetricSupported(false); |
| 137 | metricOfIntRea.setExpenseIsInternal(true); |
| 138 | metricOfIntRea.setErrorMetric((byte) 0); |
| 139 | metricOfIntRea.setErrorMetricSupported(false); |
| 140 | metricOfIntRea.setExpenseIsInternal(true); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame^] | 141 | Ip4Address ip4Address = isisInterface.interfaceIpAddress(); |
| 142 | byte[] ipAddress = ip4Address.toOctets(); |
| 143 | ipAddress[ipAddress.length - 1] = 0; |
| 144 | metricOfIntRea.setIpAddress(Ip4Address.valueOf(ipAddress)); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 145 | metricOfIntRea.setSubnetAddres(Ip4Address.valueOf(isisInterface.networkMask())); |
| 146 | ipInterReacTlv.addInternalReachabilityMetric(metricOfIntRea); |
| 147 | lsp.addTlv(ipInterReacTlv); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame^] | 148 | |
| 149 | tlvHeader.setTlvType(TlvType.IPEXTENDEDREACHABILITY.value()); |
| 150 | tlvHeader.setTlvLength(0); |
| 151 | IpExtendedReachabilityTlv extendedTlv = new IpExtendedReachabilityTlv(tlvHeader); |
| 152 | extendedTlv.setDown(false); |
| 153 | extendedTlv.setMetric(10); |
| 154 | extendedTlv.setPrefix("192.168.7"); |
| 155 | extendedTlv.setPrefixLength(24); |
| 156 | extendedTlv.setSubTlvLength((byte) 0); |
| 157 | extendedTlv.setSubTlvPresence(false); |
| 158 | lsp.addTlv(extendedTlv); |
| 159 | |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 160 | return lsp; |
| 161 | } |
| 162 | |
| 163 | public IsisHeader getHeader(IsisPduType pduType) { |
| 164 | IsisHeader isisHeader = new IsisHeader(); |
| 165 | isisHeader.setIrpDiscriminator((byte) IsisConstants.IRPDISCRIMINATOR); |
| 166 | isisHeader.setPduHeaderLength((byte) IsisUtil.getPduHeaderLength(pduType.value())); |
| 167 | isisHeader.setVersion((byte) IsisConstants.ISISVERSION); |
tejeshwer degala | 3fe1ed5 | 2016-04-22 17:04:01 +0530 | [diff] [blame^] | 168 | isisHeader.setIdLength((byte) IsisConstants.SYSTEMIDLENGTH); |
Dhruv Dhody | e64b93e | 2016-04-20 19:26:55 +0530 | [diff] [blame] | 169 | isisHeader.setIsisPduType(pduType.value()); |
| 170 | isisHeader.setVersion2((byte) IsisConstants.ISISVERSION); |
| 171 | isisHeader.setReserved((byte) IsisConstants.RESERVED); |
| 172 | isisHeader.setMaximumAreaAddresses((byte) IsisConstants.MAXAREAADDRESS); |
| 173 | return isisHeader; |
| 174 | } |
| 175 | } |