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