blob: 455de3484a944c7b6bfcf5bdfefd29fe158f69ee [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.apache.felix.scr.annotations.Activate;
Jian Lid1ce10a2018-06-12 13:47:23 +090019import org.apache.felix.scr.annotations.Component;
Jian Li4df75b12018-06-07 22:11:04 +090020import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Lid1ce10a2018-06-12 13:47:23 +090025import org.apache.felix.scr.annotations.Service;
Jian Li4df75b12018-06-07 22:11:04 +090026import org.onlab.util.Tools;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090029import org.onosproject.openstacktelemetry.api.RestTelemetryConfigService;
30import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090031import org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig;
32import org.osgi.service.component.ComponentContext;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Dictionary;
37
Jian Lid1ce10a2018-06-12 13:47:23 +090038import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
Jian Li4df75b12018-06-07 22:11:04 +090039import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_ENDPOINT;
40import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_METHOD;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_REQUEST_MEDIA_TYPE;
42import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_RESPONSE_MEDIA_TYPE;
43import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_IP;
44import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_PORT;
Jian Lid1ce10a2018-06-12 13:47:23 +090045import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Lib9fe3492018-06-07 17:19:07 +090046
47/**
48 * REST server configuration manager for publishing openstack telemetry.
49 */
Jian Lid1ce10a2018-06-12 13:47:23 +090050@Component(immediate = true)
51@Service
Jian Lib9fe3492018-06-07 17:19:07 +090052public class RestTelemetryConfigManager implements RestTelemetryConfigService {
53
Jian Li4df75b12018-06-07 22:11:04 +090054 private final Logger log = LoggerFactory.getLogger(getClass());
55
Jian Lid1ce10a2018-06-12 13:47:23 +090056 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090057 private static final String ADDRESS = "address";
58 private static final String PORT = "port";
59 private static final String ENDPOINT = "endpoint";
60 private static final String METHOD = "method";
61 private static final String REQUEST_MEDIA_TYPE = "requestMediaType";
62 private static final String RESPONSE_MEDIA_TYPE = "responseMediaType";
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected ComponentConfigService componentConfigService;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected RestTelemetryAdminService restTelemetryAdminService;
69
70 @Property(name = ADDRESS, value = DEFAULT_REST_SERVER_IP,
71 label = "Default IP address to establish initial connection to REST server")
72 protected String address = DEFAULT_REST_SERVER_IP;
73
74 @Property(name = PORT, intValue = DEFAULT_REST_SERVER_PORT,
75 label = "Default port number to establish initial connection to REST server")
76 protected Integer port = DEFAULT_REST_SERVER_PORT;
77
78 @Property(name = ENDPOINT, value = DEFAULT_REST_ENDPOINT,
79 label = "Endpoint of REST server")
80 protected String endpoint = DEFAULT_REST_ENDPOINT;
81
82 @Property(name = METHOD, value = DEFAULT_REST_METHOD,
83 label = "HTTP method of REST server")
84 protected String method = DEFAULT_REST_METHOD;
85
86 @Property(name = REQUEST_MEDIA_TYPE, value = DEFAULT_REST_REQUEST_MEDIA_TYPE,
87 label = "Request media type of REST server")
88 protected String requestMediaType = DEFAULT_REST_REQUEST_MEDIA_TYPE;
89
90 @Property(name = RESPONSE_MEDIA_TYPE, value = DEFAULT_REST_RESPONSE_MEDIA_TYPE,
91 label = "Response media type of REST server")
92 protected String responseMediaType = DEFAULT_REST_RESPONSE_MEDIA_TYPE;
93
Jian Lid1ce10a2018-06-12 13:47:23 +090094 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
95 label = "Specify the default behavior of telemetry service")
96 protected Boolean enableService = DEFAULT_DISABLE;
97
Jian Li4df75b12018-06-07 22:11:04 +090098 @Activate
99 protected void activate(ComponentContext context) {
100 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900101
102 if (enableService) {
103 restTelemetryAdminService.start(getConfig());
104 }
Jian Li4df75b12018-06-07 22:11:04 +0900105 log.info("Started");
106 }
107
108 @Deactivate
109 protected void deactivate() {
110 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900111
112 if (enableService) {
113 restTelemetryAdminService.stop();
114 }
Jian Li4df75b12018-06-07 22:11:04 +0900115 log.info("Stopped");
116 }
117
118 @Modified
119 private void modified(ComponentContext context) {
120 readComponentConfiguration(context);
Jian Lid1ce10a2018-06-12 13:47:23 +0900121
122 if (enableService) {
123 if (restTelemetryAdminService.isRunning()) {
124 restTelemetryAdminService.restart(getConfig());
125 } else {
126 restTelemetryAdminService.start(getConfig());
127 }
128 } else {
129 if (restTelemetryAdminService.isRunning()) {
130 restTelemetryAdminService.stop();
131 }
132 }
Jian Li4df75b12018-06-07 22:11:04 +0900133 log.info("Modified");
134 }
135
Jian Lib9fe3492018-06-07 17:19:07 +0900136 @Override
137 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900138 return new DefaultRestTelemetryConfig.DefaultBuilder()
139 .withAddress(address)
140 .withPort(port)
141 .withEndpoint(endpoint)
142 .withMethod(method)
143 .withRequestMediaType(requestMediaType)
144 .withResponseMediaType(responseMediaType)
145 .build();
146 }
147
148 /**
149 * Extracts properties from the component configuration context.
150 *
151 * @param context the component context
152 */
153 private void readComponentConfiguration(ComponentContext context) {
154 Dictionary<?, ?> properties = context.getProperties();
155
156 String addressStr = Tools.get(properties, ADDRESS);
157 address = addressStr != null ? addressStr : DEFAULT_REST_SERVER_IP;
158 log.info("Configured. REST server address is {}", address);
159
160 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
161 if (portConfigured == null) {
162 port = DEFAULT_REST_SERVER_PORT;
163 log.info("REST server port is NOT configured, default value is {}", port);
164 } else {
165 port = portConfigured;
166 log.info("Configured. REST server port is {}", port);
167 }
168
169 String endpointStr = Tools.get(properties, ENDPOINT);
170 endpoint = endpointStr != null ? endpointStr : DEFAULT_REST_ENDPOINT;
171 log.info("Configured. REST server endpoint is {}", endpoint);
172
173 String methodStr = Tools.get(properties, METHOD);
174 method = methodStr != null ? methodStr : DEFAULT_REST_METHOD;
175 log.info("Configured. REST server default HTTP method is {}", method);
176
177 String requestMediaTypeStr = Tools.get(properties, REQUEST_MEDIA_TYPE);
178 requestMediaType = requestMediaTypeStr != null ?
179 requestMediaTypeStr : DEFAULT_REST_REQUEST_MEDIA_TYPE;
180 log.info("Configured. REST server request media type is {}", requestMediaType);
181
182 String responseMediaTypeStr = Tools.get(properties, RESPONSE_MEDIA_TYPE);
183 responseMediaType = responseMediaTypeStr != null ?
184 responseMediaTypeStr : DEFAULT_REST_RESPONSE_MEDIA_TYPE;
185 log.info("Configured. REST server response media type is {}", responseMediaType);
Jian Lid1ce10a2018-06-12 13:47:23 +0900186
187 Boolean enableServiceConfigured =
188 getBooleanProperty(properties, ENABLE_SERVICE);
189 if (enableServiceConfigured == null) {
190 enableService = DEFAULT_DISABLE;
191 log.info("REST service enable flag is NOT " +
192 "configured, default value is {}", enableService);
193 } else {
194 enableService = enableServiceConfigured;
195 log.info("Configured. REST service enable flag is {}", enableService);
196 }
Jian Lib9fe3492018-06-07 17:19:07 +0900197 }
198}