boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +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; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 17 | |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.openstacktelemetry.api.PrometheusTelemetryAdminService; |
| 21 | import org.onosproject.openstacktelemetry.api.PrometheusTelemetryConfigService; |
| 22 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 23 | import org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig; |
| 24 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Modified; |
| 29 | import org.osgi.service.component.annotations.Reference; |
| 30 | import org.osgi.service.component.annotations.ReferenceCardinality; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Dictionary; |
| 35 | |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 36 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT; |
| 37 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT; |
| 38 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 39 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_ENABLE_SERVICE; |
| 40 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_ADDRESS; |
| 41 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_PROMETHEUS_EXPORTER_PORT; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 42 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
| 43 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService; |
| 44 | |
| 45 | /** |
| 46 | * Prometheus exporter configuration manager for publishing openstack telemetry. |
| 47 | */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 48 | @Component( |
| 49 | immediate = true, |
| 50 | service = PrometheusTelemetryConfigService.class, |
| 51 | property = { |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 52 | PROP_PROMETHEUS_ENABLE_SERVICE + ":Boolean=" + PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT, |
| 53 | PROP_PROMETHEUS_EXPORTER_ADDRESS + "=" + PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT, |
| 54 | PROP_PROMETHEUS_EXPORTER_PORT + ":Integer=" + PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 55 | } |
| 56 | ) |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 57 | public class PrometheusTelemetryConfigManager implements PrometheusTelemetryConfigService { |
| 58 | |
| 59 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 60 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 62 | protected ComponentConfigService componentConfigService; |
| 63 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 65 | protected PrometheusTelemetryAdminService prometheusTelemetryAdminService; |
| 66 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 67 | /** Default IP address of prometheus exporter. */ |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 68 | protected String address = PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 69 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 70 | /** Default port number of prometheus exporter. */ |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 71 | protected Integer port = PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 72 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 73 | /** Specify the default behavior of telemetry service. */ |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 74 | protected Boolean enableService = PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 75 | |
| 76 | @Activate |
| 77 | protected void activate(ComponentContext context) { |
| 78 | componentConfigService.registerProperties(getClass()); |
| 79 | if (enableService) { |
| 80 | prometheusTelemetryAdminService.start(getConfig()); |
| 81 | } |
| 82 | log.info("Started"); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | protected void deactivate() { |
| 87 | componentConfigService.unregisterProperties(getClass(), false); |
| 88 | if (enableService) { |
| 89 | prometheusTelemetryAdminService.stop(); |
| 90 | } |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | @Modified |
| 95 | private void modified(ComponentContext context) { |
| 96 | readComponentConfiguration(context); |
| 97 | initTelemetryService(prometheusTelemetryAdminService, getConfig(), enableService); |
| 98 | log.info("Modified"); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public TelemetryConfig getConfig() { |
| 103 | return new DefaultPrometheusTelemetryConfig.DefaultBuilder() |
| 104 | .withAddress(address) |
| 105 | .withPort(port) |
| 106 | .build(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Extracts properties from the component configuration context. |
| 111 | * |
| 112 | * @param context the component context |
| 113 | */ |
| 114 | private void readComponentConfiguration(ComponentContext context) { |
| 115 | Dictionary<?, ?> properties = context.getProperties(); |
| 116 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 117 | String addressStr = Tools.get(properties, PROP_PROMETHEUS_EXPORTER_ADDRESS); |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 118 | address = addressStr != null ? addressStr : PROP_PROMETHEUS_EXPORTER_ADDRESS_DEFAULT; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 119 | log.info("Configured. Prometheus exporter address is {}", address); |
| 120 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 121 | Integer portConfigured = Tools.getIntegerProperty(properties, PROP_PROMETHEUS_EXPORTER_PORT); |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 122 | if (portConfigured == null) { |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 123 | port = PROP_PROMETHEUS_EXPORTER_PORT_DEFAULT; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 124 | log.info("Prometheus exporter port is NOT configured, default value is {}", port); |
| 125 | } else { |
| 126 | port = portConfigured; |
| 127 | log.info("Configured. Prometheus exporter port is {}", port); |
| 128 | } |
| 129 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 130 | Boolean enableServiceConfigured = getBooleanProperty(properties, PROP_PROMETHEUS_ENABLE_SERVICE); |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 131 | if (enableServiceConfigured == null) { |
boyoung2 | a8549d2 | 2018-11-23 20:42:37 +0900 | [diff] [blame] | 132 | enableService = PROP_PROMETHEUS_ENABLE_SERVICE_DEFAULT; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 133 | log.info("Prometheus service enable flag is NOT " + |
| 134 | "configured, default value is {}", enableService); |
| 135 | } else { |
| 136 | enableService = enableServiceConfigured; |
| 137 | log.info("Configured. Prometheus service enable flag is {}", enableService); |
| 138 | } |
| 139 | } |
| 140 | } |