Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.net.driver; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 20 | import org.apache.commons.configuration.ConfigurationException; |
| 21 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 22 | import org.apache.commons.configuration.XMLConfiguration; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.io.InputStream; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | /** |
| 29 | * Utility capable of reading driver configuration XML resources and producing |
| 30 | * a device driver provider as a result. |
| 31 | * <p> |
| 32 | * The drivers stream structure is as follows: |
| 33 | * </p> |
| 34 | * <pre> |
| 35 | * <drivers> |
| 36 | * <driver name=“...” [manufacturer="..." hwVersion="..." swVersion="..."]> |
| 37 | * <behaviour api="..." impl="..."/> |
| 38 | * ... |
| 39 | * [<property name=“key”>value</key>] |
| 40 | * ... |
| 41 | * </driver> |
| 42 | * ... |
| 43 | * </drivers> |
| 44 | * </pre> |
| 45 | */ |
| 46 | public class XmlDriverLoader { |
| 47 | |
| 48 | private static final String DRIVERS = "drivers"; |
| 49 | private static final String DRIVER = "driver"; |
| 50 | |
| 51 | private static final String BEHAVIOUR = "behaviour"; |
| 52 | private static final String PROPERTY = "property"; |
| 53 | |
| 54 | private static final String NAME = "[@name]"; |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 55 | private static final String EXTENDS = "[@extends]"; |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 56 | private static final String MFG = "[@manufacturer]"; |
| 57 | private static final String HW = "[@hwVersion]"; |
| 58 | private static final String SW = "[@swVersion]"; |
| 59 | private static final String API = "[@api]"; |
| 60 | private static final String IMPL = "[@impl]"; |
| 61 | |
| 62 | private final ClassLoader classLoader; |
| 63 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 64 | private Map<String, Driver> drivers = Maps.newHashMap(); |
| 65 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Creates a new driver loader capable of loading drivers from the supplied |
| 68 | * class loader. |
| 69 | * |
| 70 | * @param classLoader class loader to use |
| 71 | */ |
| 72 | public XmlDriverLoader(ClassLoader classLoader) { |
| 73 | this.classLoader = classLoader; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Loads the specified drivers resource as an XML stream and parses it to |
| 78 | * produce a ready-to-register driver provider. |
| 79 | * |
| 80 | * @param driversStream stream containing the drivers definitions |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 81 | * @param resolver driver resolver |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 82 | * @return driver provider |
| 83 | * @throws java.io.IOException if issues are encountered reading the stream |
| 84 | * or parsing the driver definitions within |
| 85 | */ |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 86 | public DefaultDriverProvider loadDrivers(InputStream driversStream, |
| 87 | DriverResolver resolver) throws IOException { |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 88 | try { |
| 89 | XMLConfiguration cfg = new XMLConfiguration(); |
| 90 | cfg.setRootElementName(DRIVERS); |
| 91 | cfg.setAttributeSplittingDisabled(true); |
| 92 | |
| 93 | cfg.load(driversStream); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 94 | return loadDrivers(cfg, resolver); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 95 | } catch (ConfigurationException e) { |
| 96 | throw new IOException("Unable to load drivers", e); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Loads a driver provider from the supplied hierarchical configuration. |
| 102 | * |
| 103 | * @param driversCfg hierarchical configuration containing the drivers definitions |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 104 | * @param resolver driver resolver |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 105 | * @return driver provider |
| 106 | */ |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 107 | public DefaultDriverProvider loadDrivers(HierarchicalConfiguration driversCfg, |
| 108 | DriverResolver resolver) { |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 109 | DefaultDriverProvider provider = new DefaultDriverProvider(); |
| 110 | for (HierarchicalConfiguration cfg : driversCfg.configurationsAt(DRIVER)) { |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 111 | DefaultDriver driver = loadDriver(cfg, resolver); |
| 112 | drivers.put(driver.name(), driver); |
| 113 | provider.addDriver(driver); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 114 | } |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 115 | drivers.clear(); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 116 | return provider; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Loads a driver from the supplied hierarchical configuration. |
| 121 | * |
| 122 | * @param driverCfg hierarchical configuration containing the driver definition |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 123 | * @param resolver driver resolver |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 124 | * @return driver |
| 125 | */ |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 126 | public DefaultDriver loadDriver(HierarchicalConfiguration driverCfg, |
| 127 | DriverResolver resolver) { |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 128 | String name = driverCfg.getString(NAME); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 129 | String parentName = driverCfg.getString(EXTENDS); |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 130 | String manufacturer = driverCfg.getString(MFG, ""); |
| 131 | String hwVersion = driverCfg.getString(HW, ""); |
| 132 | String swVersion = driverCfg.getString(SW, ""); |
| 133 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 134 | Driver parent = parentName != null ? resolve(parentName, resolver) : null; |
| 135 | return new DefaultDriver(name, parent, manufacturer, hwVersion, swVersion, |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 136 | parseBehaviours(driverCfg), |
| 137 | parseProperties(driverCfg)); |
| 138 | } |
| 139 | |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 140 | // Resolves the driver by name locally at first and then using the specified resolver. |
| 141 | private Driver resolve(String parentName, DriverResolver resolver) { |
| 142 | Driver driver = drivers.get(parentName); |
| 143 | return driver != null ? driver : |
| 144 | (resolver != null ? resolver.getDriver(parentName) : null); |
| 145 | } |
| 146 | |
Thomas Vachuska | a8f4e7d | 2015-01-08 17:31:55 -0800 | [diff] [blame] | 147 | // Parses the behaviours section. |
| 148 | private Map<Class<? extends Behaviour>, Class<? extends Behaviour>> |
| 149 | parseBehaviours(HierarchicalConfiguration driverCfg) { |
| 150 | ImmutableMap.Builder<Class<? extends Behaviour>, |
| 151 | Class<? extends Behaviour>> behaviours = ImmutableMap.builder(); |
| 152 | for (HierarchicalConfiguration b : driverCfg.configurationsAt(BEHAVIOUR)) { |
| 153 | behaviours.put(getClass(b.getString(API)), getClass(b.getString(IMPL))); |
| 154 | } |
| 155 | return behaviours.build(); |
| 156 | } |
| 157 | |
| 158 | // Parses the properties section. |
| 159 | private Map<String, String> parseProperties(HierarchicalConfiguration driverCfg) { |
| 160 | ImmutableMap.Builder<String, String> properties = ImmutableMap.builder(); |
| 161 | for (HierarchicalConfiguration b : driverCfg.configurationsAt(PROPERTY)) { |
| 162 | properties.put(b.getString(NAME), (String) b.getRootNode().getValue()); |
| 163 | } |
| 164 | return properties.build(); |
| 165 | } |
| 166 | |
| 167 | @SuppressWarnings("unchecked") |
| 168 | private Class<? extends Behaviour> getClass(String className) { |
| 169 | try { |
| 170 | return (Class<? extends Behaviour>) classLoader.loadClass(className); |
| 171 | } catch (ClassNotFoundException e) { |
| 172 | throw new IllegalArgumentException("Unable to load class " + className, e); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } |