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; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.util.Tools; |
| 27 | import org.onosproject.cfg.ComponentConfigService; |
| 28 | import org.onosproject.openstacktelemetry.api.PrometheusTelemetryAdminService; |
| 29 | import org.onosproject.openstacktelemetry.api.PrometheusTelemetryConfigService; |
| 30 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 31 | import org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig; |
| 32 | import org.osgi.service.component.ComponentContext; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Dictionary; |
| 37 | |
| 38 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE; |
| 39 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_ENABLE; |
| 40 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_PROMETHEUS_EXPORTER_IP; |
| 41 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_PROMETHEUS_EXPORTER_PORT; |
| 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 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
| 50 | public class PrometheusTelemetryConfigManager implements PrometheusTelemetryConfigService { |
| 51 | |
| 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
| 54 | private static final String ENABLE_SERVICE = "enableService"; |
| 55 | private static final String ADDRESS = "address"; |
| 56 | private static final String PORT = "port"; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected ComponentConfigService componentConfigService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected PrometheusTelemetryAdminService prometheusTelemetryAdminService; |
| 63 | |
| 64 | @Property(name = ADDRESS, value = DEFAULT_PROMETHEUS_EXPORTER_IP, |
| 65 | label = "Default IP address of prometheus exporter") |
| 66 | protected String address = DEFAULT_PROMETHEUS_EXPORTER_IP; |
| 67 | |
| 68 | @Property(name = PORT, intValue = DEFAULT_PROMETHEUS_EXPORTER_PORT, |
| 69 | label = "Default port number of prometheus exporter") |
| 70 | protected Integer port = DEFAULT_PROMETHEUS_EXPORTER_PORT; |
| 71 | |
| 72 | @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_ENABLE, |
| 73 | label = "Specify the default behavior of telemetry service") |
| 74 | protected Boolean enableService = DEFAULT_ENABLE; |
| 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 | |
| 117 | String addressStr = Tools.get(properties, ADDRESS); |
| 118 | address = addressStr != null ? addressStr : DEFAULT_PROMETHEUS_EXPORTER_IP; |
| 119 | log.info("Configured. Prometheus exporter address is {}", address); |
| 120 | |
| 121 | Integer portConfigured = Tools.getIntegerProperty(properties, PORT); |
| 122 | if (portConfigured == null) { |
| 123 | port = DEFAULT_PROMETHEUS_EXPORTER_PORT; |
| 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 | |
| 130 | Boolean enableServiceConfigured = getBooleanProperty(properties, ENABLE_SERVICE); |
| 131 | if (enableServiceConfigured == null) { |
| 132 | enableService = DEFAULT_DISABLE; |
| 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 | } |