Thomas Vachuska | 43de6ee | 2017-03-15 15:45:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Thomas Vachuska | 43de6ee | 2017-03-15 15:45:23 -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 | */ |
| 16 | package org.onosproject.net.driver; |
| 17 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.osgi.service.component.annotations.Activate; |
| 19 | import org.osgi.service.component.annotations.Deactivate; |
| 20 | import org.osgi.service.component.annotations.Reference; |
| 21 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Thomas Vachuska | 43de6ee | 2017-03-15 15:45:23 -0700 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | /** |
| 26 | * Abstract bootstrapper for loading and registering driver definitions that |
| 27 | * are independent from the default driver definitions. |
| 28 | */ |
Thomas Vachuska | 43de6ee | 2017-03-15 15:45:23 -0700 | [diff] [blame] | 29 | public abstract class AbstractIndependentDriverLoader { |
| 30 | |
| 31 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 32 | |
| 33 | private DriverProvider provider; |
| 34 | private final String path; |
| 35 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Thomas Vachuska | 43de6ee | 2017-03-15 15:45:23 -0700 | [diff] [blame] | 37 | protected DriverAdminService driverAdminService; |
| 38 | |
| 39 | /** |
| 40 | * Creates a new loader for resource with the specified path. |
| 41 | * |
| 42 | * @param path drivers definition XML resource path |
| 43 | */ |
| 44 | protected AbstractIndependentDriverLoader(String path) { |
| 45 | this.path = path; |
| 46 | } |
| 47 | |
| 48 | @Activate |
| 49 | protected void activate() { |
| 50 | try { |
| 51 | provider = new XmlDriverLoader(getClass().getClassLoader(), driverAdminService) |
| 52 | .loadDrivers(getClass().getResourceAsStream(path), driverAdminService); |
| 53 | driverAdminService.registerProvider(provider); |
| 54 | } catch (Exception e) { |
| 55 | log.error("Unable to load {} driver definitions", path, e); |
| 56 | } |
| 57 | log.info("Started"); |
| 58 | } |
| 59 | |
| 60 | @Deactivate |
| 61 | protected void deactivate() { |
| 62 | driverAdminService.unregisterProvider(provider); |
| 63 | log.info("Stopped"); |
| 64 | } |
| 65 | |
| 66 | } |