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, |
alessio | 22f2197 | 2020-10-30 17:01:07 +0100 | [diff] [blame^] | 59 | ICR_OPT_LABEL_SELECTION + "=" + ICR_OPT_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 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 74 | /** Indicates whether or not to use flow objective-based compilers. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 75 | private boolean useFlowObjectives = ICR_USE_FLOW_OBJECTIVES_DEFAULT; |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 76 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 77 | /** Defines the label selection algorithm - RANDOM or FIRST_FIT. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 78 | private String labelSelection = ICR_LABEL_SELECTION_DEFAULT; |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 79 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 80 | /** Defines the optimization for label selection algorithm - NONE, NO_SWAP, MIN_SWAP. */ |
Ray Milkey | d04e227 | 2018-10-16 18:20:18 -0700 | [diff] [blame] | 81 | private String optLabelSelection = ICR_OPT_LABEL_SELECTION_DEFAULT; |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 82 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 83 | /** 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] | 84 | private boolean optimizeInstructions = ICR_FLOW_OPTIMIZATION_DEFAULT; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 85 | |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 86 | /** 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] | 87 | private boolean useCopyTtl = ICR_COPY_TTL_DEFAULT; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 88 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 89 | private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap(); |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 90 | |
| 91 | // FIXME: temporary code for switching old compiler to new compiler |
Ray Milkey | 92d206b | 2018-02-12 15:01:24 -0800 | [diff] [blame] | 92 | private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap(); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 93 | |
| 94 | @Activate |
| 95 | public void activate() { |
| 96 | cfgService.registerProperties(getClass()); |
| 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
| 102 | cfgService.unregisterProperties(getClass(), false); |
| 103 | log.info("Stopped"); |
| 104 | } |
| 105 | |
| 106 | @Modified |
| 107 | public void modified(ComponentContext context) { |
| 108 | if (context == null) { |
Thomas Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 109 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 110 | log.info("Settings: labelSelection={}", labelSelection); |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 111 | log.info("Settings: useFlowOptimization={}", optimizeInstructions); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 112 | log.info("Settings: useCopyTtl={}", useCopyTtl); |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 113 | log.info("Settings: optLabelSelection={}", optLabelSelection); |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 114 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | boolean newFlowObjectives; |
| 119 | try { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 120 | String s = Tools.get(context.getProperties(), ICR_USE_FLOW_OBJECTIVES); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 121 | newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim()); |
| 122 | } catch (ClassCastException e) { |
| 123 | newFlowObjectives = useFlowObjectives; |
| 124 | } |
| 125 | |
| 126 | if (useFlowObjectives != newFlowObjectives) { |
| 127 | useFlowObjectives = newFlowObjectives; |
| 128 | changeCompilers(); |
Thomas Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 129 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 130 | } |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 131 | |
| 132 | String newLabelSelection; |
| 133 | try { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 134 | String s = Tools.get(context.getProperties(), ICR_LABEL_SELECTION); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 135 | newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim(); |
| 136 | } catch (ClassCastException e) { |
| 137 | newLabelSelection = labelSelection; |
| 138 | } |
| 139 | |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 140 | if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInSelEnum(newLabelSelection)) { |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 141 | labelSelection = newLabelSelection; |
| 142 | changeLabelSelections(); |
| 143 | log.info("Settings: labelSelection={}", labelSelection); |
| 144 | } |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 145 | |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 146 | String newOptLabelSelection; |
| 147 | try { |
| 148 | // The optimization behavior provided by the user |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 149 | String optLabelSelected = Tools.get(context.getProperties(), ICR_OPT_LABEL_SELECTION); |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 150 | // Parse the content of the string |
| 151 | newOptLabelSelection = isNullOrEmpty(optLabelSelected) ? optLabelSelection : optLabelSelected.trim(); |
| 152 | } catch (ClassCastException e) { |
| 153 | newOptLabelSelection = optLabelSelection; |
| 154 | } |
| 155 | |
| 156 | if (!optLabelSelection.equals(newOptLabelSelection) && LabelAllocator.isInOptEnum(newOptLabelSelection)) { |
| 157 | optLabelSelection = newOptLabelSelection; |
| 158 | changeOptLabelSelections(); |
| 159 | log.info("Settings: optLabelSelection={}", optLabelSelection); |
| 160 | } |
| 161 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 162 | boolean newFlowOptimization; |
| 163 | try { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 164 | String s = Tools.get(context.getProperties(), ICR_FLOW_OPTIMIZATION); |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 165 | newFlowOptimization = isNullOrEmpty(s) ? optimizeInstructions : Boolean.parseBoolean(s.trim()); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 166 | } catch (ClassCastException e) { |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 167 | newFlowOptimization = optimizeInstructions; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 170 | if (optimizeInstructions != newFlowOptimization) { |
| 171 | optimizeInstructions = newFlowOptimization; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 172 | changeFlowOptimization(); |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 173 | log.info("Settings: useFlowOptimization={}", optimizeInstructions); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | boolean newCopyTtl; |
| 177 | try { |
Thomas Vachuska | f566fa2 | 2018-10-30 14:03:36 -0700 | [diff] [blame] | 178 | String s = Tools.get(context.getProperties(), ICR_COPY_TTL); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 179 | newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim()); |
| 180 | } catch (ClassCastException e) { |
| 181 | newCopyTtl = useCopyTtl; |
| 182 | } |
| 183 | |
| 184 | if (useCopyTtl != newCopyTtl) { |
| 185 | useCopyTtl = newCopyTtl; |
| 186 | changeCopyTtl(); |
| 187 | log.info("Settings: useCopyTtl={}", useCopyTtl); |
| 188 | } |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Registers the specified compiler for the given intent class. |
| 193 | * |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 194 | * @param cls the intent class |
| 195 | * @param compiler the intent compiler |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 196 | * @param flowBased true if the compiler is flow based |
| 197 | * @param <T> the type of intent |
| 198 | */ |
| 199 | @SuppressWarnings("unchecked") |
| 200 | <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler, |
| 201 | boolean flowBased) { |
| 202 | if (flowBased) { |
Ray Milkey | 92d206b | 2018-02-12 15:01:24 -0800 | [diff] [blame] | 203 | flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 204 | } else { |
| 205 | flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler); |
| 206 | } |
| 207 | if (flowBased == useFlowObjectives) { |
| 208 | extensionService.registerCompiler(cls, compiler); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Unregisters the compiler for the specified intent class. |
| 214 | * |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 215 | * @param cls the intent class |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 216 | * @param flowBased true if the compiler is flow based |
| 217 | * @param <T> the type of intent |
| 218 | */ |
| 219 | @SuppressWarnings("unchecked") |
| 220 | <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) { |
| 221 | if (flowBased) { |
| 222 | flowObjectiveBased.remove(cls); |
| 223 | } else { |
| 224 | flowRuleBased.remove(cls); |
| 225 | } |
| 226 | if (flowBased == useFlowObjectives) { |
| 227 | extensionService.unregisterCompiler(cls); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | private void changeCompilers() { |
| 232 | if (useFlowObjectives) { |
| 233 | flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls)); |
Ray Milkey | 92d206b | 2018-02-12 15:01:24 -0800 | [diff] [blame] | 234 | flowObjectiveBased.forEach((cls, compiler) -> { |
| 235 | extensionService.registerCompiler(cls, compiler); |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 236 | }); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 237 | } else { |
| 238 | flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls)); |
| 239 | flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler)); |
| 240 | } |
| 241 | } |
| 242 | |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 243 | private void changeLabelSelections() { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 244 | LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Pier Luigi | 0b4222e | 2017-07-21 09:47:14 +0200 | [diff] [blame] | 247 | private void changeOptLabelSelections() { |
| 248 | LinkCollectionCompiler.labelAllocator.setOptLabelSelection(optLabelSelection); |
| 249 | } |
| 250 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 251 | private void changeFlowOptimization() { |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 252 | LinkCollectionCompiler.optimizeInstructions = optimizeInstructions; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | private void changeCopyTtl() { |
| 256 | LinkCollectionCompiler.copyTtl = useCopyTtl; |
| 257 | } |
| 258 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 259 | } |