Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.net.intent.impl.installer; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.net.flow.FlowRule; |
| 25 | import org.onosproject.net.flow.FlowRuleOperations; |
| 26 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 27 | import org.onosproject.net.flow.FlowRuleService; |
| 28 | import org.onosproject.net.intent.FlowRuleIntent; |
| 29 | import org.onosproject.net.intent.IntentInstallCoordinator; |
| 30 | import org.onosproject.net.intent.IntentData; |
| 31 | import org.onosproject.net.intent.IntentExtensionService; |
| 32 | import org.onosproject.net.intent.IntentOperationContext; |
| 33 | import org.onosproject.net.intent.IntentInstaller; |
| 34 | import org.onosproject.net.intent.impl.IntentManager; |
Yi Tseng | 24d9be7 | 2017-05-12 11:28:13 -0700 | [diff] [blame] | 35 | import org.onosproject.net.intent.ObjectiveTrackerService; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | import java.util.Collection; |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 39 | import java.util.Collections; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | import java.util.Optional; |
| 42 | import java.util.Set; |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 43 | import java.util.stream.Collectors; |
Yi Tseng | 49a2b2e | 2017-05-11 14:32:16 -0700 | [diff] [blame] | 44 | |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 45 | import static org.onosproject.net.intent.IntentInstaller.Direction.ADD; |
| 46 | import static org.onosproject.net.intent.IntentInstaller.Direction.REMOVE; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * Installer for FlowRuleIntent. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | public 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 Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 83 | if (!toInstall.isPresent() && !toUninstall.isPresent()) { |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 84 | // Nothing to do. |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 85 | intentInstallCoordinator.intentInstallSuccess(context); |
| 86 | return; |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 87 | } |
| 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 Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 97 | trackIntentResources(toUninstall.get(), uninstallIntents, REMOVE); |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 98 | |
| 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 Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 104 | } else { |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 105 | // 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 Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 123 | List<FlowRule> flowRuleToModify; |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 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 |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 128 | flowRuleToModify = flowRulesToInstall.stream() |
| 129 | .filter(flowRule -> flowRulesToUninstall.stream().anyMatch(flowRule::equals)) |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 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 | |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 138 | flowRulesToUninstall.removeAll(flowRuleToModify); |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 139 | flowRulesToUninstall.removeAll(dontTouch); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 140 | flowRulesToInstall.removeAll(flowRuleToModify); |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 141 | flowRulesToInstall.removeAll(dontTouch); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 142 | flowRuleToModify.removeAll(dontTouch); |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 143 | |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 144 | if (flowRulesToInstall.isEmpty() && flowRulesToUninstall.isEmpty() && flowRuleToModify.isEmpty()) { |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 145 | // There is no flow rules to install/uninstall |
| 146 | intentInstallCoordinator.intentInstallSuccess(context); |
| 147 | return; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 151 | // Add flows |
| 152 | flowRulesToInstall.forEach(builder::add); |
Yi Tseng | c29d882 | 2017-10-25 16:19:25 -0700 | [diff] [blame] | 153 | // Modify flows |
| 154 | flowRuleToModify.forEach(builder::modify); |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 155 | // Remove flows |
| 156 | flowRulesToUninstall.forEach(builder::remove); |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 157 | |
| 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 Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 171 | 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 Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 195 | case REMOVE: |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 196 | trackerService.removeTrackedResources(intentData.key(), intentData.intent().resources()); |
| 197 | intentsToApply.forEach(installable -> |
| 198 | trackerService.removeTrackedResources(intentData.intent().key(), |
| 199 | installable.resources())); |
| 200 | break; |
Yi Tseng | 4a7d1e1 | 2017-07-10 16:19:17 -0700 | [diff] [blame] | 201 | default: |
| 202 | log.warn("Unknown resource tracking direction."); |
| 203 | break; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 206 | } |