blob: e14115c9b71ed0db617d0adc9fad31e8822cc98d [file] [log] [blame]
Thomas Vachuskabdbdd242016-03-01 01:55:55 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
Yi Tsenga64f0c82017-02-03 11:17:15 -080020import com.google.common.collect.Sets;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Modified;
25import org.apache.felix.scr.annotations.Property;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
29import org.onlab.util.Tools;
30import org.onosproject.cfg.ComponentConfigService;
31import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.IntentCompiler;
33import org.onosproject.net.intent.IntentExtensionService;
Pier Ventref8543d82016-09-28 19:49:33 -070034import org.onosproject.net.resource.impl.LabelAllocator;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080035import org.osgi.service.component.ComponentContext;
36import org.slf4j.Logger;
37
38import java.util.Map;
Yi Tsenga64f0c82017-02-03 11:17:15 -080039import java.util.Set;
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080040
41import static com.google.common.base.Strings.isNullOrEmpty;
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * Auxiliary utility to register either flow-rule compilers or flow-objective
46 * compilers.
47 */
48@Component
49@Service(value = IntentConfigurableRegistrator.class)
50public class IntentConfigurableRegistrator {
51
52 private final Logger log = getLogger(getClass());
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected IntentExtensionService extensionService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected ComponentConfigService cfgService;
59
60 private static final boolean DEFAULT_FLOW_OBJECTIVES = false;
61 @Property(name = "useFlowObjectives",
62 boolValue = DEFAULT_FLOW_OBJECTIVES,
Pier Ventref8543d82016-09-28 19:49:33 -070063 label = "Indicates whether or not to use flow objective-based compilers")
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080064 private boolean useFlowObjectives = DEFAULT_FLOW_OBJECTIVES;
65
Pier Ventref8543d82016-09-28 19:49:33 -070066 private static final String DEFAULT_LABEL_SELECTION = "RANDOM";
67 @Property(name = "labelSelection",
68 value = DEFAULT_LABEL_SELECTION,
69 label = "Defines the label selection algorithm - RANDOM or FIRST_FIT")
70 private String labelSelection = DEFAULT_LABEL_SELECTION;
71
Pier Ventre81c47bf2016-11-04 07:26:22 -070072 private static final boolean DEFAULT_FLOW_OPTIMIZATION = false;
73 @Property(name = "useFlowOptimization",
74 boolValue = DEFAULT_FLOW_OPTIMIZATION,
75 label = "Indicates whether or not to optimize the flows in the link collection compiler")
76 private boolean useFlowOptimization = DEFAULT_FLOW_OPTIMIZATION;
77
78 private static final boolean DEFAULT_COPY_TTL = false;
79 @Property(name = "useCopyTtl",
80 boolValue = DEFAULT_COPY_TTL,
81 label = "Indicates whether or not to use copy ttl in the link collection compiler")
82 private boolean useCopyTtl = DEFAULT_COPY_TTL;
83
Yi Tsenga64f0c82017-02-03 11:17:15 -080084 /**
85 * Temporary for switching old compiler and new compiler.
86 * @deprecated 1.10 Kingfisher
87 */
88 private static final String DEFAULT_FLOW_OBJECTIVE_COMPILER =
89 "org.onosproject.net.intent.impl.compiler.LinkCollectionIntentFlowObjectiveCompiler";
90 @Deprecated
91 @Property(name = "defaultFlowObjectiveCompiler",
92 value = DEFAULT_FLOW_OBJECTIVE_COMPILER,
93 label = "Default compiler to generate flow objective")
94 private String defaultFlowObjectiveCompiler = DEFAULT_FLOW_OBJECTIVE_COMPILER;
95
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080096 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
Yi Tsenga64f0c82017-02-03 11:17:15 -080097
98 // FIXME: temporary code for switching old compiler to new compiler
99 private final Map<Class<Intent>, Set<IntentCompiler<Intent>>> flowObjectiveBased = Maps.newConcurrentMap();
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800100
101 @Activate
102 public void activate() {
103 cfgService.registerProperties(getClass());
104 log.info("Started");
105 }
106
107 @Deactivate
108 public void deactivate() {
109 cfgService.unregisterProperties(getClass(), false);
110 log.info("Stopped");
111 }
112
113 @Modified
114 public void modified(ComponentContext context) {
115 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -0800116 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Pier Ventref8543d82016-09-28 19:49:33 -0700117 log.info("Settings: labelSelection={}", labelSelection);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700118 log.info("Settings: useFlowOptimization={}", useFlowOptimization);
119 log.info("Settings: useCopyTtl={}", useCopyTtl);
Yi Tsenga64f0c82017-02-03 11:17:15 -0800120
121 // FIXME: temporary code for switching old compiler to new compiler
122 log.info("Settings: defaultFlowObjectiveCompiler={}", defaultFlowObjectiveCompiler);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800123 return;
124 }
125
126 boolean newFlowObjectives;
127 try {
128 String s = Tools.get(context.getProperties(), "useFlowObjectives");
129 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
130 } catch (ClassCastException e) {
131 newFlowObjectives = useFlowObjectives;
132 }
133
134 if (useFlowObjectives != newFlowObjectives) {
135 useFlowObjectives = newFlowObjectives;
136 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -0800137 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800138 }
Pier Ventref8543d82016-09-28 19:49:33 -0700139
Yi Tsenga64f0c82017-02-03 11:17:15 -0800140 // FIXME: temporary code for switching old compiler to new compiler
141 String newDefaultFlowObjectiveCompiler;
142 try {
143 String s = Tools.get(context.getProperties(), "defaultFlowObjectiveCompiler");
144 newDefaultFlowObjectiveCompiler = isNullOrEmpty(s) ? defaultFlowObjectiveCompiler : s.trim();
145 } catch (ClassCastException e) {
146 newDefaultFlowObjectiveCompiler = defaultFlowObjectiveCompiler;
147 }
148
149 if (!defaultFlowObjectiveCompiler.equals(newDefaultFlowObjectiveCompiler)) {
150 defaultFlowObjectiveCompiler = newDefaultFlowObjectiveCompiler;
151 changeCompilers();
152 log.info("Settings: defaultFlowObjectiveCompiler={}", defaultFlowObjectiveCompiler);
153 }
154
Pier Ventref8543d82016-09-28 19:49:33 -0700155 String newLabelSelection;
156 try {
157 String s = Tools.get(context.getProperties(), "labelSelection");
158 newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim();
159 } catch (ClassCastException e) {
160 newLabelSelection = labelSelection;
161 }
162
163 if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInEnum(newLabelSelection)) {
164 labelSelection = newLabelSelection;
165 changeLabelSelections();
166 log.info("Settings: labelSelection={}", labelSelection);
167 }
Pier Ventre81c47bf2016-11-04 07:26:22 -0700168
169 boolean newFlowOptimization;
170 try {
171 String s = Tools.get(context.getProperties(), "useFlowOptimization");
172 newFlowOptimization = isNullOrEmpty(s) ? useFlowOptimization : Boolean.parseBoolean(s.trim());
173 } catch (ClassCastException e) {
174 newFlowOptimization = useFlowOptimization;
175 }
176
177 if (useFlowOptimization != newFlowOptimization) {
178 useFlowOptimization = newFlowOptimization;
179 changeFlowOptimization();
180 log.info("Settings: useFlowOptimization={}", useFlowOptimization);
181 }
182
183 boolean newCopyTtl;
184 try {
185 String s = Tools.get(context.getProperties(), "useCopyTtl");
186 newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim());
187 } catch (ClassCastException e) {
188 newCopyTtl = useCopyTtl;
189 }
190
191 if (useCopyTtl != newCopyTtl) {
192 useCopyTtl = newCopyTtl;
193 changeCopyTtl();
194 log.info("Settings: useCopyTtl={}", useCopyTtl);
195 }
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800196 }
197
198 /**
199 * Registers the specified compiler for the given intent class.
200 *
Pier Ventref8543d82016-09-28 19:49:33 -0700201 * @param cls the intent class
202 * @param compiler the intent compiler
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800203 * @param flowBased true if the compiler is flow based
204 * @param <T> the type of intent
205 */
206 @SuppressWarnings("unchecked")
207 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
208 boolean flowBased) {
209 if (flowBased) {
Yi Tsenga64f0c82017-02-03 11:17:15 -0800210 // FIXME: temporary code for switching old compiler to new compiler
211 flowObjectiveBased.compute((Class<Intent>) cls, (clz, compilers) -> {
212 if (compilers == null) {
213 compilers = Sets.newHashSet();
214 }
215
216 compilers.add((IntentCompiler<Intent>) compiler);
217 return compilers;
218 });
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800219 } else {
220 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
221 }
222 if (flowBased == useFlowObjectives) {
223 extensionService.registerCompiler(cls, compiler);
224 }
225 }
226
227 /**
228 * Unregisters the compiler for the specified intent class.
229 *
Pier Ventref8543d82016-09-28 19:49:33 -0700230 * @param cls the intent class
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800231 * @param flowBased true if the compiler is flow based
232 * @param <T> the type of intent
233 */
234 @SuppressWarnings("unchecked")
235 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
236 if (flowBased) {
237 flowObjectiveBased.remove(cls);
238 } else {
239 flowRuleBased.remove(cls);
240 }
241 if (flowBased == useFlowObjectives) {
242 extensionService.unregisterCompiler(cls);
243 }
244 }
245
246 private void changeCompilers() {
247 if (useFlowObjectives) {
248 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
Yi Tsenga64f0c82017-02-03 11:17:15 -0800249 // FIXME: temporary code for switching old compiler to new compiler
250 flowObjectiveBased.forEach((cls, compilers) -> {
251 compilers.forEach(compiler -> {
252 // filter out flow objective compiler which doesn't match
253 if (compiler.getClass().getName().equals(defaultFlowObjectiveCompiler)) {
254 extensionService.registerCompiler(cls, compiler);
255 }
256 });
257 });
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800258 } else {
259 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
260 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
261 }
262 }
263
Pier Ventref8543d82016-09-28 19:49:33 -0700264 private void changeLabelSelections() {
Pier Ventre766995d2016-10-05 22:15:56 -0700265 LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection);
Pier Ventref8543d82016-09-28 19:49:33 -0700266 }
267
Pier Ventre81c47bf2016-11-04 07:26:22 -0700268 private void changeFlowOptimization() {
269 LinkCollectionCompiler.optimize = useFlowOptimization;
270 }
271
272 private void changeCopyTtl() {
273 LinkCollectionCompiler.copyTtl = useCopyTtl;
274 }
275
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800276}