blob: 5788dd85856be8feedf238aa1a4652221ee6462a [file] [log] [blame]
Thomas Vachuskabdbdd242016-03-01 01:55:55 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuskabdbdd242016-03-01 01:55:55 -08003 *
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.compiler;
18
19import com.google.common.collect.Maps;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080020import org.onlab.util.Tools;
21import org.onosproject.cfg.ComponentConfigService;
22import org.onosproject.net.intent.Intent;
23import org.onosproject.net.intent.IntentCompiler;
24import org.onosproject.net.intent.IntentExtensionService;
Pier Ventref8543d82016-09-28 19:49:33 -070025import org.onosproject.net.resource.impl.LabelAllocator;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080026import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070027import org.osgi.service.component.annotations.Activate;
28import org.osgi.service.component.annotations.Component;
29import org.osgi.service.component.annotations.Deactivate;
30import org.osgi.service.component.annotations.Modified;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080033import org.slf4j.Logger;
34
35import java.util.Map;
36
37import static com.google.common.base.Strings.isNullOrEmpty;
Ray Milkeyd04e2272018-10-16 18:20:18 -070038import static org.onosproject.net.OsgiPropertyConstants.ICR_COPY_TTL;
39import static org.onosproject.net.OsgiPropertyConstants.ICR_COPY_TTL_DEFAULT;
40import static org.onosproject.net.OsgiPropertyConstants.ICR_FLOW_OPTIMIZATION;
41import static org.onosproject.net.OsgiPropertyConstants.ICR_LABEL_SELECTION;
42import static org.onosproject.net.OsgiPropertyConstants.ICR_OPT_LABEL_SELECTION;
43import static org.onosproject.net.OsgiPropertyConstants.ICR_USE_FLOW_OBJECTIVES;
44import static org.onosproject.net.OsgiPropertyConstants.ICR_USE_FLOW_OBJECTIVES_DEFAULT;
45import static org.onosproject.net.OsgiPropertyConstants.ICR_FLOW_OPTIMIZATION_DEFAULT;
46import static org.onosproject.net.OsgiPropertyConstants.ICR_LABEL_SELECTION_DEFAULT;
47import static org.onosproject.net.OsgiPropertyConstants.ICR_OPT_LABEL_SELECTION_DEFAULT;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080048import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Auxiliary utility to register either flow-rule compilers or flow-objective
52 * compilers.
53 */
Ray Milkeyd04e2272018-10-16 18:20:18 -070054@Component(
55 service = IntentConfigurableRegistrator.class,
56 property = {
Ray Milkey2d7bca12018-10-17 14:51:52 -070057 ICR_USE_FLOW_OBJECTIVES + ":Boolean=" + ICR_USE_FLOW_OBJECTIVES_DEFAULT,
Ray Milkeyd04e2272018-10-16 18:20:18 -070058 ICR_LABEL_SELECTION + "=" + ICR_LABEL_SELECTION_DEFAULT,
alessio22f21972020-10-30 17:01:07 +010059 ICR_OPT_LABEL_SELECTION + "=" + ICR_OPT_LABEL_SELECTION_DEFAULT,
Ray Milkey2d7bca12018-10-17 14:51:52 -070060 ICR_FLOW_OPTIMIZATION + ":Boolean=" + ICR_FLOW_OPTIMIZATION_DEFAULT,
61 ICR_COPY_TTL + ":Boolean=" + ICR_COPY_TTL_DEFAULT
Ray Milkeyd04e2272018-10-16 18:20:18 -070062 }
63)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080064public class IntentConfigurableRegistrator {
65
66 private final Logger log = getLogger(getClass());
67
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080069 protected IntentExtensionService extensionService;
70
Ray Milkeyd84f89b2018-08-17 14:54:17 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080072 protected ComponentConfigService cfgService;
73
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070074 /** Indicates whether or not to use flow objective-based compilers. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070075 private boolean useFlowObjectives = ICR_USE_FLOW_OBJECTIVES_DEFAULT;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080076
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070077 /** Defines the label selection algorithm - RANDOM or FIRST_FIT. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070078 private String labelSelection = ICR_LABEL_SELECTION_DEFAULT;
Pier Ventref8543d82016-09-28 19:49:33 -070079
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070080 /** Defines the optimization for label selection algorithm - NONE, NO_SWAP, MIN_SWAP. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070081 private String optLabelSelection = ICR_OPT_LABEL_SELECTION_DEFAULT;
Pier Luigi0b4222e2017-07-21 09:47:14 +020082
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070083 /** Indicates whether or not to optimize the flows in the link collection compiler. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070084 private boolean optimizeInstructions = ICR_FLOW_OPTIMIZATION_DEFAULT;
Pier Ventre81c47bf2016-11-04 07:26:22 -070085
Thomas Vachuskaf566fa22018-10-30 14:03:36 -070086 /** Indicates whether or not to use copy ttl in the link collection compiler. */
Ray Milkeyd04e2272018-10-16 18:20:18 -070087 private boolean useCopyTtl = ICR_COPY_TTL_DEFAULT;
Pier Ventre81c47bf2016-11-04 07:26:22 -070088
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080089 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
Yi Tsenga64f0c82017-02-03 11:17:15 -080090
91 // FIXME: temporary code for switching old compiler to new compiler
Ray Milkey92d206b2018-02-12 15:01:24 -080092 private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap();
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080093
94 @Activate
95 public void activate() {
96 cfgService.registerProperties(getClass());
97 log.info("Started");
98 }
99
100 @Deactivate
101 public void deactivate() {
102 cfgService.unregisterProperties(getClass(), false);
103 log.info("Stopped");
104 }
105
106 @Modified
107 public void modified(ComponentContext context) {
108 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -0800109 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Pier Ventref8543d82016-09-28 19:49:33 -0700110 log.info("Settings: labelSelection={}", labelSelection);
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700111 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700112 log.info("Settings: useCopyTtl={}", useCopyTtl);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200113 log.info("Settings: optLabelSelection={}", optLabelSelection);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800114
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800115 return;
116 }
117
118 boolean newFlowObjectives;
119 try {
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700120 String s = Tools.get(context.getProperties(), ICR_USE_FLOW_OBJECTIVES);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800121 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
122 } catch (ClassCastException e) {
123 newFlowObjectives = useFlowObjectives;
124 }
125
126 if (useFlowObjectives != newFlowObjectives) {
127 useFlowObjectives = newFlowObjectives;
128 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -0800129 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800130 }
Pier Ventref8543d82016-09-28 19:49:33 -0700131
132 String newLabelSelection;
133 try {
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700134 String s = Tools.get(context.getProperties(), ICR_LABEL_SELECTION);
Pier Ventref8543d82016-09-28 19:49:33 -0700135 newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim();
136 } catch (ClassCastException e) {
137 newLabelSelection = labelSelection;
138 }
139
Pier Luigi0b4222e2017-07-21 09:47:14 +0200140 if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInSelEnum(newLabelSelection)) {
Pier Ventref8543d82016-09-28 19:49:33 -0700141 labelSelection = newLabelSelection;
142 changeLabelSelections();
143 log.info("Settings: labelSelection={}", labelSelection);
144 }
Pier Ventre81c47bf2016-11-04 07:26:22 -0700145
Pier Luigi0b4222e2017-07-21 09:47:14 +0200146 String newOptLabelSelection;
147 try {
148 // The optimization behavior provided by the user
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700149 String optLabelSelected = Tools.get(context.getProperties(), ICR_OPT_LABEL_SELECTION);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200150 // Parse the content of the string
151 newOptLabelSelection = isNullOrEmpty(optLabelSelected) ? optLabelSelection : optLabelSelected.trim();
152 } catch (ClassCastException e) {
153 newOptLabelSelection = optLabelSelection;
154 }
155
156 if (!optLabelSelection.equals(newOptLabelSelection) && LabelAllocator.isInOptEnum(newOptLabelSelection)) {
157 optLabelSelection = newOptLabelSelection;
158 changeOptLabelSelections();
159 log.info("Settings: optLabelSelection={}", optLabelSelection);
160 }
161
Pier Ventre81c47bf2016-11-04 07:26:22 -0700162 boolean newFlowOptimization;
163 try {
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700164 String s = Tools.get(context.getProperties(), ICR_FLOW_OPTIMIZATION);
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700165 newFlowOptimization = isNullOrEmpty(s) ? optimizeInstructions : Boolean.parseBoolean(s.trim());
Pier Ventre81c47bf2016-11-04 07:26:22 -0700166 } catch (ClassCastException e) {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700167 newFlowOptimization = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700168 }
169
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700170 if (optimizeInstructions != newFlowOptimization) {
171 optimizeInstructions = newFlowOptimization;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700172 changeFlowOptimization();
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700173 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700174 }
175
176 boolean newCopyTtl;
177 try {
Thomas Vachuskaf566fa22018-10-30 14:03:36 -0700178 String s = Tools.get(context.getProperties(), ICR_COPY_TTL);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700179 newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim());
180 } catch (ClassCastException e) {
181 newCopyTtl = useCopyTtl;
182 }
183
184 if (useCopyTtl != newCopyTtl) {
185 useCopyTtl = newCopyTtl;
186 changeCopyTtl();
187 log.info("Settings: useCopyTtl={}", useCopyTtl);
188 }
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800189 }
190
191 /**
192 * Registers the specified compiler for the given intent class.
193 *
Pier Ventref8543d82016-09-28 19:49:33 -0700194 * @param cls the intent class
195 * @param compiler the intent compiler
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800196 * @param flowBased true if the compiler is flow based
197 * @param <T> the type of intent
198 */
199 @SuppressWarnings("unchecked")
200 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
201 boolean flowBased) {
202 if (flowBased) {
Ray Milkey92d206b2018-02-12 15:01:24 -0800203 flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800204 } else {
205 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
206 }
207 if (flowBased == useFlowObjectives) {
208 extensionService.registerCompiler(cls, compiler);
209 }
210 }
211
212 /**
213 * Unregisters the compiler for the specified intent class.
214 *
Pier Ventref8543d82016-09-28 19:49:33 -0700215 * @param cls the intent class
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800216 * @param flowBased true if the compiler is flow based
217 * @param <T> the type of intent
218 */
219 @SuppressWarnings("unchecked")
220 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
221 if (flowBased) {
222 flowObjectiveBased.remove(cls);
223 } else {
224 flowRuleBased.remove(cls);
225 }
226 if (flowBased == useFlowObjectives) {
227 extensionService.unregisterCompiler(cls);
228 }
229 }
230
231 private void changeCompilers() {
232 if (useFlowObjectives) {
233 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
Ray Milkey92d206b2018-02-12 15:01:24 -0800234 flowObjectiveBased.forEach((cls, compiler) -> {
235 extensionService.registerCompiler(cls, compiler);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800236 });
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800237 } else {
238 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
239 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
240 }
241 }
242
Pier Ventref8543d82016-09-28 19:49:33 -0700243 private void changeLabelSelections() {
Pier Ventre766995d2016-10-05 22:15:56 -0700244 LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection);
Pier Ventref8543d82016-09-28 19:49:33 -0700245 }
246
Pier Luigi0b4222e2017-07-21 09:47:14 +0200247 private void changeOptLabelSelections() {
248 LinkCollectionCompiler.labelAllocator.setOptLabelSelection(optLabelSelection);
249 }
250
Pier Ventre81c47bf2016-11-04 07:26:22 -0700251 private void changeFlowOptimization() {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700252 LinkCollectionCompiler.optimizeInstructions = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700253 }
254
255 private void changeCopyTtl() {
256 LinkCollectionCompiler.copyTtl = useCopyTtl;
257 }
258
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800259}