blob: e3b76b610fbc64bc95c13f834339f311e5c6432f [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
Jian Lid1ce10a2018-06-12 13:47:23 +090036import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
Jian Li4df75b12018-06-07 22:11:04 +090037import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_ENDPOINT;
38import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_METHOD;
39import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_REQUEST_MEDIA_TYPE;
40import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_RESPONSE_MEDIA_TYPE;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_IP;
42import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_PORT;
Jian Lid1ce10a2018-06-12 13:47:23 +090043import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090044import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090045
46/**
47 * REST server configuration manager for publishing openstack telemetry.
48 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(immediate = true, service = RestTelemetryConfigService.class)
Jian Lib9fe3492018-06-07 17:19:07 +090050public class RestTelemetryConfigManager implements RestTelemetryConfigService {
51
Jian Li4df75b12018-06-07 22:11:04 +090052 private final Logger log = LoggerFactory.getLogger(getClass());
53
Jian Lid1ce10a2018-06-12 13:47:23 +090054 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090055 private static final String ADDRESS = "address";
56 private static final String PORT = "port";
57 private static final String ENDPOINT = "endpoint";
58 private static final String METHOD = "method";
59 private static final String REQUEST_MEDIA_TYPE = "requestMediaType";
60 private static final String RESPONSE_MEDIA_TYPE = "responseMediaType";
61
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090063 protected ComponentConfigService componentConfigService;
64
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090066 protected RestTelemetryAdminService restTelemetryAdminService;
67
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 //@Property(name = ADDRESS, value = DEFAULT_REST_SERVER_IP,
69 // label = "Default IP address to establish initial connection to REST server")
Jian Li4df75b12018-06-07 22:11:04 +090070 protected String address = DEFAULT_REST_SERVER_IP;
71
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 //@Property(name = PORT, intValue = DEFAULT_REST_SERVER_PORT,
73 // label = "Default port number to establish initial connection to REST server")
Jian Li4df75b12018-06-07 22:11:04 +090074 protected Integer port = DEFAULT_REST_SERVER_PORT;
75
Ray Milkeyd84f89b2018-08-17 14:54:17 -070076 //@Property(name = ENDPOINT, value = DEFAULT_REST_ENDPOINT,
77 // label = "Endpoint of REST server")
Jian Li4df75b12018-06-07 22:11:04 +090078 protected String endpoint = DEFAULT_REST_ENDPOINT;
79
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 //@Property(name = METHOD, value = DEFAULT_REST_METHOD,
81 // label = "HTTP method of REST server")
Jian Li4df75b12018-06-07 22:11:04 +090082 protected String method = DEFAULT_REST_METHOD;
83
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084 //@Property(name = REQUEST_MEDIA_TYPE, value = DEFAULT_REST_REQUEST_MEDIA_TYPE,
85 // label = "Request media type of REST server")
Jian Li4df75b12018-06-07 22:11:04 +090086 protected String requestMediaType = DEFAULT_REST_REQUEST_MEDIA_TYPE;
87
Ray Milkeyd84f89b2018-08-17 14:54:17 -070088 //@Property(name = RESPONSE_MEDIA_TYPE, value = DEFAULT_REST_RESPONSE_MEDIA_TYPE,
89 // label = "Response media type of REST server")
Jian Li4df75b12018-06-07 22:11:04 +090090 protected String responseMediaType = DEFAULT_REST_RESPONSE_MEDIA_TYPE;
91
Ray Milkeyd84f89b2018-08-17 14:54:17 -070092 //@Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
93 // label = "Specify the default behavior of telemetry service")
Jian Lid1ce10a2018-06-12 13:47:23 +090094 protected Boolean enableService = DEFAULT_DISABLE;
95
Jian Li4df75b12018-06-07 22:11:04 +090096 @Activate
97 protected void activate(ComponentContext context) {
98 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +090099
100 if (enableService) {
101 restTelemetryAdminService.start(getConfig());
102 }
Jian Li4df75b12018-06-07 22:11:04 +0900103 log.info("Started");
104 }
105
106 @Deactivate
107 protected void deactivate() {
108 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900109
110 if (enableService) {
111 restTelemetryAdminService.stop();
112 }
Jian Li4df75b12018-06-07 22:11:04 +0900113 log.info("Stopped");
114 }
115
116 @Modified
117 private void modified(ComponentContext context) {
118 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900119 initTelemetryService(restTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900120 log.info("Modified");
121 }
122
Jian Lib9fe3492018-06-07 17:19:07 +0900123 @Override
124 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900125 return new DefaultRestTelemetryConfig.DefaultBuilder()
126 .withAddress(address)
127 .withPort(port)
128 .withEndpoint(endpoint)
129 .withMethod(method)
130 .withRequestMediaType(requestMediaType)
131 .withResponseMediaType(responseMediaType)
132 .build();
133 }
134
135 /**
136 * Extracts properties from the component configuration context.
137 *
138 * @param context the component context
139 */
140 private void readComponentConfiguration(ComponentContext context) {
141 Dictionary<?, ?> properties = context.getProperties();
142
143 String addressStr = Tools.get(properties, ADDRESS);
144 address = addressStr != null ? addressStr : DEFAULT_REST_SERVER_IP;
145 log.info("Configured. REST server address is {}", address);
146
147 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
148 if (portConfigured == null) {
149 port = DEFAULT_REST_SERVER_PORT;
150 log.info("REST server port is NOT configured, default value is {}", port);
151 } else {
152 port = portConfigured;
153 log.info("Configured. REST server port is {}", port);
154 }
155
156 String endpointStr = Tools.get(properties, ENDPOINT);
157 endpoint = endpointStr != null ? endpointStr : DEFAULT_REST_ENDPOINT;
158 log.info("Configured. REST server endpoint is {}", endpoint);
159
160 String methodStr = Tools.get(properties, METHOD);
161 method = methodStr != null ? methodStr : DEFAULT_REST_METHOD;
162 log.info("Configured. REST server default HTTP method is {}", method);
163
164 String requestMediaTypeStr = Tools.get(properties, REQUEST_MEDIA_TYPE);
165 requestMediaType = requestMediaTypeStr != null ?
166 requestMediaTypeStr : DEFAULT_REST_REQUEST_MEDIA_TYPE;
167 log.info("Configured. REST server request media type is {}", requestMediaType);
168
169 String responseMediaTypeStr = Tools.get(properties, RESPONSE_MEDIA_TYPE);
170 responseMediaType = responseMediaTypeStr != null ?
171 responseMediaTypeStr : DEFAULT_REST_RESPONSE_MEDIA_TYPE;
172 log.info("Configured. REST server response media type is {}", responseMediaType);
Jian Lid1ce10a2018-06-12 13:47:23 +0900173
174 Boolean enableServiceConfigured =
175 getBooleanProperty(properties, ENABLE_SERVICE);
176 if (enableServiceConfigured == null) {
177 enableService = DEFAULT_DISABLE;
178 log.info("REST service enable flag is NOT " +
179 "configured, default value is {}", enableService);
180 } else {
181 enableService = enableServiceConfigured;
182 log.info("Configured. REST service enable flag is {}", enableService);
183 }
Jian Lib9fe3492018-06-07 17:19:07 +0900184 }
185}