blob: 82172559290618112d057f22baba608b0e70c501 [file] [log] [blame]
Yi Tsengc927a062017-05-02 15:02:37 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengc927a062017-05-02 15:02:37 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.intent.impl.installer;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.net.flow.FlowRule;
25import org.onosproject.net.flow.FlowRuleOperations;
26import org.onosproject.net.flow.FlowRuleOperationsContext;
27import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.net.intent.FlowRuleIntent;
29import org.onosproject.net.intent.IntentInstallCoordinator;
30import org.onosproject.net.intent.IntentData;
31import org.onosproject.net.intent.IntentExtensionService;
32import org.onosproject.net.intent.IntentOperationContext;
33import org.onosproject.net.intent.IntentInstaller;
34import org.onosproject.net.intent.impl.IntentManager;
Yi Tseng24d9be72017-05-12 11:28:13 -070035import org.onosproject.net.intent.ObjectiveTrackerService;
Yi Tsengc927a062017-05-02 15:02:37 -070036import org.slf4j.Logger;
37
38import java.util.Collection;
Yi Tseng4a7d1e12017-07-10 16:19:17 -070039import java.util.Collections;
Yi Tsengc927a062017-05-02 15:02:37 -070040import java.util.List;
41import java.util.Optional;
42import java.util.Set;
Yi Tseng4a7d1e12017-07-10 16:19:17 -070043import java.util.stream.Collectors;
Yi Tseng49a2b2e2017-05-11 14:32:16 -070044
Yi Tsengc927a062017-05-02 15:02:37 -070045import static org.onosproject.net.intent.IntentInstaller.Direction.ADD;
46import static org.onosproject.net.intent.IntentInstaller.Direction.REMOVE;
Yi Tsengc927a062017-05-02 15:02:37 -070047import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Installer for FlowRuleIntent.
51 */
52@Component(immediate = true)
53public class FlowRuleIntentInstaller implements IntentInstaller<FlowRuleIntent> {
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService intentExtensionService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected ObjectiveTrackerService trackerService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected IntentInstallCoordinator intentInstallCoordinator;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected FlowRuleService flowRuleService;
65
66 @Activate
67 public void activate() {
68 intentExtensionService.registerInstaller(FlowRuleIntent.class, this);
69 }
70
71 @Deactivate
72 public void deactivated() {
73 intentExtensionService.unregisterInstaller(FlowRuleIntent.class);
74 }
75
76 protected final Logger log = getLogger(IntentManager.class);
77
78 @Override
79 public void apply(IntentOperationContext<FlowRuleIntent> context) {
80 Optional<IntentData> toUninstall = context.toUninstall();
81 Optional<IntentData> toInstall = context.toInstall();
82
Yi Tsengc927a062017-05-02 15:02:37 -070083 if (!toInstall.isPresent() && !toUninstall.isPresent()) {
Yi Tseng4a7d1e12017-07-10 16:19:17 -070084 // Nothing to do.
Yi Tsengc927a062017-05-02 15:02:37 -070085 intentInstallCoordinator.intentInstallSuccess(context);
86 return;
Yi Tseng4a7d1e12017-07-10 16:19:17 -070087 }
88
89 List<FlowRuleIntent> uninstallIntents = context.intentsToUninstall();
90 List<FlowRuleIntent> installIntents = context.intentsToInstall();
91
92 List<FlowRule> flowRulesToUninstall;
93 List<FlowRule> flowRulesToInstall;
94
95 if (toUninstall.isPresent()) {
96 // Remove tracked resource from both Intent and installable Intents.
Yi Tsengc927a062017-05-02 15:02:37 -070097 trackIntentResources(toUninstall.get(), uninstallIntents, REMOVE);
Yi Tseng4a7d1e12017-07-10 16:19:17 -070098
99 // Retrieves all flow rules from all flow rule Intents.
100 flowRulesToUninstall = uninstallIntents.stream()
101 .map(FlowRuleIntent::flowRules)
102 .flatMap(Collection::stream)
103 .collect(Collectors.toList());
Yi Tsengc927a062017-05-02 15:02:37 -0700104 } else {
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700105 // No flow rules to be uninstalled.
106 flowRulesToUninstall = Collections.emptyList();
107 }
108
109 if (toInstall.isPresent()) {
110 // Track resource from both Intent and installable Intents.
111 trackIntentResources(toInstall.get(), installIntents, ADD);
112
113 // Retrieves all flow rules from all flow rule Intents.
114 flowRulesToInstall = installIntents.stream()
115 .map(FlowRuleIntent::flowRules)
116 .flatMap(Collection::stream)
117 .collect(Collectors.toList());
118 } else {
119 // No flow rules to be installed.
120 flowRulesToInstall = Collections.emptyList();
121 }
122
Yi Tsengc29d8822017-10-25 16:19:25 -0700123 List<FlowRule> flowRuleToModify;
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700124 List<FlowRule> dontTouch;
125
126 // If both uninstall/install list contained equal (=match conditions are equal) FlowRules,
127 // omit it from remove list, since it will/should be overwritten by install
Yi Tsengc29d8822017-10-25 16:19:25 -0700128 flowRuleToModify = flowRulesToInstall.stream()
129 .filter(flowRule -> flowRulesToUninstall.stream().anyMatch(flowRule::equals))
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700130 .collect(Collectors.toList());
131
132 // If both contained exactMatch-ing FlowRules, remove from both list,
133 // since it will result in no-op.
134 dontTouch = flowRulesToInstall.stream()
135 .filter(flowRule -> flowRulesToUninstall.stream().anyMatch(flowRule::exactMatch))
136 .collect(Collectors.toList());
137
Yi Tsengc29d8822017-10-25 16:19:25 -0700138 flowRulesToUninstall.removeAll(flowRuleToModify);
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700139 flowRulesToUninstall.removeAll(dontTouch);
Yi Tsengc29d8822017-10-25 16:19:25 -0700140 flowRulesToInstall.removeAll(flowRuleToModify);
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700141 flowRulesToInstall.removeAll(dontTouch);
Yi Tsengc29d8822017-10-25 16:19:25 -0700142 flowRuleToModify.removeAll(dontTouch);
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700143
Yi Tsengc29d8822017-10-25 16:19:25 -0700144 if (flowRulesToInstall.isEmpty() && flowRulesToUninstall.isEmpty() && flowRuleToModify.isEmpty()) {
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700145 // There is no flow rules to install/uninstall
146 intentInstallCoordinator.intentInstallSuccess(context);
147 return;
Yi Tsengc927a062017-05-02 15:02:37 -0700148 }
149
150 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700151 // Add flows
152 flowRulesToInstall.forEach(builder::add);
Yi Tsengc29d8822017-10-25 16:19:25 -0700153 // Modify flows
154 flowRuleToModify.forEach(builder::modify);
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700155 // Remove flows
156 flowRulesToUninstall.forEach(builder::remove);
Yi Tsengc927a062017-05-02 15:02:37 -0700157
158 FlowRuleOperationsContext flowRuleOperationsContext = new FlowRuleOperationsContext() {
159 @Override
160 public void onSuccess(FlowRuleOperations ops) {
161 intentInstallCoordinator.intentInstallSuccess(context);
162 }
163
164 @Override
165 public void onError(FlowRuleOperations ops) {
166 intentInstallCoordinator.intentInstallFailed(context);
167 }
168 };
169
170 FlowRuleOperations operations = builder.build(flowRuleOperationsContext);
Yi Tsengc927a062017-05-02 15:02:37 -0700171 log.debug("applying intent {} -> {} with {} rules: {}",
172 toUninstall.map(x -> x.key().toString()).orElse("<empty>"),
173 toInstall.map(x -> x.key().toString()).orElse("<empty>"),
174 operations.stages().stream().mapToLong(Set::size).sum(),
175 operations.stages());
176 flowRuleService.apply(operations);
177 }
178
179 /**
180 * Track or un-track network resource of a Intent and it's installable
181 * Intents.
182 *
183 * @param intentData the Intent data
184 * @param intentsToApply the list of flow rule Intents from the Intent
185 * @param direction the direction to determine track or un-track
186 */
187 private void trackIntentResources(IntentData intentData, List<FlowRuleIntent> intentsToApply, Direction direction) {
188 switch (direction) {
189 case ADD:
190 trackerService.addTrackedResources(intentData.key(), intentData.intent().resources());
191 intentsToApply.forEach(installable ->
192 trackerService.addTrackedResources(intentData.key(),
193 installable.resources()));
194 break;
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700195 case REMOVE:
Yi Tsengc927a062017-05-02 15:02:37 -0700196 trackerService.removeTrackedResources(intentData.key(), intentData.intent().resources());
197 intentsToApply.forEach(installable ->
198 trackerService.removeTrackedResources(intentData.intent().key(),
199 installable.resources()));
200 break;
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700201 default:
202 log.warn("Unknown resource tracking direction.");
203 break;
Yi Tsengc927a062017-05-02 15:02:37 -0700204 }
205 }
Yi Tsengc927a062017-05-02 15:02:37 -0700206}