Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstacktelemetry.impl; |
| 17 | |
| 18 | import org.onosproject.openstacktelemetry.api.TelemetryConfigAdminService; |
| 19 | import org.onosproject.openstacktelemetry.api.TelemetryConfigProvider; |
| 20 | import org.osgi.service.component.annotations.Activate; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | /** |
| 28 | * Abstract bootstrapper for loading and registering telemetry configurations |
| 29 | * that are independent from the default telemetry configurations. |
| 30 | */ |
| 31 | public abstract class AbstractTelemetryConfigLoader { |
| 32 | |
| 33 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 34 | |
| 35 | private TelemetryConfigProvider provider; |
| 36 | private final String path; |
| 37 | |
| 38 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 39 | protected TelemetryConfigAdminService configAdminService; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new loader for resource with the specified path. |
| 43 | * |
| 44 | * @param path configurations definition XML resource path |
| 45 | */ |
| 46 | protected AbstractTelemetryConfigLoader(String path) { |
| 47 | this.path = path; |
| 48 | } |
| 49 | |
| 50 | @Activate |
| 51 | protected void activate() { |
| 52 | try { |
| 53 | provider = new XmlTelemetryConfigLoader().loadTelemetryConfigs( |
| 54 | getClass().getResourceAsStream(path)); |
| 55 | configAdminService.registerProvider(provider); |
| 56 | } catch (Exception e) { |
| 57 | log.error("Unable to load {} telemetry configuration definitions", path, e); |
| 58 | } |
| 59 | log.info("Started"); |
| 60 | } |
| 61 | |
| 62 | @Deactivate |
| 63 | protected void deactivate() { |
| 64 | configAdminService.unregisterProvider(provider); |
| 65 | log.info("Stopped"); |
| 66 | } |
| 67 | } |