2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.forwardingrulesmanager.internal;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.ObjectInputStream;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.EnumSet;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentMap;
30 import org.eclipse.osgi.framework.console.CommandInterpreter;
31 import org.eclipse.osgi.framework.console.CommandProvider;
32 import org.opendaylight.controller.clustering.services.CacheConfigException;
33 import org.opendaylight.controller.clustering.services.CacheExistException;
34 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
35 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
36 import org.opendaylight.controller.clustering.services.IClusterServices;
37 import org.opendaylight.controller.configuration.IConfigurationContainerAware;
38 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
39 import org.opendaylight.controller.forwardingrulesmanager.FlowEntry;
40 import org.opendaylight.controller.forwardingrulesmanager.FlowEntryInstall;
41 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
42 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManagerAware;
43 import org.opendaylight.controller.forwardingrulesmanager.PortGroup;
44 import org.opendaylight.controller.forwardingrulesmanager.PortGroupChangeListener;
45 import org.opendaylight.controller.forwardingrulesmanager.PortGroupConfig;
46 import org.opendaylight.controller.forwardingrulesmanager.PortGroupProvider;
47 import org.opendaylight.controller.hosttracker.IfIptoHost;
48 import org.opendaylight.controller.sal.action.Action;
49 import org.opendaylight.controller.sal.action.ActionType;
50 import org.opendaylight.controller.sal.action.Controller;
51 import org.opendaylight.controller.sal.action.Flood;
52 import org.opendaylight.controller.sal.action.Output;
53 import org.opendaylight.controller.sal.action.PopVlan;
54 import org.opendaylight.controller.sal.core.ContainerFlow;
55 import org.opendaylight.controller.sal.core.IContainer;
56 import org.opendaylight.controller.sal.core.IContainerListener;
57 import org.opendaylight.controller.sal.core.Node;
58 import org.opendaylight.controller.sal.core.NodeConnector;
59 import org.opendaylight.controller.sal.core.Property;
60 import org.opendaylight.controller.sal.core.UpdateType;
61 import org.opendaylight.controller.sal.flowprogrammer.Flow;
62 import org.opendaylight.controller.sal.flowprogrammer.IFlowProgrammerListener;
63 import org.opendaylight.controller.sal.flowprogrammer.IFlowProgrammerService;
64 import org.opendaylight.controller.sal.match.Match;
65 import org.opendaylight.controller.sal.match.MatchType;
66 import org.opendaylight.controller.sal.utils.EtherTypes;
67 import org.opendaylight.controller.sal.utils.GlobalConstants;
68 import org.opendaylight.controller.sal.utils.HexEncode;
69 import org.opendaylight.controller.sal.utils.IObjectReader;
70 import org.opendaylight.controller.sal.utils.IPProtocols;
71 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
72 import org.opendaylight.controller.sal.utils.NodeCreator;
73 import org.opendaylight.controller.sal.utils.ObjectReader;
74 import org.opendaylight.controller.sal.utils.ObjectWriter;
75 import org.opendaylight.controller.sal.utils.Status;
76 import org.opendaylight.controller.sal.utils.StatusCode;
77 import org.opendaylight.controller.switchmanager.IInventoryListener;
78 import org.opendaylight.controller.switchmanager.ISwitchManager;
79 import org.opendaylight.controller.switchmanager.ISwitchManagerAware;
80 import org.opendaylight.controller.switchmanager.Subnet;
81 import org.osgi.framework.BundleContext;
82 import org.osgi.framework.FrameworkUtil;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
87 * Class that manages forwarding rule installation and removal per container of
88 * the network. It also maintains the central repository of all the forwarding
89 * rules installed on the network nodes.
91 public class ForwardingRulesManagerImpl implements IForwardingRulesManager,
92 PortGroupChangeListener, IContainerListener, ISwitchManagerAware,
93 IConfigurationContainerAware, IInventoryListener, IObjectReader,
94 ICacheUpdateAware<Long, String>, CommandProvider,
95 IFlowProgrammerListener {
96 private static final String SAVE = "Save";
97 private static final String NODEDOWN = "Node is Down";
98 private static final Logger log = LoggerFactory
99 .getLogger(ForwardingRulesManagerImpl.class);
100 private Map<Long, String> flowsSaveEvent;
101 private String frmFileName;
102 private String portGroupFileName;
103 private ConcurrentMap<Integer, FlowConfig> staticFlows;
104 private ConcurrentMap<Integer, Integer> staticFlowsOrdinal;
105 private ConcurrentMap<String, PortGroupConfig> portGroupConfigs;
106 private ConcurrentMap<PortGroupConfig, Map<Node, PortGroup>> portGroupData;
107 private ConcurrentMap<String, Object> TSPolicies;
108 private boolean inContainerMode; // being used by default instance only
110 * Flow database. It's the software view of what was installed on the
111 * switch. It is indexed by node. For convenience a version indexed by group
112 * name is also maintained. The core element is a class which contains the
113 * flow entry pushed by the functional modules and the respective container
114 * flow merged version. In absence of container flows, the two flow entries
117 private ConcurrentMap<Node, Set<FlowEntryInstall>> nodeFlows;
118 private ConcurrentMap<String, Set<FlowEntryInstall>> groupFlows;
120 * Inactive flow list. This is for the global instance of FRM It will
121 * contain all the flow entries which were installed on the global container
122 * when the first container is created.
124 private List<FlowEntry> inactiveFlows;
126 private IfIptoHost hostFinder;
127 private IContainer container;
128 private Set<IForwardingRulesManagerAware> frmAware;
129 private PortGroupProvider portGroupProvider;
130 private IFlowProgrammerService programmer;
131 private IClusterContainerServices clusterContainerService = null;
132 private ISwitchManager switchManager;
135 * Adds a flow entry onto the network node It runs various validity checks
136 * and derive the final container flows merged entries that will be
137 * attempted to be installed
140 * the original flow entry application requested to add
142 * the flag indicating if this is a asynchronous request
143 * @return the status of this request. In case of asynchronous call, it
144 * will contain the unique id assigned to this request
146 private Status addEntry(FlowEntry flowEntry, boolean async) {
149 if (flowEntry == null || flowEntry.getNode() == null) {
150 String msg = "Invalid FlowEntry";
151 String logMsg = msg + ": {}";
152 log.warn(logMsg, flowEntry);
153 return new Status(StatusCode.NOTACCEPTABLE, msg);
157 * Derive the container flow merged entries to install In presence of N
158 * container flows, we may end up with N different entries to install...
160 List<FlowEntryInstall> toInstallList = deriveInstallEntries(
161 flowEntry.clone(), container.getContainerFlows());
163 // Container Flow conflict Check
164 if (toInstallList.isEmpty()) {
165 String msg = "Flow Entry conflicts with all Container Flows";
166 String logMsg = msg + ": {}";
167 log.warn(logMsg, flowEntry);
168 return new Status(StatusCode.CONFLICT, msg);
171 // Derive the list of entries good to be installed
172 List<FlowEntryInstall> toInstallSafe = new ArrayList<FlowEntryInstall>();
173 for (FlowEntryInstall entry : toInstallList) {
174 // Conflict Check: Verify new entry would not overwrite existing
176 if (findMatch(entry.getInstall(), false) != null) {
177 log.warn("Operation Rejected: A flow with same match "
178 + "and priority exists on the target node");
179 log.trace("Aborting to install {}", entry);
182 toInstallSafe.add(entry);
185 // Declare failure if all the container flow merged entries clash with
187 if (toInstallSafe.size() == 0) {
188 String msg = "A flow with same match and priority exists "
189 + "on the target node";
190 String logMsg = msg + ": {}";
191 log.warn(logMsg, flowEntry);
192 return new Status(StatusCode.CONFLICT, msg);
195 // Try to install an entry at the time
196 Status error = new Status(null, null);
197 Status succeded = null;
198 boolean oneSucceded = false;
199 for (FlowEntryInstall installEntry : toInstallSafe) {
201 // Install and update database
202 Status ret = addEntriesInternal(installEntry, async);
204 if (ret.isSuccess()) {
207 * The first successful status response will be returned
208 * For the asynchronous call, we can discard the container flow
209 * complication for now and assume we will always deal with
210 * one flow only per request
215 log.warn("Failed to install the entry: {}. The failure is: {}",
216 installEntry, ret.getDescription());
220 return (oneSucceded) ? succeded : error;
224 * Given a flow entry and the list of container flows, it returns the list
225 * of container flow merged flow entries good to be installed on this
226 * container. If the list of container flows is null or empty, the install
227 * entry list will contain only one entry, the original flow entry. If the
228 * flow entry is congruent with all the N container flows, then the output
229 * install entry list will contain N entries. If the output list is empty,
230 * it means the passed flow entry conflicts with all the container flows.
233 * The list of container flows
234 * @return the list of container flow merged entries good to be installed on
237 private List<FlowEntryInstall> deriveInstallEntries(FlowEntry request,
238 List<ContainerFlow> cFlowList) {
239 List<FlowEntryInstall> toInstallList = new ArrayList<FlowEntryInstall>(
242 if (container.getContainerFlows() == null
243 || container.getContainerFlows().isEmpty()) {
244 // No container flows => entry good to be installed unchanged
245 toInstallList.add(new FlowEntryInstall(request.clone(), null));
247 // Create the list of entries to be installed. If the flow entry is
248 // not congruent with any container flow, no install entries will be
250 for (ContainerFlow cFlow : container.getContainerFlows()) {
251 if (cFlow.allowsFlow(request.getFlow())) {
252 toInstallList.add(new FlowEntryInstall(request.clone(),
257 return toInstallList;
261 * Modify a flow entry with a new one It runs various validity check and
262 * derive the final container flows merged flow entries to work with
264 * @param currentFlowEntry
265 * @param newFlowEntry
267 * the flag indicating if this is a asynchronous request
268 * @return the status of this request. In case of asynchronous call, it
269 * will contain the unique id assigned to this request
271 private Status modifyEntry(FlowEntry currentFlowEntry,
272 FlowEntry newFlowEntry, boolean async) {
277 if (currentFlowEntry == null || currentFlowEntry.getNode() == null
278 || newFlowEntry == null || newFlowEntry.getNode() == null) {
279 String msg = "Modify: Invalid FlowEntry";
280 String logMsg = msg + ": {} or {}";
281 log.warn(logMsg, currentFlowEntry, newFlowEntry);
282 return new Status(StatusCode.NOTACCEPTABLE, msg);
284 if (!currentFlowEntry.getNode().equals(newFlowEntry.getNode())
285 || !currentFlowEntry.getFlowName().equals(
286 newFlowEntry.getFlowName())) {
287 String msg = "Modify: Incompatible Flow Entries";
288 String logMsg = msg + ": {} and {}";
289 log.warn(logMsg, currentFlowEntry, newFlowEntry);
290 return new Status(StatusCode.NOTACCEPTABLE, msg);
294 if (currentFlowEntry.equals(newFlowEntry)) {
295 String msg = "Modify skipped as flows are the same";
296 String logMsg = msg + ": {} and {}";
297 log.debug(logMsg, currentFlowEntry, newFlowEntry);
298 return new Status(StatusCode.SUCCESS, msg);
301 // Conflict Check: Verify the new entry would not conflict with another
303 // This is a loose check on the previous original flow entry requests.
305 // on the container flow merged flow entries (if any) yet
306 FlowEntryInstall sameMatchOriginalEntry = findMatch(newFlowEntry, true);
307 if (sameMatchOriginalEntry != null
308 && !sameMatchOriginalEntry.getOriginal().equals(
310 String msg = "Operation Rejected: Another flow with same match "
311 + "and priority exists on the target node";
312 String logMsg = msg + ": {}";
313 log.warn(logMsg, currentFlowEntry);
314 return new Status(StatusCode.CONFLICT, msg);
317 // Derive the installed and toInstall entries
318 List<FlowEntryInstall> installedList = deriveInstallEntries(
319 currentFlowEntry.clone(), container.getContainerFlows());
320 List<FlowEntryInstall> toInstallList = deriveInstallEntries(
321 newFlowEntry.clone(), container.getContainerFlows());
323 if (toInstallList.isEmpty()) {
324 String msg = "Modify Operation Rejected: The new entry "
325 + "conflicts with all the container flows";
326 String logMsg = msg + ": {}";
327 log.warn(logMsg, newFlowEntry);
329 return new Status(StatusCode.CONFLICT, msg);
333 * If the two list sizes differ, it means the new flow entry does not
334 * satisfy the same number of container flows the current entry does.
335 * This is only possible when the new entry and current entry have
336 * different match. In this scenario the modification would ultimately
337 * be handled as a remove and add operations in the protocol plugin.
339 * Also, if any of the new flow entries would clash with an existing
340 * one, we cannot proceed with the modify operation, because it would
341 * fail for some entries and leave stale entries on the network node.
342 * Modify path can be taken only if it can be performed completely, for
345 * So, for the above two cases, to simplify, let's decouple the modify
346 * in: 1) remove current entries 2) install new entries
348 Status succeeded = null;
349 boolean decouple = false;
350 if (installedList.size() != toInstallList.size()) {
351 log.info("Modify: New flow entry does not satisfy the same "
352 + "number of container flows as the original entry does");
355 List<FlowEntryInstall> toInstallSafe = new ArrayList<FlowEntryInstall>();
356 for (FlowEntryInstall installEntry : toInstallList) {
357 // Conflict Check: Verify the new entry would not overwrite another
359 FlowEntryInstall sameMatchEntry = findMatch(
360 installEntry.getInstall(), false);
361 if (sameMatchEntry != null
362 && !sameMatchEntry.getOriginal().equals(currentFlowEntry)) {
363 log.info("Modify: new container flow merged flow entry "
364 + "clashes with existing flow");
367 toInstallSafe.add(installEntry);
372 // Remove current entries
373 for (FlowEntryInstall currEntry : installedList) {
374 this.removeEntryInternal(currEntry, async);
376 // Install new entries
377 for (FlowEntryInstall newEntry : toInstallSafe) {
378 succeeded = this.addEntriesInternal(newEntry, async);
382 * The two list have the same size and the entries to install do not
383 * clash with any existing flow on the network node. We assume here
384 * (and might be wrong) that the same container flows that were
385 * satisfied by the current entries are the same that are satisfied
386 * by the new entries. Let's take the risk for now.
388 * Note: modification has to be complete. If any entry modification
389 * fails, we need to stop, restore the already modified entries, and
392 Status retModify = null;
394 int size = toInstallList.size();
396 // Modify and update database
397 retModify = modifyEntryInternal(installedList.get(i),
398 toInstallList.get(i), async);
399 if (retModify.isSuccess()) {
405 // Check if uncompleted modify
407 log.warn("Unable to perform a complete modify for all "
408 + "the container flows merged entries");
409 // Restore original entries
412 log.info("Attempting to restore initial entries");
413 retExt = modifyEntryInternal(toInstallList.get(i),
414 installedList.get(i), async);
415 if (retExt.isSuccess()) {
421 // Fatal error, recovery failed
423 String msg = "Flow recovery failed ! Unrecoverable Error";
425 return new Status(StatusCode.INTERNALERROR, msg);
428 succeeded = retModify;
431 * The first successful status response will be returned.
432 * For the asynchronous call, we can discard the container flow
433 * complication for now and assume we will always deal with
434 * one flow only per request
440 * This is the function that modifies the final container flows merged
441 * entries on the network node and update the database. It expects that all
442 * the validity checks are passed
444 * @param currentEntries
447 * the flag indicating if this is a asynchronous request
448 * @return the status of this request. In case of asynchronous call, it
449 * will contain the unique id assigned to this request
451 private Status modifyEntryInternal(FlowEntryInstall currentEntries,
452 FlowEntryInstall newEntries, boolean async) {
453 // Modify the flow on the network node
454 Status status = (async)?
455 programmer.modifyFlowAsync(currentEntries.getNode(),
456 currentEntries.getInstall().getFlow(), newEntries.getInstall()
458 programmer.modifyFlow(currentEntries.getNode(),
459 currentEntries.getInstall().getFlow(), newEntries.getInstall()
463 if (!status.isSuccess()) {
465 "SDN Plugin failed to program the flow: {}. The failure is: {}",
466 newEntries.getInstall(), status.getDescription());
470 log.trace("Modified {} => {}", currentEntries.getInstall(),
471 newEntries.getInstall());
474 newEntries.setRequestId(status.getRequestId());
475 updateLocalDatabase(currentEntries, false);
476 updateLocalDatabase(newEntries, true);
482 * Remove a flow entry. If the entry is not present in the software view
483 * (entry or node not present), it return successfully
486 * the flow entry to remove
488 * the flag indicating if this is a asynchronous request
489 * @return the status of this request. In case of asynchronous call, it
490 * will contain the unique id assigned to this request
492 private synchronized Status removeEntry(FlowEntry flowEntry, boolean async) {
493 Status error = new Status(null, null);
496 if (flowEntry == null || flowEntry.getNode() == null) {
497 String msg = "Invalid FlowEntry";
498 String logMsg = msg + ": {}";
499 log.warn(logMsg, flowEntry);
500 return new Status(StatusCode.NOTACCEPTABLE, msg);
503 // Derive the container flows merged installed entries
504 List<FlowEntryInstall> installedList = deriveInstallEntries(
505 flowEntry.clone(), container.getContainerFlows());
507 Set<FlowEntryInstall> flowsOnNode = nodeFlows.get(flowEntry.getNode());
508 Status succeeded = null;
509 boolean atLeastOneRemoved = false;
510 for (FlowEntryInstall entry : installedList) {
511 if (flowsOnNode == null) {
512 String msg = "Removal skipped (Node down) for flow entry";
513 String logMsg = msg + ": {}";
514 log.debug(logMsg, flowEntry);
515 return new Status(StatusCode.SUCCESS, msg);
517 if (!flowsOnNode.contains(entry)) {
518 String logMsg = "Removal skipped (not present in software view) for flow entry: {}";
519 log.debug(logMsg, flowEntry);
520 if (installedList.size() == 1) {
521 // If we had only one entry to remove, we are done
522 return new Status(StatusCode.SUCCESS);
528 // Remove and update DB
529 Status ret = removeEntryInternal(entry, async);
531 if (!ret.isSuccess()) {
533 log.warn("Failed to remove the entry: {}. The failure is: {}",
534 entry.getInstall(), ret.getDescription());
535 if (installedList.size() == 1) {
536 // If we had only one entry to remove, this is fatal failure
541 atLeastOneRemoved = true;
546 * No worries if full removal failed. Consistency checker will take care
547 * of removing the stale entries later, or adjusting the software
548 * database if not in sync with hardware
550 return (atLeastOneRemoved) ? succeeded : error;
554 * This is the function that removes the final container flows merged entry
555 * from the network node and update the database. It expects that all the
556 * validity checks are passed
559 * the flow entry to remove
561 * the flag indicating if this is a asynchronous request
562 * @return the status of this request. In case of asynchronous call, it
563 * will contain the unique id assigned to this request
565 private Status removeEntryInternal(FlowEntryInstall entry, boolean async) {
566 // Mark the entry to be deleted (for CC just in case we fail)
570 Status status = (async)?
571 programmer.removeFlowAsync(entry.getNode(), entry
572 .getInstall().getFlow()) :
573 programmer.removeFlow(entry.getNode(), entry
574 .getInstall().getFlow());
577 if (!status.isSuccess()) {
579 "SDN Plugin failed to program the flow: {}. The failure is: {}",
580 entry.getInstall(), status.getDescription());
583 log.trace("Removed {}", entry.getInstall());
586 updateLocalDatabase(entry, false);
592 * This is the function that installs the final container flow merged entry
593 * on the network node and updates the database. It expects that all the
594 * validity and conflict checks are passed. That means it does not check
595 * whether this flow would conflict or overwrite an existing one.
598 * the flow entry to install
600 * the flag indicating if this is a asynchronous request
601 * @return the status of this request. In case of asynchronous call, it
602 * will contain the unique id assigned to this request
604 private Status addEntriesInternal(FlowEntryInstall entry, boolean async) {
605 // Install the flow on the network node
606 Status status = (async)?
607 programmer.addFlowAsync(entry.getNode(), entry.getInstall()
609 programmer.addFlow(entry.getNode(), entry.getInstall()
613 if (!status.isSuccess()) {
615 "SDN Plugin failed to program the flow: {}. The failure is: {}",
616 entry.getInstall(), status.getDescription());
620 log.trace("Added {}", entry.getInstall());
623 entry.setRequestId(status.getRequestId());
624 updateLocalDatabase(entry, true);
630 * Returns true if the flow conflicts with all the container's flows. This
631 * means that if the function returns true, the passed flow entry is
632 * congruent with at least one container flow, hence it is good to be
633 * installed on this container.
636 * @return true if flow conflicts with all the container flows, false
639 private boolean entryConflictsWithContainerFlows(FlowEntry flowEntry) {
640 List<ContainerFlow> cFlowList = container.getContainerFlows();
642 // Validity check and avoid unnecessary computation
643 // Also takes care of default container where no container flows are
645 if (cFlowList == null || cFlowList.isEmpty()) {
649 for (ContainerFlow cFlow : cFlowList) {
650 if (cFlow.allowsFlow(flowEntry.getFlow())) {
651 // Entry is allowed by at least one container flow: good to go
658 private synchronized void updateLocalDatabase(FlowEntryInstall entry,
660 // Update node indexed flow database
661 updateNodeFlowsDB(entry, add);
663 // Update group indexed flow database
664 updateGroupFlowsDB(entry, add);
668 * Update the node mapped flows database
670 private synchronized void updateNodeFlowsDB(FlowEntryInstall flowEntries, boolean add) {
671 Node node = flowEntries.getNode();
673 Set<FlowEntryInstall> flowEntrylist = this.nodeFlows.get(node);
674 if (flowEntrylist == null) {
678 flowEntrylist = new HashSet<FlowEntryInstall>();
683 flowEntrylist.add(flowEntries);
685 flowEntrylist.remove(flowEntries);
688 if (flowEntrylist.isEmpty()) {
689 this.nodeFlows.remove(node);
691 this.nodeFlows.put(node, flowEntrylist);
696 * Update the group name mapped flows database
698 private void updateGroupFlowsDB(FlowEntryInstall flowEntries, boolean add) {
699 Set<FlowEntryInstall> flowList;
700 FlowEntryInstall exists = null;
701 String flowName = flowEntries.getFlowName();
702 String groupName = flowEntries.getGroupName();
704 if (this.groupFlows == null) {
708 // Flow may not be part of a group
709 if (groupName == null) {
713 if (this.groupFlows.containsKey(groupName)) {
714 flowList = this.groupFlows.get(groupName);
719 flowList = new HashSet<FlowEntryInstall>();
723 for (FlowEntryInstall flow : flowList) {
724 if (flow.equalsByNodeAndName(flowEntries.getNode(), flowName)) {
730 if (exists == null && add == false) {
734 if (exists != null) {
735 flowList.remove(exists);
739 flowList.add(flowEntries);
742 if (flowList.isEmpty()) {
743 this.groupFlows.remove(groupName);
745 this.groupFlows.put(groupName, flowList);
750 * Remove a flow entry that has been added previously First checks if the
751 * entry is effectively present in the local database
753 @SuppressWarnings("unused")
754 private synchronized Status removeEntry(Node node, String flowName) {
755 FlowEntryInstall target = null;
758 for (FlowEntryInstall entry : this.nodeFlows.get(node)) {
759 if (entry.equalsByNodeAndName(node, flowName)) {
765 // If it is not there, stop any further processing
766 if (target == null) {
767 return new Status(StatusCode.SUCCESS, "Entry is not present");
771 Status status = programmer.removeFlow(target.getNode(), target
772 .getInstall().getFlow());
775 if (status.isSuccess()) {
776 updateLocalDatabase(target, false);
780 "SDN Plugin failed to remove the flow: {}. The failure is: {}",
781 target.getInstall(), status.getDescription());
788 public Status installFlowEntry(FlowEntry flowEntry) {
790 if (inContainerMode) {
791 String msg = "Controller in container mode: Install Refused";
792 String logMsg = msg + ": {}";
793 status = new Status(StatusCode.NOTACCEPTABLE, msg);
794 log.warn(logMsg, flowEntry);
796 status = addEntry(flowEntry, false);
802 public Status installFlowEntryAsync(FlowEntry flowEntry) {
804 if (inContainerMode) {
805 String msg = "Controller in container mode: Install Refused";
806 status = new Status(StatusCode.NOTACCEPTABLE, msg);
809 status = addEntry(flowEntry, true);
815 public Status uninstallFlowEntry(FlowEntry entry) {
817 if (inContainerMode) {
818 String msg = "Controller in container mode: Uninstall Refused";
819 String logMsg = msg + ": {}";
820 status = new Status(StatusCode.NOTACCEPTABLE, msg);
821 log.warn(logMsg, entry);
823 status = removeEntry(entry, false);
829 public Status uninstallFlowEntryAsync(FlowEntry flowEntry) {
831 if (inContainerMode) {
832 String msg = "Controller in container mode: Uninstall Refused";
833 status = new Status(StatusCode.NOTACCEPTABLE, msg);
836 status = removeEntry(flowEntry, true);
842 public Status modifyFlowEntry(FlowEntry currentFlowEntry,
843 FlowEntry newFlowEntry) {
844 Status status = null;
845 if (inContainerMode) {
846 String msg = "Controller in container mode: Modify Refused";
847 String logMsg = msg + ": {}";
848 status = new Status(StatusCode.NOTACCEPTABLE, msg);
849 log.warn(logMsg, newFlowEntry);
851 status = modifyEntry(currentFlowEntry, newFlowEntry, false);
857 public Status modifyFlowEntryAsync(FlowEntry current, FlowEntry newone) {
858 Status status = null;
859 if (inContainerMode) {
860 String msg = "Controller in container mode: Modify Refused";
861 status = new Status(StatusCode.NOTACCEPTABLE, msg);
864 status = modifyEntry(current, newone, true);
870 public Status modifyOrAddFlowEntry(FlowEntry newFlowEntry) {
872 * Run a loose check on the installed entries to decide whether to go
873 * with a add or modify method. A loose check means only check against
874 * the original flow entry requests and not against the installed flow
875 * entries which are the result of the original entry merged with the
876 * container flow(s) (if any). The modifyFlowEntry method in presence of
877 * conflicts with the Container flows (if any) would revert back to a
878 * delete + add pattern
880 FlowEntryInstall currentFlowEntries = findMatch(newFlowEntry, true);
882 if (currentFlowEntries != null) {
883 return modifyFlowEntry(currentFlowEntries.getOriginal(),
886 return installFlowEntry(newFlowEntry);
891 public Status modifyOrAddFlowEntryAsync(FlowEntry newone) {
893 * Run a loose check on the installed entries to decide whether to go
894 * with a add or modify method. A loose check means only check against
895 * the original flow entry requests and not against the installed flow
896 * entries which are the result of the original entry merged with the
897 * container flow(s) (if any). The modifyFlowEntry method in presence of
898 * conflicts with the Container flows (if any) would revert back to a
899 * delete + add pattern
901 FlowEntryInstall currentFlowEntries = findMatch(newone, true);
903 if (currentFlowEntries != null) {
904 return modifyFlowEntryAsync(currentFlowEntries.getOriginal(),
907 return installFlowEntryAsync(newone);
913 * Try to find in the database if a Flow with the same Match and priority of
914 * the passed one already exists for the specified network node. Flow,
915 * priority and network node are all specified in the FlowEntry If found,
916 * the respective FlowEntryInstall Object is returned
919 * the FlowEntry to be tested against the ones installed
921 * if true, the function will run the check against the original
922 * flow entry portion of the installed entries
923 * @return null if not found, otherwise the FlowEntryInstall which contains
924 * the existing flow entry
926 private synchronized FlowEntryInstall findMatch(FlowEntry flowEntry, boolean looseCheck) {
927 Flow flow = flowEntry.getFlow();
928 Match match = flow.getMatch();
929 short priority = flow.getPriority();
930 Set<FlowEntryInstall> thisNodeList = nodeFlows.get(flowEntry.getNode());
932 if (thisNodeList != null) {
933 for (FlowEntryInstall flowEntries : thisNodeList) {
934 flow = (looseCheck == false) ? flowEntries.getInstall()
935 .getFlow() : flowEntries.getOriginal().getFlow();
936 if (flow.getMatch().equals(match)
937 && flow.getPriority() == priority) {
945 public boolean checkFlowEntryConflict(FlowEntry flowEntry) {
946 return entryConflictsWithContainerFlows(flowEntry);
950 * Updates all installed flows because the container flow got updated This
951 * is obtained in two phases on per node basis: 1) Uninstall of all flows 2)
952 * Reinstall of all flows This is needed because a new container flows
953 * merged flow may conflict with an existing old container flows merged flow
954 * on the network node
956 private synchronized void updateFlowsContainerFlow() {
957 List<FlowEntryInstall> oldCouples = new ArrayList<FlowEntryInstall>();
958 List<FlowEntry> toReinstall = new ArrayList<FlowEntry>();
959 for (Entry<Node, Set<FlowEntryInstall>> entry : this.nodeFlows
963 if (entry.getValue() == null) {
966 // Create a set of old entries and one of original entries to be
968 for (FlowEntryInstall oldCouple : entry.getValue()) {
969 oldCouples.add(oldCouple);
970 toReinstall.add(oldCouple.getOriginal());
972 // Remove the old couples. No validity checks to be run, use the
974 for (FlowEntryInstall oldCouple : oldCouples) {
975 this.removeEntryInternal(oldCouple, false);
977 // Reinstall the original flow entries, via the regular path: new
978 // cFlow merge + validations
979 for (FlowEntry flowEntry : toReinstall) {
980 this.installFlowEntry(flowEntry);
985 public void nonClusterObjectCreate() {
986 nodeFlows = new ConcurrentHashMap<Node, Set<FlowEntryInstall>>();
987 TSPolicies = new ConcurrentHashMap<String, Object>();
988 groupFlows = new ConcurrentHashMap<String, Set<FlowEntryInstall>>();
989 staticFlowsOrdinal = new ConcurrentHashMap<Integer, Integer>();
990 portGroupConfigs = new ConcurrentHashMap<String, PortGroupConfig>();
991 portGroupData = new ConcurrentHashMap<PortGroupConfig, Map<Node, PortGroup>>();
992 staticFlows = new ConcurrentHashMap<Integer, FlowConfig>();
993 flowsSaveEvent = new HashMap<Long, String>();
994 inactiveFlows = new ArrayList<FlowEntry>(1);
997 private void registerWithOSGIConsole() {
998 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass())
1000 bundleContext.registerService(CommandProvider.class.getName(), this,
1005 public void setTSPolicyData(String policyname, Object o, boolean add) {
1008 /* Check if this policy already exists */
1009 if (!(TSPolicies.containsKey(policyname))) {
1010 TSPolicies.put(policyname, o);
1013 TSPolicies.remove(policyname);
1015 if (frmAware != null) {
1016 synchronized (frmAware) {
1017 for (IForwardingRulesManagerAware frma : frmAware) {
1019 frma.policyUpdate(policyname, add);
1020 } catch (Exception e) {
1021 log.warn("Exception on callback", e);
1029 public Map<String, Object> getTSPolicyData() {
1034 public Object getTSPolicyData(String policyName) {
1035 if (TSPolicies.containsKey(policyName)) {
1036 return TSPolicies.get(policyName);
1043 public List<FlowEntry> getFlowEntriesForGroup(String policyName) {
1044 List<FlowEntry> list = new ArrayList<FlowEntry>();
1045 if (this.groupFlows != null && this.groupFlows.containsKey(policyName)) {
1046 for (FlowEntryInstall entries : groupFlows.get(policyName)) {
1047 list.add(entries.getOriginal());
1054 public synchronized void addOutputPort(Node node, String flowName,
1055 List<NodeConnector> portList) {
1057 Set<FlowEntryInstall> flowEntryList = this.nodeFlows.get(node);
1059 for (FlowEntryInstall flow : flowEntryList) {
1060 if (flow.getFlowName().equals(flowName)) {
1061 FlowEntry currentFlowEntry = flow.getOriginal();
1062 FlowEntry newFlowEntry = currentFlowEntry.clone();
1063 for (NodeConnector dstPort : portList) {
1064 newFlowEntry.getFlow().addAction(new Output(dstPort));
1066 Status error = modifyEntry(currentFlowEntry, newFlowEntry, false);
1067 if (error.isSuccess()) {
1068 log.info("Ports {} added to FlowEntry {}", portList,
1072 "Failed to add ports {} to Flow entry {}. The failure is: {}",
1073 portList, currentFlowEntry.toString(),
1074 error.getDescription());
1079 log.warn("Failed to add ports to Flow {} on Node {}: Entry Not Found",
1084 public synchronized void removeOutputPort(Node node, String flowName,
1085 List<NodeConnector> portList) {
1087 Set<FlowEntryInstall> flowEntryList = this.nodeFlows.get(node);
1089 for (FlowEntryInstall flow : flowEntryList) {
1090 if (flow.getFlowName().equals(flowName)) {
1091 FlowEntry currentFlowEntry = flow.getOriginal();
1092 FlowEntry newFlowEntry = currentFlowEntry.clone();
1093 for (NodeConnector dstPort : portList) {
1094 Action action = new Output(dstPort);
1095 newFlowEntry.getFlow().removeAction(action);
1097 Status status = modifyEntry(currentFlowEntry, newFlowEntry, false);
1098 if (status.isSuccess()) {
1099 log.info("Ports {} removed from FlowEntry {}", portList,
1103 "Failed to remove ports {} from Flow entry {}. The failure is: {}",
1104 portList, currentFlowEntry.toString(),
1105 status.getDescription());
1111 "Failed to remove ports from Flow {} on Node {}: Entry Not Found",
1116 * This function assumes the target flow has only one output port
1119 public synchronized void replaceOutputPort(Node node, String flowName,
1120 NodeConnector outPort) {
1121 FlowEntry currentFlowEntry = null;
1122 FlowEntry newFlowEntry = null;
1123 Set<FlowEntryInstall> flowEntryList = this.nodeFlows.get(node);
1126 for (FlowEntryInstall flow : flowEntryList) {
1127 if (flow.getFlowName().equals(flowName)) {
1128 currentFlowEntry = flow.getOriginal();
1132 if (currentFlowEntry == null) {
1134 "Failed to replace output port for flow {} on node {}: Entry Not Found",
1139 // Create a flow copy with the new output port
1140 newFlowEntry = currentFlowEntry.clone();
1141 Action target = null;
1142 for (Action action : newFlowEntry.getFlow().getActions()) {
1143 if (action.getType() == ActionType.OUTPUT) {
1148 newFlowEntry.getFlow().removeAction(target);
1149 newFlowEntry.getFlow().addAction(new Output(outPort));
1151 // Modify on network node
1152 Status status = modifyEntry(currentFlowEntry, newFlowEntry, false);
1154 if (status.isSuccess()) {
1155 log.info("Output port replaced with {} for flow {} on node {}",
1156 outPort, flowName, node);
1159 "Failed to replace output port for flow {} on node {}. The failure is: {}",
1160 flowName, node, status.getDescription());
1166 public synchronized NodeConnector getOutputPort(Node node, String flowName) {
1167 Set<FlowEntryInstall> flowEntryList = this.nodeFlows.get(node);
1169 for (FlowEntryInstall flow : flowEntryList) {
1170 if (flow.getFlowName().equals(flowName)) {
1171 for (Action action : flow.getOriginal().getFlow().getActions()) {
1172 if (action.getType() == ActionType.OUTPUT) {
1173 return ((Output) action).getPort();
1182 private void cacheStartup() {
1187 @SuppressWarnings("deprecation")
1188 private void allocateCaches() {
1189 if (this.clusterContainerService == null) {
1190 log.warn("Un-initialized clusterContainerService, can't create cache");
1194 log.debug("FRM allocateCaches for Container {}", container);
1197 clusterContainerService.createCache("frm.nodeFlows",
1198 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1200 clusterContainerService.createCache("frm.groupFlows",
1201 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1203 clusterContainerService.createCache("frm.staticFlows",
1204 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1206 clusterContainerService.createCache("frm.flowsSaveEvent",
1207 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1209 clusterContainerService.createCache("frm.staticFlowsOrdinal",
1210 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1212 clusterContainerService.createCache("frm.portGroupConfigs",
1213 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1215 clusterContainerService.createCache("frm.portGroupData",
1216 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1218 clusterContainerService.createCache("frm.TSPolicies",
1219 EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL));
1221 } catch (CacheConfigException cce) {
1222 log.error("FRM CacheConfigException", cce);
1223 } catch (CacheExistException cce) {
1224 log.error("FRM CacheExistException", cce);
1228 @SuppressWarnings({ "unchecked", "deprecation" })
1229 private void retrieveCaches() {
1230 ConcurrentMap<?, ?> map;
1232 if (this.clusterContainerService == null) {
1233 log.warn("un-initialized clusterContainerService, can't retrieve cache");
1237 log.debug("FRM retrieveCaches for Container {}", container);
1239 map = clusterContainerService.getCache("frm.nodeFlows");
1241 nodeFlows = (ConcurrentMap<Node, Set<FlowEntryInstall>>) map;
1244 "FRM Cache frm.nodeFlows allocation failed for Container {}",
1248 map = clusterContainerService.getCache("frm.groupFlows");
1250 groupFlows = (ConcurrentMap<String, Set<FlowEntryInstall>>) map;
1253 "FRM Cache frm.groupFlows allocation failed for Container {}",
1257 map = clusterContainerService.getCache("frm.staticFlows");
1259 staticFlows = (ConcurrentMap<Integer, FlowConfig>) map;
1262 "FRM Cache frm.staticFlows allocation failed for Container {}",
1266 map = clusterContainerService.getCache("frm.flowsSaveEvent");
1268 flowsSaveEvent = (ConcurrentMap<Long, String>) map;
1271 "FRM Cache frm.flowsSaveEvent allocation failed for Container {}",
1275 map = clusterContainerService.getCache("frm.staticFlowsOrdinal");
1277 staticFlowsOrdinal = (ConcurrentMap<Integer, Integer>) map;
1280 "FRM Cache frm.staticFlowsOrdinal allocation failed for Container {}",
1284 map = clusterContainerService.getCache("frm.portGroupConfigs");
1286 portGroupConfigs = (ConcurrentMap<String, PortGroupConfig>) map;
1289 "FRM Cache frm.portGroupConfigs allocation failed for Container {}",
1293 map = clusterContainerService.getCache("frm.portGroupData");
1295 portGroupData = (ConcurrentMap<PortGroupConfig, Map<Node, PortGroup>>) map;
1298 "FRM Cache frm.portGroupData allocation failed for Container {}",
1302 map = clusterContainerService.getCache("frm.TSPolicies");
1304 TSPolicies = (ConcurrentMap<String, Object>) map;
1307 "FRM Cache frm.TSPolicies allocation failed for Container {}",
1313 @SuppressWarnings("deprecation")
1314 private void destroyCaches() {
1315 if (this.clusterContainerService == null) {
1316 log.warn("Un-initialized clusterContainerService, can't destroy cache");
1320 log.debug("FRM destroyCaches for Container {}", container);
1321 clusterContainerService.destroyCache("frm.nodeFlows");
1322 clusterContainerService.destroyCache("frm.TSPolicies");
1323 clusterContainerService.destroyCache("frm.groupFlows");
1324 clusterContainerService.destroyCache("frm.staticFlows");
1325 clusterContainerService.destroyCache("frm.flowsSaveEvent");
1326 clusterContainerService.destroyCache("frm.staticFlowsOrdinal");
1327 clusterContainerService.destroyCache("frm.portGroupData");
1328 clusterContainerService.destroyCache("frm.portGroupConfigs");
1329 nonClusterObjectCreate();
1332 private boolean flowConfigExists(FlowConfig config) {
1333 // As per customer requirement, flow name has to be unique on per node
1335 for (FlowConfig fc : staticFlows.values()) {
1336 if (fc.isByNameAndNodeIdEqual(config)) {
1344 public Status addStaticFlow(FlowConfig config, boolean restore) {
1345 StringBuffer resultStr = new StringBuffer();
1346 boolean multipleFlowPush = false;
1349 config.setStatus(StatusCode.SUCCESS.toString());
1352 if (flowConfigExists(config)) {
1353 error = "Entry with this name on specified switch already exists";
1355 "Entry with this name on specified switch already exists: {}",
1357 config.setStatus(error);
1358 return new Status(StatusCode.CONFLICT, error);
1361 // Skip validation check if we are trying to restore a saved config
1362 if (!restore && !config.isValid(container, resultStr)) {
1363 log.warn("Invalid Configuration for flow {}. The failure is {}",
1364 config, resultStr.toString());
1365 error = "Invalid Configuration (" + resultStr.toString() + ")";
1366 config.setStatus(error);
1367 return new Status(StatusCode.BADREQUEST, error);
1370 if ((config.getIngressPort() == null) && config.getPortGroup() != null) {
1371 for (String portGroupName : portGroupConfigs.keySet()) {
1372 if (portGroupName.equalsIgnoreCase(config.getPortGroup())) {
1373 multipleFlowPush = true;
1377 if (!multipleFlowPush) {
1379 "Invalid Configuration(Invalid PortGroup Name) for flow {}",
1381 error = "Invalid Configuration (Invalid PortGroup Name)";
1382 config.setStatus(error);
1383 return new Status(StatusCode.BADREQUEST, error);
1388 * If requested program the entry in hardware first before updating the
1391 if (!multipleFlowPush) {
1393 if (config.installInHw()) {
1394 FlowEntry entry = config.getFlowEntry();
1395 status = this.addEntry(entry, false);
1396 if (!status.isSuccess()) {
1397 config.setStatus(status.getDescription());
1406 * When the control reaches this point, either of the following
1407 * conditions is true 1. This is a single entry configuration (non
1408 * PortGroup) and the hardware installation is successful 2. This is a
1409 * multiple entry configuration (PortGroup) and hardware installation is
1410 * NOT done directly on this event. 3. The User prefers to retain the
1411 * configuration in Controller and skip hardware installation.
1413 * Hence it is safe to update the StaticFlow DB at this point.
1415 * Note : For the case of PortGrouping, it is essential to have this DB
1416 * populated before the PortGroupListeners can query for the DB
1417 * triggered using portGroupChanged event...
1419 Integer ordinal = staticFlowsOrdinal.get(0);
1420 staticFlowsOrdinal.put(0, ++ordinal);
1421 staticFlows.put(ordinal, config);
1423 if (multipleFlowPush) {
1424 PortGroupConfig pgconfig = portGroupConfigs.get(config
1426 Map<Node, PortGroup> existingData = portGroupData.get(pgconfig);
1427 if (existingData != null) {
1428 portGroupChanged(pgconfig, existingData, true);
1431 return new Status(StatusCode.SUCCESS);
1434 private void addStaticFlowsToSwitch(Node node) {
1435 for (FlowConfig config : staticFlows.values()) {
1436 if (config.isPortGroupEnabled()) {
1439 if (config.getNode().equals(node)) {
1440 if (config.installInHw()
1441 && !config.getStatus().equals(
1442 StatusCode.SUCCESS.toString())) {
1443 Status status = this.addEntry(config.getFlowEntry(), false);
1444 config.setStatus(status.getDescription());
1450 private void updateStaticFlowConfigsOnNodeDown(Node node) {
1451 log.trace("Updating Static Flow configs on node down: {}", node);
1453 List<Integer> toRemove = new ArrayList<Integer>();
1454 for (Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1456 FlowConfig config = entry.getValue();
1458 if (config.isPortGroupEnabled()) {
1462 if (config.installInHw() && config.getNode().equals(node)) {
1463 if (config.isInternalFlow()) {
1464 // Take note of this controller generated static flow
1465 toRemove.add(entry.getKey());
1467 config.setStatus(NODEDOWN);
1471 // Remove controller generated static flows for this node
1472 for (Integer index : toRemove) {
1473 staticFlows.remove(index);
1477 private void updateStaticFlowConfigsOnContainerModeChange(UpdateType update) {
1478 log.trace("Updating Static Flow configs on container mode change: {}",
1481 for (FlowConfig config : staticFlows.values()) {
1482 if (config.isPortGroupEnabled()) {
1485 if (config.installInHw()) {
1488 config.setStatus("Removed from node because in container mode");
1491 config.setStatus(StatusCode.SUCCESS.toString());
1499 public Status removeStaticFlow(FlowConfig config) {
1501 * No config.isInternal() check as NB does not take this path and GUI
1502 * cannot issue a delete on an internal generated flow. We need this
1503 * path to be accessible when switch mode is changed from proactive to
1504 * reactive, so that we can remove the internal generated LLDP and ARP
1507 for (Map.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1508 if (entry.getValue().isByNameAndNodeIdEqual(config)) {
1509 // Program the network node
1510 Status status = this.removeEntry(config.getFlowEntry(), false);
1511 // Update configuration database if programming was successful
1512 if (status.isSuccess()) {
1513 staticFlows.remove(entry.getKey());
1516 entry.getValue().setStatus(status.getDescription());
1521 return new Status(StatusCode.NOTFOUND, "Entry Not Present");
1525 public Status removeStaticFlow(String name, Node node) {
1526 for (Map.Entry<Integer, FlowConfig> mapEntry : staticFlows.entrySet()) {
1527 FlowConfig entry = mapEntry.getValue();
1528 Status status = new Status(null, null);
1529 if (entry.isByNameAndNodeIdEqual(name, node)) {
1530 // Validity check for api3 entry point
1531 if (entry.isInternalFlow()) {
1532 String msg = "Invalid operation: Controller generated "
1533 + "flow cannot be deleted";
1534 String logMsg = msg + ": {}";
1535 log.warn(logMsg, name);
1536 return new Status(StatusCode.NOTACCEPTABLE, msg);
1538 if (!entry.isPortGroupEnabled()) {
1539 // Program the network node
1540 status = this.removeEntry(entry.getFlowEntry(), false);
1542 // Update configuration database if programming was successful
1543 if (status.isSuccess()) {
1544 staticFlows.remove(mapEntry.getKey());
1547 entry.setStatus(status.getDescription());
1552 return new Status(StatusCode.NOTFOUND, "Entry Not Present");
1555 public Status modifyStaticFlow(FlowConfig newFlowConfig) {
1556 // Validity check for api3 entry point
1557 if (newFlowConfig.isInternalFlow()) {
1558 String msg = "Invalid operation: Controller generated flow "
1559 + "cannot be modified";
1560 String logMsg = msg + ": {}";
1561 log.warn(logMsg, newFlowConfig);
1562 return new Status(StatusCode.NOTACCEPTABLE, msg);
1566 StringBuffer resultStr = new StringBuffer();
1567 if (!newFlowConfig.isValid(container, resultStr)) {
1568 String msg = "Invalid Configuration (" + resultStr.toString() + ")";
1569 newFlowConfig.setStatus(msg);
1570 log.warn("Invalid Configuration for flow {}. The failure is {}",
1571 newFlowConfig, resultStr.toString());
1572 return new Status(StatusCode.BADREQUEST, msg);
1575 FlowConfig oldFlowConfig = null;
1576 Integer index = null;
1577 for (Map.Entry<Integer, FlowConfig> mapEntry : staticFlows.entrySet()) {
1578 FlowConfig entry = mapEntry.getValue();
1579 if (entry.isByNameAndNodeIdEqual(newFlowConfig.getName(),
1580 newFlowConfig.getNode())) {
1581 oldFlowConfig = entry;
1582 index = mapEntry.getKey();
1587 if (oldFlowConfig == null) {
1588 String msg = "Attempt to modify a non existing static flow";
1589 String logMsg = msg + ": {}";
1590 log.warn(logMsg, newFlowConfig);
1591 return new Status(StatusCode.NOTFOUND, msg);
1594 // Do not attempt to reinstall the flow, warn user
1595 if (newFlowConfig.equals(oldFlowConfig)) {
1596 String msg = "No modification detected";
1598 "Static flow modification skipped. New flow and old flow are the same: {}",
1600 return new Status(StatusCode.SUCCESS, msg);
1603 // If flow is installed, program the network node
1604 Status status = new Status(StatusCode.SUCCESS, "Saved in config");
1605 if (oldFlowConfig.installInHw()) {
1606 status = this.modifyEntry(oldFlowConfig.getFlowEntry(),
1607 newFlowConfig.getFlowEntry(), false);
1610 // Update configuration database if programming was successful
1611 if (status.isSuccess()) {
1612 newFlowConfig.setStatus(status.getDescription());
1613 staticFlows.put(index, newFlowConfig);
1620 public Status toggleStaticFlowStatus(String name, Node node) {
1621 return toggleStaticFlowStatus(getStaticFlow(name, node));
1625 public Status toggleStaticFlowStatus(FlowConfig config) {
1626 if (config == null) {
1627 String msg = "Invalid request: null flow config";
1629 return new Status(StatusCode.BADREQUEST, msg);
1631 // Validity check for api3 entry point
1632 if (config.isInternalFlow()) {
1633 String msg = "Invalid operation: Controller generated flow "
1634 + "cannot be modified";
1635 String logMsg = msg + ": {}";
1636 log.warn(logMsg, config);
1637 return new Status(StatusCode.NOTACCEPTABLE, msg);
1640 for (Map.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
1641 FlowConfig conf = entry.getValue();
1642 if (conf.isByNameAndNodeIdEqual(config)) {
1643 // Program the network node
1644 Status status = new Status(StatusCode.SUCCESS);
1645 if (conf.installInHw()) {
1646 status = this.removeEntry(conf.getFlowEntry(), false);
1648 status = this.addEntry(conf.getFlowEntry(), false);
1650 if (!status.isSuccess()) {
1651 conf.setStatus(status.getDescription());
1655 // Update Configuration database
1656 conf.setStatus(StatusCode.SUCCESS.toString());
1657 conf.toggleStatus();
1661 return new Status(StatusCode.NOTFOUND,
1662 "Unable to locate the entry. Failed to toggle status");
1666 * Uninstall all the Flow Entries present in the software view A copy of
1667 * each entry is stored in the inactive list so that it can be re-applied
1668 * when needed This function is called on the default container instance of
1669 * FRM only when the first container is created
1671 private void uninstallAllFlowEntries() {
1672 log.info("Uninstalling all flows");
1674 // Store entries / create target list
1675 for (ConcurrentMap.Entry<Node, Set<FlowEntryInstall>> mapEntry : nodeFlows
1677 for (FlowEntryInstall flowEntries : mapEntry.getValue()) {
1678 inactiveFlows.add(flowEntries.getOriginal());
1682 // Now remove the entries
1683 for (FlowEntry flowEntry : inactiveFlows) {
1684 Status status = this.removeEntry(flowEntry, false);
1685 if (!status.isSuccess()) {
1686 log.warn("Failed to remove entry: {}. The failure is: {}",
1687 flowEntry, status.getDescription());
1693 * Re-install all the Flow Entries present in the inactive list The inactive
1694 * list will be empty at the end of this call This function is called on the
1695 * default container instance of FRM only when the last container is deleted
1697 private void reinstallAllFlowEntries() {
1698 log.info("Reinstalling all inactive flows");
1700 for (FlowEntry flowEntry : this.inactiveFlows) {
1701 Status status = this.addEntry(flowEntry, false);
1702 if (!status.isSuccess()) {
1703 log.warn("Failed to install entry: {}. The failure is: {}",
1704 flowEntry, status.getDescription());
1708 // Empty inactive list in any case
1709 inactiveFlows.clear();
1712 public List<FlowConfig> getStaticFlows() {
1713 return getStaticFlowsOrderedList(staticFlows, staticFlowsOrdinal.get(0)
1717 // TODO: need to come out with a better algorithm for mantaining the order
1718 // of the configuration entries
1719 // with actual one, index associated to deleted entries cannot be reused and
1721 private List<FlowConfig> getStaticFlowsOrderedList(
1722 ConcurrentMap<Integer, FlowConfig> flowMap, int maxKey) {
1723 List<FlowConfig> orderedList = new ArrayList<FlowConfig>();
1724 for (int i = 0; i <= maxKey; i++) {
1725 FlowConfig entry = flowMap.get(i);
1726 if (entry != null) {
1727 orderedList.add(entry);
1734 public FlowConfig getStaticFlow(String name, Node node) {
1735 for (FlowConfig config : staticFlows.values()) {
1736 if (config.isByNameAndNodeIdEqual(name, node)) {
1744 public List<FlowConfig> getStaticFlows(Node node) {
1745 List<FlowConfig> list = new ArrayList<FlowConfig>();
1746 for (FlowConfig config : staticFlows.values()) {
1747 if (config.onNode(node)) {
1755 public List<String> getStaticFlowNamesForNode(Node node) {
1756 List<String> list = new ArrayList<String>();
1757 for (FlowConfig config : staticFlows.values()) {
1758 if (config.onNode(node)) {
1759 list.add(config.getName());
1766 public List<Node> getListNodeWithConfiguredFlows() {
1767 Set<Node> set = new HashSet<Node>();
1768 for (FlowConfig config : staticFlows.values()) {
1769 set.add(config.getNode());
1771 return new ArrayList<Node>(set);
1774 @SuppressWarnings("unchecked")
1775 private void loadFlowConfiguration() {
1776 ObjectReader objReader = new ObjectReader();
1777 ConcurrentMap<Integer, FlowConfig> confList = (ConcurrentMap<Integer, FlowConfig>) objReader
1778 .read(this, frmFileName);
1780 ConcurrentMap<String, PortGroupConfig> pgConfig = (ConcurrentMap<String, PortGroupConfig>) objReader
1781 .read(this, portGroupFileName);
1783 if (pgConfig != null) {
1784 for (Map.Entry<String, PortGroupConfig> entry : pgConfig.entrySet()) {
1785 addPortGroupConfig(entry.getKey(), entry.getValue()
1786 .getMatchString(), true);
1790 if (confList == null) {
1795 for (Integer key : confList.keySet()) {
1796 if (key.intValue() > maxKey)
1797 maxKey = key.intValue();
1800 for (FlowConfig conf : getStaticFlowsOrderedList(confList, maxKey)) {
1801 addStaticFlow(conf, true);
1806 public Object readObject(ObjectInputStream ois)
1807 throws FileNotFoundException, IOException, ClassNotFoundException {
1808 return ois.readObject();
1811 public Status saveConfig() {
1812 // Publish the save config event to the cluster nodes
1813 flowsSaveEvent.put(new Date().getTime(), SAVE);
1814 return saveConfigInternal();
1817 private Status saveConfigInternal() {
1818 ObjectWriter objWriter = new ObjectWriter();
1819 ConcurrentHashMap<Integer, FlowConfig> nonDynamicFlows = new ConcurrentHashMap<Integer, FlowConfig>();
1820 for (Integer ordinal : staticFlows.keySet()) {
1821 FlowConfig config = staticFlows.get(ordinal);
1822 // Do not save dynamic and controller generated static flows
1823 if (config.isDynamic() || config.isInternalFlow()) {
1826 nonDynamicFlows.put(ordinal, config);
1828 objWriter.write(nonDynamicFlows, frmFileName);
1829 objWriter.write(new ConcurrentHashMap<String, PortGroupConfig>(
1830 portGroupConfigs), portGroupFileName);
1831 return new Status(StatusCode.SUCCESS, null);
1835 public void entryCreated(Long key, String cacheName, boolean local) {
1839 public void entryUpdated(Long key, String new_value, String cacheName,
1840 boolean originLocal) {
1841 saveConfigInternal();
1845 public void entryDeleted(Long key, String cacheName, boolean originLocal) {
1849 public void subnetNotify(Subnet sub, boolean add) {
1852 private void installImplicitARPReplyPunt(Node node) {
1858 List<String> puntAction = new ArrayList<String>();
1859 puntAction.add(ActionType.CONTROLLER.toString());
1861 FlowConfig allowARP = new FlowConfig();
1862 allowARP.setInstallInHw(true);
1863 allowARP.setName("**Punt ARP Reply");
1864 allowARP.setPriority("500");
1865 allowARP.setNode(node);
1866 allowARP.setEtherType("0x"
1867 + Integer.toHexString(EtherTypes.ARP.intValue()).toUpperCase());
1868 allowARP.setDstMac(HexEncode.bytesToHexString(switchManager
1869 .getControllerMAC()));
1870 allowARP.setActions(puntAction);
1871 addStaticFlow(allowARP, false);
1875 public void modeChangeNotify(Node node, boolean proactive) {
1876 List<FlowConfig> defaultConfigs = new ArrayList<FlowConfig>();
1878 List<String> puntAction = new ArrayList<String>();
1879 puntAction.add(ActionType.CONTROLLER.toString());
1881 FlowConfig allowARP = new FlowConfig();
1882 allowARP.setInstallInHw(true);
1883 allowARP.setName("**Punt ARP");
1884 allowARP.setPriority("1");
1885 allowARP.setNode(node);
1886 allowARP.setEtherType("0x"
1887 + Integer.toHexString(EtherTypes.ARP.intValue()).toUpperCase());
1888 allowARP.setActions(puntAction);
1889 defaultConfigs.add(allowARP);
1891 FlowConfig allowLLDP = new FlowConfig();
1892 allowLLDP.setInstallInHw(true);
1893 allowLLDP.setName("**Punt LLDP");
1894 allowLLDP.setPriority("1");
1895 allowLLDP.setNode(node);
1898 + Integer.toHexString(EtherTypes.LLDP.intValue())
1900 allowLLDP.setActions(puntAction);
1901 defaultConfigs.add(allowLLDP);
1903 List<String> dropAction = new ArrayList<String>();
1904 dropAction.add(ActionType.DROP.toString());
1906 FlowConfig dropAllConfig = new FlowConfig();
1907 dropAllConfig.setInstallInHw(true);
1908 dropAllConfig.setName("**Catch-All Drop");
1909 dropAllConfig.setPriority("0");
1910 dropAllConfig.setNode(node);
1911 dropAllConfig.setActions(dropAction);
1912 defaultConfigs.add(dropAllConfig);
1914 for (FlowConfig fc : defaultConfigs) {
1916 addStaticFlow(fc, false);
1918 removeStaticFlow(fc);
1922 log.info("Set Switch {} Mode to {}", node, proactive);
1926 * Remove from the databases all the flows installed on the node
1930 private synchronized void cleanDatabaseForNode(Node node) {
1931 log.info("Cleaning Flow database for Node {}", node.toString());
1933 // Find out which groups the node's flows are part of
1934 Set<String> affectedGroups = new HashSet<String>();
1935 Set<FlowEntryInstall> flowEntryList = nodeFlows.get(node);
1936 if (flowEntryList != null) {
1937 for (FlowEntryInstall entry : flowEntryList) {
1938 String groupName = entry.getGroupName();
1939 if (groupName != null) {
1940 affectedGroups.add(groupName);
1945 // Remove the node's flows from the group indexed flow database
1946 if (!affectedGroups.isEmpty()) {
1947 for (String group : affectedGroups) {
1948 Set<FlowEntryInstall> flowList = groupFlows.get(group);
1949 Set<FlowEntryInstall> toRemove = new HashSet<FlowEntryInstall>();
1950 for (FlowEntryInstall entry : flowList) {
1951 if (node.equals(entry.getNode())) {
1952 toRemove.add(entry);
1955 flowList.removeAll(toRemove);
1956 if (flowList.isEmpty()) {
1957 groupFlows.remove(group);
1962 // Remove the node's flows from the node indexed flow database
1963 nodeFlows.remove(node);
1967 public void notifyNode(Node node, UpdateType type,
1968 Map<String, Property> propMap) {
1971 addStaticFlowsToSwitch(node);
1974 cleanDatabaseForNode(node);
1975 updateStaticFlowConfigsOnNodeDown(node);
1983 public void notifyNodeConnector(NodeConnector nodeConnector,
1984 UpdateType type, Map<String, Property> propMap) {
1987 private FlowConfig getDerivedFlowConfig(FlowConfig original,
1988 String configName, Short port) {
1989 FlowConfig derivedFlow = new FlowConfig(original);
1990 derivedFlow.setDynamic(true);
1991 derivedFlow.setPortGroup(null);
1992 derivedFlow.setName(original.getName() + "_" + configName + "_" + port);
1993 derivedFlow.setIngressPort(port + "");
1997 private void addPortGroupFlows(PortGroupConfig config, Node node,
1999 for (Iterator<FlowConfig> it = staticFlows.values().iterator(); it
2001 FlowConfig staticFlow = it.next();
2002 if (staticFlow.getPortGroup() == null) {
2005 if ((staticFlow.getNode().equals(node))
2006 && (staticFlow.getPortGroup().equals(config.getName()))) {
2007 for (Short port : data.getPorts()) {
2008 FlowConfig derivedFlow = getDerivedFlowConfig(staticFlow,
2009 config.getName(), port);
2010 addStaticFlow(derivedFlow, false);
2016 private void removePortGroupFlows(PortGroupConfig config, Node node,
2018 for (Iterator<FlowConfig> it = staticFlows.values().iterator(); it
2020 FlowConfig staticFlow = it.next();
2021 if (staticFlow.getPortGroup() == null) {
2024 if ((staticFlow.getNode().equals(node))
2025 && (staticFlow.getPortGroup().equals(config.getName()))) {
2026 for (Short port : data.getPorts()) {
2027 FlowConfig derivedFlow = getDerivedFlowConfig(staticFlow,
2028 config.getName(), port);
2029 removeStaticFlow(derivedFlow);
2036 public void portGroupChanged(PortGroupConfig config,
2037 Map<Node, PortGroup> data, boolean add) {
2038 log.info("PortGroup Changed for: {} Data: {}", config, portGroupData);
2039 Map<Node, PortGroup> existingData = portGroupData.get(config);
2040 if (existingData != null) {
2041 for (Map.Entry<Node, PortGroup> entry : data.entrySet()) {
2042 PortGroup existingPortGroup = existingData.get(entry.getKey());
2043 if (existingPortGroup == null) {
2045 existingData.put(entry.getKey(), entry.getValue());
2046 addPortGroupFlows(config, entry.getKey(),
2051 existingPortGroup.getPorts().addAll(
2052 entry.getValue().getPorts());
2053 addPortGroupFlows(config, entry.getKey(),
2056 existingPortGroup.getPorts().removeAll(
2057 entry.getValue().getPorts());
2058 removePortGroupFlows(config, entry.getKey(),
2065 portGroupData.put(config, data);
2066 for (Node swid : data.keySet()) {
2067 addPortGroupFlows(config, swid, data.get(swid));
2073 public boolean addPortGroupConfig(String name, String regex, boolean restore) {
2074 PortGroupConfig config = portGroupConfigs.get(name);
2078 if ((portGroupProvider == null) && !restore) {
2081 if ((portGroupProvider != null)
2082 && (!portGroupProvider.isMatchCriteriaSupported(regex))) {
2086 config = new PortGroupConfig(name, regex);
2087 portGroupConfigs.put(name, config);
2088 if (portGroupProvider != null) {
2089 portGroupProvider.createPortGroupConfig(config);
2094 public boolean delPortGroupConfig(String name) {
2095 PortGroupConfig config = portGroupConfigs.get(name);
2096 if (config == null) {
2100 if (portGroupProvider != null) {
2101 portGroupProvider.deletePortGroupConfig(config);
2103 portGroupConfigs.remove(name);
2107 private void usePortGroupConfig(String name) {
2108 PortGroupConfig config = portGroupConfigs.get(name);
2109 if (config == null) {
2112 if (portGroupProvider != null) {
2113 Map<Node, PortGroup> data = portGroupProvider
2114 .getPortGroupData(config);
2115 portGroupData.put(config, data);
2120 public Map<String, PortGroupConfig> getPortGroupConfigs() {
2121 return portGroupConfigs;
2124 public boolean isPortGroupSupported() {
2125 if (portGroupProvider == null) {
2131 // Fir PortGroupProvider to use regular Dependency Manager
2132 /* @SuppressWarnings("rawtypes") */
2133 /* public void bind(Object arg0, Map arg1) throws Exception { */
2134 /* if (arg0 instanceof PortGroupProvider) { */
2135 /* setPortGroupProvider((PortGroupProvider)arg0); */
2139 /* @SuppressWarnings("rawtypes") */
2141 /* public void unbind(Object arg0, Map arg1) throws Exception { */
2142 /* if (arg0 instanceof PortGroupProvider) { */
2143 /* portGroupProvider = null; */
2147 public void setIContainer(IContainer s) {
2151 public void unsetIContainer(IContainer s) {
2152 if (this.container == s) {
2153 this.container = null;
2157 public PortGroupProvider getPortGroupProvider() {
2158 return portGroupProvider;
2161 public void unsetPortGroupProvider(PortGroupProvider portGroupProvider) {
2162 this.portGroupProvider = null;
2165 public void setPortGroupProvider(PortGroupProvider portGroupProvider) {
2166 this.portGroupProvider = portGroupProvider;
2167 portGroupProvider.registerPortGroupChange(this);
2168 for (PortGroupConfig config : portGroupConfigs.values()) {
2169 portGroupProvider.createPortGroupConfig(config);
2173 public void setHostFinder(IfIptoHost hostFinder) {
2174 this.hostFinder = hostFinder;
2177 public void unsetHostFinder(IfIptoHost hostFinder) {
2178 if (this.hostFinder == hostFinder) {
2179 this.hostFinder = null;
2183 public void setFrmAware(IForwardingRulesManagerAware obj) {
2184 this.frmAware.add(obj);
2187 public void unsetFrmAware(IForwardingRulesManagerAware obj) {
2188 this.frmAware.remove(obj);
2191 void setClusterContainerService(IClusterContainerServices s) {
2192 log.debug("Cluster Service set");
2193 this.clusterContainerService = s;
2196 void unsetClusterContainerService(IClusterContainerServices s) {
2197 if (this.clusterContainerService == s) {
2198 log.debug("Cluster Service removed!");
2199 this.clusterContainerService = null;
2203 private String getContainerName() {
2204 if (container == null) {
2205 return GlobalConstants.DEFAULT.toString();
2207 return container.getName();
2211 * Function called by the dependency manager when all the required
2212 * dependencies are satisfied
2216 frmAware = Collections
2217 .synchronizedSet(new HashSet<IForwardingRulesManagerAware>());
2218 frmFileName = GlobalConstants.STARTUPHOME.toString()
2219 + "frm_staticflows_" + this.getContainerName() + ".conf";
2220 portGroupFileName = GlobalConstants.STARTUPHOME.toString()
2221 + "portgroup_" + this.getContainerName() + ".conf";
2223 inContainerMode = false;
2225 if (portGroupProvider != null) {
2226 portGroupProvider.registerPortGroupChange(this);
2229 nonClusterObjectCreate();
2233 registerWithOSGIConsole();
2236 * If we are not the first cluster node to come up, do not initialize
2237 * the static flow entries ordinal
2239 if (staticFlowsOrdinal.size() == 0) {
2240 staticFlowsOrdinal.put(0, Integer.valueOf(0));
2245 * Function called by the dependency manager when at least one dependency
2246 * become unsatisfied or when the component is shutting down because for
2247 * example bundle is being stopped.
2255 * Function called by dependency manager after "init ()" is called and after
2256 * the services provided by the class are registered in the service registry
2261 * Read startup and build database if we have not already gotten the
2262 * configurations synced from another node
2264 if (staticFlows.isEmpty()) {
2265 loadFlowConfiguration();
2270 * Function called by the dependency manager before the services exported by
2271 * the component are unregistered, this will be followed by a "destroy ()"
2278 public void setFlowProgrammerService(IFlowProgrammerService service) {
2279 this.programmer = service;
2282 public void unsetFlowProgrammerService(IFlowProgrammerService service) {
2283 if (this.programmer == service) {
2284 this.programmer = null;
2288 public void setSwitchManager(ISwitchManager switchManager) {
2289 this.switchManager = switchManager;
2292 public void unsetSwitchManager(ISwitchManager switchManager) {
2293 if (this.switchManager == switchManager) {
2294 this.switchManager = null;
2299 public void tagUpdated(String containerName, Node n, short oldTag,
2300 short newTag, UpdateType t) {
2305 public void containerFlowUpdated(String containerName,
2306 ContainerFlow previousFlow, ContainerFlow currentFlow, UpdateType t) {
2308 * Whether it is an addition or removal, we have to recompute the merged
2309 * flows entries taking into account all the current container flows
2310 * because flow merging is not an injective function
2312 updateFlowsContainerFlow();
2316 public void nodeConnectorUpdated(String containerName, NodeConnector p,
2322 public void containerModeUpdated(UpdateType update) {
2325 this.inContainerMode = true;
2326 this.uninstallAllFlowEntries();
2329 this.inContainerMode = false;
2330 this.reinstallAllFlowEntries();
2335 // Update our configuration DB
2336 updateStaticFlowConfigsOnContainerModeChange(update);
2343 public String getHelp() {
2344 StringBuffer help = new StringBuffer();
2345 help.append("---FRM Matrix Application---\n");
2346 help.append("\t printMatrixData - Prints the Matrix Configs\n");
2347 help.append("\t addMatrixConfig <name> <regex>\n");
2348 help.append("\t delMatrixConfig <name>\n");
2349 help.append("\t useMatrixConfig <name>\n");
2350 return help.toString();
2353 public void _printMatrixData(CommandInterpreter ci) {
2354 ci.println("Configs : ");
2355 ci.println("---------");
2356 ci.println(portGroupConfigs);
2358 ci.println("Data : ");
2359 ci.println("------");
2360 ci.println(portGroupData);
2363 public void _addMatrixConfig(CommandInterpreter ci) {
2364 String name = ci.nextArgument();
2365 String regex = ci.nextArgument();
2366 addPortGroupConfig(name, regex, false);
2369 public void _delMatrixConfig(CommandInterpreter ci) {
2370 String name = ci.nextArgument();
2371 delPortGroupConfig(name);
2374 public void _useMatrixConfig(CommandInterpreter ci) {
2375 String name = ci.nextArgument();
2376 usePortGroupConfig(name);
2379 public void _arpPunt(CommandInterpreter ci) {
2380 String switchId = ci.nextArgument();
2381 long swid = HexEncode.stringToLong(switchId);
2382 Node node = NodeCreator.createOFNode(swid);
2383 installImplicitARPReplyPunt(node);
2386 public void _frmaddflow(CommandInterpreter ci) throws UnknownHostException {
2388 String nodeId = ci.nextArgument();
2389 if (nodeId == null) {
2390 ci.print("Node id not specified");
2394 node = NodeCreator.createOFNode(Long.valueOf(nodeId));
2395 } catch (NumberFormatException e) {
2396 ci.print("Node id not a number");
2399 ci.println(this.programmer.addFlow(node, getSampleFlow(node)));
2402 public void _frmremoveflow(CommandInterpreter ci)
2403 throws UnknownHostException {
2405 String nodeId = ci.nextArgument();
2406 if (nodeId == null) {
2407 ci.print("Node id not specified");
2411 node = NodeCreator.createOFNode(Long.valueOf(nodeId));
2412 } catch (NumberFormatException e) {
2413 ci.print("Node id not a number");
2416 ci.println(this.programmer.removeFlow(node, getSampleFlow(node)));
2419 private Flow getSampleFlow(Node node) throws UnknownHostException {
2420 NodeConnector port = NodeConnectorCreator.createOFNodeConnector(
2422 NodeConnector oport = NodeConnectorCreator.createOFNodeConnector(
2424 byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
2425 (byte) 0x9a, (byte) 0xbc };
2426 byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d,
2427 (byte) 0x5e, (byte) 0x6f };
2428 InetAddress srcIP = InetAddress.getByName("172.28.30.50");
2429 InetAddress dstIP = InetAddress.getByName("171.71.9.52");
2430 InetAddress ipMask = InetAddress.getByName("255.255.255.0");
2431 InetAddress ipMask2 = InetAddress.getByName("255.0.0.0");
2432 short ethertype = EtherTypes.IPv4.shortValue();
2433 short vlan = (short) 27;
2436 byte proto = IPProtocols.TCP.byteValue();
2437 short src = (short) 55000;
2441 * Create a SAL Flow aFlow
2443 Match match = new Match();
2444 match.setField(MatchType.IN_PORT, port);
2445 match.setField(MatchType.DL_SRC, srcMac);
2446 match.setField(MatchType.DL_DST, dstMac);
2447 match.setField(MatchType.DL_TYPE, ethertype);
2448 match.setField(MatchType.DL_VLAN, vlan);
2449 match.setField(MatchType.DL_VLAN_PR, vlanPr);
2450 match.setField(MatchType.NW_SRC, srcIP, ipMask);
2451 match.setField(MatchType.NW_DST, dstIP, ipMask2);
2452 match.setField(MatchType.NW_TOS, tos);
2453 match.setField(MatchType.NW_PROTO, proto);
2454 match.setField(MatchType.TP_SRC, src);
2455 match.setField(MatchType.TP_DST, dst);
2457 List<Action> actions = new ArrayList<Action>();
2458 actions.add(new Output(oport));
2459 actions.add(new PopVlan());
2460 actions.add(new Flood());
2461 actions.add(new Controller());
2462 return new Flow(match, actions);
2466 public Status saveConfiguration() {
2467 return saveConfig();
2470 public void _frmNodeFlows(CommandInterpreter ci) {
2471 boolean verbose = false;
2472 String verboseCheck = ci.nextArgument();
2473 if (verboseCheck != null) {
2474 verbose = verboseCheck.equals("true");
2477 // Dump per node database
2478 for (Entry<Node, Set<FlowEntryInstall>> entry : this.nodeFlows
2480 Node node = entry.getKey();
2481 for (FlowEntryInstall flow : entry.getValue()) {
2483 ci.println(node + " " + flow.getFlowName());
2485 ci.println(node + " " + flow.toString());
2491 public void _frmGroupFlows(CommandInterpreter ci) {
2492 boolean verbose = false;
2493 String verboseCheck = ci.nextArgument();
2494 if (verboseCheck != null) {
2495 verbose = verboseCheck.equalsIgnoreCase("true");
2498 // Dump per node database
2499 for (Entry<String, Set<FlowEntryInstall>> entry : this.groupFlows
2501 String group = entry.getKey();
2502 ci.println("Group " + group + ":");
2503 for (FlowEntryInstall flow : entry.getValue()) {
2505 ci.println(flow.getNode() + " " + flow.getFlowName());
2507 ci.println(flow.getNode() + " " + flow.toString());
2514 public void flowRemoved(Node node, Flow flow) {
2515 log.trace("Received flow removed notification on {} for {}", node, flow);
2516 // For flow entry identification, only match and priority matter
2517 FlowEntry toFind = new FlowEntry("any", "any", flow, node);
2518 FlowEntryInstall installedEntry = this.findMatch(toFind, false);
2519 if (installedEntry == null) {
2520 log.trace("Entry is not know to us");
2524 // Update Static flow status
2525 for (Map.Entry<Integer, FlowConfig> entry : staticFlows.entrySet()) {
2526 FlowConfig conf = entry.getValue();
2527 if (conf.isByNameAndNodeIdEqual(installedEntry.getFlowName(), node)) {
2528 // Update Configuration database
2529 conf.toggleStatus();
2533 // Update software views
2534 this.updateLocalDatabase(installedEntry, false);
2538 public synchronized void flowErrorReported(Node node, long rid, Object err) {
2539 log.trace("Got error {} for message rid {} from node {}",
2540 new Object[] {err, rid, node });
2542 * If this was for a flow install, remove the corresponding entry
2543 * from the software view. If it was a Looking for the rid going through the
2544 * software database.
2545 * TODO: A more efficient rid <-> FlowEntryInstall mapping will
2546 * have to be added in future
2548 Set<FlowEntryInstall> entries = nodeFlows.get(node);
2549 if (entries != null) {
2550 FlowEntryInstall target = null;
2551 for (FlowEntryInstall entry : entries) {
2552 if (entry.getRequestId() == rid) {
2557 if (target != null) {
2558 // This was a flow install, update database
2559 this.updateLocalDatabase(target, false);
2564 if (frmAware != null) {
2565 synchronized (frmAware) {
2566 for (IForwardingRulesManagerAware frma : frmAware) {
2568 frma.requestFailed(rid, err.toString());
2569 } catch (Exception e) {
2570 log.warn("Failed to notify {}", frma);
2578 public Status solicitStatusResponse(Node node, boolean blocking) {
2579 Status rv = new Status(StatusCode.INTERNALERROR);
2581 if (this.programmer != null) {
2583 rv = programmer.syncSendBarrierMessage(node);
2585 rv = programmer.asyncSendBarrierMessage(node);