blob: 0712d461e6d3e9b077d1ef52bdf372086bfa8b72 [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 = {
57 ICR_USE_FLOW_OBJECTIVES + "=" + ICR_USE_FLOW_OBJECTIVES_DEFAULT,
58 ICR_LABEL_SELECTION + "=" + ICR_LABEL_SELECTION_DEFAULT,
59 ICR_OPT_LABEL_SELECTION + "=" + ICR_LABEL_SELECTION_DEFAULT,
60 ICR_FLOW_OPTIMIZATION + "=" + ICR_FLOW_OPTIMIZATION_DEFAULT,
61 ICR_COPY_TTL + "=" + ICR_COPY_TTL_DEFAULT
62 }
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 //@Property(name = "useFlowObjectives",
75 // boolValue = DEFAULT_FLOW_OBJECTIVES,
76 // label = "Indicates whether or not to use flow objective-based compilers")
Ray Milkeyd04e2272018-10-16 18:20:18 -070077 private boolean useFlowObjectives = ICR_USE_FLOW_OBJECTIVES_DEFAULT;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080078
Ray Milkeyd84f89b2018-08-17 14:54:17 -070079 //@Property(name = "labelSelection",
80 // value = DEFAULT_LABEL_SELECTION,
81 // label = "Defines the label selection algorithm - RANDOM or FIRST_FIT")
Ray Milkeyd04e2272018-10-16 18:20:18 -070082 private String labelSelection = ICR_LABEL_SELECTION_DEFAULT;
Pier Ventref8543d82016-09-28 19:49:33 -070083
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 //@Property(name = "optLabelSelection",
85 // value = DEFAULT_OPT_LABEL_SELECTION,
86 // label = "Defines the optimization for label selection algorithm - NONE, NO_SWAP, MIN_SWAP")
Ray Milkeyd04e2272018-10-16 18:20:18 -070087 private String optLabelSelection = ICR_OPT_LABEL_SELECTION_DEFAULT;
Pier Luigi0b4222e2017-07-21 09:47:14 +020088
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 //@Property(name = "optimizeInstructions",
90 // boolValue = DEFAULT_FLOW_OPTIMIZATION,
91 // label = "Indicates whether or not to optimize the flows in the link collection compiler")
Ray Milkeyd04e2272018-10-16 18:20:18 -070092 private boolean optimizeInstructions = ICR_FLOW_OPTIMIZATION_DEFAULT;
Pier Ventre81c47bf2016-11-04 07:26:22 -070093
Ray Milkeyd84f89b2018-08-17 14:54:17 -070094 //@Property(name = "useCopyTtl",
95 // boolValue = DEFAULT_COPY_TTL,
96 // label = "Indicates whether or not to use copy ttl in the link collection compiler")
Ray Milkeyd04e2272018-10-16 18:20:18 -070097 private boolean useCopyTtl = ICR_COPY_TTL_DEFAULT;
Pier Ventre81c47bf2016-11-04 07:26:22 -070098
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080099 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
Yi Tsenga64f0c82017-02-03 11:17:15 -0800100
101 // FIXME: temporary code for switching old compiler to new compiler
Ray Milkey92d206b2018-02-12 15:01:24 -0800102 private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap();
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800103
104 @Activate
105 public void activate() {
106 cfgService.registerProperties(getClass());
107 log.info("Started");
108 }
109
110 @Deactivate
111 public void deactivate() {
112 cfgService.unregisterProperties(getClass(), false);
113 log.info("Stopped");
114 }
115
116 @Modified
117 public void modified(ComponentContext context) {
118 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -0800119 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Pier Ventref8543d82016-09-28 19:49:33 -0700120 log.info("Settings: labelSelection={}", labelSelection);
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700121 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700122 log.info("Settings: useCopyTtl={}", useCopyTtl);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200123 log.info("Settings: optLabelSelection={}", optLabelSelection);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800124
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800125 return;
126 }
127
128 boolean newFlowObjectives;
129 try {
130 String s = Tools.get(context.getProperties(), "useFlowObjectives");
131 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
132 } catch (ClassCastException e) {
133 newFlowObjectives = useFlowObjectives;
134 }
135
136 if (useFlowObjectives != newFlowObjectives) {
137 useFlowObjectives = newFlowObjectives;
138 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -0800139 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800140 }
Pier Ventref8543d82016-09-28 19:49:33 -0700141
142 String newLabelSelection;
143 try {
144 String s = Tools.get(context.getProperties(), "labelSelection");
145 newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim();
146 } catch (ClassCastException e) {
147 newLabelSelection = labelSelection;
148 }
149
Pier Luigi0b4222e2017-07-21 09:47:14 +0200150 if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInSelEnum(newLabelSelection)) {
Pier Ventref8543d82016-09-28 19:49:33 -0700151 labelSelection = newLabelSelection;
152 changeLabelSelections();
153 log.info("Settings: labelSelection={}", labelSelection);
154 }
Pier Ventre81c47bf2016-11-04 07:26:22 -0700155
Pier Luigi0b4222e2017-07-21 09:47:14 +0200156 String newOptLabelSelection;
157 try {
158 // The optimization behavior provided by the user
159 String optLabelSelected = Tools.get(context.getProperties(), "optLabelSelection");
160 // Parse the content of the string
161 newOptLabelSelection = isNullOrEmpty(optLabelSelected) ? optLabelSelection : optLabelSelected.trim();
162 } catch (ClassCastException e) {
163 newOptLabelSelection = optLabelSelection;
164 }
165
166 if (!optLabelSelection.equals(newOptLabelSelection) && LabelAllocator.isInOptEnum(newOptLabelSelection)) {
167 optLabelSelection = newOptLabelSelection;
168 changeOptLabelSelections();
169 log.info("Settings: optLabelSelection={}", optLabelSelection);
170 }
171
Pier Ventre81c47bf2016-11-04 07:26:22 -0700172 boolean newFlowOptimization;
173 try {
174 String s = Tools.get(context.getProperties(), "useFlowOptimization");
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700175 newFlowOptimization = isNullOrEmpty(s) ? optimizeInstructions : Boolean.parseBoolean(s.trim());
Pier Ventre81c47bf2016-11-04 07:26:22 -0700176 } catch (ClassCastException e) {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700177 newFlowOptimization = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700178 }
179
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700180 if (optimizeInstructions != newFlowOptimization) {
181 optimizeInstructions = newFlowOptimization;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700182 changeFlowOptimization();
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700183 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700184 }
185
186 boolean newCopyTtl;
187 try {
188 String s = Tools.get(context.getProperties(), "useCopyTtl");
189 newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim());
190 } catch (ClassCastException e) {
191 newCopyTtl = useCopyTtl;
192 }
193
194 if (useCopyTtl != newCopyTtl) {
195 useCopyTtl = newCopyTtl;
196 changeCopyTtl();
197 log.info("Settings: useCopyTtl={}", useCopyTtl);
198 }
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800199 }
200
201 /**
202 * Registers the specified compiler for the given intent class.
203 *
Pier Ventref8543d82016-09-28 19:49:33 -0700204 * @param cls the intent class
205 * @param compiler the intent compiler
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800206 * @param flowBased true if the compiler is flow based
207 * @param <T> the type of intent
208 */
209 @SuppressWarnings("unchecked")
210 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
211 boolean flowBased) {
212 if (flowBased) {
Ray Milkey92d206b2018-02-12 15:01:24 -0800213 flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800214 } else {
215 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
216 }
217 if (flowBased == useFlowObjectives) {
218 extensionService.registerCompiler(cls, compiler);
219 }
220 }
221
222 /**
223 * Unregisters the compiler for the specified intent class.
224 *
Pier Ventref8543d82016-09-28 19:49:33 -0700225 * @param cls the intent class
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800226 * @param flowBased true if the compiler is flow based
227 * @param <T> the type of intent
228 */
229 @SuppressWarnings("unchecked")
230 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
231 if (flowBased) {
232 flowObjectiveBased.remove(cls);
233 } else {
234 flowRuleBased.remove(cls);
235 }
236 if (flowBased == useFlowObjectives) {
237 extensionService.unregisterCompiler(cls);
238 }
239 }
240
241 private void changeCompilers() {
242 if (useFlowObjectives) {
243 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
Ray Milkey92d206b2018-02-12 15:01:24 -0800244 flowObjectiveBased.forEach((cls, compiler) -> {
245 extensionService.registerCompiler(cls, compiler);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800246 });
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800247 } else {
248 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
249 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
250 }
251 }
252
Pier Ventref8543d82016-09-28 19:49:33 -0700253 private void changeLabelSelections() {
Pier Ventre766995d2016-10-05 22:15:56 -0700254 LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection);
Pier Ventref8543d82016-09-28 19:49:33 -0700255 }
256
Pier Luigi0b4222e2017-07-21 09:47:14 +0200257 private void changeOptLabelSelections() {
258 LinkCollectionCompiler.labelAllocator.setOptLabelSelection(optLabelSelection);
259 }
260
Pier Ventre81c47bf2016-11-04 07:26:22 -0700261 private void changeFlowOptimization() {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700262 LinkCollectionCompiler.optimizeInstructions = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700263 }
264
265 private void changeCopyTtl() {
266 LinkCollectionCompiler.copyTtl = useCopyTtl;
267 }
268
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800269}