blob: a71303498d19e49d601e60b85ed70b10cf4a5cb8 [file] [log] [blame]
Yi Tsengc927a062017-05-02 15:02:37 -07001/*
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
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.domain.DomainIntent;
25import org.onosproject.net.domain.DomainIntentOperations;
26import org.onosproject.net.domain.DomainIntentOperationsContext;
27import org.onosproject.net.domain.DomainIntentService;
28import org.onosproject.net.intent.IntentData;
29import org.onosproject.net.intent.IntentExtensionService;
30import org.onosproject.net.intent.IntentInstallCoordinator;
31import org.onosproject.net.intent.IntentInstaller;
32import org.onosproject.net.intent.IntentOperationContext;
33import org.onosproject.net.intent.impl.IntentManager;
34import org.onosproject.net.intent.impl.ObjectiveTrackerService;
35import org.slf4j.Logger;
36
37import java.util.List;
38import java.util.Optional;
39
40import static org.slf4j.LoggerFactory.getLogger;
41
42/**
43 * Installer for domain Intent.
44 */
45@Component(immediate = true)
46public 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}