blob: f86cdbaf3feeb40baa7ba9629dff656f197872b5 [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;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.apache.felix.scr.annotations.Service;
28import org.onlab.util.Tools;
29import org.onosproject.cfg.ComponentConfigService;
30import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentCompiler;
32import org.onosproject.net.intent.IntentExtensionService;
Pier Ventref8543d82016-09-28 19:49:33 -070033import org.onosproject.net.resource.impl.LabelAllocator;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080034import org.osgi.service.component.ComponentContext;
35import org.slf4j.Logger;
36
37import java.util.Map;
38
39import static com.google.common.base.Strings.isNullOrEmpty;
40import static org.slf4j.LoggerFactory.getLogger;
41
42/**
43 * Auxiliary utility to register either flow-rule compilers or flow-objective
44 * compilers.
45 */
46@Component
47@Service(value = IntentConfigurableRegistrator.class)
48public class IntentConfigurableRegistrator {
49
50 private final Logger log = getLogger(getClass());
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected IntentExtensionService extensionService;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected ComponentConfigService cfgService;
57
58 private static final boolean DEFAULT_FLOW_OBJECTIVES = false;
59 @Property(name = "useFlowObjectives",
60 boolValue = DEFAULT_FLOW_OBJECTIVES,
Pier Ventref8543d82016-09-28 19:49:33 -070061 label = "Indicates whether or not to use flow objective-based compilers")
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080062 private boolean useFlowObjectives = DEFAULT_FLOW_OBJECTIVES;
63
Pier Ventref8543d82016-09-28 19:49:33 -070064 private static final String DEFAULT_LABEL_SELECTION = "RANDOM";
65 @Property(name = "labelSelection",
66 value = DEFAULT_LABEL_SELECTION,
67 label = "Defines the label selection algorithm - RANDOM or FIRST_FIT")
68 private String labelSelection = DEFAULT_LABEL_SELECTION;
69
Pier Luigi0b4222e2017-07-21 09:47:14 +020070 private static final String DEFAULT_OPT_LABEL_SELECTION = "NONE";
71 @Property(name = "optLabelSelection",
72 value = DEFAULT_OPT_LABEL_SELECTION,
73 label = "Defines the optimization for label selection algorithm - NONE, NO_SWAP, MIN_SWAP")
74 private String optLabelSelection = DEFAULT_OPT_LABEL_SELECTION;
75
Pier Ventre81c47bf2016-11-04 07:26:22 -070076 private static final boolean DEFAULT_FLOW_OPTIMIZATION = false;
Yi Tseng84c5a3d2017-04-14 16:42:59 -070077 @Property(name = "optimizeInstructions",
Pier Ventre81c47bf2016-11-04 07:26:22 -070078 boolValue = DEFAULT_FLOW_OPTIMIZATION,
79 label = "Indicates whether or not to optimize the flows in the link collection compiler")
Yi Tseng84c5a3d2017-04-14 16:42:59 -070080 private boolean optimizeInstructions = DEFAULT_FLOW_OPTIMIZATION;
Pier Ventre81c47bf2016-11-04 07:26:22 -070081
82 private static final boolean DEFAULT_COPY_TTL = false;
83 @Property(name = "useCopyTtl",
84 boolValue = DEFAULT_COPY_TTL,
85 label = "Indicates whether or not to use copy ttl in the link collection compiler")
86 private boolean useCopyTtl = DEFAULT_COPY_TTL;
87
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080088 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
Yi Tsenga64f0c82017-02-03 11:17:15 -080089
90 // FIXME: temporary code for switching old compiler to new compiler
Ray Milkey92d206b2018-02-12 15:01:24 -080091 private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap();
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080092
93 @Activate
94 public void activate() {
95 cfgService.registerProperties(getClass());
96 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
101 cfgService.unregisterProperties(getClass(), false);
102 log.info("Stopped");
103 }
104
105 @Modified
106 public void modified(ComponentContext context) {
107 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -0800108 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Pier Ventref8543d82016-09-28 19:49:33 -0700109 log.info("Settings: labelSelection={}", labelSelection);
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700110 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700111 log.info("Settings: useCopyTtl={}", useCopyTtl);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200112 log.info("Settings: optLabelSelection={}", optLabelSelection);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800113
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800114 return;
115 }
116
117 boolean newFlowObjectives;
118 try {
119 String s = Tools.get(context.getProperties(), "useFlowObjectives");
120 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
121 } catch (ClassCastException e) {
122 newFlowObjectives = useFlowObjectives;
123 }
124
125 if (useFlowObjectives != newFlowObjectives) {
126 useFlowObjectives = newFlowObjectives;
127 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -0800128 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800129 }
Pier Ventref8543d82016-09-28 19:49:33 -0700130
131 String newLabelSelection;
132 try {
133 String s = Tools.get(context.getProperties(), "labelSelection");
134 newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim();
135 } catch (ClassCastException e) {
136 newLabelSelection = labelSelection;
137 }
138
Pier Luigi0b4222e2017-07-21 09:47:14 +0200139 if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInSelEnum(newLabelSelection)) {
Pier Ventref8543d82016-09-28 19:49:33 -0700140 labelSelection = newLabelSelection;
141 changeLabelSelections();
142 log.info("Settings: labelSelection={}", labelSelection);
143 }
Pier Ventre81c47bf2016-11-04 07:26:22 -0700144
Pier Luigi0b4222e2017-07-21 09:47:14 +0200145 String newOptLabelSelection;
146 try {
147 // The optimization behavior provided by the user
148 String optLabelSelected = Tools.get(context.getProperties(), "optLabelSelection");
149 // Parse the content of the string
150 newOptLabelSelection = isNullOrEmpty(optLabelSelected) ? optLabelSelection : optLabelSelected.trim();
151 } catch (ClassCastException e) {
152 newOptLabelSelection = optLabelSelection;
153 }
154
155 if (!optLabelSelection.equals(newOptLabelSelection) && LabelAllocator.isInOptEnum(newOptLabelSelection)) {
156 optLabelSelection = newOptLabelSelection;
157 changeOptLabelSelections();
158 log.info("Settings: optLabelSelection={}", optLabelSelection);
159 }
160
Pier Ventre81c47bf2016-11-04 07:26:22 -0700161 boolean newFlowOptimization;
162 try {
163 String s = Tools.get(context.getProperties(), "useFlowOptimization");
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700164 newFlowOptimization = isNullOrEmpty(s) ? optimizeInstructions : Boolean.parseBoolean(s.trim());
Pier Ventre81c47bf2016-11-04 07:26:22 -0700165 } catch (ClassCastException e) {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700166 newFlowOptimization = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700167 }
168
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700169 if (optimizeInstructions != newFlowOptimization) {
170 optimizeInstructions = newFlowOptimization;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700171 changeFlowOptimization();
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700172 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700173 }
174
175 boolean newCopyTtl;
176 try {
177 String s = Tools.get(context.getProperties(), "useCopyTtl");
178 newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim());
179 } catch (ClassCastException e) {
180 newCopyTtl = useCopyTtl;
181 }
182
183 if (useCopyTtl != newCopyTtl) {
184 useCopyTtl = newCopyTtl;
185 changeCopyTtl();
186 log.info("Settings: useCopyTtl={}", useCopyTtl);
187 }
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800188 }
189
190 /**
191 * Registers the specified compiler for the given intent class.
192 *
Pier Ventref8543d82016-09-28 19:49:33 -0700193 * @param cls the intent class
194 * @param compiler the intent compiler
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800195 * @param flowBased true if the compiler is flow based
196 * @param <T> the type of intent
197 */
198 @SuppressWarnings("unchecked")
199 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
200 boolean flowBased) {
201 if (flowBased) {
Ray Milkey92d206b2018-02-12 15:01:24 -0800202 flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800203 } else {
204 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
205 }
206 if (flowBased == useFlowObjectives) {
207 extensionService.registerCompiler(cls, compiler);
208 }
209 }
210
211 /**
212 * Unregisters the compiler for the specified intent class.
213 *
Pier Ventref8543d82016-09-28 19:49:33 -0700214 * @param cls the intent class
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800215 * @param flowBased true if the compiler is flow based
216 * @param <T> the type of intent
217 */
218 @SuppressWarnings("unchecked")
219 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
220 if (flowBased) {
221 flowObjectiveBased.remove(cls);
222 } else {
223 flowRuleBased.remove(cls);
224 }
225 if (flowBased == useFlowObjectives) {
226 extensionService.unregisterCompiler(cls);
227 }
228 }
229
230 private void changeCompilers() {
231 if (useFlowObjectives) {
232 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
Ray Milkey92d206b2018-02-12 15:01:24 -0800233 flowObjectiveBased.forEach((cls, compiler) -> {
234 extensionService.registerCompiler(cls, compiler);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800235 });
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800236 } else {
237 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
238 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
239 }
240 }
241
Pier Ventref8543d82016-09-28 19:49:33 -0700242 private void changeLabelSelections() {
Pier Ventre766995d2016-10-05 22:15:56 -0700243 LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection);
Pier Ventref8543d82016-09-28 19:49:33 -0700244 }
245
Pier Luigi0b4222e2017-07-21 09:47:14 +0200246 private void changeOptLabelSelections() {
247 LinkCollectionCompiler.labelAllocator.setOptLabelSelection(optLabelSelection);
248 }
249
Pier Ventre81c47bf2016-11-04 07:26:22 -0700250 private void changeFlowOptimization() {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700251 LinkCollectionCompiler.optimizeInstructions = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700252 }
253
254 private void changeCopyTtl() {
255 LinkCollectionCompiler.copyTtl = useCopyTtl;
256 }
257
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800258}