blob: 0cdd01d79fc71c8fbf01996bc5e88e87d8935e58 [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
123 List<FlowRule> dontUninstall;
124 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
128 dontUninstall = flowRulesToUninstall.stream()
129 .filter(flowRule -> flowRulesToInstall.stream().anyMatch(flowRule::equals))
130 .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
138 flowRulesToUninstall.removeAll(dontUninstall);
139 flowRulesToUninstall.removeAll(dontTouch);
140 flowRulesToInstall.removeAll(dontTouch);
141
142 if (flowRulesToInstall.isEmpty() && flowRulesToUninstall.isEmpty()) {
143 // There is no flow rules to install/uninstall
144 intentInstallCoordinator.intentInstallSuccess(context);
145 return;
Yi Tsengc927a062017-05-02 15:02:37 -0700146 }
147
148 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700149 // Add flows
150 flowRulesToInstall.forEach(builder::add);
151 // Remove flows
152 flowRulesToUninstall.forEach(builder::remove);
Yi Tsengc927a062017-05-02 15:02:37 -0700153
154 FlowRuleOperationsContext flowRuleOperationsContext = new FlowRuleOperationsContext() {
155 @Override
156 public void onSuccess(FlowRuleOperations ops) {
157 intentInstallCoordinator.intentInstallSuccess(context);
158 }
159
160 @Override
161 public void onError(FlowRuleOperations ops) {
162 intentInstallCoordinator.intentInstallFailed(context);
163 }
164 };
165
166 FlowRuleOperations operations = builder.build(flowRuleOperationsContext);
Yi Tsengc927a062017-05-02 15:02:37 -0700167 log.debug("applying intent {} -> {} with {} rules: {}",
168 toUninstall.map(x -> x.key().toString()).orElse("<empty>"),
169 toInstall.map(x -> x.key().toString()).orElse("<empty>"),
170 operations.stages().stream().mapToLong(Set::size).sum(),
171 operations.stages());
172 flowRuleService.apply(operations);
173 }
174
175 /**
176 * Track or un-track network resource of a Intent and it's installable
177 * Intents.
178 *
179 * @param intentData the Intent data
180 * @param intentsToApply the list of flow rule Intents from the Intent
181 * @param direction the direction to determine track or un-track
182 */
183 private void trackIntentResources(IntentData intentData, List<FlowRuleIntent> intentsToApply, Direction direction) {
184 switch (direction) {
185 case ADD:
186 trackerService.addTrackedResources(intentData.key(), intentData.intent().resources());
187 intentsToApply.forEach(installable ->
188 trackerService.addTrackedResources(intentData.key(),
189 installable.resources()));
190 break;
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700191 case REMOVE:
Yi Tsengc927a062017-05-02 15:02:37 -0700192 trackerService.removeTrackedResources(intentData.key(), intentData.intent().resources());
193 intentsToApply.forEach(installable ->
194 trackerService.removeTrackedResources(intentData.intent().key(),
195 installable.resources()));
196 break;
Yi Tseng4a7d1e12017-07-10 16:19:17 -0700197 default:
198 log.warn("Unknown resource tracking direction.");
199 break;
Yi Tsengc927a062017-05-02 15:02:37 -0700200 }
201 }
Yi Tsengc927a062017-05-02 15:02:37 -0700202}