Avantika-Huawei
Committed by Gerrit Code Review

[ONOS-4164] Flow decode in bgpcep flow provider

Change-Id: Ib64529e37ccff5b392a2ccb24341b5a780408f6c
(cherry picked from commit a4f66f62)
...@@ -26,6 +26,7 @@ import org.apache.felix.scr.annotations.ReferenceCardinality; ...@@ -26,6 +26,7 @@ import org.apache.felix.scr.annotations.ReferenceCardinality;
26 import org.onlab.packet.Ip4Address; 26 import org.onlab.packet.Ip4Address;
27 import org.onlab.packet.IpAddress; 27 import org.onlab.packet.IpAddress;
28 import org.onlab.packet.IpPrefix; 28 import org.onlab.packet.IpPrefix;
29 +import org.onlab.packet.MplsLabel;
29 import org.onosproject.bgp.controller.BgpController; 30 import org.onosproject.bgp.controller.BgpController;
30 import org.onosproject.cfg.ComponentConfigService; 31 import org.onosproject.cfg.ComponentConfigService;
31 import org.onosproject.core.ApplicationId; 32 import org.onosproject.core.ApplicationId;
...@@ -35,20 +36,31 @@ import org.onosproject.incubator.net.tunnel.Tunnel; ...@@ -35,20 +36,31 @@ import org.onosproject.incubator.net.tunnel.Tunnel;
35 import org.onosproject.incubator.net.tunnel.TunnelId; 36 import org.onosproject.incubator.net.tunnel.TunnelId;
36 import org.onosproject.incubator.net.tunnel.TunnelService; 37 import org.onosproject.incubator.net.tunnel.TunnelService;
37 import org.onosproject.net.ConnectPoint; 38 import org.onosproject.net.ConnectPoint;
39 +import org.onosproject.net.Device;
38 import org.onosproject.net.DeviceId; 40 import org.onosproject.net.DeviceId;
39 import org.onosproject.net.Link; 41 import org.onosproject.net.Link;
40 import org.onosproject.net.Path; 42 import org.onosproject.net.Path;
41 import org.onosproject.net.PortNumber; 43 import org.onosproject.net.PortNumber;
44 +import org.onosproject.net.device.DeviceService;
42 import org.onosproject.net.flow.FlowRule; 45 import org.onosproject.net.flow.FlowRule;
43 import org.onosproject.net.flow.FlowRuleBatchOperation; 46 import org.onosproject.net.flow.FlowRuleBatchOperation;
44 import org.onosproject.net.flow.FlowRuleProvider; 47 import org.onosproject.net.flow.FlowRuleProvider;
45 import org.onosproject.net.flow.FlowRuleProviderRegistry; 48 import org.onosproject.net.flow.FlowRuleProviderRegistry;
46 import org.onosproject.net.flow.FlowRuleProviderService; 49 import org.onosproject.net.flow.FlowRuleProviderService;
50 +import org.onosproject.net.flow.TrafficSelector;
47 import org.onosproject.net.flow.criteria.Criterion; 51 import org.onosproject.net.flow.criteria.Criterion;
52 +import org.onosproject.net.flow.criteria.IPCriterion;
53 +import org.onosproject.net.flow.criteria.MetadataCriterion;
54 +import org.onosproject.net.flow.criteria.MplsBosCriterion;
55 +import org.onosproject.net.flow.criteria.MplsCriterion;
56 +import org.onosproject.net.flow.criteria.PortCriterion;
57 +import org.onosproject.net.flow.criteria.TcpPortCriterion;
58 +import org.onosproject.net.flow.criteria.TunnelIdCriterion;
48 import org.onosproject.net.flowobjective.Objective; 59 import org.onosproject.net.flowobjective.Objective;
49 import org.onosproject.net.provider.AbstractProvider; 60 import org.onosproject.net.provider.AbstractProvider;
50 import org.onosproject.net.provider.ProviderId; 61 import org.onosproject.net.provider.ProviderId;
51 import org.onosproject.net.resource.ResourceService; 62 import org.onosproject.net.resource.ResourceService;
63 +import org.onosproject.pcep.controller.PccId;
52 import org.onosproject.pcep.controller.PcepClient; 64 import org.onosproject.pcep.controller.PcepClient;
53 import org.onosproject.pcep.controller.PcepClientController; 65 import org.onosproject.pcep.controller.PcepClientController;
54 import org.onosproject.pcepio.exceptions.PcepParseException; 66 import org.onosproject.pcepio.exceptions.PcepParseException;
...@@ -106,12 +118,16 @@ public class BgpcepFlowRuleProvider extends AbstractProvider ...@@ -106,12 +118,16 @@ public class BgpcepFlowRuleProvider extends AbstractProvider
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected TunnelService tunnelService; 119 protected TunnelService tunnelService;
108 120
121 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 + protected DeviceService deviceService;
123 +
109 private FlowRuleProviderService providerService; 124 private FlowRuleProviderService providerService;
110 private PcepLabelObject labelObj; 125 private PcepLabelObject labelObj;
111 public static final int OUT_LABEL_TYPE = 0; 126 public static final int OUT_LABEL_TYPE = 0;
112 public static final int IN_LABEL_TYPE = 1; 127 public static final int IN_LABEL_TYPE = 1;
113 public static final long IDENTIFIER_SET = 0x100000000L; 128 public static final long IDENTIFIER_SET = 0x100000000L;
114 public static final long SET = 0xFFFFFFFFL; 129 public static final long SET = 0xFFFFFFFFL;
130 + private static final String LSRID = "lsrId";
115 131
116 /** 132 /**
117 * Creates a BgpFlow host provider. 133 * Creates a BgpFlow host provider.
...@@ -138,34 +154,88 @@ public class BgpcepFlowRuleProvider extends AbstractProvider ...@@ -138,34 +154,88 @@ public class BgpcepFlowRuleProvider extends AbstractProvider
138 @Override 154 @Override
139 public void applyFlowRule(FlowRule... flowRules) { 155 public void applyFlowRule(FlowRule... flowRules) {
140 for (FlowRule flowRule : flowRules) { 156 for (FlowRule flowRule : flowRules) {
141 - applyRule(flowRule); 157 + processRule(flowRule, Objective.Operation.ADD);
142 } 158 }
143 } 159 }
144 160
145 - private void applyRule(FlowRule flowRule) { 161 + @Override
146 - flowRule.selector().criteria() 162 + public void removeFlowRule(FlowRule... flowRules) {
147 - .forEach(c -> { 163 + for (FlowRule flowRule : flowRules) {
148 - // If Criterion type is MPLS_LABEL, push labels through PCEP client 164 + processRule(flowRule, Objective.Operation.REMOVE);
149 - if (c.type() == Criterion.Type.MPLS_LABEL) { 165 + }
150 - PcepClient pcc; 166 + }
151 - /** PCC client session is based on LSR ID, get the LSR ID for a specific device to 167 +
152 - push the flows */ 168 + private void processRule(FlowRule flowRule, Objective.Operation type) {
153 - 169 + MplsLabel mplsLabel = null;
154 - //TODO: commented code has dependency with other patch 170 + IpPrefix ip4Prefix = null;
155 - /* Set<TeRouterId> lrsIds = resourceService.getAvailableResourceValues(Resources 171 + PortNumber port = null;
156 - .discrete(flowRule.deviceId()).id(), TeRouterId.class); 172 + TunnelId tunnelId = null;
157 - 173 + long labelType = 0;
158 - lrsIds.forEach(lsrId -> 174 + boolean bottomOfStack = false;
159 - { 175 + int srcPort = 0;
160 - if (pcepController.getClient(PccId.pccId(lsrId)) != null) { 176 + int dstPort = 0;
161 - pcc = pcepController.getClient(PccId.pccId(lsrId)); 177 +
162 - } 178 + TrafficSelector selector = flowRule.selector();
163 - });*/ 179 + for (Criterion c : selector.criteria()) {
164 - // TODO: Build message and send the PCEP label message via PCEP client 180 + switch (c.type()) {
165 - } else { 181 + case MPLS_LABEL:
166 - // TODO: Get the BGP peer based on deviceId and send the message 182 + MplsCriterion lc = (MplsCriterion) c;
167 - } 183 + mplsLabel = lc.label();
168 - }); 184 + break;
185 + case IPV4_SRC:
186 + IPCriterion ipCriterion = (IPCriterion) c;
187 + ip4Prefix = ipCriterion.ip().getIp4Prefix();
188 + break;
189 + case IN_PORT:
190 + PortCriterion inPort = (PortCriterion) c;
191 + port = inPort.port();
192 + break;
193 + case TCP_SRC:
194 + TcpPortCriterion srcTcpPort = (TcpPortCriterion) c;
195 + srcPort = srcTcpPort.tcpPort().toInt();
196 + break;
197 + case TCP_DST:
198 + TcpPortCriterion dstTcpPort = (TcpPortCriterion) c;
199 + dstPort = dstTcpPort.tcpPort().toInt();
200 + break;
201 + case TUNNEL_ID:
202 + TunnelIdCriterion tc = (TunnelIdCriterion) c;
203 + tunnelId = TunnelId.valueOf(String.valueOf(tc.tunnelId()));
204 + break;
205 + case METADATA:
206 + MetadataCriterion metadata = (MetadataCriterion) c;
207 + labelType = metadata.metadata();
208 + break;
209 + case MPLS_BOS:
210 + MplsBosCriterion mplsBos = (MplsBosCriterion) c;
211 + bottomOfStack = mplsBos.mplsBos();
212 + break;
213 + default:
214 + break;
215 + }
216 + }
217 +
218 + checkNotNull(mplsLabel);
219 + LabelResourceId label = LabelResourceId.labelResourceId(mplsLabel.toInt());
220 +
221 + try {
222 + if (tunnelId != null) {
223 + pushLocalLabels(flowRule.deviceId(), label, port, tunnelId, bottomOfStack, labelType, type);
224 + return;
225 + }
226 +
227 + if (srcPort != 0 && dstPort != 0) {
228 + pushAdjacencyLabel(flowRule.deviceId(), label, PortNumber.portNumber(srcPort),
229 + PortNumber.portNumber(dstPort), type);
230 + return;
231 + }
232 +
233 + pushGlobalNodeLabel(flowRule.deviceId(), label, ip4Prefix, type, bottomOfStack);
234 +
235 + } catch (PcepParseException e) {
236 + log.error("Exception occured while sending label message to PCC {}", e.getMessage());
237 + }
238 +
169 } 239 }
170 240
171 /** 241 /**
...@@ -174,19 +244,13 @@ public class BgpcepFlowRuleProvider extends AbstractProvider ...@@ -174,19 +244,13 @@ public class BgpcepFlowRuleProvider extends AbstractProvider
174 * @return PCEP client 244 * @return PCEP client
175 */ 245 */
176 private PcepClient getPcepClient(DeviceId deviceId) { 246 private PcepClient getPcepClient(DeviceId deviceId) {
177 - PcepClient pcc; 247 + Device device = deviceService.getDevice(deviceId);
178 - //TODO: commented code has dependency 248 +
179 - /* Set<TeRouterId> lrsIds = resourceService.getAvailableResourceValues(Resources 249 + // In future projections instead of annotations will be used to fetch LSR ID.
180 - .discrete(deviceId()).id(), TeRouterId.class); 250 + String lsrId = device.annotations().value(LSRID);
181 - 251 +
182 - lrsIds.forEach(lsrId -> 252 + PcepClient pcc = pcepController.getClient(PccId.pccId(IpAddress.valueOf(lsrId)));
183 - { 253 + return pcc;
184 - if (pcepController.getClient(PccId.pccId(lsrId)) != null) {
185 - pcc = pcepController.getClient(PccId.pccId(lsrId));
186 - return pcc
187 - }
188 - });*/
189 - return null;
190 } 254 }
191 255
192 //Pushes node labels to the specified device. 256 //Pushes node labels to the specified device.
...@@ -195,7 +259,6 @@ public class BgpcepFlowRuleProvider extends AbstractProvider ...@@ -195,7 +259,6 @@ public class BgpcepFlowRuleProvider extends AbstractProvider
195 259
196 checkNotNull(deviceId); 260 checkNotNull(deviceId);
197 checkNotNull(labelId); 261 checkNotNull(labelId);
198 - checkNotNull(ipPrefix);
199 checkNotNull(type); 262 checkNotNull(type);
200 263
201 PcepClient pc = getPcepClient(deviceId); 264 PcepClient pc = getPcepClient(deviceId);
...@@ -206,6 +269,11 @@ public class BgpcepFlowRuleProvider extends AbstractProvider ...@@ -206,6 +269,11 @@ public class BgpcepFlowRuleProvider extends AbstractProvider
206 269
207 LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>(); 270 LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>();
208 271
272 + if (ipPrefix == null) {
273 + // Pushing self node label to device.
274 + IpPrefix.valueOf(pc.getPccId().ipAddress(), 32);
275 + }
276 +
209 PcepFecObjectIPv4 fecObject = pc.factory().buildFecObjectIpv4() 277 PcepFecObjectIPv4 fecObject = pc.factory().buildFecObjectIpv4()
210 .setNodeID(ipPrefix.address().getIp4Address().toInt()) 278 .setNodeID(ipPrefix.address().getIp4Address().toInt())
211 .build(); 279 .build();
...@@ -484,39 +552,6 @@ public class BgpcepFlowRuleProvider extends AbstractProvider ...@@ -484,39 +552,6 @@ public class BgpcepFlowRuleProvider extends AbstractProvider
484 } 552 }
485 553
486 @Override 554 @Override
487 - public void removeFlowRule(FlowRule... flowRules) {
488 - for (FlowRule flowRule : flowRules) {
489 - removeRule(flowRule);
490 - }
491 - }
492 -
493 - private void removeRule(FlowRule flowRule) {
494 - flowRule.selector().criteria()
495 - .forEach(c -> {
496 - // If Criterion type is MPLS_LABEL, remove the specified flow rules
497 - if (c.type() == Criterion.Type.MPLS_LABEL) {
498 - PcepClient pcc;
499 - /** PCC client session is based on LSR ID, get the LSR ID for a specific device to
500 - push the flows */
501 -
502 - //TODO: commented code has dependency with other patch
503 - /* Set<TeRouterId> lrsIds = resourceService.getAvailableResourceValues(Resources
504 - .discrete(flowRule.deviceId()).id(), TeRouterId.class);
505 -
506 - lrsIds.forEach(lsrId ->
507 - {
508 - if (pcepController.getClient(PccId.pccId(lsrId)) != null) {
509 - pcc = pcepController.getClient(PccId.pccId(lsrId));
510 - }
511 - });*/
512 - // TODO: Build message and send the PCEP label message via PCEP client
513 - } else {
514 - // TODO: Get the BGP peer based on deviceId and send the message
515 - }
516 - });
517 - }
518 -
519 - @Override
520 public void removeRulesById(ApplicationId id, FlowRule... flowRules) { 555 public void removeRulesById(ApplicationId id, FlowRule... flowRules) {
521 // TODO 556 // TODO
522 removeFlowRule(flowRules); 557 removeFlowRule(flowRules);
......