1 package org.opendaylight.controller.forwardingrulesmanager.consumer.impl;
3 import java.util.ArrayList;
4 import java.util.EnumSet;
5 import java.util.HashMap;
8 import java.util.Map.Entry;
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.ConcurrentMap;
12 import java.util.concurrent.Future;
14 import org.opendaylight.controller.clustering.services.CacheConfigException;
15 import org.opendaylight.controller.clustering.services.CacheExistException;
16 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
17 import org.opendaylight.controller.clustering.services.IClusterServices;
18 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
19 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
21 import org.opendaylight.controller.sal.common.util.Rpcs;
22 import org.opendaylight.controller.sal.core.IContainer;
23 import org.opendaylight.controller.sal.utils.GlobalConstants;
24 import org.opendaylight.controller.sal.utils.ServiceHelper;
25 import org.opendaylight.controller.sal.utils.Status;
26 import org.opendaylight.controller.sal.utils.StatusCode;
27 import org.opendaylight.controller.switchmanager.ISwitchManager;
28 import org.opendaylight.controller.switchmanager.Switch;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.Flows;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.Flow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeErrorNotification;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeExperimenterErrorNotification;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowListener;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SwitchFlowRemoved;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
53 import org.opendaylight.yangtools.concepts.Registration;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.binding.NotificationListener;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 public class FlowConsumerImpl implements IForwardingRulesManager {
62 protected static final Logger logger = LoggerFactory.getLogger(FlowConsumerImpl.class);
63 private final FlowEventListener flowEventListener = new FlowEventListener();
64 private Registration<NotificationListener> listener1Reg;
65 private SalFlowService flowService;
66 // private FlowDataListener listener;
67 private FlowDataCommitHandler commitHandler;
68 private static ConcurrentHashMap<FlowKey, Flow> originalSwView;
69 private static ConcurrentMap<FlowKey, Flow> installedSwView;
70 private IClusterContainerServices clusterContainerService = null;
71 private IContainer container;
72 private static final String NAMEREGEX = "^[a-zA-Z0-9]+$";
73 private static ConcurrentMap<Integer, Flow> staticFlows;
74 private static ConcurrentMap<Integer, Integer> staticFlowsOrdinal = new ConcurrentHashMap<Integer, Integer>();
76 * Inactive flow list. This is for the global instance of FRM It will
77 * contain all the flow entries which were installed on the global container
78 * when the first container is created.
80 private static ConcurrentMap<FlowKey, Flow> inactiveFlows;
83 * /* Per node indexing
85 private static ConcurrentMap<Node, List<Flow>> nodeFlows;
86 private boolean inContainerMode; // being used by global instance only
88 public FlowConsumerImpl() {
89 InstanceIdentifier<? extends DataObject> path = InstanceIdentifier.builder(Flows.class).child(Flow.class)
91 flowService = FRMConsumerImpl.getProviderSession().getRpcService(SalFlowService.class);
93 if (null == flowService) {
94 logger.error("Consumer SAL Service is down or NULL. FRM may not function as intended");
95 System.out.println("Consumer SAL Service is down or NULL.");
99 // listener = new FlowDataListener();
102 // FRMConsumerImpl.getDataBrokerService().registerDataChangeListener(path,
104 // logger.error("Failed to listen on flow data modifcation events");
105 // System.out.println("Consumer SAL Service is down or NULL.");
110 listener1Reg = FRMConsumerImpl.getNotificationService().registerNotificationListener(flowEventListener);
112 if (null == listener1Reg) {
113 logger.error("Listener to listen on flow data modifcation events");
114 System.out.println("Consumer SAL Service is down or NULL.");
118 System.out.println("-------------------------------------------------------------------");
120 commitHandler = new FlowDataCommitHandler();
121 FRMConsumerImpl.getDataProviderService().registerCommitHandler(path, commitHandler);
122 clusterContainerService = (IClusterContainerServices) ServiceHelper.getGlobalInstance(
123 IClusterContainerServices.class, this);
124 container = (IContainer) ServiceHelper.getGlobalInstance(IContainer.class, this);
126 * If we are not the first cluster node to come up, do not initialize
127 * the static flow entries ordinal
129 if (staticFlowsOrdinal.size() == 0) {
130 staticFlowsOrdinal.put(0, Integer.valueOf(0));
134 private void allocateCaches() {
136 if (this.clusterContainerService == null) {
137 logger.warn("Un-initialized clusterContainerService, can't create cache");
142 clusterContainerService.createCache("frm.originalSwView",
143 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
144 clusterContainerService.createCache("frm.installedSwView",
145 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
146 clusterContainerService
147 .createCache("frm.staticFlows", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
148 clusterContainerService.createCache("frm.staticFlowsOrdinal",
149 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
150 clusterContainerService.createCache("frm.inactiveFlows",
151 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
152 clusterContainerService.createCache("frm.nodeFlows", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
153 clusterContainerService.createCache("frm.groupFlows", EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
154 } catch (CacheConfigException cce) {
155 logger.error("CacheConfigException");
156 } catch (CacheExistException cce) {
157 logger.error("CacheExistException");
161 private void addFlowTest() {
163 NodeRef nodeOne = createNodeRef("foo:node:1");
164 AddFlowInputBuilder input1 = new AddFlowInputBuilder();
166 input1.setNode(nodeOne);
167 AddFlowInput firstMsg = input1.build();
169 if (null != flowService) {
170 System.out.println(flowService.toString());
172 System.out.println("ConsumerFlowService is NULL");
174 @SuppressWarnings("unused")
175 Future<RpcResult<AddFlowOutput>> result1 = flowService.addFlow(firstMsg);
177 } catch (Exception e) {
178 // TODO Auto-generated catch block
184 * Adds flow to the southbound plugin and our internal database
189 private void addFlow(InstanceIdentifier<?> path, Flow dataObject) {
191 AddFlowInputBuilder input = new AddFlowInputBuilder();
192 List<Instruction> inst = (dataObject).getInstructions().getInstruction();
193 input.setNode((dataObject).getNode());
194 input.setPriority((dataObject).getPriority());
195 input.setMatch((dataObject).getMatch());
196 input.setCookie((dataObject).getCookie());
197 input.setInstructions((dataObject).getInstructions());
198 dataObject.getMatch().getLayer3Match();
199 for (int i = 0; i < inst.size(); i++) {
200 System.out.println("i = " + i + inst.get(i).getInstruction().toString());
201 System.out.println("i = " + i + inst.get(i).toString());
204 System.out.println("Instruction list" + (dataObject).getInstructions().getInstruction().toString());
206 // updating the staticflow cache
207 Integer ordinal = staticFlowsOrdinal.get(0);
208 staticFlowsOrdinal.put(0, ++ordinal);
209 staticFlows.put(ordinal, dataObject);
211 // We send flow to the sounthbound plugin
212 flowService.addFlow(input.build());
213 updateLocalDatabase((NodeFlow) dataObject, true);
217 * Removes flow to the southbound plugin and our internal database
222 private void removeFlow(InstanceIdentifier<?> path, Flow dataObject) {
224 RemoveFlowInputBuilder input = new RemoveFlowInputBuilder();
225 List<Instruction> inst = (dataObject).getInstructions().getInstruction();
226 input.setNode((dataObject).getNode());
227 input.setPriority((dataObject).getPriority());
228 input.setMatch((dataObject).getMatch());
229 input.setCookie((dataObject).getCookie());
230 input.setInstructions((dataObject).getInstructions());
231 dataObject.getMatch().getLayer3Match();
232 for (int i = 0; i < inst.size(); i++) {
233 System.out.println("i = " + i + inst.get(i).getInstruction().toString());
234 System.out.println("i = " + i + inst.get(i).toString());
237 System.out.println("Instruction list" + (dataObject).getInstructions().getInstruction().toString());
239 // updating the staticflow cache
240 Integer ordinal = staticFlowsOrdinal.get(0);
241 staticFlowsOrdinal.put(0, ++ordinal);
242 staticFlows.put(ordinal, dataObject);
244 // We send flow to the sounthbound plugin
245 flowService.removeFlow(input.build());
246 updateLocalDatabase((NodeFlow) dataObject, false);
250 * Update flow to the southbound plugin and our internal database
255 private void updateFlow(InstanceIdentifier<?> path, Flow dataObject) {
257 UpdateFlowInputBuilder input = new UpdateFlowInputBuilder();
258 UpdatedFlowBuilder updatedflowbuilder = new UpdatedFlowBuilder();
259 updatedflowbuilder.fieldsFrom(dataObject);
260 input.setUpdatedFlow(updatedflowbuilder.build());
262 // updating the staticflow cache
263 Integer ordinal = staticFlowsOrdinal.get(0);
264 staticFlowsOrdinal.put(0, ++ordinal);
265 staticFlows.put(ordinal, dataObject);
267 // We send flow to the sounthbound plugin
268 flowService.updateFlow(input.build());
269 updateLocalDatabase((NodeFlow) dataObject, true);
272 @SuppressWarnings("unchecked")
273 private void commitToPlugin(internalTransaction transaction) {
274 for (Entry<InstanceIdentifier<?>, Flow> entry : transaction.additions.entrySet()) {
275 System.out.println("Coming add cc in FlowDatacommitHandler");
276 addFlow(entry.getKey(), entry.getValue());
278 for (@SuppressWarnings("unused")
279 Entry<InstanceIdentifier<?>, Flow> entry : transaction.updates.entrySet()) {
280 System.out.println("Coming update cc in FlowDatacommitHandler");
281 updateFlow(entry.getKey(), entry.getValue());
284 for (Entry<InstanceIdentifier<?>, Flow> entry : transaction.removals.entrySet()) {
285 System.out.println("Coming remove cc in FlowDatacommitHandler");
286 removeFlow(entry.getKey(), entry.getValue());
291 private final class FlowDataCommitHandler implements DataCommitHandler<InstanceIdentifier<?>, DataObject> {
293 @SuppressWarnings("unchecked")
295 public DataCommitTransaction requestCommit(DataModification<InstanceIdentifier<?>, DataObject> modification) {
296 // We should verify transaction
297 System.out.println("Coming in FlowDatacommitHandler");
298 internalTransaction transaction = new internalTransaction(modification);
299 transaction.prepareUpdate();
304 private final class internalTransaction implements DataCommitTransaction<InstanceIdentifier<?>, DataObject> {
306 private final DataModification<InstanceIdentifier<?>, DataObject> modification;
309 public DataModification<InstanceIdentifier<?>, DataObject> getModification() {
313 public internalTransaction(DataModification<InstanceIdentifier<?>, DataObject> modification) {
314 this.modification = modification;
317 Map<InstanceIdentifier<?>, Flow> additions = new HashMap<>();
318 Map<InstanceIdentifier<?>, Flow> updates = new HashMap<>();
319 Map<InstanceIdentifier<?>, Flow> removals = new HashMap<>();
322 * We create a plan which flows will be added, which will be updated and
323 * which will be removed based on our internal state.
326 void prepareUpdate() {
328 Set<Entry<InstanceIdentifier<?>, DataObject>> puts = modification.getUpdatedConfigurationData().entrySet();
329 for (Entry<InstanceIdentifier<?>, DataObject> entry : puts) {
331 // validating the DataObject
333 Status status = validate(container, (NodeFlow) entry);
334 if (!status.isSuccess()) {
335 logger.warn("Invalid Configuration for flow {}. The failure is {}", entry, status.getDescription());
336 String error = "Invalid Configuration (" + status.getDescription() + ")";
341 if (flowEntryExists((NodeFlow) entry)) {
342 String error = "Entry with this name on specified table already exists";
343 logger.warn("Entry with this name on specified table already exists: {}", entry);
347 if (originalSwView.containsKey(entry)) {
348 logger.warn("Operation Rejected: A flow with same match and priority exists on the target node");
349 logger.trace("Aborting to install {}", entry);
352 if (!FRMUtil.validateMatch((NodeFlow) entry)) {
353 logger.error("Not a valid Match");
356 if (!FRMUtil.validateInstructions((NodeFlow) entry)) {
357 logger.error("Not a valid Instruction");
360 if (entry.getValue() instanceof Flow) {
361 Flow flow = (Flow) entry.getValue();
362 preparePutEntry(entry.getKey(), flow);
367 // removals = modification.getRemovedConfigurationData();
368 Set<InstanceIdentifier<?>> removedData = modification.getRemovedConfigurationData();
369 for (InstanceIdentifier<?> removal : removedData) {
370 DataObject value = modification.getOriginalConfigurationData().get(removal);
371 if (value instanceof Flow) {
372 removals.put(removal, (Flow) value);
378 private void preparePutEntry(InstanceIdentifier<?> key, Flow flow) {
379 Flow original = originalSwView.get(key);
380 if (original != null) {
381 // It is update for us
382 System.out.println("Coming update in FlowDatacommitHandler");
383 updates.put(key, flow);
385 // It is addition for us
386 System.out.println("Coming add in FlowDatacommitHandler");
387 additions.put(key, flow);
392 * We are OK to go with execution of plan
396 public RpcResult<Void> finish() throws IllegalStateException {
398 commitToPlugin(this);
399 // We return true if internal transaction is successful.
400 // return Rpcs.getRpcResult(true, null, Collections.emptySet());
401 return Rpcs.getRpcResult(true, null, null);
406 * We should rollback our preparation
410 public RpcResult<Void> rollback() throws IllegalStateException {
411 // NOOP - we did not modified any internal state during
412 // requestCommit phase
413 // return Rpcs.getRpcResult(true, null, Collections.emptySet());
414 return Rpcs.getRpcResult(true, null, null);
418 public Status validate(IContainer container, NodeFlow dataObject) {
420 // container validation
423 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container.getName();
424 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
425 containerName, this);
426 // flow Name validation
427 if (dataObject.getFlowName() == null || dataObject.getFlowName().trim().isEmpty()
428 || !dataObject.getFlowName().matches(NAMEREGEX)) {
429 return new Status(StatusCode.BADREQUEST, "Invalid Flow name");
432 if (dataObject.getNode() == null) {
433 return new Status(StatusCode.BADREQUEST, "Node is null");
436 if (switchManager != null) {
437 for (Switch device : switchManager.getNetworkDevices()) {
438 node = (Node) device.getNode();
439 if (device.getNode().equals(dataObject.getNode())) {
445 return new Status(StatusCode.BADREQUEST, String.format("Node %s not found", node));
448 logger.debug("switchmanager is not set yet");
451 if (dataObject.getPriority() != null) {
452 if (dataObject.getPriority() < 0 || dataObject.getPriority() > 65535) {
453 return new Status(StatusCode.BADREQUEST, String.format("priority %s is not in the range 0 - 65535",
454 dataObject.getPriority()));
458 return new Status(StatusCode.SUCCESS);
461 private boolean flowEntryExists(NodeFlow config) {
462 // Flow name has to be unique on per table id basis
463 for (ConcurrentMap.Entry<FlowKey, Flow> entry : originalSwView.entrySet()) {
464 if (entry.getValue().getFlowName().equals(config.getFlowName())
465 && entry.getValue().getTableId().equals(config.getTableId())) {
473 final class FlowEventListener implements SalFlowListener {
475 List<FlowAdded> addedFlows = new ArrayList<>();
476 List<FlowRemoved> removedFlows = new ArrayList<>();
477 List<FlowUpdated> updatedFlows = new ArrayList<>();
480 public void onFlowAdded(FlowAdded notification) {
481 System.out.println("added flow..........................");
482 addedFlows.add(notification);
486 public void onFlowRemoved(FlowRemoved notification) {
487 removedFlows.add(notification);
491 public void onFlowUpdated(FlowUpdated notification) {
492 updatedFlows.add(notification);
496 public void onSwitchFlowRemoved(SwitchFlowRemoved notification) {
501 public void onNodeErrorNotification(NodeErrorNotification notification) {
502 // TODO Auto-generated method stub
507 public void onNodeExperimenterErrorNotification(NodeExperimenterErrorNotification notification) {
508 // TODO Auto-generated method stub
514 // Commented out DataChangeListene - to be used by Stats
516 // final class FlowDataListener implements DataChangeListener {
517 // private SalFlowService flowService;
519 // public FlowDataListener() {
524 // public void onDataChanged(
525 // DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
526 // System.out.println("Coming in onDataChange..............");
527 // @SuppressWarnings("unchecked")
528 // Collection<DataObject> additions = (Collection<DataObject>)
529 // change.getCreatedConfigurationData();
530 // // we can check for getCreated, getDeleted or getUpdated from DataChange
532 // for (DataObject dataObject : additions) {
533 // if (dataObject instanceof NodeFlow) {
534 // NodeRef nodeOne = createNodeRef("foo:node:1");
535 // // validating the dataObject here
536 // AddFlowInputBuilder input = new AddFlowInputBuilder();
537 // input.setNode(((NodeFlow) dataObject).getNode());
538 // input.setNode(nodeOne);
539 // // input.setPriority(((NodeFlow) dataObject).getPriority());
540 // //input.setMatch(((NodeFlow) dataObject).getMatch());
541 // //input.setFlowTable(((NodeFlow) dataObject).getFlowTable());
542 // //input.setCookie(((NodeFlow) dataObject).getCookie());
543 // //input.setAction(((NodeFlow) dataObject).getAction());
545 // @SuppressWarnings("unused")
546 // Future<RpcResult<java.lang.Void>> result =
547 // flowService.addFlow(input.build());
553 private static void updateLocalDatabase(NodeFlow entry, boolean add) {
555 updateSwViewes(entry, add);
557 updateNodeFlowsDB(entry, add);
562 * Update the node mapped flows database
564 private static void updateSwViewes(NodeFlow entry, boolean add) {
566 FlowConsumerImpl.originalSwView.put((FlowKey) entry, (Flow) entry);
567 installedSwView.put((FlowKey) entry, (Flow) entry);
569 originalSwView.remove(entry);
570 installedSwView.remove(entry);
576 public List<DataObject> get() {
578 List<DataObject> orderedList = new ArrayList<DataObject>();
579 ConcurrentMap<Integer, Flow> flowMap = staticFlows;
580 int maxKey = staticFlowsOrdinal.get(0).intValue();
581 for (int i = 0; i <= maxKey; i++) {
582 Flow entry = flowMap.get(i);
584 orderedList.add(entry);
591 public DataObject getWithName(String name, org.opendaylight.controller.sal.core.Node n) {
592 if (this instanceof FlowConsumerImpl) {
593 for (ConcurrentMap.Entry<Integer, Flow> flowEntry : staticFlows.entrySet()) {
594 Flow flow = flowEntry.getValue();
595 if (flow.getNode().equals(n) && flow.getFlowName().equals(name)) {
597 return flowEntry.getValue();
605 * Update the node mapped flows database
607 private static void updateNodeFlowsDB(NodeFlow entry, boolean add) {
608 Node node = (Node) entry.getNode();
610 List<Flow> nodeIndeces = nodeFlows.get(node);
611 if (nodeIndeces == null) {
615 nodeIndeces = new ArrayList<Flow>();
620 nodeIndeces.add((Flow) entry);
622 nodeIndeces.remove(entry);
625 // Update cache across cluster
626 if (nodeIndeces.isEmpty()) {
627 nodeFlows.remove(node);
629 nodeFlows.put(node, nodeIndeces);
633 private static NodeRef createNodeRef(String string) {
634 NodeKey key = new NodeKey(new NodeId(string));
635 InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
638 return new NodeRef(path);