blob: 5816ef7cb4cd45835aaeacb647a908255fab7b3c [file] [log] [blame]
Thomas Vachuska43de6ee2017-03-15 15:45:23 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Thomas Vachuska43de6ee2017-03-15 15:45:23 -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 */
16package org.onosproject.net.driver;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.osgi.service.component.annotations.Activate;
19import org.osgi.service.component.annotations.Deactivate;
20import org.osgi.service.component.annotations.Reference;
21import org.osgi.service.component.annotations.ReferenceCardinality;
Thomas Vachuska43de6ee2017-03-15 15:45:23 -070022import org.slf4j.Logger;
23import 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 Vachuska43de6ee2017-03-15 15:45:23 -070029public abstract class AbstractIndependentDriverLoader {
30
31 private final Logger log = LoggerFactory.getLogger(getClass());
32
33 private DriverProvider provider;
34 private final String path;
35
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Thomas Vachuska43de6ee2017-03-15 15:45:23 -070037 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}