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