alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [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.driver.pipeline; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Deactivate; |
| 20 | import org.apache.felix.scr.annotations.Reference; |
| 21 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 22 | import org.onosproject.net.driver.DriverAdminService; |
| 23 | import org.onosproject.net.driver.DriverProvider; |
| 24 | import org.onosproject.net.driver.XmlDriverLoader; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import java.io.IOException; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 30 | import java.io.InputStream; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Bootstrap for built in drivers. |
| 34 | */ |
| 35 | @Component(immediate = true) |
| 36 | public class DefaultDrivers { |
| 37 | |
| 38 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 39 | |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 40 | private static final String DRIVERS_XML = "/onos-drivers.xml"; |
| 41 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 42 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 43 | protected DriverAdminService driverService; |
| 44 | |
| 45 | private DriverProvider provider; |
| 46 | |
| 47 | @Activate |
| 48 | protected void activate() { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 49 | ClassLoader classLoader = getClass().getClassLoader(); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 50 | try { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 51 | InputStream stream = classLoader.getResourceAsStream(DRIVERS_XML); |
| 52 | provider = new XmlDriverLoader(classLoader).loadDrivers(stream); |
| 53 | driverService.registerProvider(provider); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 54 | } catch (IOException e) { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 55 | log.error("Unable to load default drivers", e); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 56 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 57 | log.info("Started"); |
| 58 | } |
| 59 | |
| 60 | @Deactivate |
| 61 | protected void deactivate() { |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 62 | if (provider != null) { |
| 63 | driverService.unregisterProvider(provider); |
| 64 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 65 | log.info("Stopped"); |
| 66 | } |
| 67 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 68 | } |