Priyanka B | 9bee080 | 2016-04-27 22:06:02 +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.provider.bgpcep.flow.impl; |
| 17 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 29 | import org.onlab.packet.Ip4Address; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.IpPrefix; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 32 | import org.onlab.packet.MplsLabel; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 33 | import org.onosproject.bgp.controller.BgpController; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 35 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 36 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 37 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 38 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 39 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 40 | import org.onosproject.net.ConnectPoint; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 41 | import org.onosproject.net.Device; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 42 | import org.onosproject.net.DeviceId; |
| 43 | import org.onosproject.net.Link; |
| 44 | import org.onosproject.net.Path; |
| 45 | import org.onosproject.net.PortNumber; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 46 | import org.onosproject.net.device.DeviceService; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 47 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 48 | import org.onosproject.net.flow.DefaultFlowEntry; |
| 49 | import org.onosproject.net.flow.FlowEntry; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 50 | import org.onosproject.net.flow.FlowRule; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 51 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 52 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 53 | import org.onosproject.net.flow.FlowRuleProvider; |
| 54 | import org.onosproject.net.flow.FlowRuleProviderRegistry; |
| 55 | import org.onosproject.net.flow.FlowRuleProviderService; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 56 | import org.onosproject.net.flow.TrafficSelector; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 57 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 58 | import org.onosproject.net.flow.criteria.Criterion; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 59 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 60 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
| 61 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
| 62 | import org.onosproject.net.flow.criteria.MplsCriterion; |
| 63 | import org.onosproject.net.flow.criteria.PortCriterion; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 64 | import org.onosproject.net.flow.criteria.TunnelIdCriterion; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 65 | import org.onosproject.net.provider.AbstractProvider; |
| 66 | import org.onosproject.net.provider.ProviderId; |
| 67 | import org.onosproject.net.resource.ResourceService; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 68 | import org.onosproject.pcep.controller.PccId; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 69 | import org.onosproject.pcep.controller.PcepClient; |
| 70 | import org.onosproject.pcep.controller.PcepClientController; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 71 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 72 | import org.onosproject.pcepio.protocol.PcepEroObject; |
| 73 | import org.onosproject.pcepio.protocol.PcepFecObjectIPv4; |
| 74 | import org.onosproject.pcepio.protocol.PcepFecObjectIPv4Adjacency; |
| 75 | import org.onosproject.pcepio.protocol.PcepLabelObject; |
| 76 | import org.onosproject.pcepio.protocol.PcepLabelUpdate; |
| 77 | import org.onosproject.pcepio.protocol.PcepLabelUpdateMsg; |
| 78 | import org.onosproject.pcepio.protocol.PcepLspObject; |
| 79 | import org.onosproject.pcepio.protocol.PcepMsgPath; |
| 80 | import org.onosproject.pcepio.protocol.PcepSrpObject; |
| 81 | import org.onosproject.pcepio.protocol.PcepUpdateMsg; |
| 82 | import org.onosproject.pcepio.protocol.PcepUpdateRequest; |
| 83 | import org.onosproject.pcepio.types.IPv4SubObject; |
| 84 | import org.onosproject.pcepio.types.NexthopIPv4addressTlv; |
| 85 | import org.onosproject.pcepio.types.PcepLabelDownload; |
| 86 | import org.onosproject.pcepio.types.PcepLabelMap; |
| 87 | import org.onosproject.pcepio.types.PcepValueType; |
| 88 | import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 89 | import org.onosproject.pcep.controller.SrpIdGenerators; |
| 90 | import org.onosproject.pcep.controller.PcepAnnotationKeys; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 91 | import org.osgi.service.component.ComponentContext; |
| 92 | import org.slf4j.Logger; |
| 93 | |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 94 | import static org.onosproject.pcep.controller.PcepSyncStatus.IN_SYNC; |
| 95 | import static org.onosproject.pcep.controller.PcepSyncStatus.SYNCED; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 96 | import static org.onosproject.net.flow.criteria.Criterion.Type.EXTENSION; |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 97 | import static com.google.common.base.Preconditions.checkNotNull; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 98 | import static org.slf4j.LoggerFactory.getLogger; |
| 99 | |
| 100 | /** |
| 101 | * Implementation of BGP-PCEP flow provider. |
| 102 | */ |
| 103 | @Component(immediate = true) |
| 104 | public class BgpcepFlowRuleProvider extends AbstractProvider |
| 105 | implements FlowRuleProvider { |
| 106 | |
| 107 | private final Logger log = getLogger(getClass()); |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected FlowRuleProviderRegistry providerRegistry; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 113 | protected BgpController bgpController; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected PcepClientController pcepController; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | protected ResourceService resourceService; |
| 120 | |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected TunnelService tunnelService; |
| 123 | |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected DeviceService deviceService; |
| 126 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 127 | private FlowRuleProviderService providerService; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 128 | private PcepLabelObject labelObj; |
| 129 | public static final int OUT_LABEL_TYPE = 0; |
| 130 | public static final int IN_LABEL_TYPE = 1; |
| 131 | public static final long IDENTIFIER_SET = 0x100000000L; |
| 132 | public static final long SET = 0xFFFFFFFFL; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 133 | private static final String LSRID = "lsrId"; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 134 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 135 | private enum PcepFlowType { |
| 136 | ADD, |
| 137 | MODIFY, |
| 138 | REMOVE |
| 139 | } |
| 140 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 141 | /** |
| 142 | * Creates a BgpFlow host provider. |
| 143 | */ |
| 144 | public BgpcepFlowRuleProvider() { |
| 145 | super(new ProviderId("l3", "org.onosproject.provider.bgpcep")); |
| 146 | } |
| 147 | |
| 148 | @Activate |
| 149 | public void activate(ComponentContext context) { |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 150 | providerService = providerRegistry.register(this); |
| 151 | log.info("Started"); |
| 152 | } |
| 153 | |
| 154 | @Deactivate |
| 155 | public void deactivate(ComponentContext context) { |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 156 | providerRegistry.unregister(this); |
| 157 | providerService = null; |
| 158 | log.info("Stopped"); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void applyFlowRule(FlowRule... flowRules) { |
| 163 | for (FlowRule flowRule : flowRules) { |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 164 | processRule(flowRule, PcepFlowType.ADD); |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 168 | @Override |
| 169 | public void removeFlowRule(FlowRule... flowRules) { |
| 170 | for (FlowRule flowRule : flowRules) { |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 171 | processRule(flowRule, PcepFlowType.REMOVE); |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 172 | } |
| 173 | } |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 174 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 175 | private void processRule(FlowRule flowRule, PcepFlowType type) { |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 176 | MplsLabel mplsLabel = null; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 177 | IpPrefix ip4PrefixSrc = null; |
| 178 | IpPrefix ip4PrefixDst = null; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 179 | PortNumber port = null; |
| 180 | TunnelId tunnelId = null; |
| 181 | long labelType = 0; |
| 182 | boolean bottomOfStack = false; |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 183 | |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 184 | TrafficSelector selector = flowRule.selector(); |
| 185 | for (Criterion c : selector.criteria()) { |
| 186 | switch (c.type()) { |
| 187 | case MPLS_LABEL: |
| 188 | MplsCriterion lc = (MplsCriterion) c; |
| 189 | mplsLabel = lc.label(); |
| 190 | break; |
| 191 | case IPV4_SRC: |
| 192 | IPCriterion ipCriterion = (IPCriterion) c; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 193 | ip4PrefixSrc = ipCriterion.ip().getIp4Prefix(); |
| 194 | break; |
| 195 | case IPV4_DST: |
| 196 | ipCriterion = (IPCriterion) c; |
| 197 | ip4PrefixDst = ipCriterion.ip().getIp4Prefix(); |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 198 | break; |
| 199 | case IN_PORT: |
| 200 | PortCriterion inPort = (PortCriterion) c; |
| 201 | port = inPort.port(); |
| 202 | break; |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 203 | case TUNNEL_ID: |
| 204 | TunnelIdCriterion tc = (TunnelIdCriterion) c; |
| 205 | tunnelId = TunnelId.valueOf(String.valueOf(tc.tunnelId())); |
| 206 | break; |
| 207 | case METADATA: |
| 208 | MetadataCriterion metadata = (MetadataCriterion) c; |
| 209 | labelType = metadata.metadata(); |
| 210 | break; |
| 211 | case MPLS_BOS: |
| 212 | MplsBosCriterion mplsBos = (MplsBosCriterion) c; |
| 213 | bottomOfStack = mplsBos.mplsBos(); |
| 214 | break; |
| 215 | default: |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | checkNotNull(mplsLabel); |
| 221 | LabelResourceId label = LabelResourceId.labelResourceId(mplsLabel.toInt()); |
| 222 | |
| 223 | try { |
| 224 | if (tunnelId != null) { |
| 225 | pushLocalLabels(flowRule.deviceId(), label, port, tunnelId, bottomOfStack, labelType, type); |
| 226 | return; |
| 227 | } |
| 228 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 229 | if (ip4PrefixDst != null) { |
| 230 | pushAdjacencyLabel(flowRule.deviceId(), label, ip4PrefixSrc, ip4PrefixDst, type); |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 234 | pushGlobalNodeLabel(flowRule.deviceId(), label, ip4PrefixSrc, type, bottomOfStack); |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 235 | |
| 236 | } catch (PcepParseException e) { |
| 237 | log.error("Exception occured while sending label message to PCC {}", e.getMessage()); |
| 238 | } |
| 239 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 240 | } |
| 241 | |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 242 | /** |
| 243 | * Returns PCEP client. |
| 244 | * |
| 245 | * @return PCEP client |
| 246 | */ |
| 247 | private PcepClient getPcepClient(DeviceId deviceId) { |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 248 | Device device = deviceService.getDevice(deviceId); |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 249 | |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 250 | // In future projections instead of annotations will be used to fetch LSR ID. |
| 251 | String lsrId = device.annotations().value(LSRID); |
| 252 | |
| 253 | PcepClient pcc = pcepController.getClient(PccId.pccId(IpAddress.valueOf(lsrId))); |
| 254 | return pcc; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | //Pushes node labels to the specified device. |
| 258 | private void pushGlobalNodeLabel(DeviceId deviceId, LabelResourceId labelId, |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 259 | IpPrefix ipPrefix, PcepFlowType type, boolean isBos) throws PcepParseException { |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 260 | |
| 261 | checkNotNull(deviceId); |
| 262 | checkNotNull(labelId); |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 263 | checkNotNull(type); |
| 264 | |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 265 | PcepClient pc = getPcepClient(deviceId); |
| 266 | if (pc == null) { |
| 267 | log.error("PCEP client not found"); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>(); |
| 272 | |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 273 | if (ipPrefix == null) { |
| 274 | // Pushing self node label to device. |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 275 | ipPrefix = IpPrefix.valueOf(pc.getPccId().ipAddress(), 32); |
Avantika-Huawei | a4f66f6 | 2016-05-25 16:52:08 +0530 | [diff] [blame] | 276 | } |
| 277 | |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 278 | PcepFecObjectIPv4 fecObject = pc.factory().buildFecObjectIpv4() |
| 279 | .setNodeID(ipPrefix.address().getIp4Address().toInt()) |
| 280 | .build(); |
| 281 | |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 282 | boolean bSFlag = false; |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 283 | if (pc.labelDbSyncStatus() == IN_SYNC && !isBos) { |
| 284 | // Need to set sync flag in all messages till sync completes. |
| 285 | bSFlag = true; |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag); |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 289 | |
| 290 | //Global NODE-SID as label object |
| 291 | PcepLabelObject labelObject = pc.factory().buildLabelObject() |
| 292 | .setLabel((int) labelId.labelId()) |
| 293 | .build(); |
| 294 | |
| 295 | PcepLabelMap labelMap = new PcepLabelMap(); |
| 296 | labelMap.setFecObject(fecObject); |
| 297 | labelMap.setLabelObject(labelObject); |
| 298 | labelMap.setSrpObject(srpObj); |
| 299 | |
| 300 | labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject() |
| 301 | .setLabelMap(labelMap) |
| 302 | .build()); |
| 303 | |
| 304 | PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg() |
| 305 | .setPcLabelUpdateList(labelUpdateList) |
| 306 | .build(); |
| 307 | |
| 308 | pc.sendMessage(labelMsg); |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 309 | |
| 310 | if (isBos) { |
| 311 | // Sync is completed. |
| 312 | pc.setLabelDbSyncStatus(SYNCED); |
| 313 | } |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 314 | } |
| 315 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 316 | private PcepSrpObject getSrpObject(PcepClient pc, PcepFlowType type, boolean bSFlag) |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 317 | throws PcepParseException { |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 318 | PcepSrpObject srpObj; |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 319 | boolean bRFlag = false; |
| 320 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 321 | if (!type.equals(PcepFlowType.ADD)) { |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 322 | // To cleanup labels, R bit is set |
| 323 | bRFlag = true; |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 324 | } |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 325 | |
| 326 | srpObj = pc.factory().buildSrpObject() |
| 327 | .setRFlag(bRFlag) |
| 328 | .setSFlag(bSFlag) |
| 329 | .setSrpID(SrpIdGenerators.create()) |
| 330 | .build(); |
| 331 | |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 332 | return srpObj; |
| 333 | } |
| 334 | |
| 335 | //Pushes adjacency labels to the specified device. |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 336 | private void pushAdjacencyLabel(DeviceId deviceId, LabelResourceId labelId, IpPrefix ip4PrefixSrc, |
| 337 | IpPrefix ip4PrefixDst, PcepFlowType type) |
| 338 | throws PcepParseException { |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 339 | |
| 340 | checkNotNull(deviceId); |
| 341 | checkNotNull(labelId); |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 342 | checkNotNull(ip4PrefixSrc); |
| 343 | checkNotNull(ip4PrefixDst); |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 344 | checkNotNull(type); |
| 345 | |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 346 | PcepClient pc = getPcepClient(deviceId); |
| 347 | if (pc == null) { |
| 348 | log.error("PCEP client not found"); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>(); |
| 353 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 354 | int srcPortNo = ip4PrefixSrc.address().getIp4Address().toInt(); |
| 355 | int dstPortNo = ip4PrefixDst.address().getIp4Address().toInt(); |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 356 | |
| 357 | PcepFecObjectIPv4Adjacency fecAdjObject = pc.factory().buildFecIpv4Adjacency() |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 358 | .seRemoteIPv4Address(dstPortNo) |
| 359 | .seLocalIPv4Address(srcPortNo) |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 360 | .build(); |
| 361 | |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 362 | boolean bSFlag = false; |
| 363 | if (pc.labelDbSyncStatus() == IN_SYNC) { |
| 364 | // Need to set sync flag in all messages till sync completes. |
| 365 | bSFlag = true; |
| 366 | } |
| 367 | |
| 368 | PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag); |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 369 | |
| 370 | //Adjacency label object |
| 371 | PcepLabelObject labelObject = pc.factory().buildLabelObject() |
| 372 | .setLabel((int) labelId.labelId()) |
| 373 | .build(); |
| 374 | |
| 375 | PcepLabelMap labelMap = new PcepLabelMap(); |
| 376 | labelMap.setFecObject(fecAdjObject); |
| 377 | labelMap.setLabelObject(labelObject); |
| 378 | labelMap.setSrpObject(srpObj); |
| 379 | |
| 380 | labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject() |
| 381 | .setLabelMap(labelMap) |
| 382 | .build()); |
| 383 | |
| 384 | PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg() |
| 385 | .setPcLabelUpdateList(labelUpdateList) |
| 386 | .build(); |
| 387 | |
| 388 | pc.sendMessage(labelMsg); |
| 389 | } |
| 390 | |
| 391 | //Pushes local labels to the device which is specific to path [CR-case]. |
| 392 | private void pushLocalLabels(DeviceId deviceId, LabelResourceId labelId, |
| 393 | PortNumber portNum, TunnelId tunnelId, |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 394 | Boolean isBos, Long labelType, PcepFlowType type) throws PcepParseException { |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 395 | |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 396 | checkNotNull(deviceId); |
| 397 | checkNotNull(labelId); |
| 398 | checkNotNull(portNum); |
| 399 | checkNotNull(tunnelId); |
| 400 | checkNotNull(labelType); |
| 401 | checkNotNull(type); |
| 402 | |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 403 | PcepClient pc = getPcepClient(deviceId); |
| 404 | if (pc == null) { |
| 405 | log.error("PCEP client not found"); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | PcepLspObject lspObj; |
| 410 | LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>(); |
| 411 | LinkedList<PcepLabelObject> labelObjects = new LinkedList<>(); |
| 412 | PcepSrpObject srpObj; |
| 413 | PcepLabelDownload labelDownload = new PcepLabelDownload(); |
| 414 | LinkedList<PcepValueType> optionalTlv = new LinkedList<>(); |
| 415 | |
| 416 | long portNo = portNum.toLong(); |
| 417 | portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo; |
| 418 | |
| 419 | optionalTlv.add(NexthopIPv4addressTlv.of((int) portNo)); |
| 420 | |
| 421 | Tunnel tunnel = tunnelService.queryTunnel(tunnelId); |
| 422 | |
| 423 | PcepLabelObject labelObj = pc.factory().buildLabelObject() |
| 424 | .setOFlag(labelType == OUT_LABEL_TYPE ? true : false) |
| 425 | .setOptionalTlv(optionalTlv) |
| 426 | .setLabel((int) labelId.labelId()) |
| 427 | .build(); |
| 428 | |
| 429 | /** |
| 430 | * Check whether transit node or not. For transit node, label update message should include IN and OUT labels. |
| 431 | * Hence store IN label object and next when out label comes add IN and OUT label objects and encode label |
| 432 | * update message and send to specified client. |
| 433 | */ |
| 434 | if (!deviceId.equals(tunnel.path().src().deviceId()) && !deviceId.equals(tunnel.path().dst().deviceId())) { |
| 435 | //Device is transit node |
| 436 | if (labelType == IN_LABEL_TYPE) { |
| 437 | //Store label object having IN label value |
| 438 | this.labelObj = labelObj; |
| 439 | return; |
| 440 | } |
| 441 | //Add IN label object |
| 442 | labelObjects.add(this.labelObj); |
| 443 | } |
| 444 | |
| 445 | //Add OUT label object in case of transit node |
| 446 | labelObjects.add(labelObj); |
| 447 | |
Avantika-Huawei | 7c14e66 | 2016-05-16 19:13:34 +0530 | [diff] [blame] | 448 | srpObj = getSrpObject(pc, type, false); |
Priyanka B | bd455ca | 2016-05-05 22:13:25 +0530 | [diff] [blame] | 449 | |
| 450 | String lspId = tunnel.annotations().value(PcepAnnotationKeys.PLSP_ID); |
| 451 | String plspId = tunnel.annotations().value(PcepAnnotationKeys.LOCAL_LSP_ID); |
| 452 | String tunnelIdentifier = tunnel.annotations().value(PcepAnnotationKeys.PCC_TUNNEL_ID); |
| 453 | |
| 454 | LinkedList<PcepValueType> tlvs = new LinkedList<>(); |
| 455 | StatefulIPv4LspIdentifiersTlv lspIdTlv = new StatefulIPv4LspIdentifiersTlv(((IpTunnelEndPoint) tunnel.src()) |
| 456 | .ip().getIp4Address().toInt(), Short.valueOf(lspId), Short.valueOf(tunnelIdentifier), 0, |
| 457 | ((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()); |
| 458 | tlvs.add(lspIdTlv); |
| 459 | |
| 460 | lspObj = pc.factory().buildLspObject() |
| 461 | .setRFlag(false) |
| 462 | .setAFlag(true) |
| 463 | .setDFlag(true) |
| 464 | .setPlspId(Integer.valueOf(plspId)) |
| 465 | .setOptionalTlv(tlvs) |
| 466 | .build(); |
| 467 | |
| 468 | labelDownload.setLabelList(labelObjects); |
| 469 | labelDownload.setLspObject(lspObj); |
| 470 | labelDownload.setSrpObject(srpObj); |
| 471 | |
| 472 | labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject() |
| 473 | .setLabelDownload(labelDownload) |
| 474 | .build()); |
| 475 | |
| 476 | PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg() |
| 477 | .setPcLabelUpdateList(labelUpdateList) |
| 478 | .build(); |
| 479 | |
| 480 | pc.sendMessage(labelMsg); |
| 481 | |
| 482 | //If isBos is true, label download is done along the LSP, send PCEP update message. |
| 483 | if (isBos) { |
| 484 | sendPcepUpdateMsg(pc, lspObj, tunnel); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | //Sends PCEP update message. |
| 489 | private void sendPcepUpdateMsg(PcepClient pc, PcepLspObject lspObj, Tunnel tunnel) throws PcepParseException { |
| 490 | LinkedList<PcepUpdateRequest> updateRequestList = new LinkedList<>(); |
| 491 | LinkedList<PcepValueType> subObjects = createEroSubObj(tunnel.path()); |
| 492 | |
| 493 | if (subObjects == null) { |
| 494 | log.error("ERO subjects not present"); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | PcepSrpObject srpObj = pc.factory().buildSrpObject() |
| 499 | .setRFlag(false) |
| 500 | .setSrpID(SrpIdGenerators.create()) |
| 501 | .build(); |
| 502 | |
| 503 | PcepEroObject eroObj = pc.factory().buildEroObject() |
| 504 | .setSubObjects(subObjects) |
| 505 | .build(); |
| 506 | |
| 507 | PcepMsgPath msgPath = pc.factory().buildPcepMsgPath() |
| 508 | .setEroObject(eroObj) |
| 509 | .build(); |
| 510 | |
| 511 | PcepUpdateRequest updateReq = pc.factory().buildPcepUpdateRequest() |
| 512 | .setSrpObject(srpObj) |
| 513 | .setMsgPath(msgPath) |
| 514 | .setLspObject(lspObj) |
| 515 | .build(); |
| 516 | |
| 517 | updateRequestList.add(updateReq); |
| 518 | |
| 519 | //TODO: P = 1 is it P flag in PCEP obj header |
| 520 | PcepUpdateMsg updateMsg = pc.factory().buildUpdateMsg() |
| 521 | .setUpdateRequestList(updateRequestList) |
| 522 | .build(); |
| 523 | |
| 524 | pc.sendMessage(updateMsg); |
| 525 | } |
| 526 | |
| 527 | private LinkedList<PcepValueType> createEroSubObj(Path path) { |
| 528 | LinkedList<PcepValueType> subObjects = new LinkedList<>(); |
| 529 | List<Link> links = path.links(); |
| 530 | ConnectPoint source = null; |
| 531 | ConnectPoint destination = null; |
| 532 | IpAddress ipDstAddress = null; |
| 533 | IpAddress ipSrcAddress = null; |
| 534 | PcepValueType subObj = null; |
| 535 | long portNo; |
| 536 | |
| 537 | for (Link link : links) { |
| 538 | source = link.src(); |
| 539 | if (!(source.equals(destination))) { |
| 540 | //set IPv4SubObject for ERO object |
| 541 | portNo = source.port().toLong(); |
| 542 | portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo; |
| 543 | ipSrcAddress = Ip4Address.valueOf((int) portNo); |
| 544 | subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt()); |
| 545 | subObjects.add(subObj); |
| 546 | } |
| 547 | |
| 548 | destination = link.dst(); |
| 549 | portNo = destination.port().toLong(); |
| 550 | portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo; |
| 551 | ipDstAddress = Ip4Address.valueOf((int) portNo); |
| 552 | subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt()); |
| 553 | subObjects.add(subObj); |
| 554 | } |
| 555 | return subObjects; |
| 556 | } |
| 557 | |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 558 | @Override |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 559 | public void removeRulesById(ApplicationId id, FlowRule... flowRules) { |
| 560 | // TODO |
| 561 | removeFlowRule(flowRules); |
| 562 | } |
| 563 | |
| 564 | @Override |
| 565 | public void executeBatch(FlowRuleBatchOperation batch) { |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 566 | Collection<FlowEntry> flowEntries = new ArrayList<>(); |
| 567 | |
| 568 | for (FlowRuleBatchEntry fbe : batch.getOperations()) { |
| 569 | Criterion criteria = fbe.target().selector().getCriterion(EXTENSION); |
| 570 | |
| 571 | switch (fbe.operator()) { |
| 572 | case ADD: |
| 573 | if (criteria == null) { |
| 574 | processRule(fbe.target(), PcepFlowType.ADD); |
| 575 | flowEntries.add(new DefaultFlowEntry(fbe.target(), FlowEntryState.ADDED, 0, 0, 0)); |
| 576 | } |
| 577 | break; |
| 578 | case REMOVE: |
| 579 | if (criteria == null) { |
| 580 | processRule(fbe.target(), PcepFlowType.REMOVE); |
| 581 | flowEntries.add(new DefaultFlowEntry(fbe.target(), FlowEntryState.REMOVED, 0, 0, 0)); |
| 582 | } |
| 583 | break; |
| 584 | default: |
| 585 | log.error("Unknown flow operation: {}", fbe); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | CompletedBatchOperation status = new CompletedBatchOperation(true, Collections.emptySet(), batch.deviceId()); |
| 590 | providerService.batchOperationCompleted(batch.id(), status); |
| 591 | providerService.pushFlowMetrics(batch.deviceId(), flowEntries); |
Priyanka B | 9bee080 | 2016-04-27 22:06:02 +0530 | [diff] [blame] | 592 | } |
| 593 | } |