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