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