blob: b6e74b77597425c06e8b536d1036c3111619463d [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;
38import static org.slf4j.LoggerFactory.getLogger;
39
40/**
41 * Auxiliary utility to register either flow-rule compilers or flow-objective
42 * compilers.
43 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044@Component(service = IntentConfigurableRegistrator.class)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080045public class IntentConfigurableRegistrator {
46
47 private final Logger log = getLogger(getClass());
48
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080050 protected IntentExtensionService extensionService;
51
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080053 protected ComponentConfigService cfgService;
54
55 private static final boolean DEFAULT_FLOW_OBJECTIVES = false;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 //@Property(name = "useFlowObjectives",
57 // boolValue = DEFAULT_FLOW_OBJECTIVES,
58 // label = "Indicates whether or not to use flow objective-based compilers")
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080059 private boolean useFlowObjectives = DEFAULT_FLOW_OBJECTIVES;
60
Pier Ventref8543d82016-09-28 19:49:33 -070061 private static final String DEFAULT_LABEL_SELECTION = "RANDOM";
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 //@Property(name = "labelSelection",
63 // value = DEFAULT_LABEL_SELECTION,
64 // label = "Defines the label selection algorithm - RANDOM or FIRST_FIT")
Pier Ventref8543d82016-09-28 19:49:33 -070065 private String labelSelection = DEFAULT_LABEL_SELECTION;
66
Pier Luigi0b4222e2017-07-21 09:47:14 +020067 private static final String DEFAULT_OPT_LABEL_SELECTION = "NONE";
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 //@Property(name = "optLabelSelection",
69 // value = DEFAULT_OPT_LABEL_SELECTION,
70 // label = "Defines the optimization for label selection algorithm - NONE, NO_SWAP, MIN_SWAP")
Pier Luigi0b4222e2017-07-21 09:47:14 +020071 private String optLabelSelection = DEFAULT_OPT_LABEL_SELECTION;
72
Pier Ventre81c47bf2016-11-04 07:26:22 -070073 private static final boolean DEFAULT_FLOW_OPTIMIZATION = false;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 //@Property(name = "optimizeInstructions",
75 // boolValue = DEFAULT_FLOW_OPTIMIZATION,
76 // label = "Indicates whether or not to optimize the flows in the link collection compiler")
Yi Tseng84c5a3d2017-04-14 16:42:59 -070077 private boolean optimizeInstructions = DEFAULT_FLOW_OPTIMIZATION;
Pier Ventre81c47bf2016-11-04 07:26:22 -070078
79 private static final boolean DEFAULT_COPY_TTL = false;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 //@Property(name = "useCopyTtl",
81 // boolValue = DEFAULT_COPY_TTL,
82 // label = "Indicates whether or not to use copy ttl in the link collection compiler")
Pier Ventre81c47bf2016-11-04 07:26:22 -070083 private boolean useCopyTtl = DEFAULT_COPY_TTL;
84
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080085 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
Yi Tsenga64f0c82017-02-03 11:17:15 -080086
87 // FIXME: temporary code for switching old compiler to new compiler
Ray Milkey92d206b2018-02-12 15:01:24 -080088 private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap();
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080089
90 @Activate
91 public void activate() {
92 cfgService.registerProperties(getClass());
93 log.info("Started");
94 }
95
96 @Deactivate
97 public void deactivate() {
98 cfgService.unregisterProperties(getClass(), false);
99 log.info("Stopped");
100 }
101
102 @Modified
103 public void modified(ComponentContext context) {
104 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -0800105 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Pier Ventref8543d82016-09-28 19:49:33 -0700106 log.info("Settings: labelSelection={}", labelSelection);
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700107 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700108 log.info("Settings: useCopyTtl={}", useCopyTtl);
Pier Luigi0b4222e2017-07-21 09:47:14 +0200109 log.info("Settings: optLabelSelection={}", optLabelSelection);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800110
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800111 return;
112 }
113
114 boolean newFlowObjectives;
115 try {
116 String s = Tools.get(context.getProperties(), "useFlowObjectives");
117 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
118 } catch (ClassCastException e) {
119 newFlowObjectives = useFlowObjectives;
120 }
121
122 if (useFlowObjectives != newFlowObjectives) {
123 useFlowObjectives = newFlowObjectives;
124 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -0800125 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800126 }
Pier Ventref8543d82016-09-28 19:49:33 -0700127
128 String newLabelSelection;
129 try {
130 String s = Tools.get(context.getProperties(), "labelSelection");
131 newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim();
132 } catch (ClassCastException e) {
133 newLabelSelection = labelSelection;
134 }
135
Pier Luigi0b4222e2017-07-21 09:47:14 +0200136 if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInSelEnum(newLabelSelection)) {
Pier Ventref8543d82016-09-28 19:49:33 -0700137 labelSelection = newLabelSelection;
138 changeLabelSelections();
139 log.info("Settings: labelSelection={}", labelSelection);
140 }
Pier Ventre81c47bf2016-11-04 07:26:22 -0700141
Pier Luigi0b4222e2017-07-21 09:47:14 +0200142 String newOptLabelSelection;
143 try {
144 // The optimization behavior provided by the user
145 String optLabelSelected = Tools.get(context.getProperties(), "optLabelSelection");
146 // Parse the content of the string
147 newOptLabelSelection = isNullOrEmpty(optLabelSelected) ? optLabelSelection : optLabelSelected.trim();
148 } catch (ClassCastException e) {
149 newOptLabelSelection = optLabelSelection;
150 }
151
152 if (!optLabelSelection.equals(newOptLabelSelection) && LabelAllocator.isInOptEnum(newOptLabelSelection)) {
153 optLabelSelection = newOptLabelSelection;
154 changeOptLabelSelections();
155 log.info("Settings: optLabelSelection={}", optLabelSelection);
156 }
157
Pier Ventre81c47bf2016-11-04 07:26:22 -0700158 boolean newFlowOptimization;
159 try {
160 String s = Tools.get(context.getProperties(), "useFlowOptimization");
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700161 newFlowOptimization = isNullOrEmpty(s) ? optimizeInstructions : Boolean.parseBoolean(s.trim());
Pier Ventre81c47bf2016-11-04 07:26:22 -0700162 } catch (ClassCastException e) {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700163 newFlowOptimization = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700164 }
165
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700166 if (optimizeInstructions != newFlowOptimization) {
167 optimizeInstructions = newFlowOptimization;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700168 changeFlowOptimization();
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700169 log.info("Settings: useFlowOptimization={}", optimizeInstructions);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700170 }
171
172 boolean newCopyTtl;
173 try {
174 String s = Tools.get(context.getProperties(), "useCopyTtl");
175 newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim());
176 } catch (ClassCastException e) {
177 newCopyTtl = useCopyTtl;
178 }
179
180 if (useCopyTtl != newCopyTtl) {
181 useCopyTtl = newCopyTtl;
182 changeCopyTtl();
183 log.info("Settings: useCopyTtl={}", useCopyTtl);
184 }
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800185 }
186
187 /**
188 * Registers the specified compiler for the given intent class.
189 *
Pier Ventref8543d82016-09-28 19:49:33 -0700190 * @param cls the intent class
191 * @param compiler the intent compiler
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800192 * @param flowBased true if the compiler is flow based
193 * @param <T> the type of intent
194 */
195 @SuppressWarnings("unchecked")
196 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
197 boolean flowBased) {
198 if (flowBased) {
Ray Milkey92d206b2018-02-12 15:01:24 -0800199 flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800200 } else {
201 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
202 }
203 if (flowBased == useFlowObjectives) {
204 extensionService.registerCompiler(cls, compiler);
205 }
206 }
207
208 /**
209 * Unregisters the compiler for the specified intent class.
210 *
Pier Ventref8543d82016-09-28 19:49:33 -0700211 * @param cls the intent class
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800212 * @param flowBased true if the compiler is flow based
213 * @param <T> the type of intent
214 */
215 @SuppressWarnings("unchecked")
216 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
217 if (flowBased) {
218 flowObjectiveBased.remove(cls);
219 } else {
220 flowRuleBased.remove(cls);
221 }
222 if (flowBased == useFlowObjectives) {
223 extensionService.unregisterCompiler(cls);
224 }
225 }
226
227 private void changeCompilers() {
228 if (useFlowObjectives) {
229 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
Ray Milkey92d206b2018-02-12 15:01:24 -0800230 flowObjectiveBased.forEach((cls, compiler) -> {
231 extensionService.registerCompiler(cls, compiler);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800232 });
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800233 } else {
234 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
235 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
236 }
237 }
238
Pier Ventref8543d82016-09-28 19:49:33 -0700239 private void changeLabelSelections() {
Pier Ventre766995d2016-10-05 22:15:56 -0700240 LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection);
Pier Ventref8543d82016-09-28 19:49:33 -0700241 }
242
Pier Luigi0b4222e2017-07-21 09:47:14 +0200243 private void changeOptLabelSelections() {
244 LinkCollectionCompiler.labelAllocator.setOptLabelSelection(optLabelSelection);
245 }
246
Pier Ventre81c47bf2016-11-04 07:26:22 -0700247 private void changeFlowOptimization() {
Yi Tseng84c5a3d2017-04-14 16:42:59 -0700248 LinkCollectionCompiler.optimizeInstructions = optimizeInstructions;
Pier Ventre81c47bf2016-11-04 07:26:22 -0700249 }
250
251 private void changeCopyTtl() {
252 LinkCollectionCompiler.copyTtl = useCopyTtl;
253 }
254
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800255}