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