Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 20 | import org.onlab.util.Tools; |
| 21 | import org.onosproject.cfg.ComponentConfigService; |
| 22 | import org.onosproject.net.intent.Intent; |
| 23 | import org.onosproject.net.intent.IntentCompiler; |
| 24 | import org.onosproject.net.intent.IntentExtensionService; |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 25 | import org.onosproject.net.resource.impl.LabelAllocator; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 26 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 27 | import org.osgi.service.component.annotations.Activate; |
| 28 | import org.osgi.service.component.annotations.Component; |
| 29 | import org.osgi.service.component.annotations.Deactivate; |
| 30 | import org.osgi.service.component.annotations.Modified; |
| 31 | import org.osgi.service.component.annotations.Reference; |
| 32 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.Map; |
| 36 | |
| 37 | import static com.google.common.base.Strings.isNullOrEmpty; |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 38 | import static org.onosproject.net.OsgiPropertyConstants.ICR_COPY_TTL; |
| 39 | import static org.onosproject.net.OsgiPropertyConstants.ICR_COPY_TTL_DEFAULT; |
| 40 | import static org.onosproject.net.OsgiPropertyConstants.ICR_FLOW_OPTIMIZATION; |
| 41 | import static org.onosproject.net.OsgiPropertyConstants.ICR_LABEL_SELECTION; |
| 42 | import static org.onosproject.net.OsgiPropertyConstants.ICR_OPT_LABEL_SELECTION; |
| 43 | import static org.onosproject.net.OsgiPropertyConstants.ICR_USE_FLOW_OBJECTIVES; |
| 44 | import static org.onosproject.net.OsgiPropertyConstants.ICR_USE_FLOW_OBJECTIVES_DEFAULT; |
| 45 | import static org.onosproject.net.OsgiPropertyConstants.ICR_FLOW_OPTIMIZATION_DEFAULT; |
| 46 | import static org.onosproject.net.OsgiPropertyConstants.ICR_LABEL_SELECTION_DEFAULT; |
| 47 | import static org.onosproject.net.OsgiPropertyConstants.ICR_OPT_LABEL_SELECTION_DEFAULT; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Auxiliary utility to register either flow-rule compilers or flow-objective |
| 52 | * compilers. |
| 53 | */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 54 | @Component( |
| 55 | service = IntentConfigurableRegistrator.class, |
| 56 | property = { |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 57 | ICR_USE_FLOW_OBJECTIVES + ":Boolean=" + ICR_USE_FLOW_OBJECTIVES_DEFAULT, |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 58 | ICR_LABEL_SELECTION + "=" + ICR_LABEL_SELECTION_DEFAULT, |
| 59 | ICR_OPT_LABEL_SELECTION + "=" + ICR_LABEL_SELECTION_DEFAULT, |
Ray Milkey | 2d7bca1 | 2018-10-17 14:51:52 -0700 | [diff] [blame] | 60 | ICR_FLOW_OPTIMIZATION + ":Boolean=" + ICR_FLOW_OPTIMIZATION_DEFAULT, |
| 61 | ICR_COPY_TTL + ":Boolean=" + ICR_COPY_TTL_DEFAULT |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 62 | } |
| 63 | ) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 64 | public class IntentConfigurableRegistrator { |
| 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 69 | protected IntentExtensionService extensionService; |
| 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 72 | protected ComponentConfigService cfgService; |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | //@Property(name = "useFlowObjectives", |
| 75 | // boolValue = DEFAULT_FLOW_OBJECTIVES, |
| 76 | // label = "Indicates whether or not to use flow objective-based compilers") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 77 | private boolean useFlowObjectives = ICR_USE_FLOW_OBJECTIVES_DEFAULT; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 78 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 79 | //@Property(name = "labelSelection", |
| 80 | // value = DEFAULT_LABEL_SELECTION, |
| 81 | // label = "Defines the label selection algorithm - RANDOM or FIRST_FIT") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 82 | private String labelSelection = ICR_LABEL_SELECTION_DEFAULT; |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 84 | //@Property(name = "optLabelSelection", |
| 85 | // value = DEFAULT_OPT_LABEL_SELECTION, |
| 86 | // label = "Defines the optimization for label selection algorithm - NONE, NO_SWAP, MIN_SWAP") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 87 | private String optLabelSelection = ICR_OPT_LABEL_SELECTION_DEFAULT; |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 88 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 89 | //@Property(name = "optimizeInstructions", |
| 90 | // boolValue = DEFAULT_FLOW_OPTIMIZATION, |
| 91 | // label = "Indicates whether or not to optimize the flows in the link collection compiler") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 92 | private boolean optimizeInstructions = ICR_FLOW_OPTIMIZATION_DEFAULT; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 93 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 94 | //@Property(name = "useCopyTtl", |
| 95 | // boolValue = DEFAULT_COPY_TTL, |
| 96 | // label = "Indicates whether or not to use copy ttl in the link collection compiler") |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 97 | private boolean useCopyTtl = ICR_COPY_TTL_DEFAULT; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 98 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 99 | private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap(); |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 100 | |
| 101 | // FIXME: temporary code for switching old compiler to new compiler |
Ray Milkey | 92d206b | 2018-02-12 15:01:24 -0800 | [diff] [blame] | 102 | private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap(); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 103 | |
| 104 | @Activate |
| 105 | public void activate() { |
| 106 | cfgService.registerProperties(getClass()); |
| 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate() { |
| 112 | cfgService.unregisterProperties(getClass(), false); |
| 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Modified |
| 117 | public void modified(ComponentContext context) { |
| 118 | if (context == null) { |
Thomas Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 119 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 120 | log.info("Settings: labelSelection={}", labelSelection); |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 121 | log.info("Settings: useFlowOptimization={}", optimizeInstructions); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 122 | log.info("Settings: useCopyTtl={}", useCopyTtl); |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 123 | log.info("Settings: optLabelSelection={}", optLabelSelection); |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 124 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
| 128 | boolean newFlowObjectives; |
| 129 | try { |
| 130 | String s = Tools.get(context.getProperties(), "useFlowObjectives"); |
| 131 | newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim()); |
| 132 | } catch (ClassCastException e) { |
| 133 | newFlowObjectives = useFlowObjectives; |
| 134 | } |
| 135 | |
| 136 | if (useFlowObjectives != newFlowObjectives) { |
| 137 | useFlowObjectives = newFlowObjectives; |
| 138 | changeCompilers(); |
Thomas Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 139 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 140 | } |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 141 | |
| 142 | String newLabelSelection; |
| 143 | try { |
| 144 | String s = Tools.get(context.getProperties(), "labelSelection"); |
| 145 | newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim(); |
| 146 | } catch (ClassCastException e) { |
| 147 | newLabelSelection = labelSelection; |
| 148 | } |
| 149 | |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 150 | if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInSelEnum(newLabelSelection)) { |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 151 | labelSelection = newLabelSelection; |
| 152 | changeLabelSelections(); |
| 153 | log.info("Settings: labelSelection={}", labelSelection); |
| 154 | } |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 155 | |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 156 | String newOptLabelSelection; |
| 157 | try { |
| 158 | // The optimization behavior provided by the user |
| 159 | String optLabelSelected = Tools.get(context.getProperties(), "optLabelSelection"); |
| 160 | // Parse the content of the string |
| 161 | newOptLabelSelection = isNullOrEmpty(optLabelSelected) ? optLabelSelection : optLabelSelected.trim(); |
| 162 | } catch (ClassCastException e) { |
| 163 | newOptLabelSelection = optLabelSelection; |
| 164 | } |
| 165 | |
| 166 | if (!optLabelSelection.equals(newOptLabelSelection) && LabelAllocator.isInOptEnum(newOptLabelSelection)) { |
| 167 | optLabelSelection = newOptLabelSelection; |
| 168 | changeOptLabelSelections(); |
| 169 | log.info("Settings: optLabelSelection={}", optLabelSelection); |
| 170 | } |
| 171 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 172 | boolean newFlowOptimization; |
| 173 | try { |
| 174 | String s = Tools.get(context.getProperties(), "useFlowOptimization"); |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 175 | newFlowOptimization = isNullOrEmpty(s) ? optimizeInstructions : Boolean.parseBoolean(s.trim()); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 176 | } catch (ClassCastException e) { |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 177 | newFlowOptimization = optimizeInstructions; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 180 | if (optimizeInstructions != newFlowOptimization) { |
| 181 | optimizeInstructions = newFlowOptimization; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 182 | changeFlowOptimization(); |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 183 | log.info("Settings: useFlowOptimization={}", optimizeInstructions); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | boolean newCopyTtl; |
| 187 | try { |
| 188 | String s = Tools.get(context.getProperties(), "useCopyTtl"); |
| 189 | newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim()); |
| 190 | } catch (ClassCastException e) { |
| 191 | newCopyTtl = useCopyTtl; |
| 192 | } |
| 193 | |
| 194 | if (useCopyTtl != newCopyTtl) { |
| 195 | useCopyTtl = newCopyTtl; |
| 196 | changeCopyTtl(); |
| 197 | log.info("Settings: useCopyTtl={}", useCopyTtl); |
| 198 | } |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Registers the specified compiler for the given intent class. |
| 203 | * |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 204 | * @param cls the intent class |
| 205 | * @param compiler the intent compiler |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 206 | * @param flowBased true if the compiler is flow based |
| 207 | * @param <T> the type of intent |
| 208 | */ |
| 209 | @SuppressWarnings("unchecked") |
| 210 | <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler, |
| 211 | boolean flowBased) { |
| 212 | if (flowBased) { |
Ray Milkey | 92d206b | 2018-02-12 15:01:24 -0800 | [diff] [blame] | 213 | flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 214 | } else { |
| 215 | flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler); |
| 216 | } |
| 217 | if (flowBased == useFlowObjectives) { |
| 218 | extensionService.registerCompiler(cls, compiler); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Unregisters the compiler for the specified intent class. |
| 224 | * |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 225 | * @param cls the intent class |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 226 | * @param flowBased true if the compiler is flow based |
| 227 | * @param <T> the type of intent |
| 228 | */ |
| 229 | @SuppressWarnings("unchecked") |
| 230 | <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) { |
| 231 | if (flowBased) { |
| 232 | flowObjectiveBased.remove(cls); |
| 233 | } else { |
| 234 | flowRuleBased.remove(cls); |
| 235 | } |
| 236 | if (flowBased == useFlowObjectives) { |
| 237 | extensionService.unregisterCompiler(cls); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | private void changeCompilers() { |
| 242 | if (useFlowObjectives) { |
| 243 | flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls)); |
Ray Milkey | 92d206b | 2018-02-12 15:01:24 -0800 | [diff] [blame] | 244 | flowObjectiveBased.forEach((cls, compiler) -> { |
| 245 | extensionService.registerCompiler(cls, compiler); |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 246 | }); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 247 | } else { |
| 248 | flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls)); |
| 249 | flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler)); |
| 250 | } |
| 251 | } |
| 252 | |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 253 | private void changeLabelSelections() { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 254 | LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 257 | private void changeOptLabelSelections() { |
| 258 | LinkCollectionCompiler.labelAllocator.setOptLabelSelection(optLabelSelection); |
| 259 | } |
| 260 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 261 | private void changeFlowOptimization() { |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 262 | LinkCollectionCompiler.optimizeInstructions = optimizeInstructions; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | private void changeCopyTtl() { |
| 266 | LinkCollectionCompiler.copyTtl = useCopyTtl; |
| 267 | } |
| 268 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 269 | } |