Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.net.intent.impl.compiler; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.onlab.util.Tools; |
| 29 | import org.onosproject.cfg.ComponentConfigService; |
| 30 | import org.onosproject.net.intent.Intent; |
| 31 | import org.onosproject.net.intent.IntentCompiler; |
| 32 | import org.onosproject.net.intent.IntentExtensionService; |
| 33 | import org.osgi.service.component.ComponentContext; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Map; |
| 37 | |
| 38 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 39 | import 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) |
| 47 | public 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 Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 81 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 82 | 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 Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 96 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 97 | } |
| 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 | } |