alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [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 | */ |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 16 | package org.onosproject.net.driver; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
Jonathan Hart | 17d0045 | 2015-04-21 17:10:00 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Component; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Deactivate; |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 26 | /** |
Thomas Vachuska | 800a7a4 | 2016-02-16 16:11:13 -0800 | [diff] [blame] | 27 | * Abstract boot-strapper for loading and registering driver definitions. |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 28 | */ |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 29 | @Component |
| 30 | public abstract class AbstractDriverLoader { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 31 | |
| 32 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 33 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 34 | private DriverProvider provider; |
Thomas Vachuska | 800a7a4 | 2016-02-16 16:11:13 -0800 | [diff] [blame] | 35 | private final String path; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 36 | |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 37 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 38 | protected DriverAdminService driverAdminService; |
| 39 | |
Thomas Vachuska | 800a7a4 | 2016-02-16 16:11:13 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Creates a new loader for resource with the specified path. |
| 42 | * |
| 43 | * @param path drivers definition XML resource path |
| 44 | */ |
| 45 | protected AbstractDriverLoader(String path) { |
| 46 | this.path = path; |
| 47 | } |
| 48 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 49 | @Activate |
| 50 | protected void activate() { |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 51 | try { |
Thomas Vachuska | e7ea688 | 2016-07-22 10:22:46 -0700 | [diff] [blame] | 52 | provider = new XmlDriverLoader(getClass().getClassLoader(), driverAdminService) |
Thomas Vachuska | 800a7a4 | 2016-02-16 16:11:13 -0800 | [diff] [blame] | 53 | .loadDrivers(getClass().getResourceAsStream(path), driverAdminService); |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 54 | driverAdminService.registerProvider(provider); |
Thomas Vachuska | 635c2d7 | 2015-05-08 14:32:13 -0700 | [diff] [blame] | 55 | } catch (Exception e) { |
Thomas Vachuska | 800a7a4 | 2016-02-16 16:11:13 -0800 | [diff] [blame] | 56 | log.error("Unable to load {} driver definitions", path, e); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 57 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 58 | log.info("Started"); |
| 59 | } |
| 60 | |
| 61 | @Deactivate |
| 62 | protected void deactivate() { |
Thomas Vachuska | 866b46a | 2015-04-30 00:26:55 -0700 | [diff] [blame] | 63 | driverAdminService.unregisterProvider(provider); |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 64 | log.info("Stopped"); |
| 65 | } |
| 66 | |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 67 | } |