blob: 194bf08ba7b313961ce67823608b6a877031d9a8 [file] [log] [blame]
Dhruv Dhodye64b93e2016-04-20 19:26:55 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Dhruv Dhodye64b93e2016-04-20 19:26:55 +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.isis.io.util;
17
18import org.onlab.packet.Ip4Address;
19import org.onlab.packet.MacAddress;
20import org.onosproject.isis.controller.IsisInterface;
21import org.onosproject.isis.controller.IsisNeighbor;
22import org.onosproject.isis.controller.IsisNetworkType;
23import org.onosproject.isis.controller.IsisPduType;
24import org.onosproject.isis.io.isispacket.IsisHeader;
25import org.onosproject.isis.io.isispacket.pdu.AttachedToOtherAreas;
26import org.onosproject.isis.io.isispacket.pdu.LsPdu;
27import org.onosproject.isis.io.isispacket.tlv.AreaAddressTlv;
28import org.onosproject.isis.io.isispacket.tlv.HostNameTlv;
tejeshwer degala3fe1ed52016-04-22 17:04:01 +053029import org.onosproject.isis.io.isispacket.tlv.IpExtendedReachabilityTlv;
Dhruv Dhodye64b93e2016-04-20 19:26:55 +053030import org.onosproject.isis.io.isispacket.tlv.IpInterfaceAddressTlv;
31import org.onosproject.isis.io.isispacket.tlv.IpInternalReachabilityTlv;
32import org.onosproject.isis.io.isispacket.tlv.IsReachabilityTlv;
33import org.onosproject.isis.io.isispacket.tlv.MetricOfInternalReachability;
34import org.onosproject.isis.io.isispacket.tlv.MetricsOfReachability;
35import org.onosproject.isis.io.isispacket.tlv.ProtocolSupportedTlv;
36import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
37import org.onosproject.isis.io.isispacket.tlv.TlvType;
38
39import java.util.List;
40
41/**
42 * Representation of link state PDU generator.
43 */
44public 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 degala3fe1ed52016-04-22 17:04:01 +0530119 metricsOfReachability.setNeighborId(neighbor.neighborSystemId() + ".00");
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530120 }
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 degala3fe1ed52016-04-22 17:04:01 +0530141 Ip4Address ip4Address = isisInterface.interfaceIpAddress();
142 byte[] ipAddress = ip4Address.toOctets();
sunish vk4b5ce002016-05-09 20:18:35 +0530143 // ipAddress[ipAddress.length - 1] = 0;
144 byte[] networkmass = isisInterface.networkMask();
145 // metric calculation part
146 byte[] result = new byte[ipAddress.length];
147 result[0] = (byte) (ipAddress[0] & networkmass[0]);
148 result[1] = (byte) (ipAddress[1] & networkmass[1]);
149 result[2] = (byte) (ipAddress[2] & networkmass[2]);
150 result[3] = (byte) (ipAddress[3] & networkmass[3]);
151 metricOfIntRea.setIpAddress(Ip4Address.valueOf(result));
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700152 metricOfIntRea.setSubnetAddress(Ip4Address.valueOf(isisInterface.networkMask()));
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530153 ipInterReacTlv.addInternalReachabilityMetric(metricOfIntRea);
154 lsp.addTlv(ipInterReacTlv);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530155
156 tlvHeader.setTlvType(TlvType.IPEXTENDEDREACHABILITY.value());
157 tlvHeader.setTlvLength(0);
158 IpExtendedReachabilityTlv extendedTlv = new IpExtendedReachabilityTlv(tlvHeader);
159 extendedTlv.setDown(false);
160 extendedTlv.setMetric(10);
161 extendedTlv.setPrefix("192.168.7");
162 extendedTlv.setPrefixLength(24);
163 extendedTlv.setSubTlvLength((byte) 0);
164 extendedTlv.setSubTlvPresence(false);
165 lsp.addTlv(extendedTlv);
166
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530167 return lsp;
168 }
169
170 public IsisHeader getHeader(IsisPduType pduType) {
171 IsisHeader isisHeader = new IsisHeader();
172 isisHeader.setIrpDiscriminator((byte) IsisConstants.IRPDISCRIMINATOR);
173 isisHeader.setPduHeaderLength((byte) IsisUtil.getPduHeaderLength(pduType.value()));
174 isisHeader.setVersion((byte) IsisConstants.ISISVERSION);
tejeshwer degala3fe1ed52016-04-22 17:04:01 +0530175 isisHeader.setIdLength((byte) IsisConstants.SYSTEMIDLENGTH);
Dhruv Dhodye64b93e2016-04-20 19:26:55 +0530176 isisHeader.setIsisPduType(pduType.value());
177 isisHeader.setVersion2((byte) IsisConstants.ISISVERSION);
178 isisHeader.setReserved((byte) IsisConstants.RESERVED);
179 isisHeader.setMaximumAreaAddresses((byte) IsisConstants.MAXAREAADDRESS);
180 return isisHeader;
181 }
Ray Milkeyc108a6b2017-08-23 15:23:50 -0700182}