Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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.domain.DomainIntent; |
| 25 | import org.onosproject.net.domain.DomainIntentOperations; |
| 26 | import org.onosproject.net.domain.DomainIntentOperationsContext; |
| 27 | import org.onosproject.net.domain.DomainIntentService; |
| 28 | import org.onosproject.net.intent.IntentData; |
| 29 | import org.onosproject.net.intent.IntentExtensionService; |
| 30 | import org.onosproject.net.intent.IntentInstallCoordinator; |
| 31 | import org.onosproject.net.intent.IntentInstaller; |
| 32 | import org.onosproject.net.intent.IntentOperationContext; |
| 33 | import org.onosproject.net.intent.impl.IntentManager; |
Yi Tseng | 24d9be7 | 2017-05-12 11:28:13 -0700 | [diff] [blame^] | 34 | import org.onosproject.net.intent.ObjectiveTrackerService; |
Yi Tseng | c927a06 | 2017-05-02 15:02:37 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import java.util.List; |
| 38 | import java.util.Optional; |
| 39 | |
| 40 | import static org.slf4j.LoggerFactory.getLogger; |
| 41 | |
| 42 | /** |
| 43 | * Installer for domain Intent. |
| 44 | */ |
| 45 | @Component(immediate = true) |
| 46 | public class DomainIntentInstaller implements IntentInstaller<DomainIntent> { |
| 47 | |
| 48 | private final Logger log = getLogger(IntentManager.class); |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected IntentExtensionService intentExtensionService; |
| 52 | |
| 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected ObjectiveTrackerService trackerService; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected IntentInstallCoordinator intentInstallCoordinator; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected DomainIntentService domainIntentService; |
| 61 | |
| 62 | @Activate |
| 63 | public void activated() { |
| 64 | intentExtensionService.registerInstaller(DomainIntent.class, this); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | public void deactivated() { |
| 69 | intentExtensionService.unregisterInstaller(DomainIntent.class); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void apply(IntentOperationContext<DomainIntent> context) { |
| 74 | Optional<IntentData> toUninstall = context.toUninstall(); |
| 75 | Optional<IntentData> toInstall = context.toInstall(); |
| 76 | |
| 77 | List<DomainIntent> uninstallIntents = context.intentsToUninstall(); |
| 78 | List<DomainIntent> installIntents = context.intentsToInstall(); |
| 79 | |
| 80 | if (!toInstall.isPresent() && !toUninstall.isPresent()) { |
| 81 | intentInstallCoordinator.intentInstallSuccess(context); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | if (toUninstall.isPresent()) { |
| 86 | IntentData intentData = toUninstall.get(); |
| 87 | trackerService.removeTrackedResources(intentData.key(), intentData.intent().resources()); |
| 88 | uninstallIntents.forEach(installable -> |
| 89 | trackerService.removeTrackedResources(intentData.intent().key(), |
| 90 | installable.resources())); |
| 91 | } |
| 92 | |
| 93 | if (toInstall.isPresent()) { |
| 94 | IntentData intentData = toInstall.get(); |
| 95 | trackerService.addTrackedResources(intentData.key(), intentData.intent().resources()); |
| 96 | installIntents.forEach(installable -> |
| 97 | trackerService.addTrackedResources(intentData.key(), |
| 98 | installable.resources())); |
| 99 | } |
| 100 | |
| 101 | // Generate domain Intent operations |
| 102 | DomainIntentOperations.Builder builder = DomainIntentOperations.builder(); |
| 103 | DomainIntentOperationsContext domainOperationsContext; |
| 104 | |
| 105 | uninstallIntents.forEach(builder::remove); |
| 106 | installIntents.forEach(builder::add); |
| 107 | |
| 108 | domainOperationsContext = new DomainIntentOperationsContext() { |
| 109 | @Override |
| 110 | public void onSuccess(DomainIntentOperations idops) { |
| 111 | intentInstallCoordinator.intentInstallSuccess(context); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onError(DomainIntentOperations idos) { |
| 116 | intentInstallCoordinator.intentInstallFailed(context); |
| 117 | } |
| 118 | }; |
| 119 | log.debug("submitting domain intent {} -> {}", |
| 120 | toUninstall.map(x -> x.key().toString()).orElse("<empty>"), |
| 121 | toInstall.map(x -> x.key().toString()).orElse("<empty>")); |
| 122 | |
| 123 | // Submit domain Inten operations with domain context |
| 124 | domainIntentService.sumbit(builder.build(domainOperationsContext)); |
| 125 | } |
| 126 | } |