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