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 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 70 | private static final boolean DEFAULT_FLOW_OPTIMIZATION = false; |
| 71 | @Property(name = "useFlowOptimization", |
| 72 | boolValue = DEFAULT_FLOW_OPTIMIZATION, |
| 73 | label = "Indicates whether or not to optimize the flows in the link collection compiler") |
| 74 | private boolean useFlowOptimization = DEFAULT_FLOW_OPTIMIZATION; |
| 75 | |
| 76 | private static final boolean DEFAULT_COPY_TTL = false; |
| 77 | @Property(name = "useCopyTtl", |
| 78 | boolValue = DEFAULT_COPY_TTL, |
| 79 | label = "Indicates whether or not to use copy ttl in the link collection compiler") |
| 80 | private boolean useCopyTtl = DEFAULT_COPY_TTL; |
| 81 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 82 | private final Map<Class<Intent>, IntentCompiler<Intent>> flowRuleBased = Maps.newConcurrentMap(); |
| 83 | private final Map<Class<Intent>, IntentCompiler<Intent>> flowObjectiveBased = Maps.newConcurrentMap(); |
| 84 | |
| 85 | @Activate |
| 86 | public void activate() { |
| 87 | cfgService.registerProperties(getClass()); |
| 88 | log.info("Started"); |
| 89 | } |
| 90 | |
| 91 | @Deactivate |
| 92 | public void deactivate() { |
| 93 | cfgService.unregisterProperties(getClass(), false); |
| 94 | log.info("Stopped"); |
| 95 | } |
| 96 | |
| 97 | @Modified |
| 98 | public void modified(ComponentContext context) { |
| 99 | if (context == null) { |
Thomas Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 100 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 101 | log.info("Settings: labelSelection={}", labelSelection); |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 102 | log.info("Settings: useFlowOptimization={}", useFlowOptimization); |
| 103 | log.info("Settings: useCopyTtl={}", useCopyTtl); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
| 107 | boolean newFlowObjectives; |
| 108 | try { |
| 109 | String s = Tools.get(context.getProperties(), "useFlowObjectives"); |
| 110 | newFlowObjectives = isNullOrEmpty(s) ? useFlowObjectives : Boolean.parseBoolean(s.trim()); |
| 111 | } catch (ClassCastException e) { |
| 112 | newFlowObjectives = useFlowObjectives; |
| 113 | } |
| 114 | |
| 115 | if (useFlowObjectives != newFlowObjectives) { |
| 116 | useFlowObjectives = newFlowObjectives; |
| 117 | changeCompilers(); |
Thomas Vachuska | 3debf50 | 2016-03-01 12:01:21 -0800 | [diff] [blame] | 118 | log.info("Settings: useFlowObjectives={}", useFlowObjectives); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 119 | } |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 120 | |
| 121 | String newLabelSelection; |
| 122 | try { |
| 123 | String s = Tools.get(context.getProperties(), "labelSelection"); |
| 124 | newLabelSelection = isNullOrEmpty(s) ? labelSelection : s.trim(); |
| 125 | } catch (ClassCastException e) { |
| 126 | newLabelSelection = labelSelection; |
| 127 | } |
| 128 | |
| 129 | if (!labelSelection.equals(newLabelSelection) && LabelAllocator.isInEnum(newLabelSelection)) { |
| 130 | labelSelection = newLabelSelection; |
| 131 | changeLabelSelections(); |
| 132 | log.info("Settings: labelSelection={}", labelSelection); |
| 133 | } |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 134 | |
| 135 | boolean newFlowOptimization; |
| 136 | try { |
| 137 | String s = Tools.get(context.getProperties(), "useFlowOptimization"); |
| 138 | newFlowOptimization = isNullOrEmpty(s) ? useFlowOptimization : Boolean.parseBoolean(s.trim()); |
| 139 | } catch (ClassCastException e) { |
| 140 | newFlowOptimization = useFlowOptimization; |
| 141 | } |
| 142 | |
| 143 | if (useFlowOptimization != newFlowOptimization) { |
| 144 | useFlowOptimization = newFlowOptimization; |
| 145 | changeFlowOptimization(); |
| 146 | log.info("Settings: useFlowOptimization={}", useFlowOptimization); |
| 147 | } |
| 148 | |
| 149 | boolean newCopyTtl; |
| 150 | try { |
| 151 | String s = Tools.get(context.getProperties(), "useCopyTtl"); |
| 152 | newCopyTtl = isNullOrEmpty(s) ? useCopyTtl : Boolean.parseBoolean(s.trim()); |
| 153 | } catch (ClassCastException e) { |
| 154 | newCopyTtl = useCopyTtl; |
| 155 | } |
| 156 | |
| 157 | if (useCopyTtl != newCopyTtl) { |
| 158 | useCopyTtl = newCopyTtl; |
| 159 | changeCopyTtl(); |
| 160 | log.info("Settings: useCopyTtl={}", useCopyTtl); |
| 161 | } |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Registers the specified compiler for the given intent class. |
| 166 | * |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 167 | * @param cls the intent class |
| 168 | * @param compiler the intent compiler |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 169 | * @param flowBased true if the compiler is flow based |
| 170 | * @param <T> the type of intent |
| 171 | */ |
| 172 | @SuppressWarnings("unchecked") |
| 173 | <T extends Intent> void registerCompiler(Class<T> cls, IntentCompiler<T> compiler, |
| 174 | boolean flowBased) { |
| 175 | if (flowBased) { |
| 176 | flowObjectiveBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler); |
| 177 | } else { |
| 178 | flowRuleBased.put((Class<Intent>) cls, (IntentCompiler<Intent>) compiler); |
| 179 | } |
| 180 | if (flowBased == useFlowObjectives) { |
| 181 | extensionService.registerCompiler(cls, compiler); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Unregisters the compiler for the specified intent class. |
| 187 | * |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 188 | * @param cls the intent class |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 189 | * @param flowBased true if the compiler is flow based |
| 190 | * @param <T> the type of intent |
| 191 | */ |
| 192 | @SuppressWarnings("unchecked") |
| 193 | <T extends Intent> void unregisterCompiler(Class<T> cls, boolean flowBased) { |
| 194 | if (flowBased) { |
| 195 | flowObjectiveBased.remove(cls); |
| 196 | } else { |
| 197 | flowRuleBased.remove(cls); |
| 198 | } |
| 199 | if (flowBased == useFlowObjectives) { |
| 200 | extensionService.unregisterCompiler(cls); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private void changeCompilers() { |
| 205 | if (useFlowObjectives) { |
| 206 | flowRuleBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls)); |
| 207 | flowObjectiveBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler)); |
| 208 | } else { |
| 209 | flowObjectiveBased.forEach((cls, compiler) -> extensionService.unregisterCompiler(cls)); |
| 210 | flowRuleBased.forEach((cls, compiler) -> extensionService.registerCompiler(cls, compiler)); |
| 211 | } |
| 212 | } |
| 213 | |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 214 | private void changeLabelSelections() { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 215 | LinkCollectionCompiler.labelAllocator.setLabelSelection(labelSelection); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 218 | private void changeFlowOptimization() { |
| 219 | LinkCollectionCompiler.optimize = useFlowOptimization; |
| 220 | } |
| 221 | |
| 222 | private void changeCopyTtl() { |
| 223 | LinkCollectionCompiler.copyTtl = useCopyTtl; |
| 224 | } |
| 225 | |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 226 | } |