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; |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 33 | import org.onosproject.net.resource.impl.LabelAllocator; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 34 | import org.osgi.service.component.ComponentContext; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import java.util.Map; |
| 38 | |
| 39 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 40 | import 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) |
| 48 | public 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 Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 61 | label = "Indicates whether or not to use flow objective-based compilers") |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 62 | private boolean useFlowObjectives = DEFAULT_FLOW_OBJECTIVES; |
| 63 | |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 64 | 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 Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 70 | 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 Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 88 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 89 | log.info("Settings: labelSelection={}", labelSelection); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 90 | 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 Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 104 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 105 | } |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 106 | |
| 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 Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Registers the specified compiler for the given intent class. |
| 124 | * |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 125 | * @param cls the intent class |
| 126 | * @param compiler the intent compiler |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 127 | * @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 Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 146 | * @param cls the intent class |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 147 | * @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 Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame^] | 172 | private void changeLabelSelections() { |
| 173 | PathCompiler.labelAllocator.setLabelSelection(labelSelection); |
| 174 | } |
| 175 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 176 | } |