blob: 50578de58478c580fbc47d288fee492a05f1306a [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
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080070 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
71 private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap();
72
73 @Activate
74 public void activate() {
75 cfgService.registerProperties(getClass());
76 log.info("Started");
77 }
78
79 @Deactivate
80 public void deactivate() {
81 cfgService.unregisterProperties(getClass(), false);
82 log.info("Stopped");
83 }
84
85 @Modified
86 public void modified(ComponentContext context) {
87 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -080088 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Pier Ventref8543d82016-09-28 19:49:33 -070089 log.info("Settings: labelSelection={}", labelSelection);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080090 return;
91 }
92
93 boolean newFlowObjectives;
94 try {
95 String s = Tools.get(context.getProperties(), "useFlowObjectives");
96 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
97 } catch (ClassCastException e) {
98 newFlowObjectives = useFlowObjectives;
99 }
100
101 if (useFlowObjectives != newFlowObjectives) {
102 useFlowObjectives = newFlowObjectives;
103 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -0800104 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800105 }
Pier Ventref8543d82016-09-28 19:49:33 -0700106
107 String newLabelSelection;
108 try {
109 String s = Tools.get(context.getProperties(), "labelSelection");
110 newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim();
111 } catch (ClassCastException e) {
112 newLabelSelection = labelSelection;
113 }
114
115 if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInEnum(newLabelSelection)) {
116 labelSelection = newLabelSelection;
117 changeLabelSelections();
118 log.info("Settings: labelSelection={}", labelSelection);
119 }
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800120 }
121
122 /**
123 * Registers the specified compiler for the given intent class.
124 *
Pier Ventref8543d82016-09-28 19:49:33 -0700125 * @param cls the intent class
126 * @param compiler the intent compiler
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800127 * @param flowBased true if the compiler is flow based
128 * @param <T> the type of intent
129 */
130 @SuppressWarnings("unchecked")
131 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
132 boolean flowBased) {
133 if (flowBased) {
134 flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
135 } else {
136 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
137 }
138 if (flowBased == useFlowObjectives) {
139 extensionService.registerCompiler(cls, compiler);
140 }
141 }
142
143 /**
144 * Unregisters the compiler for the specified intent class.
145 *
Pier Ventref8543d82016-09-28 19:49:33 -0700146 * @param cls the intent class
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800147 * @param flowBased true if the compiler is flow based
148 * @param <T> the type of intent
149 */
150 @SuppressWarnings("unchecked")
151 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
152 if (flowBased) {
153 flowObjectiveBased.remove(cls);
154 } else {
155 flowRuleBased.remove(cls);
156 }
157 if (flowBased == useFlowObjectives) {
158 extensionService.unregisterCompiler(cls);
159 }
160 }
161
162 private void changeCompilers() {
163 if (useFlowObjectives) {
164 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
165 flowObjectiveBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
166 } else {
167 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
168 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
169 }
170 }
171
Pier Ventref8543d82016-09-28 19:49:33 -0700172 private void changeLabelSelections() {
173 PathCompiler.labelAllocator.setLabelSelection(labelSelection);
174 }
175
Thomas Vachuskabdbdd242016-03-01 01:55:55 -0800176}