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