blob: 16b62cbae1cff0a5af530e7c71c4f6040a9bfeeb [file] [log] [blame]
Jian Lib9fe3492018-06-07 17:19:07 +09001/*
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 */
16package org.onosproject.openstacktelemetry.impl;
17
Jian Li4df75b12018-06-07 22:11:04 +090018import org.onlab.util.Tools;
19import org.onosproject.cfg.ComponentConfigService;
20import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090021import org.onosproject.openstacktelemetry.api.RestTelemetryConfigService;
22import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090023import org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig;
24import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Deactivate;
28import org.osgi.service.component.annotations.Modified;
29import org.osgi.service.component.annotations.Reference;
30import org.osgi.service.component.annotations.ReferenceCardinality;
Jian Li4df75b12018-06-07 22:11:04 +090031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.Dictionary;
35
Ray Milkey8e406512018-10-24 15:56:50 -070036import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_SERVER_ADDRESS;
37import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_ENABLE_SERVICE;
38import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_ENABLE_SERVICE_DEFAULT;
39import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_ENDPOINT;
40import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_METHOD;
41import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_SERVER_PORT;
42import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_REQUEST_MEDIA_TYPE;
43import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_RESPONSE_MEDIA_TYPE;
44import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_ENDPOINT_DEFAULT;
45import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_METHOD_DEFAULT;
46import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_REQUEST_MEDIA_TYPE_DEFAULT;
47import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_RESPONSE_MEDIA_TYPE_DEFAULT;
48import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_SERVER_ADDRESS_DEFAULT;
49import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_REST_SERVER_PORT_DEFAULT;
Jian Lid1ce10a2018-06-12 13:47:23 +090050import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090051import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090052
53/**
54 * REST server configuration manager for publishing openstack telemetry.
55 */
Ray Milkey8e406512018-10-24 15:56:50 -070056@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 Lib9fe3492018-06-07 17:19:07 +090069public class RestTelemetryConfigManager implements RestTelemetryConfigService {
70
Jian Li4df75b12018-06-07 22:11:04 +090071 private final Logger log = LoggerFactory.getLogger(getClass());
72
Ray Milkeyd84f89b2018-08-17 14:54:17 -070073 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090074 protected ComponentConfigService componentConfigService;
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090077 protected RestTelemetryAdminService restTelemetryAdminService;
78
Ray Milkey8e406512018-10-24 15:56:50 -070079 /** Default IP address to establish initial connection to REST server. */
80 protected String address = PROP_REST_SERVER_ADDRESS_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090081
Ray Milkey8e406512018-10-24 15:56:50 -070082 /** Default port number to establish initial connection to REST server. */
83 protected Integer port = PROP_REST_SERVER_PORT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090084
Ray Milkey8e406512018-10-24 15:56:50 -070085 /** Endpoint of REST server. */
86 protected String endpoint = PROP_REST_ENDPOINT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090087
Ray Milkey8e406512018-10-24 15:56:50 -070088 /** HTTP method of REST server. */
89 protected String method = PROP_REST_METHOD_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090090
Ray Milkey8e406512018-10-24 15:56:50 -070091 /** Request media type of REST server. */
92 protected String requestMediaType = PROP_REST_REQUEST_MEDIA_TYPE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090093
Ray Milkey8e406512018-10-24 15:56:50 -070094 /** Response media type of REST server. */
95 protected String responseMediaType = PROP_REST_RESPONSE_MEDIA_TYPE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090096
Ray Milkey8e406512018-10-24 15:56:50 -070097 /** Specify the default behavior of telemetry service. */
98 protected Boolean enableService = PROP_REST_ENABLE_SERVICE_DEFAULT;
Jian Lid1ce10a2018-06-12 13:47:23 +090099
Jian Li4df75b12018-06-07 22:11:04 +0900100 @Activate
101 protected void activate(ComponentContext context) {
102 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900103
104 if (enableService) {
105 restTelemetryAdminService.start(getConfig());
106 }
Jian Li4df75b12018-06-07 22:11:04 +0900107 log.info("Started");
108 }
109
110 @Deactivate
111 protected void deactivate() {
112 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900113
114 if (enableService) {
115 restTelemetryAdminService.stop();
116 }
Jian Li4df75b12018-06-07 22:11:04 +0900117 log.info("Stopped");
118 }
119
120 @Modified
121 private void modified(ComponentContext context) {
122 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900123 initTelemetryService(restTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900124 log.info("Modified");
125 }
126
Jian Lib9fe3492018-06-07 17:19:07 +0900127 @Override
128 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900129 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 Milkey8e406512018-10-24 15:56:50 -0700147 String addressStr = Tools.get(properties, PROP_REST_SERVER_ADDRESS);
148 address = addressStr != null ? addressStr : PROP_REST_SERVER_ADDRESS_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900149 log.info("Configured. REST server address is {}", address);
150
Ray Milkey8e406512018-10-24 15:56:50 -0700151 Integer portConfigured = Tools.getIntegerProperty(properties, PROP_REST_SERVER_PORT);
Jian Li4df75b12018-06-07 22:11:04 +0900152 if (portConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700153 port = PROP_REST_SERVER_PORT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900154 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 Milkey8e406512018-10-24 15:56:50 -0700160 String endpointStr = Tools.get(properties, PROP_REST_ENDPOINT);
161 endpoint = endpointStr != null ? endpointStr : PROP_REST_ENDPOINT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900162 log.info("Configured. REST server endpoint is {}", endpoint);
163
Ray Milkey8e406512018-10-24 15:56:50 -0700164 String methodStr = Tools.get(properties, PROP_REST_METHOD);
165 method = methodStr != null ? methodStr : PROP_REST_METHOD_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900166 log.info("Configured. REST server default HTTP method is {}", method);
167
Ray Milkey8e406512018-10-24 15:56:50 -0700168 String requestMediaTypeStr = Tools.get(properties, PROP_REST_REQUEST_MEDIA_TYPE);
Jian Li4df75b12018-06-07 22:11:04 +0900169 requestMediaType = requestMediaTypeStr != null ?
Ray Milkey8e406512018-10-24 15:56:50 -0700170 requestMediaTypeStr : PROP_REST_REQUEST_MEDIA_TYPE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900171 log.info("Configured. REST server request media type is {}", requestMediaType);
172
Ray Milkey8e406512018-10-24 15:56:50 -0700173 String responseMediaTypeStr = Tools.get(properties, PROP_REST_RESPONSE_MEDIA_TYPE);
Jian Li4df75b12018-06-07 22:11:04 +0900174 responseMediaType = responseMediaTypeStr != null ?
Ray Milkey8e406512018-10-24 15:56:50 -0700175 responseMediaTypeStr : PROP_REST_RESPONSE_MEDIA_TYPE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900176 log.info("Configured. REST server response media type is {}", responseMediaType);
Jian Lid1ce10a2018-06-12 13:47:23 +0900177
178 Boolean enableServiceConfigured =
Ray Milkey8e406512018-10-24 15:56:50 -0700179 getBooleanProperty(properties, PROP_REST_ENABLE_SERVICE);
Jian Lid1ce10a2018-06-12 13:47:23 +0900180 if (enableServiceConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700181 enableService = PROP_REST_ENABLE_SERVICE_DEFAULT;
Jian Lid1ce10a2018-06-12 13:47:23 +0900182 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 Lib9fe3492018-06-07 17:19:07 +0900188 }
189}