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