Jian Li | b9fe349 | 2018-06-07 17:19:07 +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 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacktelemetry.api.RestTelemetryConfigService; |
| 22 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig; |
| 24 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -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; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Dictionary; |
| 35 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 36 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 37 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_ENDPOINT; |
| 38 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_METHOD; |
| 39 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_REQUEST_MEDIA_TYPE; |
| 40 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_RESPONSE_MEDIA_TYPE; |
| 41 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_IP; |
| 42 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_PORT; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 43 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 44 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * REST server configuration manager for publishing openstack telemetry. |
| 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 49 | @Component(immediate = true, service = RestTelemetryConfigService.class) |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 50 | public class RestTelemetryConfigManager implements RestTelemetryConfigService { |
| 51 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 54 | private static final String ENABLE_SERVICE = "enableService"; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 55 | private static final String ADDRESS = "address"; |
| 56 | private static final String PORT = "port"; |
| 57 | private static final String ENDPOINT = "endpoint"; |
| 58 | private static final String METHOD = "method"; |
| 59 | private static final String REQUEST_MEDIA_TYPE = "requestMediaType"; |
| 60 | private static final String RESPONSE_MEDIA_TYPE = "responseMediaType"; |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 63 | protected ComponentConfigService componentConfigService; |
| 64 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 66 | protected RestTelemetryAdminService restTelemetryAdminService; |
| 67 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 68 | //@Property(name = ADDRESS, value = DEFAULT_REST_SERVER_IP, |
| 69 | // label = "Default IP address to establish initial connection to REST server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 70 | protected String address = DEFAULT_REST_SERVER_IP; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 72 | //@Property(name = PORT, intValue = DEFAULT_REST_SERVER_PORT, |
| 73 | // label = "Default port number to establish initial connection to REST server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 74 | protected Integer port = DEFAULT_REST_SERVER_PORT; |
| 75 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 76 | //@Property(name = ENDPOINT, value = DEFAULT_REST_ENDPOINT, |
| 77 | // label = "Endpoint of REST server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 78 | protected String endpoint = DEFAULT_REST_ENDPOINT; |
| 79 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 80 | //@Property(name = METHOD, value = DEFAULT_REST_METHOD, |
| 81 | // label = "HTTP method of REST server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 82 | protected String method = DEFAULT_REST_METHOD; |
| 83 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 84 | //@Property(name = REQUEST_MEDIA_TYPE, value = DEFAULT_REST_REQUEST_MEDIA_TYPE, |
| 85 | // label = "Request media type of REST server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 86 | protected String requestMediaType = DEFAULT_REST_REQUEST_MEDIA_TYPE; |
| 87 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 88 | //@Property(name = RESPONSE_MEDIA_TYPE, value = DEFAULT_REST_RESPONSE_MEDIA_TYPE, |
| 89 | // label = "Response media type of REST server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 90 | protected String responseMediaType = DEFAULT_REST_RESPONSE_MEDIA_TYPE; |
| 91 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 92 | //@Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE, |
| 93 | // label = "Specify the default behavior of telemetry service") |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 94 | protected Boolean enableService = DEFAULT_DISABLE; |
| 95 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 96 | @Activate |
| 97 | protected void activate(ComponentContext context) { |
| 98 | componentConfigService.registerProperties(getClass()); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 99 | |
| 100 | if (enableService) { |
| 101 | restTelemetryAdminService.start(getConfig()); |
| 102 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 103 | log.info("Started"); |
| 104 | } |
| 105 | |
| 106 | @Deactivate |
| 107 | protected void deactivate() { |
| 108 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 109 | |
| 110 | if (enableService) { |
| 111 | restTelemetryAdminService.stop(); |
| 112 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Modified |
| 117 | private void modified(ComponentContext context) { |
| 118 | readComponentConfiguration(context); |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 119 | initTelemetryService(restTelemetryAdminService, getConfig(), enableService); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 120 | log.info("Modified"); |
| 121 | } |
| 122 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 123 | @Override |
| 124 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 125 | return new DefaultRestTelemetryConfig.DefaultBuilder() |
| 126 | .withAddress(address) |
| 127 | .withPort(port) |
| 128 | .withEndpoint(endpoint) |
| 129 | .withMethod(method) |
| 130 | .withRequestMediaType(requestMediaType) |
| 131 | .withResponseMediaType(responseMediaType) |
| 132 | .build(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Extracts properties from the component configuration context. |
| 137 | * |
| 138 | * @param context the component context |
| 139 | */ |
| 140 | private void readComponentConfiguration(ComponentContext context) { |
| 141 | Dictionary<?, ?> properties = context.getProperties(); |
| 142 | |
| 143 | String addressStr = Tools.get(properties, ADDRESS); |
| 144 | address = addressStr != null ? addressStr : DEFAULT_REST_SERVER_IP; |
| 145 | log.info("Configured. REST server address is {}", address); |
| 146 | |
| 147 | Integer portConfigured = Tools.getIntegerProperty(properties, PORT); |
| 148 | if (portConfigured == null) { |
| 149 | port = DEFAULT_REST_SERVER_PORT; |
| 150 | log.info("REST server port is NOT configured, default value is {}", port); |
| 151 | } else { |
| 152 | port = portConfigured; |
| 153 | log.info("Configured. REST server port is {}", port); |
| 154 | } |
| 155 | |
| 156 | String endpointStr = Tools.get(properties, ENDPOINT); |
| 157 | endpoint = endpointStr != null ? endpointStr : DEFAULT_REST_ENDPOINT; |
| 158 | log.info("Configured. REST server endpoint is {}", endpoint); |
| 159 | |
| 160 | String methodStr = Tools.get(properties, METHOD); |
| 161 | method = methodStr != null ? methodStr : DEFAULT_REST_METHOD; |
| 162 | log.info("Configured. REST server default HTTP method is {}", method); |
| 163 | |
| 164 | String requestMediaTypeStr = Tools.get(properties, REQUEST_MEDIA_TYPE); |
| 165 | requestMediaType = requestMediaTypeStr != null ? |
| 166 | requestMediaTypeStr : DEFAULT_REST_REQUEST_MEDIA_TYPE; |
| 167 | log.info("Configured. REST server request media type is {}", requestMediaType); |
| 168 | |
| 169 | String responseMediaTypeStr = Tools.get(properties, RESPONSE_MEDIA_TYPE); |
| 170 | responseMediaType = responseMediaTypeStr != null ? |
| 171 | responseMediaTypeStr : DEFAULT_REST_RESPONSE_MEDIA_TYPE; |
| 172 | log.info("Configured. REST server response media type is {}", responseMediaType); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 173 | |
| 174 | Boolean enableServiceConfigured = |
| 175 | getBooleanProperty(properties, ENABLE_SERVICE); |
| 176 | if (enableServiceConfigured == null) { |
| 177 | enableService = DEFAULT_DISABLE; |
| 178 | log.info("REST service enable flag is NOT " + |
| 179 | "configured, default value is {}", enableService); |
| 180 | } else { |
| 181 | enableService = enableServiceConfigured; |
| 182 | log.info("Configured. REST service enable flag is {}", enableService); |
| 183 | } |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 184 | } |
| 185 | } |