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