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