blob: fea1e7bbb1f651cbcae37d268e15e3097b2ae6ab [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;
33import org.osgi.service.component.ComponentContext;
34import org.slf4j.Logger;
35
36import java.util.Map;
37
38import static com.google.common.base.Strings.isNullOrEmpty;
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Auxiliary utility to register either flow-rule compilers or flow-objective
43 * compilers.
44 */
45@Component
46@Service(value = IntentConfigurableRegistrator.class)
47public class IntentConfigurableRegistrator {
48
49 private final Logger log = getLogger(getClass());
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected IntentExtensionService extensionService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected ComponentConfigService cfgService;
56
57 private static final boolean DEFAULT_FLOW_OBJECTIVES = false;
58 @Property(name = "useFlowObjectives",
59 boolValue = DEFAULT_FLOW_OBJECTIVES,
60 label = "Indicates whether to use flow objective-based compilers")
61 private boolean useFlowObjectives = DEFAULT_FLOW_OBJECTIVES;
62
63 private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap();
64 private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap();
65
66 @Activate
67 public void activate() {
68 cfgService.registerProperties(getClass());
69 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
74 cfgService.unregisterProperties(getClass(), false);
75 log.info("Stopped");
76 }
77
78 @Modified
79 public void modified(ComponentContext context) {
80 if (context == null) {
Thomas Vachuska3debf502016-03-01 12:01:21 -080081 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080082 return;
83 }
84
85 boolean newFlowObjectives;
86 try {
87 String s = Tools.get(context.getProperties(), "useFlowObjectives");
88 newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim());
89 } catch (ClassCastException e) {
90 newFlowObjectives = useFlowObjectives;
91 }
92
93 if (useFlowObjectives != newFlowObjectives) {
94 useFlowObjectives = newFlowObjectives;
95 changeCompilers();
Thomas Vachuska3debf502016-03-01 12:01:21 -080096 log.info("Settings: useFlowObjectives={}", useFlowObjectives);
Thomas Vachuskabdbdd242016-03-01 01:55:55 -080097 }
98 }
99
100 /**
101 * Registers the specified compiler for the given intent class.
102 *
103 * @param cls intent class
104 * @param compiler intent compiler
105 * @param flowBased true if the compiler is flow based
106 * @param <T> the type of intent
107 */
108 @SuppressWarnings("unchecked")
109 <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler,
110 boolean flowBased) {
111 if (flowBased) {
112 flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
113 } else {
114 flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler);
115 }
116 if (flowBased == useFlowObjectives) {
117 extensionService.registerCompiler(cls, compiler);
118 }
119 }
120
121 /**
122 * Unregisters the compiler for the specified intent class.
123 *
124 * @param cls intent class
125 * @param flowBased true if the compiler is flow based
126 * @param <T> the type of intent
127 */
128 @SuppressWarnings("unchecked")
129 <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) {
130 if (flowBased) {
131 flowObjectiveBased.remove(cls);
132 } else {
133 flowRuleBased.remove(cls);
134 }
135 if (flowBased == useFlowObjectives) {
136 extensionService.unregisterCompiler(cls);
137 }
138 }
139
140 private void changeCompilers() {
141 if (useFlowObjectives) {
142 flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
143 flowObjectiveBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
144 } else {
145 flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls));
146 flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler));
147 }
148 }
149
150}