blob: 40f7f77bbafde8587d82bb121423d9acf35b4792 [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;
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 Ventre81c47bf2016-11-04 07:26:22 -070070 private static final boolean DEFAULT_FLOW_OPTIMIZATION = false;
71 @Property(name = "useFlowOptimization",
72 boolValue = DEFAULT_FLOW_OPTIMIZATION,
73 label = "Indicates whether or not to optimize the flows in the link collection compiler")
74 private boolean useFlowOptimization = DEFAULT_FLOW_OPTIMIZATION;
75
76 private static final boolean DEFAULT_COPY_TTL = false;
77 @Property(name = "useCopyTtl",
78 boolValue = DEFAULT_COPY_TTL,
79 label = "Indicates whether or not to use copy ttl in the link collection compiler")
80 private boolean useCopyTtl = DEFAULT_COPY_TTL;
81
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080082 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
83 private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap();
84
85 @Activate
86 public void activate() {
87 cfgService.registerProperties(getClass());
88 log.info("Started");
89 }
90
91 @Deactivate
92 public void deactivate() {
93 cfgService.unregisterProperties(getClass(), false);
94 log.info("Stopped");
95 }
96
97 @Modified
98 public void modified(ComponentContext context) {
99 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -0800100 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Pier Ventref8543d82016-09-28 19:49:33 -0700101 log.info("Settings: labelSelection={}", labelSelection);
Pier Ventre81c47bf2016-11-04 07:26:22 -0700102 log.info("Settings: useFlowOptimization={}", useFlowOptimization);
103 log.info("Settings: useCopyTtl={}", useCopyTtl);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800104 return;
105 }
106
107 boolean newFlowObjectives;
108 try {
109 String s = Tools.get(context.getProperties(), "useFlowObjectives");
110 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
111 } catch (ClassCastException e) {
112 newFlowObjectives = useFlowObjectives;
113 }
114
115 if (useFlowObjectives != newFlowObjectives) {
116 useFlowObjectives = newFlowObjectives;
117 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -0800118 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800119 }
Pier Ventref8543d82016-09-28 19:49:33 -0700120
121 String newLabelSelection;
122 try {
123 String s = Tools.get(context.getProperties(), "labelSelection");
124 newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim();
125 } catch (ClassCastException e) {
126 newLabelSelection = labelSelection;
127 }
128
129 if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInEnum(newLabelSelection)) {
130 labelSelection = newLabelSelection;
131 changeLabelSelections();
132 log.info("Settings: labelSelection={}", labelSelection);
133 }
Pier Ventre81c47bf2016-11-04 07:26:22 -0700134
135 boolean newFlowOptimization;
136 try {
137 String s = Tools.get(context.getProperties(), "useFlowOptimization");
138 newFlowOptimization = isNullOrEmpty(s) ? useFlowOptimization : Boolean.parseBoolean(s.trim());
139 } catch (ClassCastException e) {
140 newFlowOptimization = useFlowOptimization;
141 }
142
143 if (useFlowOptimization != newFlowOptimization) {
144 useFlowOptimization = newFlowOptimization;
145 changeFlowOptimization();
146 log.info("Settings: useFlowOptimization={}", useFlowOptimization);
147 }
148
149 boolean newCopyTtl;
150 try {
151 String s = Tools.get(context.getProperties(), "useCopyTtl");
152 newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim());
153 } catch (ClassCastException e) {
154 newCopyTtl = useCopyTtl;
155 }
156
157 if (useCopyTtl != newCopyTtl) {
158 useCopyTtl = newCopyTtl;
159 changeCopyTtl();
160 log.info("Settings: useCopyTtl={}", useCopyTtl);
161 }
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800162 }
163
164 /**
165 * Registers the specified compiler for the given intent class.
166 *
Pier Ventref8543d82016-09-28 19:49:33 -0700167 * @param cls the intent class
168 * @param compiler the intent compiler
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800169 * @param flowBased true if the compiler is flow based
170 * @param <T> the type of intent
171 */
172 @SuppressWarnings("unchecked")
173 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
174 boolean flowBased) {
175 if (flowBased) {
176 flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
177 } else {
178 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
179 }
180 if (flowBased == useFlowObjectives) {
181 extensionService.registerCompiler(cls, compiler);
182 }
183 }
184
185 /**
186 * Unregisters the compiler for the specified intent class.
187 *
Pier Ventref8543d82016-09-28 19:49:33 -0700188 * @param cls the intent class
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800189 * @param flowBased true if the compiler is flow based
190 * @param <T> the type of intent
191 */
192 @SuppressWarnings("unchecked")
193 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
194 if (flowBased) {
195 flowObjectiveBased.remove(cls);
196 } else {
197 flowRuleBased.remove(cls);
198 }
199 if (flowBased == useFlowObjectives) {
200 extensionService.unregisterCompiler(cls);
201 }
202 }
203
204 private void changeCompilers() {
205 if (useFlowObjectives) {
206 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
207 flowObjectiveBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
208 } else {
209 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
210 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
211 }
212 }
213
Pier Ventref8543d82016-09-28 19:49:33 -0700214 private void changeLabelSelections() {
Pier Ventre766995d2016-10-05 22:15:56 -0700215 LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection);
Pier Ventref8543d82016-09-28 19:49:33 -0700216 }
217
Pier Ventre81c47bf2016-11-04 07:26:22 -0700218 private void changeFlowOptimization() {
219 LinkCollectionCompiler.optimize = useFlowOptimization;
220 }
221
222 private void changeCopyTtl() {
223 LinkCollectionCompiler.copyTtl = useCopyTtl;
224 }
225
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800226}