blob: e35c1b43540682529fa6a50bb0be34998a5bd23d [file] [log] [blame]
alshabib77b88482015-04-07 15:47:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
alshabib77b88482015-04-07 15:47:50 -07003 *
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 Campanella238d96e2016-01-20 11:52:02 -080016package org.onosproject.net.driver;
alshabib77b88482015-04-07 15:47:50 -070017
18import org.apache.felix.scr.annotations.Activate;
Jonathan Hart17d00452015-04-21 17:10:00 -070019import org.apache.felix.scr.annotations.Component;
alshabib77b88482015-04-07 15:47:50 -070020import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska866b46a2015-04-30 00:26:55 -070021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
alshabib77b88482015-04-07 15:47:50 -070023import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
alshabib77b88482015-04-07 15:47:50 -070026/**
Thomas Vachuska800a7a42016-02-16 16:11:13 -080027 * Abstract boot-strapper for loading and registering driver definitions.
alshabib77b88482015-04-07 15:47:50 -070028 */
Andrea Campanella238d96e2016-01-20 11:52:02 -080029@Component
30public abstract class AbstractDriverLoader {
alshabib77b88482015-04-07 15:47:50 -070031
32 private final Logger log = LoggerFactory.getLogger(getClass());
33
alshabib77b88482015-04-07 15:47:50 -070034 private DriverProvider provider;
Thomas Vachuska800a7a42016-02-16 16:11:13 -080035 private final String path;
alshabib77b88482015-04-07 15:47:50 -070036
Thomas Vachuska866b46a2015-04-30 00:26:55 -070037 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
38 protected DriverAdminService driverAdminService;
39
Thomas Vachuska800a7a42016-02-16 16:11:13 -080040 /**
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
alshabib77b88482015-04-07 15:47:50 -070049 @Activate
50 protected void activate() {
alshabib77b88482015-04-07 15:47:50 -070051 try {
Thomas Vachuska800a7a42016-02-16 16:11:13 -080052 provider = new XmlDriverLoader(getClass().getClassLoader())
53 .loadDrivers(getClass().getResourceAsStream(path), driverAdminService);
Thomas Vachuska866b46a2015-04-30 00:26:55 -070054 driverAdminService.registerProvider(provider);
Thomas Vachuska635c2d72015-05-08 14:32:13 -070055 } catch (Exception e) {
Thomas Vachuska800a7a42016-02-16 16:11:13 -080056 log.error("Unable to load {} driver definitions", path, e);
alshabib77b88482015-04-07 15:47:50 -070057 }
alshabib77b88482015-04-07 15:47:50 -070058 log.info("Started");
59 }
60
61 @Deactivate
62 protected void deactivate() {
Thomas Vachuska866b46a2015-04-30 00:26:55 -070063 driverAdminService.unregisterProvider(provider);
alshabib77b88482015-04-07 15:47:50 -070064 log.info("Stopped");
65 }
66
alshabib77b88482015-04-07 15:47:50 -070067}