Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +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.pce.pceservice; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 19 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 20 | import java.util.Collection; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 21 | import java.util.Collections; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 22 | import java.util.HashMap; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 23 | import java.util.Iterator; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 24 | import java.util.LinkedList; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 25 | import java.util.List; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | import java.util.Optional; |
| 28 | import java.util.Map.Entry; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 29 | import java.util.Set; |
Priyanka B | 9fa4ed3 | 2016-05-27 11:59:24 +0530 | [diff] [blame] | 30 | import java.util.concurrent.Executors; |
| 31 | import java.util.concurrent.ScheduledExecutorService; |
| 32 | import java.util.concurrent.TimeUnit; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 33 | |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 34 | import org.onlab.packet.Ethernet; |
| 35 | import org.onlab.packet.IPv4; |
| 36 | |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 37 | import org.apache.felix.scr.annotations.Activate; |
| 38 | import org.apache.felix.scr.annotations.Component; |
| 39 | import org.apache.felix.scr.annotations.Deactivate; |
| 40 | import org.apache.felix.scr.annotations.Reference; |
| 41 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 42 | import org.apache.felix.scr.annotations.Service; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 43 | import org.onlab.packet.IpAddress; |
| 44 | import org.onlab.packet.IpPrefix; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 45 | import org.onlab.packet.TCP; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 46 | import org.onlab.util.Bandwidth; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 47 | import org.onosproject.core.ApplicationId; |
| 48 | import org.onosproject.core.CoreService; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 49 | import org.onosproject.incubator.net.resource.label.LabelResourceAdminService; |
| 50 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 51 | import org.onosproject.incubator.net.resource.label.LabelResourceService; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 52 | import org.onosproject.core.IdGenerator; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 53 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 54 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 55 | import org.onosproject.incubator.net.tunnel.LabelStack; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 56 | import org.onosproject.incubator.net.tunnel.Tunnel; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 57 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 58 | import org.onosproject.incubator.net.tunnel.TunnelEvent; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 59 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 60 | import org.onosproject.incubator.net.tunnel.TunnelListener; |
| 61 | import org.onosproject.incubator.net.tunnel.TunnelName; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 62 | import org.onosproject.incubator.net.tunnel.TunnelService; |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 63 | import org.onosproject.mastership.MastershipService; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 64 | import org.onosproject.net.config.NetworkConfigEvent; |
| 65 | import org.onosproject.net.config.NetworkConfigListener; |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 66 | import org.onosproject.net.config.NetworkConfigService; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 67 | import org.onosproject.net.DefaultAnnotations; |
| 68 | import org.onosproject.net.DefaultAnnotations.Builder; |
| 69 | import org.onosproject.net.Device; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 70 | import org.onosproject.net.DeviceId; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 71 | import org.onosproject.net.Link; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 72 | import org.onosproject.net.Path; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 73 | import org.onosproject.net.device.DeviceEvent; |
| 74 | import org.onosproject.net.device.DeviceListener; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 75 | import org.onosproject.net.device.DeviceService; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 76 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 77 | import org.onosproject.net.flowobjective.Objective; |
| 78 | import org.onosproject.net.intent.Constraint; |
| 79 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 80 | import org.onosproject.net.link.LinkListener; |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 81 | import org.onosproject.net.link.LinkEvent; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 82 | import org.onosproject.net.link.LinkService; |
| 83 | import org.onosproject.net.MastershipRole; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 84 | import org.onosproject.pce.pceservice.constraint.CapabilityConstraint; |
| 85 | import org.onosproject.pce.pceservice.constraint.CapabilityConstraint.CapabilityType; |
| 86 | import org.onosproject.pce.pceservice.constraint.CostConstraint; |
| 87 | import org.onosproject.pce.pceservice.constraint.SharedBandwidthConstraint; |
| 88 | import org.onosproject.net.resource.Resource; |
| 89 | import org.onosproject.net.resource.ResourceAllocation; |
| 90 | import org.onosproject.net.resource.ResourceConsumer; |
| 91 | import org.onosproject.net.resource.ResourceQueryService; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 92 | import org.onosproject.net.resource.ResourceService; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 93 | import org.onosproject.net.resource.Resources; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 94 | import org.onosproject.net.topology.LinkWeight; |
| 95 | import org.onosproject.net.topology.PathService; |
| 96 | import org.onosproject.net.topology.TopologyEdge; |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 97 | import org.onosproject.net.topology.TopologyEvent; |
| 98 | import org.onosproject.net.topology.TopologyListener; |
| 99 | import org.onosproject.net.topology.TopologyService; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 100 | import org.onosproject.pce.pceservice.api.PceService; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 101 | import org.onosproject.pce.pcestore.PcePathInfo; |
| 102 | import org.onosproject.pce.pcestore.PceccTunnelInfo; |
| 103 | import org.onosproject.pce.pcestore.api.PceStore; |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 104 | import org.onosproject.pcep.api.DeviceCapability; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 105 | import org.onosproject.store.serializers.KryoNamespaces; |
| 106 | import org.onosproject.store.service.DistributedSet; |
| 107 | import org.onosproject.store.service.Serializer; |
| 108 | import org.onosproject.store.service.StorageService; |
| 109 | import org.slf4j.Logger; |
| 110 | import org.slf4j.LoggerFactory; |
| 111 | |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 112 | import com.google.common.collect.ImmutableList; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 113 | import com.google.common.collect.ImmutableSet; |
| 114 | |
| 115 | import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS; |
| 116 | import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT; |
| 117 | import static org.onosproject.incubator.net.tunnel.Tunnel.State.ESTABLISHED; |
| 118 | import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE; |
| 119 | import static org.onosproject.pce.pceservice.LspType.WITH_SIGNALLING; |
| 120 | import static org.onosproject.pce.pceservice.LspType.SR_WITHOUT_SIGNALLING; |
| 121 | import static org.onosproject.pce.pceservice.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR; |
| 122 | |
| 123 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.BANDWIDTH; |
| 124 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.LOCAL_LSP_ID; |
| 125 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.LSP_SIG_TYPE; |
| 126 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.PCE_INIT; |
| 127 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.PLSP_ID; |
| 128 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.PCC_TUNNEL_ID; |
| 129 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.DELEGATE; |
| 130 | import static org.onosproject.pce.pceservice.PcepAnnotationKeys.COST_TYPE; |
| 131 | |
| 132 | import org.onosproject.net.packet.InboundPacket; |
| 133 | import org.onosproject.net.packet.PacketContext; |
| 134 | import org.onosproject.net.packet.PacketProcessor; |
| 135 | import org.onosproject.net.packet.PacketService; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 136 | |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 137 | /** |
| 138 | * Implementation of PCE service. |
| 139 | */ |
| 140 | @Component(immediate = true) |
| 141 | @Service |
| 142 | public class PceManager implements PceService { |
| 143 | private static final Logger log = LoggerFactory.getLogger(PceManager.class); |
| 144 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 145 | public static final long GLOBAL_LABEL_SPACE_MIN = 4097; |
| 146 | public static final long GLOBAL_LABEL_SPACE_MAX = 5121; |
| 147 | private static final String DEVICE_NULL = "Device-cannot be null"; |
| 148 | private static final String LINK_NULL = "Link-cannot be null"; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 149 | public static final String PCE_SERVICE_APP = "org.onosproject.pce"; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 150 | private static final String LOCAL_LSP_ID_GEN_TOPIC = "pcep-local-lsp-id"; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 151 | public static final String DEVICE_TYPE = "type"; |
| 152 | public static final String L3_DEVICE = "L3"; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 153 | private static final int PREFIX_LENGTH = 32; |
| 154 | |
| 155 | private static final String TUNNEL_CONSUMER_ID_GEN_TOPIC = "pcep-tunnel-consumer-id"; |
| 156 | private IdGenerator tunnelConsumerIdGen; |
| 157 | |
| 158 | private static final String LSRID = "lsrId"; |
| 159 | private static final String TRUE = "true"; |
| 160 | private static final String FALSE = "false"; |
| 161 | private static final String END_OF_SYNC_IP_PREFIX = "0.0.0.0/32"; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 162 | public static final int PCEP_PORT = 4189; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 163 | |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 164 | private IdGenerator localLspIdIdGen; |
| 165 | protected DistributedSet<Short> localLspIdFreeList; |
| 166 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 167 | // LSR-id and device-id mapping for checking capability if L3 device is not |
| 168 | // having its capability |
| 169 | private Map<String, DeviceId> lsrIdDeviceIdMap = new HashMap<>(); |
| 170 | |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 171 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 172 | protected CoreService coreService; |
| 173 | |
| 174 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 175 | protected ResourceService resourceService; |
| 176 | |
| 177 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 178 | protected ResourceQueryService resourceQueryService; |
| 179 | |
| 180 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 181 | protected PathService pathService; |
| 182 | |
| 183 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 184 | protected PceStore pceStore; |
| 185 | |
| 186 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 187 | protected TunnelService tunnelService; |
| 188 | |
| 189 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 190 | protected StorageService storageService; |
| 191 | |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 192 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 193 | protected PacketService packetService; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 194 | |
| 195 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 196 | protected DeviceService deviceService; |
| 197 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 198 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 199 | protected LinkService linkService; |
| 200 | |
| 201 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 202 | protected NetworkConfigService netCfgService; |
| 203 | |
| 204 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 205 | protected LabelResourceAdminService labelRsrcAdminService; |
| 206 | |
| 207 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 208 | protected LabelResourceService labelRsrcService; |
| 209 | |
| 210 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 211 | protected FlowObjectiveService flowObjectiveService; |
| 212 | |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 213 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 214 | protected MastershipService mastershipService; |
| 215 | |
| 216 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 217 | protected TopologyService topologyService; |
| 218 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 219 | private TunnelListener listener = new InnerTunnelListener(); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 220 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 221 | private LinkListener linkListener = new InternalLinkListener(); |
| 222 | private InternalConfigListener cfgListener = new InternalConfigListener(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 223 | private BasicPceccHandler crHandler; |
| 224 | private PceccSrTeBeHandler srTeHandler; |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 225 | private ApplicationId appId; |
| 226 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 227 | private final PcepPacketProcessor processor = new PcepPacketProcessor(); |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 228 | private final TopologyListener topologyListener = new InternalTopologyListener(); |
Priyanka B | 9fa4ed3 | 2016-05-27 11:59:24 +0530 | [diff] [blame] | 229 | private ScheduledExecutorService executor; |
| 230 | |
| 231 | public static final int INITIAL_DELAY = 30; |
| 232 | public static final int PERIODIC_DELAY = 30; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 233 | |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 234 | /** |
| 235 | * Creates new instance of PceManager. |
| 236 | */ |
| 237 | public PceManager() { |
| 238 | } |
| 239 | |
| 240 | @Activate |
| 241 | protected void activate() { |
| 242 | appId = coreService.registerApplication(PCE_SERVICE_APP); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 243 | crHandler = BasicPceccHandler.getInstance(); |
| 244 | crHandler.initialize(labelRsrcService, flowObjectiveService, appId, pceStore); |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 245 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 246 | srTeHandler = PceccSrTeBeHandler.getInstance(); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 247 | srTeHandler.initialize(labelRsrcAdminService, labelRsrcService, flowObjectiveService, appId, pceStore, |
| 248 | deviceService); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 249 | |
| 250 | tunnelService.addListener(listener); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 251 | deviceService.addListener(deviceListener); |
| 252 | linkService.addListener(linkListener); |
| 253 | netCfgService.addListener(cfgListener); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 254 | |
| 255 | tunnelConsumerIdGen = coreService.getIdGenerator(TUNNEL_CONSUMER_ID_GEN_TOPIC); |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 256 | localLspIdIdGen = coreService.getIdGenerator(LOCAL_LSP_ID_GEN_TOPIC); |
| 257 | localLspIdFreeList = storageService.<Short>setBuilder() |
| 258 | .withName("pcepLocalLspIdDeletedList") |
| 259 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 260 | .build() |
| 261 | .asDistributedSet(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 262 | |
| 263 | packetService.addProcessor(processor, PacketProcessor.director(4)); |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 264 | topologyService.addListener(topologyListener); |
Priyanka B | 9fa4ed3 | 2016-05-27 11:59:24 +0530 | [diff] [blame] | 265 | executor = Executors.newSingleThreadScheduledExecutor(); |
| 266 | //Start a timer when the component is up, with initial delay of 30min and periodic delays at 30min |
| 267 | executor.scheduleAtFixedRate(new GlobalOptimizationTimer(), INITIAL_DELAY, PERIODIC_DELAY, TimeUnit.MINUTES); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 268 | |
| 269 | // Reserve global node pool |
| 270 | if (!srTeHandler.reserveGlobalPool(GLOBAL_LABEL_SPACE_MIN, GLOBAL_LABEL_SPACE_MAX)) { |
| 271 | log.debug("Global node pool was already reserved."); |
| 272 | } |
| 273 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 274 | log.info("Started"); |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Deactivate |
| 278 | protected void deactivate() { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 279 | tunnelService.removeListener(listener); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 280 | deviceService.removeListener(deviceListener); |
| 281 | linkService.removeListener(linkListener); |
| 282 | netCfgService.removeListener(cfgListener); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 283 | packetService.removeProcessor(processor); |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 284 | topologyService.removeListener(topologyListener); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 285 | // Shutdown the thread when component is deactivated |
Priyanka B | 9fa4ed3 | 2016-05-27 11:59:24 +0530 | [diff] [blame] | 286 | executor.shutdown(); |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 287 | log.info("Stopped"); |
| 288 | } |
| 289 | |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 290 | /** |
| 291 | * Returns an edge-weight capable of evaluating links on the basis of the |
| 292 | * specified constraints. |
| 293 | * |
| 294 | * @param constraints path constraints |
| 295 | * @return edge-weight function |
| 296 | */ |
| 297 | private LinkWeight weight(List<Constraint> constraints) { |
| 298 | return new TeConstraintBasedLinkWeight(constraints); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Computes a path between two devices. |
| 303 | * |
| 304 | * @param src ingress device |
| 305 | * @param dst egress device |
| 306 | * @param constraints path constraints |
| 307 | * @return computed path based on constraints |
| 308 | */ |
| 309 | protected Set<Path> computePath(DeviceId src, DeviceId dst, List<Constraint> constraints) { |
| 310 | if (pathService == null) { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 311 | return ImmutableSet.of(); |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 312 | } |
| 313 | Set<Path> paths = pathService.getPaths(src, dst, weight(constraints)); |
| 314 | if (!paths.isEmpty()) { |
| 315 | return paths; |
| 316 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 317 | return ImmutableSet.of(); |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 318 | } |
| 319 | |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 320 | //[TODO:] handle requests in queue |
| 321 | @Override |
| 322 | public boolean setupPath(DeviceId src, DeviceId dst, String tunnelName, List<Constraint> constraints, |
| 323 | LspType lspType) { |
| 324 | checkNotNull(src); |
| 325 | checkNotNull(dst); |
| 326 | checkNotNull(tunnelName); |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 327 | checkNotNull(lspType); |
| 328 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 329 | // Convert from DeviceId to TunnelEndPoint |
| 330 | Device srcDevice = deviceService.getDevice(src); |
| 331 | Device dstDevice = deviceService.getDevice(dst); |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 332 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 333 | if (srcDevice == null || dstDevice == null) { |
| 334 | // Device is not known. |
| 335 | pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType)); |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | // In future projections instead of annotations will be used to fetch LSR ID. |
| 340 | String srcLsrId = srcDevice.annotations().value(LSRID); |
| 341 | String dstLsrId = dstDevice.annotations().value(LSRID); |
| 342 | |
| 343 | if (srcLsrId == null || dstLsrId == null) { |
| 344 | // LSR id is not known. |
| 345 | pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType)); |
| 346 | return false; |
| 347 | } |
| 348 | |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 349 | // Get device config from netconfig, to ascertain that session with ingress is present. |
| 350 | DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(srcLsrId), DeviceCapability.class); |
| 351 | if (cfg == null) { |
| 352 | log.debug("No session to ingress."); |
| 353 | pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType)); |
| 354 | return false; |
| 355 | } |
| 356 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 357 | TunnelEndPoint srcEndPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(srcLsrId)); |
| 358 | TunnelEndPoint dstEndPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dstLsrId)); |
| 359 | |
| 360 | double bwConstraintValue = 0; |
| 361 | CostConstraint costConstraint = null; |
| 362 | if (constraints != null) { |
| 363 | constraints.add(CapabilityConstraint.of(CapabilityType.valueOf(lspType.name()))); |
| 364 | Iterator<Constraint> iterator = constraints.iterator(); |
| 365 | |
| 366 | while (iterator.hasNext()) { |
| 367 | Constraint constraint = iterator.next(); |
| 368 | if (constraint instanceof BandwidthConstraint) { |
| 369 | bwConstraintValue = ((BandwidthConstraint) constraint).bandwidth().bps(); |
| 370 | } else if (constraint instanceof CostConstraint) { |
| 371 | costConstraint = (CostConstraint) constraint; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Add cost at the end of the list of constraints. The path computation algorithm also computes cumulative |
| 377 | * cost. The function which checks the limiting/capability constraints also returns per link cost. This |
| 378 | * function can either return the result of limiting/capability constraint validation or the value of link |
| 379 | * cost, depending upon what is the last constraint in the loop. |
| 380 | */ |
| 381 | if (costConstraint != null) { |
| 382 | constraints.remove(costConstraint); |
| 383 | constraints.add(costConstraint); |
| 384 | } |
| 385 | } else { |
| 386 | constraints = new LinkedList<>(); |
| 387 | constraints.add(CapabilityConstraint.of(CapabilityType.valueOf(lspType.name()))); |
| 388 | } |
| 389 | |
| 390 | Set<Path> computedPathSet = computePath(src, dst, constraints); |
| 391 | |
| 392 | // NO-PATH |
| 393 | if (computedPathSet.isEmpty()) { |
| 394 | pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType)); |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | Builder annotationBuilder = DefaultAnnotations.builder(); |
| 399 | if (bwConstraintValue != 0) { |
| 400 | annotationBuilder.set(BANDWIDTH, String.valueOf(bwConstraintValue)); |
| 401 | } |
| 402 | if (costConstraint != null) { |
| 403 | annotationBuilder.set(COST_TYPE, String.valueOf(costConstraint.type())); |
| 404 | } |
| 405 | annotationBuilder.set(LSP_SIG_TYPE, lspType.name()); |
| 406 | annotationBuilder.set(PCE_INIT, TRUE); |
| 407 | annotationBuilder.set(DELEGATE, TRUE); |
| 408 | |
| 409 | Path computedPath = computedPathSet.iterator().next(); |
| 410 | LabelStack labelStack = null; |
| 411 | |
| 412 | if (lspType == SR_WITHOUT_SIGNALLING) { |
| 413 | labelStack = srTeHandler.computeLabelStack(computedPath); |
| 414 | // Failed to form a label stack. |
| 415 | if (labelStack == null) { |
| 416 | pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType)); |
| 417 | return false; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if (lspType != WITH_SIGNALLING) { |
| 422 | /* |
| 423 | * Local LSP id which is assigned by RSVP for RSVP signalled LSPs, will be assigned by |
| 424 | * PCE for non-RSVP signalled LSPs. |
| 425 | */ |
| 426 | annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(getNextLocalLspId())); |
| 427 | } |
| 428 | |
| 429 | // For SR-TE tunnels, call SR manager for label stack and put it inside tunnel. |
| 430 | Tunnel tunnel = new DefaultTunnel(null, srcEndPoint, dstEndPoint, MPLS, INIT, null, null, |
| 431 | TunnelName.tunnelName(tunnelName), computedPath, |
| 432 | labelStack, annotationBuilder.build()); |
| 433 | |
| 434 | // Allocate bandwidth. |
| 435 | TunnelConsumerId consumerId = null; |
| 436 | if (bwConstraintValue != 0) { |
| 437 | consumerId = reserveBandwidth(computedPath, bwConstraintValue, null); |
| 438 | if (consumerId == null) { |
| 439 | pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType)); |
| 440 | return false; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | TunnelId tunnelId = tunnelService.setupTunnel(appId, src, tunnel, computedPath); |
| 445 | if (tunnelId == null) { |
| 446 | pceStore.addFailedPathInfo(new PcePathInfo(src, dst, tunnelName, constraints, lspType)); |
| 447 | if (consumerId != null) { |
| 448 | resourceService.release(consumerId); |
| 449 | } |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | if (consumerId != null) { |
| 454 | // Store tunnel consumer id in LSP-Label store. |
| 455 | PceccTunnelInfo pceccTunnelInfo = new PceccTunnelInfo(null, consumerId); |
| 456 | pceStore.addTunnelInfo(tunnelId, pceccTunnelInfo); |
| 457 | } |
| 458 | return true; |
| 459 | } |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 460 | |
| 461 | @Override |
| 462 | public boolean updatePath(TunnelId tunnelId, List<Constraint> constraints) { |
| 463 | checkNotNull(tunnelId); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 464 | Set<Path> computedPathSet = null; |
| 465 | Tunnel tunnel = tunnelService.queryTunnel(tunnelId); |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 466 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 467 | if (tunnel == null) { |
| 468 | return false; |
| 469 | } |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 470 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 471 | if (tunnel.type() != MPLS || FALSE.equalsIgnoreCase(tunnel.annotations().value(DELEGATE))) { |
| 472 | // Only delegated LSPs can be updated. |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | List<Link> links = tunnel.path().links(); |
| 477 | String lspSigType = tunnel.annotations().value(LSP_SIG_TYPE); |
| 478 | double bwConstraintValue = 0; |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 479 | String costType = null; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 480 | SharedBandwidthConstraint shBwConstraint = null; |
| 481 | BandwidthConstraint bwConstraint = null; |
| 482 | CostConstraint costConstraint = null; |
| 483 | |
| 484 | if (constraints != null) { |
| 485 | // Call path computation in shared bandwidth mode. |
| 486 | Iterator<Constraint> iterator = constraints.iterator(); |
| 487 | while (iterator.hasNext()) { |
| 488 | Constraint constraint = iterator.next(); |
| 489 | if (constraint instanceof BandwidthConstraint) { |
| 490 | bwConstraint = (BandwidthConstraint) constraint; |
| 491 | bwConstraintValue = bwConstraint.bandwidth().bps(); |
| 492 | } else if (constraint instanceof CostConstraint) { |
| 493 | costConstraint = (CostConstraint) constraint; |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 494 | costType = costConstraint.type().name(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | // Remove and keep the cost constraint at the end of the list of constraints. |
| 499 | if (costConstraint != null) { |
| 500 | constraints.remove(costConstraint); |
| 501 | } |
| 502 | |
| 503 | Bandwidth existingBwValue = null; |
| 504 | String existingBwAnnotation = tunnel.annotations().value(BANDWIDTH); |
| 505 | if (existingBwAnnotation != null) { |
| 506 | existingBwValue = Bandwidth.bps(Double.parseDouble(existingBwAnnotation)); |
| 507 | |
| 508 | /* |
| 509 | * The computation is a shared bandwidth constraint based, so need to remove bandwidth constraint which |
| 510 | * has been utilized to create shared bandwidth constraint. |
| 511 | */ |
| 512 | if (bwConstraint != null) { |
| 513 | constraints.remove(bwConstraint); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (existingBwValue != null) { |
| 518 | shBwConstraint = new SharedBandwidthConstraint(links, existingBwValue, bwConstraint.bandwidth()); |
| 519 | constraints.add(shBwConstraint); |
| 520 | } |
| 521 | } else { |
| 522 | constraints = new LinkedList<>(); |
| 523 | } |
| 524 | |
| 525 | constraints.add(CapabilityConstraint.of(CapabilityType.valueOf(lspSigType))); |
| 526 | if (costConstraint != null) { |
| 527 | constraints.add(costConstraint); |
| 528 | } |
| 529 | |
| 530 | computedPathSet = computePath(links.get(0).src().deviceId(), links.get(links.size() - 1).dst().deviceId(), |
| 531 | constraints); |
| 532 | |
| 533 | // NO-PATH |
| 534 | if (computedPathSet.isEmpty()) { |
| 535 | return false; |
| 536 | } |
| 537 | |
| 538 | Builder annotationBuilder = DefaultAnnotations.builder(); |
| 539 | annotationBuilder.set(BANDWIDTH, String.valueOf(bwConstraintValue)); |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 540 | if (costType != null) { |
| 541 | annotationBuilder.set(COST_TYPE, costType); |
| 542 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 543 | annotationBuilder.set(LSP_SIG_TYPE, lspSigType); |
| 544 | annotationBuilder.set(PCE_INIT, TRUE); |
| 545 | annotationBuilder.set(DELEGATE, TRUE); |
| 546 | annotationBuilder.set(PLSP_ID, tunnel.annotations().value(PLSP_ID)); |
| 547 | annotationBuilder.set(PCC_TUNNEL_ID, tunnel.annotations().value(PCC_TUNNEL_ID)); |
| 548 | |
| 549 | Path computedPath = computedPathSet.iterator().next(); |
| 550 | LabelStack labelStack = null; |
| 551 | TunnelConsumerId consumerId = null; |
| 552 | LspType lspType = LspType.valueOf(lspSigType); |
| 553 | long localLspId = 0; |
| 554 | if (lspType != WITH_SIGNALLING) { |
| 555 | /* |
| 556 | * Local LSP id which is assigned by RSVP for RSVP signalled LSPs, will be assigned by |
| 557 | * PCE for non-RSVP signalled LSPs. |
| 558 | */ |
| 559 | localLspId = getNextLocalLspId(); |
| 560 | annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(localLspId)); |
| 561 | |
| 562 | if (lspType == SR_WITHOUT_SIGNALLING) { |
| 563 | labelStack = srTeHandler.computeLabelStack(computedPath); |
| 564 | // Failed to form a label stack. |
| 565 | if (labelStack == null) { |
| 566 | return false; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | Tunnel updatedTunnel = new DefaultTunnel(null, tunnel.src(), tunnel.dst(), MPLS, INIT, null, null, |
| 572 | tunnel.tunnelName(), computedPath, |
| 573 | labelStack, annotationBuilder.build()); |
| 574 | |
| 575 | // Allocate shared bandwidth. |
| 576 | if (bwConstraintValue != 0) { |
| 577 | consumerId = reserveBandwidth(computedPath, bwConstraintValue, shBwConstraint); |
| 578 | if (consumerId == null) { |
| 579 | return false; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | TunnelId updatedTunnelId = tunnelService.setupTunnel(appId, links.get(0).src().deviceId(), updatedTunnel, |
| 584 | computedPath); |
| 585 | |
| 586 | if (updatedTunnelId == null) { |
| 587 | if (consumerId != null) { |
| 588 | resourceService.release(consumerId); |
| 589 | } |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | if (consumerId != null) { |
| 594 | // Store tunnel consumer id in LSP-Label store. |
| 595 | PceccTunnelInfo pceccTunnelInfo = new PceccTunnelInfo(null, consumerId); |
| 596 | pceStore.addTunnelInfo(updatedTunnelId, pceccTunnelInfo); |
| 597 | } |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | |
| 601 | @Override |
| 602 | public boolean releasePath(TunnelId tunnelId) { |
| 603 | checkNotNull(tunnelId); |
| 604 | // 1. Query Tunnel from Tunnel manager. |
| 605 | Tunnel tunnel = tunnelService.queryTunnel(tunnelId); |
| 606 | |
| 607 | if (tunnel == null) { |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | // 2. Call tunnel service. |
| 612 | return tunnelService.downTunnel(appId, tunnel.tunnelId()); |
| 613 | } |
| 614 | |
| 615 | @Override |
| 616 | public Iterable<Tunnel> queryAllPath() { |
| 617 | return tunnelService.queryTunnel(MPLS); |
| 618 | } |
| 619 | |
| 620 | @Override |
| 621 | public Tunnel queryPath(TunnelId tunnelId) { |
| 622 | return tunnelService.queryTunnel(tunnelId); |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Returns the next local LSP identifier to be used either by getting from |
| 627 | * freed list if available otherwise generating a new one. |
| 628 | * |
| 629 | * @return value of local LSP identifier |
| 630 | */ |
| 631 | private short getNextLocalLspId() { |
| 632 | // If there is any free id use it. Otherwise generate new id. |
| 633 | if (localLspIdFreeList.isEmpty()) { |
| 634 | return (short) localLspIdIdGen.getNewId(); |
| 635 | } |
| 636 | Iterator<Short> it = localLspIdFreeList.iterator(); |
| 637 | Short value = it.next(); |
| 638 | localLspIdFreeList.remove(value); |
| 639 | return value; |
| 640 | } |
| 641 | |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 642 | protected class TeConstraintBasedLinkWeight implements LinkWeight { |
| 643 | |
| 644 | private final List<Constraint> constraints; |
| 645 | |
| 646 | /** |
| 647 | * Creates a new edge-weight function capable of evaluating links |
| 648 | * on the basis of the specified constraints. |
| 649 | * |
| 650 | * @param constraints path constraints |
| 651 | */ |
| 652 | public TeConstraintBasedLinkWeight(List<Constraint> constraints) { |
| 653 | if (constraints == null) { |
| 654 | this.constraints = Collections.emptyList(); |
| 655 | } else { |
| 656 | this.constraints = ImmutableList.copyOf(constraints); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | @Override |
| 661 | public double weight(TopologyEdge edge) { |
| 662 | if (!constraints.iterator().hasNext()) { |
| 663 | //Takes default cost/hopcount as 1 if no constraints specified |
| 664 | return 1.0; |
| 665 | } |
| 666 | |
| 667 | Iterator<Constraint> it = constraints.iterator(); |
| 668 | double cost = 1; |
| 669 | |
| 670 | //If any constraint fails return -1 also value of cost returned from cost constraint can't be negative |
| 671 | while (it.hasNext() && cost > 0) { |
| 672 | Constraint constraint = it.next(); |
| 673 | if (constraint instanceof CapabilityConstraint) { |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 674 | cost = ((CapabilityConstraint) constraint).isValidLink(edge.link(), deviceService, |
| 675 | netCfgService) ? 1 : -1; |
Priyanka B | b696358 | 2016-05-20 20:21:20 +0530 | [diff] [blame] | 676 | } else { |
| 677 | cost = constraint.cost(edge.link(), resourceService::isAvailable); |
| 678 | } |
| 679 | } |
| 680 | return cost; |
| 681 | } |
| 682 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 683 | |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 684 | //TODO: annotations used for temporarily later projection/network config will be used |
| 685 | private class InternalTopologyListener implements TopologyListener { |
| 686 | @Override |
| 687 | public void event(TopologyEvent event) { |
| 688 | event.reasons().forEach(e -> { |
| 689 | //If event type is link removed, get the impacted tunnel |
| 690 | if (e instanceof LinkEvent) { |
| 691 | LinkEvent linkEvent = (LinkEvent) e; |
| 692 | if (linkEvent.type() == LinkEvent.Type.LINK_REMOVED) { |
| 693 | tunnelService.queryTunnel(MPLS).forEach(t -> { |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 694 | if (t.path().links().contains((e.subject()))) { |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 695 | // Check whether this ONOS instance is master for ingress device if yes, |
| 696 | // recompute and send update |
| 697 | checkForMasterAndUpdateTunnel(t.path().src().deviceId(), t); |
| 698 | } |
| 699 | }); |
| 700 | } |
| 701 | } |
| 702 | }); |
| 703 | } |
| 704 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 705 | |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 706 | private boolean checkForMasterAndUpdateTunnel(DeviceId src, Tunnel tunnel) { |
| 707 | /** |
| 708 | * Master of ingress node will recompute and also delegation flag must be set. |
| 709 | */ |
| 710 | if (mastershipService.isLocalMaster(src) |
| 711 | && Boolean.valueOf(tunnel.annotations().value(DELEGATE)) != null) { |
| 712 | LinkedList<Constraint> constraintList = new LinkedList<>(); |
| 713 | |
| 714 | if (tunnel.annotations().value(BANDWIDTH) != null) { |
| 715 | //Requested bandwidth will be same as previous allocated bandwidth for the tunnel |
| 716 | BandwidthConstraint localConst = new BandwidthConstraint(Bandwidth.bps(Double.parseDouble(tunnel |
| 717 | .annotations().value(BANDWIDTH)))); |
| 718 | constraintList.add(localConst); |
| 719 | } |
| 720 | if (tunnel.annotations().value(COST_TYPE) != null) { |
| 721 | constraintList.add(CostConstraint.of(CostConstraint.Type.valueOf(tunnel.annotations().value( |
| 722 | COST_TYPE)))); |
| 723 | } |
Priyanka B | 9fa4ed3 | 2016-05-27 11:59:24 +0530 | [diff] [blame] | 724 | |
| 725 | /* |
| 726 | * If tunnel was UP after recomputation failed then store failed path in PCE store send PCIntiate(remove) |
| 727 | * and If tunnel is failed and computation fails nothing to do because tunnel status will be same[Failed] |
| 728 | */ |
| 729 | if (!updatePath(tunnel.tunnelId(), constraintList) && !tunnel.state().equals(Tunnel.State.FAILED)) { |
Priyanka B | 3f92c5a | 2016-05-27 10:14:16 +0530 | [diff] [blame] | 730 | // If updation fails store in PCE store as failed path |
| 731 | // then PCInitiate (Remove) |
| 732 | pceStore.addFailedPathInfo(new PcePathInfo(tunnel.path().src().deviceId(), tunnel |
| 733 | .path().dst().deviceId(), tunnel.tunnelName().value(), constraintList, |
| 734 | LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)))); |
| 735 | //Release that tunnel calling PCInitiate |
| 736 | releasePath(tunnel.tunnelId()); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | // Allocates the bandwidth locally for PCECC tunnels. |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 744 | private TunnelConsumerId reserveBandwidth(Path computedPath, double bandwidthConstraint, |
| 745 | SharedBandwidthConstraint shBwConstraint) { |
| 746 | checkNotNull(computedPath); |
| 747 | checkNotNull(bandwidthConstraint); |
| 748 | Resource resource = null; |
| 749 | double bwToAllocate = 0; |
| 750 | |
| 751 | TunnelConsumerId consumer = TunnelConsumerId.valueOf(tunnelConsumerIdGen.getNewId()); |
| 752 | |
| 753 | /** |
| 754 | * Shared bandwidth sub-case : Lesser bandwidth required than original - |
| 755 | * No reservation required. |
| 756 | */ |
| 757 | Double additionalBwValue = null; |
| 758 | if (shBwConstraint != null) { |
| 759 | additionalBwValue = ((bandwidthConstraint - shBwConstraint.sharedBwValue().bps()) <= 0) ? null |
| 760 | : (bandwidthConstraint - shBwConstraint.sharedBwValue().bps()); |
| 761 | } |
| 762 | |
| 763 | Optional<ResourceAllocation> resAlloc = null; |
| 764 | for (Link link : computedPath.links()) { |
| 765 | bwToAllocate = 0; |
| 766 | if ((shBwConstraint != null) && (shBwConstraint.links().contains(link))) { |
| 767 | if (additionalBwValue != null) { |
| 768 | bwToAllocate = bandwidthConstraint - additionalBwValue; |
| 769 | } |
| 770 | } else { |
| 771 | bwToAllocate = bandwidthConstraint; |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * In shared bandwidth cases, where new BW is lesser than old BW, it |
| 776 | * is not required to allocate anything. |
| 777 | */ |
| 778 | if (bwToAllocate != 0) { |
| 779 | resource = Resources.continuous(link.src().deviceId(), link.src().port(), Bandwidth.class) |
| 780 | .resource(bwToAllocate); |
| 781 | resAlloc = resourceService.allocate(consumer, resource); |
| 782 | |
| 783 | // If allocation for any link fails, then release the partially allocated bandwidth. |
| 784 | if (!resAlloc.isPresent()) { |
| 785 | resourceService.release(consumer); |
| 786 | return null; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * Note: Storing of tunnel consumer id is done by caller of bandwidth reservation function. So deleting tunnel |
| 793 | * consumer id should be done by caller of bandwidth releasing function. This will prevent ambiguities related |
| 794 | * to who is supposed to store/delete. |
| 795 | */ |
| 796 | return consumer; |
| 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Deallocates the bandwidth which is reserved locally for PCECC tunnels. |
| 801 | */ |
| 802 | private void releaseBandwidth(Tunnel tunnel) { |
| 803 | // Between same source and destination, search the tunnel with same symbolic path name. |
| 804 | Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnel.src(), tunnel.dst()); |
| 805 | Tunnel newTunnel = null; |
| 806 | for (Tunnel tunnelObj : tunnelQueryResult) { |
| 807 | if (tunnel.tunnelName().value().equals(tunnelObj.tunnelName().value())) { |
| 808 | newTunnel = tunnelObj; |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | // Even if one link is shared, the bandwidth release should happen based on shared mechanism. |
| 814 | boolean isLinkShared = false; |
| 815 | if (newTunnel != null) { |
| 816 | for (Link link : tunnel.path().links()) { |
| 817 | if (newTunnel.path().links().contains(link)) { |
| 818 | isLinkShared = true; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | if (isLinkShared) { |
| 825 | releaseSharedBandwidth(newTunnel, tunnel); |
| 826 | return; |
| 827 | } |
| 828 | |
| 829 | resourceService.release(pceStore.getTunnelInfo(tunnel.tunnelId()).tunnelConsumerId()); |
| 830 | return; |
| 831 | |
| 832 | /* |
| 833 | * Note: Storing of tunnel consumer id is done by caller of bandwidth reservation function. So deleting tunnel |
| 834 | * consumer id should be done by caller of bandwidth releasing function. This will prevent ambiguities related |
| 835 | * to who is supposed to store/delete. |
| 836 | */ |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Re-allocates the bandwidth for the tunnel for which the bandwidth was |
| 841 | * allocated in shared mode initially. |
| 842 | */ |
| 843 | private synchronized void releaseSharedBandwidth(Tunnel newTunnel, Tunnel oldTunnel) { |
| 844 | // 1. Release old tunnel's bandwidth. |
| 845 | resourceService.release(pceStore.getTunnelInfo(oldTunnel.tunnelId()).tunnelConsumerId()); |
| 846 | |
| 847 | // 2. Release new tunnel's bandwidth |
| 848 | ResourceConsumer consumer = pceStore.getTunnelInfo(newTunnel.tunnelId()).tunnelConsumerId(); |
| 849 | resourceService.release(consumer); |
| 850 | |
| 851 | // 3. Allocate new tunnel's complete bandwidth. |
| 852 | double bandwidth = Double.parseDouble(newTunnel.annotations().value(BANDWIDTH)); |
| 853 | Resource resource; |
| 854 | |
| 855 | for (Link link : newTunnel.path().links()) { |
| 856 | resource = Resources.continuous(link.src().deviceId(), link.src().port(), Bandwidth.class) |
| 857 | .resource(bandwidth); |
| 858 | resourceService.allocate(consumer, resource); // Reusing new tunnel's TunnelConsumerId intentionally. |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 859 | |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Allocates node label to specific device. |
| 865 | * |
| 866 | * @param specificDevice device to which node label needs to be allocated |
| 867 | */ |
| 868 | public void allocateNodeLabel(Device specificDevice) { |
| 869 | checkNotNull(specificDevice, DEVICE_NULL); |
| 870 | |
| 871 | DeviceId deviceId = specificDevice.id(); |
| 872 | |
| 873 | // Retrieve lsrId of a specific device |
| 874 | if (specificDevice.annotations() == null) { |
| 875 | log.debug("Device {} does not have annotations.", specificDevice.toString()); |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | String lsrId = specificDevice.annotations().value(LSRID); |
| 880 | if (lsrId == null) { |
| 881 | log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString()); |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | // Get capability config from netconfig |
| 886 | DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class); |
| 887 | if (cfg == null) { |
| 888 | log.error("Unable to find corresponding capability for a lsrd {} from NetConfig.", lsrId); |
| 889 | // Save info. When PCEP session is comes up then allocate node-label |
| 890 | lsrIdDeviceIdMap.put(lsrId, specificDevice.id()); |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | // Check whether device has SR-TE Capability |
| 895 | if (cfg.labelStackCap()) { |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 896 | srTeHandler.allocateNodeLabel(deviceId, lsrId); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * Releases node label of a specific device. |
| 902 | * |
| 903 | * @param specificDevice this device label and lsr-id information will be |
| 904 | * released in other existing devices |
| 905 | */ |
| 906 | public void releaseNodeLabel(Device specificDevice) { |
| 907 | checkNotNull(specificDevice, DEVICE_NULL); |
| 908 | |
| 909 | DeviceId deviceId = specificDevice.id(); |
| 910 | |
| 911 | // Retrieve lsrId of a specific device |
| 912 | if (specificDevice.annotations() == null) { |
| 913 | log.debug("Device {} does not have annotations.", specificDevice.toString()); |
| 914 | return; |
| 915 | } |
| 916 | |
| 917 | String lsrId = specificDevice.annotations().value(LSRID); |
| 918 | if (lsrId == null) { |
| 919 | log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString()); |
| 920 | return; |
| 921 | } |
| 922 | |
| 923 | // Get capability config from netconfig |
| 924 | DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class); |
| 925 | if (cfg == null) { |
| 926 | log.error("Unable to find corresponding capabilty for a lsrd {} from NetConfig.", lsrId); |
| 927 | return; |
| 928 | } |
| 929 | |
| 930 | // Check whether device has SR-TE Capability |
| 931 | if (cfg.labelStackCap()) { |
| 932 | if (!srTeHandler.releaseNodeLabel(deviceId, lsrId)) { |
| 933 | log.error("Unable to release node label for a device id {}.", deviceId.toString()); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * Allocates adjacency label for a link. |
| 940 | * |
| 941 | * @param link link |
| 942 | */ |
| 943 | public void allocateAdjacencyLabel(Link link) { |
| 944 | checkNotNull(link, LINK_NULL); |
| 945 | |
| 946 | Device specificDevice = deviceService.getDevice(link.src().deviceId()); |
| 947 | DeviceId deviceId = specificDevice.id(); |
| 948 | |
| 949 | // Retrieve lsrId of a specific device |
| 950 | if (specificDevice.annotations() == null) { |
| 951 | log.debug("Device {} does not have annotations.", specificDevice.toString()); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | String lsrId = specificDevice.annotations().value(LSRID); |
| 956 | if (lsrId == null) { |
| 957 | log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString()); |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | // Get capability config from netconfig |
| 962 | DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class); |
| 963 | if (cfg == null) { |
| 964 | log.error("Unable to find corresponding capabilty for a lsrd {} from NetConfig.", lsrId); |
| 965 | // Save info. When PCEP session comes up then allocate adjacency |
| 966 | // label |
| 967 | if (lsrIdDeviceIdMap.get(lsrId) != null) { |
| 968 | lsrIdDeviceIdMap.put(lsrId, specificDevice.id()); |
| 969 | } |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | // Check whether device has SR-TE Capability |
| 974 | if (cfg.labelStackCap()) { |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 975 | srTeHandler.allocateAdjacencyLabel(link); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | return; |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * Releases allocated adjacency label of a link. |
| 983 | * |
| 984 | * @param link link |
| 985 | */ |
| 986 | public void releaseAdjacencyLabel(Link link) { |
| 987 | checkNotNull(link, LINK_NULL); |
| 988 | |
| 989 | Device specificDevice = deviceService.getDevice(link.src().deviceId()); |
| 990 | DeviceId deviceId = specificDevice.id(); |
| 991 | |
| 992 | // Retrieve lsrId of a specific device |
| 993 | if (specificDevice.annotations() == null) { |
| 994 | log.debug("Device {} does not have annotations.", specificDevice.toString()); |
| 995 | return; |
| 996 | } |
| 997 | |
| 998 | String lsrId = specificDevice.annotations().value(LSRID); |
| 999 | if (lsrId == null) { |
| 1000 | log.debug("Unable to retrieve lsr-id of a device {}.", specificDevice.toString()); |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | // Get capability config from netconfig |
| 1005 | DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class); |
| 1006 | if (cfg == null) { |
| 1007 | log.error("Unable to find corresponding capabilty for a lsrd {} from NetConfig.", lsrId); |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | // Check whether device has SR-TE Capability |
| 1012 | if (cfg.labelStackCap()) { |
| 1013 | if (!srTeHandler.releaseAdjacencyLabel(link)) { |
| 1014 | log.error("Unable to release adjacency labels for a link {}.", link.toString()); |
| 1015 | return; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | return; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * Handle device events. |
| 1024 | */ |
| 1025 | private class InternalDeviceListener implements DeviceListener { |
| 1026 | @Override |
| 1027 | public void event(DeviceEvent event) { |
| 1028 | Device specificDevice = (Device) event.subject(); |
| 1029 | if (specificDevice == null) { |
| 1030 | log.error("Unable to find device from device event."); |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | switch (event.type()) { |
| 1035 | |
| 1036 | case DEVICE_ADDED: |
| 1037 | // Node-label allocation is being done during Label DB Sync. |
| 1038 | // So, when device is detected, no need to do node-label |
| 1039 | // allocation. |
| 1040 | break; |
| 1041 | |
| 1042 | case DEVICE_REMOVED: |
| 1043 | // Release node-label |
| 1044 | if (mastershipService.getLocalRole(specificDevice.id()) == MastershipRole.MASTER) { |
| 1045 | releaseNodeLabel(specificDevice); |
| 1046 | } |
| 1047 | break; |
| 1048 | |
| 1049 | default: |
| 1050 | break; |
| 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * Handle link events. |
| 1057 | */ |
| 1058 | private class InternalLinkListener implements LinkListener { |
| 1059 | @Override |
| 1060 | public void event(LinkEvent event) { |
| 1061 | Link link = (Link) event.subject(); |
| 1062 | |
| 1063 | switch (event.type()) { |
| 1064 | |
| 1065 | case LINK_ADDED: |
| 1066 | // Allocate adjacency label |
| 1067 | if (mastershipService.getLocalRole(link.src().deviceId()) == MastershipRole.MASTER) { |
| 1068 | allocateAdjacencyLabel(link); |
| 1069 | } |
| 1070 | break; |
| 1071 | |
| 1072 | case LINK_REMOVED: |
| 1073 | // Release adjacency label |
| 1074 | if (mastershipService.getLocalRole(link.src().deviceId()) == MastershipRole.MASTER) { |
| 1075 | releaseAdjacencyLabel(link); |
| 1076 | } |
| 1077 | break; |
| 1078 | |
| 1079 | default: |
| 1080 | break; |
| 1081 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | // Listens on tunnel events. |
| 1086 | private class InnerTunnelListener implements TunnelListener { |
| 1087 | @Override |
| 1088 | public void event(TunnelEvent event) { |
| 1089 | // Event gets generated with old tunnel object. |
| 1090 | Tunnel tunnel = event.subject(); |
| 1091 | if (tunnel.type() != MPLS) { |
| 1092 | return; |
| 1093 | } |
| 1094 | |
| 1095 | LspType lspType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)); |
| 1096 | String tunnelBandwidth = tunnel.annotations().value(BANDWIDTH); |
| 1097 | double bwConstraintValue = 0; |
| 1098 | if (tunnelBandwidth != null) { |
| 1099 | bwConstraintValue = Double.parseDouble(tunnelBandwidth); |
| 1100 | } |
| 1101 | |
| 1102 | switch (event.type()) { |
| 1103 | case TUNNEL_ADDED: |
| 1104 | // Allocate bandwidth for non-initiated, delegated LSPs with non-zero bandwidth (learned LSPs). |
| 1105 | String pceInit = tunnel.annotations().value(PCE_INIT); |
| 1106 | if (FALSE.equalsIgnoreCase(pceInit) |
| 1107 | && bwConstraintValue != 0) { |
| 1108 | reserveBandwidth(tunnel.path(), bwConstraintValue, null); |
| 1109 | } |
| 1110 | break; |
| 1111 | |
| 1112 | case TUNNEL_UPDATED: |
| 1113 | // Allocate/send labels for basic PCECC tunnels. |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 1114 | if ((tunnel.state() == ESTABLISHED) && (lspType == WITHOUT_SIGNALLING_AND_WITHOUT_SR) |
| 1115 | && (mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER)) { |
| 1116 | if (!crHandler.allocateLabel(tunnel)) { |
| 1117 | log.error("Unable to allocate labels for a tunnel {}.", tunnel.toString()); |
| 1118 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | if (tunnel.state() == UNSTABLE) { |
| 1122 | /* |
| 1123 | * During LSP DB sync if PCC doesn't report LSP which was PCE initiated, it's state is turned into |
| 1124 | * unstable so that it can be setup again. Add into failed path store so that it can be recomputed |
| 1125 | * and setup while global reoptimization. |
| 1126 | */ |
| 1127 | |
| 1128 | List<Constraint> constraints = new LinkedList<>(); |
| 1129 | String bandwidth = tunnel.annotations().value(BANDWIDTH); |
| 1130 | if (bandwidth != null) { |
| 1131 | constraints.add(new BandwidthConstraint(Bandwidth |
| 1132 | .bps(Double.parseDouble(bandwidth)))); |
| 1133 | } |
| 1134 | |
| 1135 | String costType = tunnel.annotations().value(COST_TYPE); |
| 1136 | if (costType != null) { |
| 1137 | CostConstraint costConstraint = new CostConstraint(CostConstraint.Type.valueOf(costType)); |
| 1138 | constraints.add(costConstraint); |
| 1139 | } |
| 1140 | |
| 1141 | constraints.add(CapabilityConstraint |
| 1142 | .of(CapabilityType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)))); |
| 1143 | |
| 1144 | List<Link> links = tunnel.path().links(); |
| 1145 | pceStore.addFailedPathInfo(new PcePathInfo(links.get(0).src().deviceId(), |
| 1146 | links.get(links.size() - 1).dst().deviceId(), |
| 1147 | tunnel.tunnelName().value(), constraints, lspType)); |
| 1148 | } |
| 1149 | break; |
| 1150 | |
| 1151 | case TUNNEL_REMOVED: |
| 1152 | if (lspType != WITH_SIGNALLING) { |
| 1153 | localLspIdFreeList.add(Short.valueOf(tunnel.annotations().value(LOCAL_LSP_ID))); |
| 1154 | } |
| 1155 | |
| 1156 | // If not zero bandwidth, and delegated (initiated LSPs will also be delegated). |
| 1157 | if (bwConstraintValue != 0) { |
| 1158 | releaseBandwidth(event.subject()); |
| 1159 | |
| 1160 | // Release basic PCECC labels. |
| 1161 | if (lspType == WITHOUT_SIGNALLING_AND_WITHOUT_SR) { |
| 1162 | // Delete stored tunnel consumer id from PCE store (while still retaining label list.) |
| 1163 | PceccTunnelInfo pceccTunnelInfo = pceStore.getTunnelInfo(tunnel.tunnelId()); |
| 1164 | pceccTunnelInfo.tunnelConsumerId(null); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 1165 | if (mastershipService.getLocalRole(tunnel.path().src().deviceId()) == MastershipRole.MASTER) { |
| 1166 | crHandler.releaseLabel(tunnel); |
| 1167 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1168 | } else { |
| 1169 | pceStore.removeTunnelInfo(tunnel.tunnelId()); |
| 1170 | } |
| 1171 | } |
| 1172 | break; |
| 1173 | |
| 1174 | default: |
| 1175 | break; |
| 1176 | |
| 1177 | } |
| 1178 | return; |
| 1179 | } |
| 1180 | } |
| 1181 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 1182 | private class InternalConfigListener implements NetworkConfigListener { |
| 1183 | |
| 1184 | @Override |
| 1185 | public void event(NetworkConfigEvent event) { |
| 1186 | |
| 1187 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) |
| 1188 | && event.configClass().equals(DeviceCapability.class)) { |
| 1189 | |
| 1190 | DeviceId deviceIdLsrId = (DeviceId) event.subject(); |
| 1191 | String lsrId = deviceIdLsrId.toString(); |
| 1192 | DeviceId deviceId = lsrIdDeviceIdMap.get(lsrId); |
| 1193 | if (deviceId == null) { |
| 1194 | log.debug("Unable to find device id for a lsr-id {} from lsr-id and device-id map.", lsrId); |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | DeviceCapability cfg = netCfgService.getConfig(DeviceId.deviceId(lsrId), DeviceCapability.class); |
| 1199 | if (cfg == null) { |
| 1200 | log.error("Unable to find corresponding capabilty for a lsrd {}.", lsrId); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
| 1204 | if (cfg.labelStackCap()) { |
| 1205 | if (mastershipService.getLocalRole(deviceId) == MastershipRole.MASTER) { |
| 1206 | // Allocate node-label |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 1207 | srTeHandler.allocateNodeLabel(deviceId, lsrId); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 1208 | |
| 1209 | // Allocate adjacency label to links which are |
| 1210 | // originated from this specific device id |
| 1211 | Set<Link> links = linkService.getDeviceEgressLinks(deviceId); |
| 1212 | for (Link link : links) { |
| 1213 | if (!srTeHandler.allocateAdjacencyLabel(link)) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | // Remove lsrId info from map |
| 1221 | lsrIdDeviceIdMap.remove(lsrId); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1226 | private boolean syncLabelDb(DeviceId deviceId) { |
| 1227 | checkNotNull(deviceId); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 1228 | |
| 1229 | Device specificDevice = deviceService.getDevice(deviceId); |
| 1230 | if (specificDevice == null) { |
| 1231 | log.error("Unable to find device for specific device id {}.", deviceId.toString()); |
| 1232 | return false; |
| 1233 | } |
| 1234 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 1235 | if (pceStore.getGlobalNodeLabel(deviceId) != null) { |
| 1236 | Map<DeviceId, LabelResourceId> globalNodeLabelMap = pceStore.getGlobalNodeLabels(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1237 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 1238 | for (Entry<DeviceId, LabelResourceId> entry : globalNodeLabelMap.entrySet()) { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1239 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 1240 | // Convert from DeviceId to TunnelEndPoint |
| 1241 | Device srcDevice = deviceService.getDevice(entry.getKey()); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1242 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 1243 | /* |
| 1244 | * If there is a slight difference in timing such that if device subsystem has removed the device but |
| 1245 | * PCE store still has it, just ignore such devices. |
| 1246 | */ |
| 1247 | if (srcDevice == null) { |
| 1248 | continue; |
| 1249 | } |
| 1250 | |
| 1251 | String srcLsrId = srcDevice.annotations().value(LSRID); |
| 1252 | if (srcLsrId == null) { |
| 1253 | continue; |
| 1254 | } |
| 1255 | |
| 1256 | srTeHandler.advertiseNodeLabelRule(deviceId, |
| 1257 | entry.getValue(), |
| 1258 | IpPrefix.valueOf(IpAddress.valueOf(srcLsrId), PREFIX_LENGTH), |
| 1259 | Objective.Operation.ADD, false); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1260 | } |
| 1261 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 1262 | Map<Link, LabelResourceId> adjLabelMap = pceStore.getAdjLabels(); |
| 1263 | for (Entry<Link, LabelResourceId> entry : adjLabelMap.entrySet()) { |
| 1264 | if (entry.getKey().src().deviceId().equals(deviceId)) { |
| 1265 | srTeHandler.installAdjLabelRule(deviceId, |
| 1266 | entry.getValue(), |
| 1267 | entry.getKey().src().port(), |
| 1268 | entry.getKey().dst().port(), |
| 1269 | Objective.Operation.ADD); |
| 1270 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | srTeHandler.advertiseNodeLabelRule(deviceId, |
| 1275 | LabelResourceId.labelResourceId(0), |
| 1276 | IpPrefix.valueOf(END_OF_SYNC_IP_PREFIX), |
| 1277 | Objective.Operation.ADD, true); |
Avantika-Huawei | 3524d85 | 2016-06-04 20:44:13 +0530 | [diff] [blame] | 1278 | |
| 1279 | log.debug("End of label DB sync for device {}", deviceId); |
| 1280 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 1281 | if (mastershipService.getLocalRole(specificDevice.id()) == MastershipRole.MASTER) { |
| 1282 | // Allocate node-label to this specific device. |
| 1283 | allocateNodeLabel(specificDevice); |
| 1284 | |
| 1285 | // Allocate adjacency label |
| 1286 | Set<Link> links = linkService.getDeviceEgressLinks(specificDevice.id()); |
| 1287 | if (links != null) { |
| 1288 | for (Link link : links) { |
| 1289 | allocateAdjacencyLabel(link); |
| 1290 | } |
| 1291 | } |
| 1292 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1293 | |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
| 1297 | // Process the packet received. |
| 1298 | private class PcepPacketProcessor implements PacketProcessor { |
| 1299 | // Process the packet received and in our case initiates the label DB sync. |
| 1300 | @Override |
| 1301 | public void process(PacketContext context) { |
| 1302 | // Stop processing if the packet has been handled, since we |
| 1303 | // can't do any more to it. |
Avantika-Huawei | 3524d85 | 2016-06-04 20:44:13 +0530 | [diff] [blame] | 1304 | log.debug("Received trigger for label DB sync."); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1305 | if (context.isHandled()) { |
| 1306 | return; |
| 1307 | } |
| 1308 | |
| 1309 | InboundPacket pkt = context.inPacket(); |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 1310 | if (pkt == null) { |
| 1311 | return; |
| 1312 | } |
| 1313 | |
| 1314 | Ethernet ethernet = pkt.parsed(); |
| 1315 | if (ethernet == null || ethernet.getEtherType() != Ethernet.TYPE_IPV4) { |
| 1316 | return; |
| 1317 | } |
| 1318 | |
| 1319 | IPv4 ipPacket = (IPv4) ethernet.getPayload(); |
| 1320 | if (ipPacket == null || ipPacket.getProtocol() != IPv4.PROTOCOL_TCP) { |
| 1321 | return; |
| 1322 | } |
| 1323 | |
| 1324 | TCP tcp = (TCP) ipPacket.getPayload(); |
| 1325 | if (tcp == null || tcp.getDestinationPort() != PCEP_PORT) { |
| 1326 | return; |
| 1327 | } |
| 1328 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 1329 | syncLabelDb(pkt.receivedFrom().deviceId()); |
| 1330 | } |
| 1331 | } |
| 1332 | |
Priyanka B | 9fa4ed3 | 2016-05-27 11:59:24 +0530 | [diff] [blame] | 1333 | //Computes path from tunnel store and also path failed to setup. |
| 1334 | private void callForOptimization() { |
| 1335 | //Recompute the LSPs which it was delegated [LSPs stored in PCE store (failed paths)] |
| 1336 | for (PcePathInfo failedPathInfo : pceStore.getFailedPathInfos()) { |
| 1337 | checkForMasterAndSetupPath(failedPathInfo); |
| 1338 | } |
| 1339 | |
| 1340 | //Recompute the LSPs for which it was delegated [LSPs stored in tunnel store] |
| 1341 | tunnelService.queryTunnel(MPLS).forEach(t -> { |
| 1342 | checkForMasterAndUpdateTunnel(t.path().src().deviceId(), t); |
| 1343 | }); |
| 1344 | } |
| 1345 | |
| 1346 | private boolean checkForMasterAndSetupPath(PcePathInfo failedPathInfo) { |
| 1347 | /** |
| 1348 | * Master of ingress node will setup the path failed stored in PCE store. |
| 1349 | */ |
| 1350 | if (mastershipService.isLocalMaster(failedPathInfo.src())) { |
| 1351 | if (setupPath(failedPathInfo.src(), failedPathInfo.dst(), failedPathInfo.name(), |
| 1352 | failedPathInfo.constraints(), failedPathInfo.lspType())) { |
| 1353 | // If computation is success remove that path |
| 1354 | pceStore.removeFailedPathInfo(failedPathInfo); |
| 1355 | return true; |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | return false; |
| 1360 | } |
| 1361 | |
| 1362 | //Timer to call global optimization |
| 1363 | private class GlobalOptimizationTimer implements Runnable { |
| 1364 | |
| 1365 | public GlobalOptimizationTimer() { |
| 1366 | } |
| 1367 | |
| 1368 | @Override |
| 1369 | public void run() { |
| 1370 | callForOptimization(); |
| 1371 | } |
| 1372 | } |
Avantika-Huawei | 73862d4 | 2016-05-12 18:58:06 +0530 | [diff] [blame] | 1373 | } |