blob: 772ab7ac1edfe41de646dd52b2f8ec464b9028b5 [file] [log] [blame]
Thomas Vachuskaf6ec97b2016-02-22 10:59:23 -08001/*
2 * Copyright 2016 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;
18
19import org.onosproject.net.flow.FlowRule;
20import org.onosproject.net.flow.FlowRuleOperations;
21import org.onosproject.net.flow.FlowRuleOperationsContext;
22import org.onosproject.net.flow.FlowRuleService;
23import org.onosproject.net.flowobjective.FlowObjectiveService;
24import org.onosproject.net.intent.FlowRuleIntent;
25import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.IntentData;
27import org.onosproject.net.intent.IntentStore;
28import org.slf4j.Logger;
29
30import java.util.Collection;
31import java.util.List;
32import java.util.Optional;
33import java.util.Set;
34import java.util.stream.Collectors;
35
36import static org.onosproject.net.intent.IntentState.*;
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Auxiliary entity responsible for installing the intents into the environment.
41 */
42class IntentInstaller {
43
44 private static final Logger log = getLogger(IntentManager.class);
45
46 private IntentStore store;
47 private ObjectiveTrackerService trackerService;
48 private FlowRuleService flowRuleService;
49 private FlowObjectiveService flowObjectiveService;
50
51 private enum Direction {
52 ADD,
53 REMOVE
54 }
55
56 /**
57 * Initializes the installer with references to required services.
58 *
59 * @param intentStore intent store
60 * @param trackerService objective tracking service
61 * @param flowRuleService flow rule service
62 * @param flowObjectiveService flow objective service
63 */
64 void init(IntentStore intentStore, ObjectiveTrackerService trackerService,
65 FlowRuleService flowRuleService, FlowObjectiveService flowObjectiveService) {
66 this.store = intentStore;
67 this.trackerService = trackerService;
68 this.flowRuleService = flowRuleService;
69 this.flowObjectiveService = flowObjectiveService;
70 }
71
72 private void applyIntentData(Optional<IntentData> intentData,
73 FlowRuleOperations.Builder builder,
74 Direction direction) {
75 if (!intentData.isPresent()) {
76 return;
77 }
78 IntentData data = intentData.get();
79
80 List<Intent> intentsToApply = data.installables();
81 if (!intentsToApply.stream().allMatch(x -> x instanceof FlowRuleIntent)) {
82 throw new IllegalStateException("installable intents must be FlowRuleIntent");
83 }
84
85 if (direction == Direction.ADD) {
86 trackerService.addTrackedResources(data.key(), data.intent().resources());
87 intentsToApply.forEach(installable ->
88 trackerService.addTrackedResources(data.key(), installable.resources()));
89 } else {
90 trackerService.removeTrackedResources(data.key(), data.intent().resources());
91 intentsToApply.forEach(installable ->
92 trackerService.removeTrackedResources(data.intent().key(),
93 installable.resources()));
94 }
95
96 // FIXME do FlowRuleIntents have stages??? Can we do uninstall work in parallel? I think so.
97 builder.newStage();
98
99 List<Collection<FlowRule>> stages = intentsToApply.stream()
100 .map(x -> (FlowRuleIntent) x)
101 .map(FlowRuleIntent::flowRules)
102 .collect(Collectors.toList());
103
104 for (Collection<FlowRule> rules : stages) {
105 if (direction == Direction.ADD) {
106 rules.forEach(builder::add);
107 } else {
108 rules.forEach(builder::remove);
109 }
110 }
111
112 }
113
114 // FIXME: Refactor to accept both FlowObjectiveIntent and FlowRuleIntents
115 // Note: Intent Manager should have never become dependent on a specific
116 // intent type.
117
118 /**
119 * Applies the specified intent updates to the environment by uninstalling
120 * and installing the intents and updating the store references appropriately.
121 *
122 * @param toUninstall optional intent to uninstall
123 * @param toInstall optional intent to install
124 */
125 void apply(Optional<IntentData> toUninstall, Optional<IntentData> toInstall) {
126 // need to consider if FlowRuleIntent is only one as installable intent or not
127
128 FlowRuleOperations.Builder builder = FlowRuleOperations.builder();
129 applyIntentData(toUninstall, builder, Direction.REMOVE);
130 applyIntentData(toInstall, builder, Direction.ADD);
131
132 FlowRuleOperations operations = builder.build(new FlowRuleOperationsContext() {
133 @Override
134 public void onSuccess(FlowRuleOperations ops) {
135 if (toInstall.isPresent()) {
136 IntentData installData = toInstall.get();
137 log.debug("Completed installing: {}", installData.key());
138 installData.setState(INSTALLED);
139 store.write(installData);
140 } else if (toUninstall.isPresent()) {
141 IntentData uninstallData = toUninstall.get();
142 log.debug("Completed withdrawing: {}", uninstallData.key());
143 switch (uninstallData.request()) {
144 case INSTALL_REQ:
145 uninstallData.setState(FAILED);
146 break;
147 case WITHDRAW_REQ:
148 default: //TODO "default" case should not happen
149 uninstallData.setState(WITHDRAWN);
150 break;
151 }
152 store.write(uninstallData);
153 }
154 }
155
156 @Override
157 public void onError(FlowRuleOperations ops) {
158 // if toInstall was cause of error, then recompile (manage/increment counter, when exceeded -> CORRUPT)
159 if (toInstall.isPresent()) {
160 IntentData installData = toInstall.get();
161 log.warn("Failed installation: {} {} on {}",
162 installData.key(), installData.intent(), ops);
163 installData.setState(CORRUPT);
164 installData.incrementErrorCount();
165 store.write(installData);
166 }
167 // if toUninstall was cause of error, then CORRUPT (another job will clean this up)
168 if (toUninstall.isPresent()) {
169 IntentData uninstallData = toUninstall.get();
170 log.warn("Failed withdrawal: {} {} on {}",
171 uninstallData.key(), uninstallData.intent(), ops);
172 uninstallData.setState(CORRUPT);
173 uninstallData.incrementErrorCount();
174 store.write(uninstallData);
175 }
176 }
177 });
178
179 if (log.isTraceEnabled()) {
180 log.trace("applying intent {} -> {} with {} rules: {}",
181 toUninstall.map(x -> x.key().toString()).orElse("<empty>"),
182 toInstall.map(x -> x.key().toString()).orElse("<empty>"),
183 operations.stages().stream().mapToLong(Set::size).sum(),
184 operations.stages());
185 }
186
187 flowRuleService.apply(operations);
188 }
189}