Jonathan Hart
Committed by Ray Milkey

LinkCollectionIntentInstaller - reuse same code for install and uninstall

  + cosmetic changes

Change-Id: Ie8dbeab8b3b3fafcebc6e48306660533dd0af4e3
...@@ -54,7 +54,7 @@ public final class LinkCollectionIntent extends ConnectivityIntent { ...@@ -54,7 +54,7 @@ public final class LinkCollectionIntent extends ConnectivityIntent {
54 TrafficTreatment treatment, 54 TrafficTreatment treatment,
55 Set<Link> links, 55 Set<Link> links,
56 ConnectPoint egressPoint) { 56 ConnectPoint egressPoint) {
57 - this(appId, selector , treatment, links, egressPoint, Collections.emptyList()); 57 + this(appId, selector, treatment, links, egressPoint, Collections.emptyList());
58 } 58 }
59 59
60 /** 60 /**
......
...@@ -15,13 +15,8 @@ ...@@ -15,13 +15,8 @@
15 */ 15 */
16 package org.onosproject.net.intent.impl; 16 package org.onosproject.net.intent.impl;
17 17
18 -import java.util.HashMap; 18 +import com.google.common.collect.HashMultimap;
19 -import java.util.HashSet; 19 +import com.google.common.collect.SetMultimap;
20 -import java.util.List;
21 -import java.util.Map;
22 -import java.util.Map.Entry;
23 -import java.util.Set;
24 -
25 import org.apache.felix.scr.annotations.Activate; 20 import org.apache.felix.scr.annotations.Activate;
26 import org.apache.felix.scr.annotations.Component; 21 import org.apache.felix.scr.annotations.Component;
27 import org.apache.felix.scr.annotations.Deactivate; 22 import org.apache.felix.scr.annotations.Deactivate;
...@@ -48,7 +43,10 @@ import org.onosproject.net.intent.IntentInstaller; ...@@ -48,7 +43,10 @@ import org.onosproject.net.intent.IntentInstaller;
48 import org.onosproject.net.intent.LinkCollectionIntent; 43 import org.onosproject.net.intent.LinkCollectionIntent;
49 import org.onosproject.net.intent.PathIntent; 44 import org.onosproject.net.intent.PathIntent;
50 45
51 -import com.google.common.collect.Lists; 46 +import java.util.Collections;
47 +import java.util.List;
48 +import java.util.Set;
49 +import java.util.stream.Collectors;
52 50
53 /** 51 /**
54 * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path 52 * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path
...@@ -79,60 +77,37 @@ public class LinkCollectionIntentInstaller ...@@ -79,60 +77,37 @@ public class LinkCollectionIntentInstaller
79 77
80 @Override 78 @Override
81 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) { 79 public List<FlowRuleBatchOperation> install(LinkCollectionIntent intent) {
82 - Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>(); 80 + return generateBatchOperations(intent, FlowRuleOperation.ADD);
83 - List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
84 -
85 - for (Link link : intent.links()) {
86 - if (outputMap.get(link.src().deviceId()) == null) {
87 - outputMap.put(link.src().deviceId(), new HashSet<PortNumber>());
88 - }
89 - outputMap.get(link.src().deviceId()).add(link.src().port());
90 -
91 - }
92 -
93 - for (ConnectPoint egressPoint : intent.egressPoints()) {
94 - if (outputMap.get(egressPoint.deviceId()) == null) {
95 - outputMap
96 - .put(egressPoint.deviceId(), new HashSet<PortNumber>());
97 } 81 }
98 - outputMap.get(egressPoint.deviceId()).add(egressPoint.port());
99 82
83 + @Override
84 + public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
85 + return generateBatchOperations(intent, FlowRuleOperation.REMOVE);
100 } 86 }
101 87
102 - for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) { 88 + private List<FlowRuleBatchOperation> generateBatchOperations(
103 - rules.add(createBatchEntry(FlowRuleOperation.ADD, intent, 89 + LinkCollectionIntent intent, FlowRuleOperation operation) {
104 - entry.getKey(), entry.getValue()));
105 - }
106 90
107 - return Lists.newArrayList(new FlowRuleBatchOperation(rules)); 91 + SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
108 - }
109 -
110 - @Override
111 - public List<FlowRuleBatchOperation> uninstall(LinkCollectionIntent intent) {
112 - Map<DeviceId, Set<PortNumber>> outputMap = new HashMap<DeviceId, Set<PortNumber>>();
113 - List<FlowRuleBatchEntry> rules = Lists.newLinkedList();
114 92
115 for (Link link : intent.links()) { 93 for (Link link : intent.links()) {
116 - if (outputMap.get(link.src().deviceId()) == null) { 94 + outputPorts.put(link.src().deviceId(), link.src().port());
117 - outputMap.put(link.src().deviceId(), new HashSet<PortNumber>());
118 - }
119 - outputMap.get(link.src().deviceId()).add(link.src().port());
120 } 95 }
121 96
122 for (ConnectPoint egressPoint : intent.egressPoints()) { 97 for (ConnectPoint egressPoint : intent.egressPoints()) {
123 - if (outputMap.get(egressPoint.deviceId()) == null) { 98 + outputPorts.put(egressPoint.deviceId(), egressPoint.port());
124 - outputMap
125 - .put(egressPoint.deviceId(), new HashSet<PortNumber>());
126 - }
127 - outputMap.get(egressPoint.deviceId()).add(egressPoint.port());
128 } 99 }
129 100
130 - for (Entry<DeviceId, Set<PortNumber>> entry : outputMap.entrySet()) { 101 + FlowRuleBatchOperation batchOperation =
131 - rules.add(createBatchEntry(FlowRuleOperation.REMOVE, intent, 102 + new FlowRuleBatchOperation(outputPorts
132 - entry.getKey(), entry.getValue())); 103 + .keys()
133 - } 104 + .stream()
105 + .map(deviceId -> createBatchEntry(operation,
106 + intent, deviceId,
107 + outputPorts.get(deviceId)))
108 + .collect(Collectors.toList()));
134 109
135 - return Lists.newArrayList(new FlowRuleBatchOperation(rules)); 110 + return Collections.singletonList(batchOperation);
136 } 111 }
137 112
138 @Override 113 @Override
...@@ -148,7 +123,7 @@ public class LinkCollectionIntentInstaller ...@@ -148,7 +123,7 @@ public class LinkCollectionIntentInstaller
148 * @param operation the FlowRuleOperation to use 123 * @param operation the FlowRuleOperation to use
149 * @param intent the link collection intent 124 * @param intent the link collection intent
150 * @param deviceId the device ID for the flow rule 125 * @param deviceId the device ID for the flow rule
151 - * @param outPort the output port of the flow rule 126 + * @param outPorts the set of output ports for the flow rule
152 * @return the new flow rule batch entry 127 * @return the new flow rule batch entry
153 */ 128 */
154 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation, 129 private FlowRuleBatchEntry createBatchEntry(FlowRuleOperation operation,
...@@ -168,8 +143,9 @@ public class LinkCollectionIntentInstaller ...@@ -168,8 +143,9 @@ public class LinkCollectionIntentInstaller
168 .builder(intent.selector()).build(); 143 .builder(intent.selector()).build();
169 144
170 FlowRule rule = new DefaultFlowRule(deviceId, 145 FlowRule rule = new DefaultFlowRule(deviceId,
171 - selector, treatment, 123, 146 + selector, treatment, 123, appId,
172 - appId, new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true); 147 + new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
148 + 0, true);
173 149
174 return new FlowRuleBatchEntry(operation, rule); 150 return new FlowRuleBatchEntry(operation, rule);
175 } 151 }
......
...@@ -94,14 +94,16 @@ public class PathIntentInstaller implements IntentInstaller<PathIntent> { ...@@ -94,14 +94,16 @@ public class PathIntentInstaller implements IntentInstaller<PathIntent> {
94 while (links.hasNext()) { 94 while (links.hasNext()) {
95 builder.matchInport(prev.port()); 95 builder.matchInport(prev.port());
96 Link link = links.next(); 96 Link link = links.next();
97 - TrafficTreatment treatment =
98 // if this is the last flow rule, apply the intent's treatments 97 // if this is the last flow rule, apply the intent's treatments
98 + TrafficTreatment treatment =
99 (links.hasNext() ? builder() : builder(intent.treatment())) 99 (links.hasNext() ? builder() : builder(intent.treatment()))
100 .setOutput(link.src().port()).build(); 100 .setOutput(link.src().port()).build();
101 101
102 FlowRule rule = new DefaultFlowRule(link.src().deviceId(), 102 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
103 builder.build(), treatment, 123, //FIXME 123 103 builder.build(), treatment, 123, //FIXME 123
104 - appId, new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true); 104 + appId,
105 + new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
106 + 0, true);
105 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule, 107 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, rule,
106 intent.id().fingerprint())); 108 intent.id().fingerprint()));
107 prev = link.dst(); 109 prev = link.dst();
...@@ -124,12 +126,14 @@ public class PathIntentInstaller implements IntentInstaller<PathIntent> { ...@@ -124,12 +126,14 @@ public class PathIntentInstaller implements IntentInstaller<PathIntent> {
124 while (links.hasNext()) { 126 while (links.hasNext()) {
125 builder.matchInport(prev.port()); 127 builder.matchInport(prev.port());
126 Link link = links.next(); 128 Link link = links.next();
127 - TrafficTreatment treatment = // if this is the last flow rule, apply the intent's treatments 129 + // if this is the last flow rule, apply the intent's treatments
130 + TrafficTreatment treatment =
128 (links.hasNext() ? builder() : builder(intent.treatment())) 131 (links.hasNext() ? builder() : builder(intent.treatment()))
129 .setOutput(link.src().port()).build(); 132 .setOutput(link.src().port()).build();
130 FlowRule rule = new DefaultFlowRule(link.src().deviceId(), 133 FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
131 - builder.build(), treatment, 134 + builder.build(), treatment, 123, appId,
132 - 123, appId, new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)), 0, true); 135 + new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
136 + 0, true);
133 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule, 137 rules.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, rule,
134 intent.id().fingerprint())); 138 intent.id().fingerprint()));
135 prev = link.dst(); 139 prev = link.dst();
......