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