Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | * the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | * specific language governing permissions and limitations under the License. |
| 12 | */ |
| 13 | |
| 14 | package org.onosproject.provider.bgp.topology.impl; |
| 15 | |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 16 | import static org.onosproject.bgp.controller.BgpDpid.uri; |
| 17 | import static org.onosproject.net.DeviceId.deviceId; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 18 | import static org.onosproject.net.Device.Type.ROUTER; |
| 19 | import static org.onosproject.net.Device.Type.VIRTUAL; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 20 | import static org.onosproject.incubator.net.resource.label.LabelResourceId.labelResourceId; |
| 21 | import static java.util.stream.Collectors.toList; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 22 | |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 23 | import java.util.LinkedList; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 24 | import java.util.List; |
| 25 | import java.util.Set; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 26 | |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 27 | import org.onlab.packet.ChassisId; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.util.Bandwidth; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Activate; |
| 31 | import org.apache.felix.scr.annotations.Component; |
| 32 | import org.apache.felix.scr.annotations.Deactivate; |
| 33 | import org.apache.felix.scr.annotations.Reference; |
| 34 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 35 | import org.onosproject.bgp.controller.BgpController; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 36 | import org.onosproject.bgp.controller.BgpDpid; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 37 | import org.onosproject.bgp.controller.BgpLinkListener; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 38 | import org.onosproject.bgp.controller.BgpNodeListener; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 39 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 40 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier; |
| 41 | import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4; |
| 42 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 43 | import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 44 | import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors; |
| 45 | import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails; |
| 46 | import org.onosproject.bgpio.types.AutonomousSystemTlv; |
| 47 | import org.onosproject.bgpio.types.BgpLSIdentifierTlv; |
| 48 | import org.onosproject.bgpio.types.BgpValueType; |
| 49 | import org.onosproject.bgpio.types.IPv4AddressTlv; |
| 50 | import org.onosproject.bgpio.types.IsIsNonPseudonode; |
| 51 | import org.onosproject.bgpio.types.IsIsPseudonode; |
| 52 | import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 53 | import org.onosproject.bgpio.types.LinkStateAttributes; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 54 | import org.onosproject.bgpio.types.OspfNonPseudonode; |
| 55 | import org.onosproject.bgpio.types.OspfPseudonode; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 56 | import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv; |
| 57 | import org.onosproject.bgpio.types.attr.BgpAttrNodeIsIsAreaId; |
| 58 | import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4; |
| 59 | import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric; |
| 60 | import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth; |
| 61 | import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 62 | import org.onosproject.core.CoreService; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 63 | import org.onosproject.incubator.net.resource.label.LabelResourceAdminService; |
| 64 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 65 | import org.onosproject.mastership.MastershipEvent; |
| 66 | import org.onosproject.mastership.MastershipListener; |
| 67 | import org.onosproject.mastership.MastershipService; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 68 | import org.onosproject.net.AnnotationKeys; |
| 69 | import org.onosproject.net.ConnectPoint; |
| 70 | import org.onosproject.net.DefaultAnnotations; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 71 | import org.onosproject.net.Device; |
| 72 | import org.onosproject.net.DeviceId; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 73 | import org.onosproject.net.Link; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 74 | import org.onosproject.net.MastershipRole; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 75 | import org.onosproject.net.PortNumber; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 76 | import org.onosproject.net.config.NetworkConfigService; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 77 | import org.onosproject.net.device.DefaultDeviceDescription; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 78 | import org.onosproject.net.device.DefaultPortDescription; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 79 | import org.onosproject.net.device.DeviceDescription; |
| 80 | import org.onosproject.net.device.DeviceProvider; |
| 81 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 82 | import org.onosproject.net.device.DeviceProviderService; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 83 | import org.onosproject.net.device.DeviceService; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 84 | import org.onosproject.net.device.PortDescription; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 85 | import org.onosproject.net.link.DefaultLinkDescription; |
| 86 | import org.onosproject.net.link.LinkDescription; |
| 87 | import org.onosproject.net.link.LinkProvider; |
| 88 | import org.onosproject.net.link.LinkProviderRegistry; |
| 89 | import org.onosproject.net.link.LinkProviderService; |
| 90 | import org.onosproject.net.link.LinkService; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 91 | import org.onosproject.net.provider.AbstractProvider; |
| 92 | import org.onosproject.net.provider.ProviderId; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 93 | import org.onosproject.net.resource.BandwidthCapacity; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 94 | import org.slf4j.Logger; |
| 95 | import org.slf4j.LoggerFactory; |
| 96 | |
| 97 | /** |
| 98 | * Provider which uses an BGP controller to detect network infrastructure topology. |
| 99 | */ |
| 100 | @Component(immediate = true) |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 101 | public class BgpTopologyProvider extends AbstractProvider implements DeviceProvider, LinkProvider { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 102 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 103 | /** |
| 104 | * Creates an instance of BGP topology provider. |
| 105 | */ |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 106 | public BgpTopologyProvider() { |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 107 | super(new ProviderId("l3", "org.onosproject.provider.bgp")); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private static final Logger log = LoggerFactory.getLogger(BgpTopologyProvider.class); |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 113 | protected DeviceProviderRegistry deviceProviderRegistry; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 116 | protected LinkProviderRegistry linkProviderRegistry; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 119 | protected BgpController controller; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 120 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected LinkService linkService; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected DeviceService deviceService; |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 128 | protected CoreService coreService; |
| 129 | |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 131 | protected MastershipService mastershipService; |
| 132 | |
| 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | protected LabelResourceAdminService labelResourceAdminService; |
| 135 | |
| 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected NetworkConfigService networkConfigService; |
| 138 | |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 139 | private DeviceProviderService deviceProviderService; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 140 | private LinkProviderService linkProviderService; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 141 | |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 142 | private InternalMastershipListener masterListener = new InternalMastershipListener(); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 143 | private InternalBgpProvider listener = new InternalBgpProvider(); |
| 144 | private static final String UNKNOWN = "unknown"; |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 145 | public static final long IDENTIFIER_SET = 0x100000000L; |
| 146 | public static final String AS_NUMBER = "asNumber"; |
| 147 | public static final String DOMAIN_IDENTIFIER = "domainIdentifier"; |
| 148 | public static final String ROUTING_UNIVERSE = "routingUniverse"; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 149 | |
| 150 | public static final String EXTERNAL_BIT = "externalBit"; |
| 151 | public static final String ABR_BIT = "abrBit"; |
| 152 | public static final String INTERNAL_BIT = "internalBit"; |
| 153 | public static final String PSEUDO = "psuedo"; |
| 154 | public static final String AREAID = "areaId"; |
| 155 | public static final String LSRID = "lsrId"; |
| 156 | public static final String COST = "cost"; |
| 157 | public static final String TE_COST = "teCost"; |
| 158 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 159 | public static final long PSEUDO_PORT = 0xffffffff; |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 160 | public static final int DELAY = 2; |
| 161 | private LabelResourceId beginLabel = labelResourceId(5122); |
| 162 | private LabelResourceId endLabel = labelResourceId(9217); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 163 | |
| 164 | @Activate |
| 165 | public void activate() { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 166 | log.debug("BgpTopologyProvider activate"); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 167 | deviceProviderService = deviceProviderRegistry.register(this); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 168 | linkProviderService = linkProviderRegistry.register(this); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 169 | controller.addListener(listener); |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 170 | mastershipService.addListener(masterListener); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 171 | controller.addLinkListener(listener); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Deactivate |
| 175 | public void deactivate() { |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 176 | log.debug("BgpTopologyProvider deactivate"); |
| 177 | deviceProviderRegistry.unregister(this); |
| 178 | deviceProviderService = null; |
| 179 | linkProviderRegistry.unregister(this); |
| 180 | linkProviderService = null; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 181 | controller.removeListener(listener); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 182 | controller.removeLinkListener(listener); |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 183 | mastershipService.removeListener(masterListener); |
| 184 | } |
| 185 | |
| 186 | private class InternalMastershipListener implements MastershipListener { |
| 187 | @Override |
| 188 | public void event(MastershipEvent event) { |
| 189 | if (event.type() == MastershipEvent.Type.MASTER_CHANGED) { |
| 190 | if (mastershipService.getMasterFor(event.subject()) != null) { |
| 191 | //Only for L3 device create label pool for that device |
| 192 | Device device = deviceService.getDevice(event.subject()); |
| 193 | if (device == null) { |
| 194 | log.debug("Device {} doesn't exist", event.subject()); |
| 195 | return; |
| 196 | } |
| 197 | //Reserve device label pool for L3 devices |
| 198 | if (device.annotations().value(LSRID) != null) { |
| 199 | createDevicePool(event.subject()); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Implements device and link update. |
| 208 | */ |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 209 | private class InternalBgpProvider implements BgpNodeListener, BgpLinkListener { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 210 | |
| 211 | @Override |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 212 | public void addNode(BgpNodeLSNlriVer4 nodeNlri, PathAttrNlriDetails details) { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 213 | log.debug("Add node {}", nodeNlri.toString()); |
| 214 | |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 215 | if (deviceProviderService == null || deviceService == null) { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 216 | return; |
| 217 | } |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 218 | Device.Type deviceType = ROUTER; |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 219 | BgpDpid nodeUri = new BgpDpid(nodeNlri); |
| 220 | DeviceId deviceId = deviceId(uri(nodeUri.toString())); |
| 221 | ChassisId cId = new ChassisId(); |
| 222 | |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 223 | /* |
| 224 | * Check if device is already there (available) , if yes not updating to core. |
| 225 | */ |
| 226 | if (deviceService.isAvailable(deviceId)) { |
| 227 | return; |
| 228 | } |
| 229 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 230 | DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); |
| 231 | |
| 232 | newBuilder.set(AnnotationKeys.TYPE, "L3"); |
| 233 | newBuilder.set(ROUTING_UNIVERSE, Long.toString(nodeNlri.getIdentifier())); |
| 234 | |
| 235 | List<BgpValueType> tlvs = nodeNlri.getLocalNodeDescriptors().getNodedescriptors().getSubTlvs(); |
| 236 | for (BgpValueType tlv : tlvs) { |
| 237 | if (tlv instanceof AutonomousSystemTlv) { |
| 238 | newBuilder.set(AS_NUMBER, Integer.toString(((AutonomousSystemTlv) tlv).getAsNum())); |
| 239 | } else if (tlv instanceof BgpLSIdentifierTlv) { |
| 240 | newBuilder.set(DOMAIN_IDENTIFIER, |
| 241 | Integer.toString(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier())); |
| 242 | } |
| 243 | if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) { |
| 244 | if (tlv instanceof IsIsPseudonode) { |
| 245 | deviceType = VIRTUAL; |
| 246 | newBuilder.set(AnnotationKeys.ROUTER_ID, new String(((IsIsPseudonode) tlv).getIsoNodeId())); |
| 247 | } else if (tlv instanceof OspfPseudonode) { |
| 248 | deviceType = VIRTUAL; |
| 249 | newBuilder |
| 250 | .set(AnnotationKeys.ROUTER_ID, Integer.toString(((OspfPseudonode) tlv).getrouterID())); |
| 251 | } else if (tlv instanceof IsIsNonPseudonode) { |
| 252 | newBuilder.set(AnnotationKeys.ROUTER_ID, new String(((IsIsNonPseudonode) tlv).getIsoNodeId())); |
| 253 | } else if (tlv instanceof OspfNonPseudonode) { |
| 254 | newBuilder.set(AnnotationKeys.ROUTER_ID, |
| 255 | Integer.toString(((OspfNonPseudonode) tlv).getrouterID())); |
| 256 | } |
| 257 | } |
| 258 | } |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 259 | DefaultAnnotations.Builder anntotations = DefaultAnnotations.builder(); |
| 260 | anntotations = getAnnotations(newBuilder, true, details); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 261 | |
| 262 | DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN, |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 263 | UNKNOWN, UNKNOWN, UNKNOWN, cId, anntotations.build()); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 264 | |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 265 | deviceProviderService.deviceConnected(deviceId, description); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 269 | public void deleteNode(BgpNodeLSNlriVer4 nodeNlri) { |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 270 | log.debug("Delete node {}", nodeNlri.toString()); |
| 271 | |
| 272 | if (deviceProviderService == null) { |
| 273 | return; |
| 274 | } |
| 275 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 276 | BgpDpid deviceUri = new BgpDpid(nodeNlri); |
| 277 | DeviceId deviceId = deviceId(uri(deviceUri.toString())); |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 278 | |
| 279 | if (labelResourceAdminService != null) { |
| 280 | //Destroy local device label pool reserved for that device |
| 281 | labelResourceAdminService.destroyDevicePool(deviceId); |
| 282 | } |
| 283 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 284 | deviceProviderService.deviceDisconnected(deviceId); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void addLink(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details) throws BgpParseException { |
| 289 | log.debug("Addlink {}", linkNlri.toString()); |
| 290 | |
| 291 | if (linkProviderService == null) { |
| 292 | return; |
| 293 | } |
| 294 | LinkDescription linkDes = buildLinkDes(linkNlri, details, true); |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 295 | |
| 296 | /* |
| 297 | * Update link ports and configure bandwidth on source and destination port using networkConfig service |
| 298 | * Only master of source link registers for bandwidth |
| 299 | */ |
| 300 | if (mastershipService.isLocalMaster(linkDes.src().deviceId())) { |
| 301 | registerBandwidth(linkDes, details); |
| 302 | } |
| 303 | |
| 304 | //Updating ports of the link |
| 305 | List<PortDescription> srcPortDescriptions = new LinkedList<>(); |
| 306 | srcPortDescriptions.add(new DefaultPortDescription(linkDes.src().port(), true)); |
| 307 | deviceProviderService.updatePorts(linkDes.src().deviceId(), srcPortDescriptions); |
| 308 | |
| 309 | List<PortDescription> dstPortDescriptions = new LinkedList<>(); |
| 310 | dstPortDescriptions.add(new DefaultPortDescription(linkDes.dst().port(), true)); |
| 311 | deviceProviderService.updatePorts(linkDes.dst().deviceId(), dstPortDescriptions); |
| 312 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 313 | linkProviderService.linkDetected(linkDes); |
| 314 | } |
| 315 | |
| 316 | //Build link description. |
| 317 | private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink) |
| 318 | throws BgpParseException { |
| 319 | long srcAddress = 0; |
| 320 | long dstAddress = 0; |
| 321 | boolean localPseduo = false; |
| 322 | boolean remotePseduo = false; |
| 323 | |
| 324 | List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs(); |
| 325 | for (BgpValueType localTlv : localTlvs) { |
| 326 | if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) { |
| 327 | localPseduo = true; |
| 328 | } |
| 329 | } |
| 330 | List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs(); |
| 331 | for (BgpValueType remoteTlv : remoteTlvs) { |
| 332 | if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) { |
| 333 | remotePseduo = true; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors(); |
| 338 | for (BgpValueType tlv : tlvs) { |
| 339 | if (tlv instanceof LinkLocalRemoteIdentifiersTlv) { |
| 340 | srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier(); |
| 341 | //Set 32nd bit. |
| 342 | srcAddress = srcAddress | IDENTIFIER_SET; |
| 343 | dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier(); |
| 344 | dstAddress = dstAddress | IDENTIFIER_SET; |
| 345 | } else if (tlv instanceof IPv4AddressTlv) { |
| 346 | if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) { |
| 347 | srcAddress = ((IPv4AddressTlv) tlv).address().toInt(); |
| 348 | } else { |
| 349 | dstAddress = ((IPv4AddressTlv) tlv).address().toInt(); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString())); |
| 355 | DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString())); |
| 356 | |
| 357 | if (localPseduo && srcAddress == 0) { |
| 358 | srcAddress = PSEUDO_PORT; |
| 359 | } else if (remotePseduo && dstAddress == 0) { |
| 360 | dstAddress = PSEUDO_PORT; |
| 361 | } |
| 362 | |
| 363 | ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress)); |
| 364 | ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress)); |
| 365 | BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId() |
| 366 | .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false, |
| 367 | linkNlri.getRouteDistinguisher()); |
| 368 | |
| 369 | BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId() |
| 370 | .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false, |
| 371 | linkNlri.getRouteDistinguisher()); |
| 372 | |
| 373 | addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri, |
| 374 | dstNodeNlri, srcId, dstId, details); |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 375 | DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder(); |
| 376 | if (details != null) { |
| 377 | annotationBuilder = getAnnotations(annotationBuilder, false, details); |
| 378 | } |
| 379 | |
| 380 | return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build()); |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo, |
| 384 | BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId, |
| 385 | PathAttrNlriDetails details) { |
| 386 | if (isAddLink) { |
| 387 | if (localPseduo) { |
| 388 | if (deviceService.getDevice(srcId) == null) { |
| 389 | for (BgpNodeListener l : controller.listener()) { |
| 390 | l.addNode(srcNodeNlri, details); |
| 391 | } |
| 392 | } |
| 393 | } else if (remotePseduo) { |
| 394 | if (deviceService.getDevice(dstId) == null) { |
| 395 | for (BgpNodeListener l : controller.listener()) { |
| 396 | l.addNode(dstNodeNlri, details); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } else { |
| 401 | if (localPseduo) { |
| 402 | Set<Link> links = linkService.getDeviceLinks(srcId); |
| 403 | if (links == null || links.isEmpty()) { |
| 404 | for (BgpNodeListener l : controller.listener()) { |
| 405 | l.deleteNode(srcNodeNlri); |
| 406 | } |
| 407 | } |
| 408 | } else if (remotePseduo) { |
| 409 | log.info("Remote pseudo delete link "); |
| 410 | Set<Link> links = linkService.getDeviceLinks(dstId); |
| 411 | if (links == null || links.isEmpty()) { |
| 412 | for (BgpNodeListener l : controller.listener()) { |
| 413 | l.deleteNode(dstNodeNlri); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException { |
| 422 | log.debug("Delete link {}", linkNlri.toString()); |
| 423 | |
| 424 | if (linkProviderService == null) { |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | LinkDescription linkDes = buildLinkDes(linkNlri, null, false); |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 429 | |
| 430 | /* |
| 431 | * Only master for the link src will release the bandwidth resource. |
| 432 | */ |
| 433 | if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) { |
| 434 | // Releases registered resource for this link |
| 435 | networkConfigService.removeConfig(linkDes.src(), BandwidthCapacity.class); |
| 436 | networkConfigService.removeConfig(linkDes.dst(), BandwidthCapacity.class); |
| 437 | } |
| 438 | |
Priyanka B | fc51c95 | 2016-03-26 14:30:33 +0530 | [diff] [blame] | 439 | linkProviderService.linkVanished(linkDes); |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 440 | } |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 441 | } |
| 442 | |
Priyanka B | 658aa698 | 2016-05-27 21:34:49 +0530 | [diff] [blame^] | 443 | // Creates label resource pool for the specific device. For all devices label range is 5122-9217 |
| 444 | private void createDevicePool(DeviceId deviceId) { |
| 445 | if (labelResourceAdminService == null) { |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | labelResourceAdminService.createDevicePool(deviceId, beginLabel, endLabel); |
| 450 | } |
| 451 | |
| 452 | private void registerBandwidth(LinkDescription linkDes, PathAttrNlriDetails details) { |
| 453 | if (details == null) { |
| 454 | log.error("Couldnot able to register bandwidth "); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | List<BgpValueType> attribute = details.pathAttributes().stream() |
| 459 | .filter(attr -> attr instanceof LinkStateAttributes).collect(toList()); |
| 460 | if (attribute.isEmpty()) { |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes(); |
| 465 | float maxReservableBw = 0; |
| 466 | |
| 467 | for (BgpValueType tlv : tlvs) { |
| 468 | switch (tlv.getType()) { |
| 469 | case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH: |
| 470 | maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth(); |
| 471 | break; |
| 472 | default: // do nothing |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (maxReservableBw == 0.0) { |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | //Configure bandwidth for src and dst port |
| 481 | BandwidthCapacity config = networkConfigService.addConfig(linkDes.src(), BandwidthCapacity.class); |
| 482 | config.capacity(Bandwidth.bps(maxReservableBw)).apply(); |
| 483 | |
| 484 | config = networkConfigService.addConfig(linkDes.dst(), BandwidthCapacity.class); |
| 485 | config.capacity(Bandwidth.bps(maxReservableBw)).apply(); |
| 486 | } |
| 487 | |
| 488 | private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode, |
| 489 | PathAttrNlriDetails details) { |
| 490 | |
| 491 | List<BgpValueType> attribute = details.pathAttributes().stream() |
| 492 | .filter(attr -> attr instanceof LinkStateAttributes).collect(toList()); |
| 493 | if (attribute.isEmpty()) { |
| 494 | return annotationBuilder; |
| 495 | } |
| 496 | List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes(); |
| 497 | boolean abrBit = false; |
| 498 | boolean externalBit = false; |
| 499 | boolean pseudo = false; |
| 500 | int igpMetric = 0; |
| 501 | int teMetric = 0; |
| 502 | byte[] areaId = null; |
| 503 | Ip4Address routerId = null; |
| 504 | for (BgpValueType tlv : tlvs) { |
| 505 | switch (tlv.getType()) { |
| 506 | case LinkStateAttributes.ATTR_NODE_FLAG_BITS: |
| 507 | abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit(); |
| 508 | externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit(); |
| 509 | break; |
| 510 | case NodeDescriptors.IGP_ROUTERID_TYPE: |
| 511 | if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) { |
| 512 | pseudo = true; |
| 513 | } |
| 514 | break; |
| 515 | case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID: |
| 516 | areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId(); |
| 517 | break; |
| 518 | case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID: |
| 519 | routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId(); |
| 520 | break; |
| 521 | case LinkStateAttributes.ATTR_LINK_IGP_METRIC: |
| 522 | igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric(); |
| 523 | break; |
| 524 | case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC: |
| 525 | teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric(); |
| 526 | break; |
| 527 | default: // do nothing |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // Annotations for device |
| 532 | if (isNode) { |
| 533 | boolean internalBit = false; |
| 534 | if (!abrBit && !externalBit) { |
| 535 | internalBit = true; |
| 536 | } |
| 537 | |
| 538 | annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit)); |
| 539 | annotationBuilder.set(ABR_BIT, String.valueOf(abrBit)); |
| 540 | annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit)); |
| 541 | annotationBuilder.set(PSEUDO, String.valueOf(pseudo)); |
| 542 | |
| 543 | if (areaId != null) { |
| 544 | annotationBuilder.set(AREAID, new String(areaId)); |
| 545 | } |
| 546 | if (routerId != null) { |
| 547 | // LsrID |
| 548 | annotationBuilder.set(LSRID, String.valueOf(routerId)); |
| 549 | } |
| 550 | } else { |
| 551 | // Annotations for link |
| 552 | if (igpMetric != 0) { |
| 553 | annotationBuilder.set(COST, String.valueOf(igpMetric)); |
| 554 | } |
| 555 | |
| 556 | if (teMetric != 0) { |
| 557 | annotationBuilder.set(TE_COST, String.valueOf(teMetric)); |
| 558 | } |
| 559 | } |
| 560 | return annotationBuilder; |
| 561 | } |
| 562 | |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 563 | @Override |
| 564 | public void triggerProbe(DeviceId deviceId) { |
| 565 | // TODO Auto-generated method stub |
| 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public boolean isReachable(DeviceId deviceId) { |
| 574 | // TODO Auto-generated method stub |
| 575 | return true; |
| 576 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 577 | |
| 578 | @Override |
| 579 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 580 | boolean enable) { |
| 581 | } |
Shashikanth VH | 1ca26ce | 2015-11-20 23:19:49 +0530 | [diff] [blame] | 582 | } |