blob: 25c7afeb25844cdcd10f27b6226a0c964127ebdc [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 Li3ed7f302018-08-27 17:16:27 +090046import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090047
48/**
49 * REST server configuration manager for publishing openstack telemetry.
50 */
Jian Lid1ce10a2018-06-12 13:47:23 +090051@Component(immediate = true)
52@Service
Jian Lib9fe3492018-06-07 17:19:07 +090053public class RestTelemetryConfigManager implements RestTelemetryConfigService {
54
Jian Li4df75b12018-06-07 22:11:04 +090055 private final Logger log = LoggerFactory.getLogger(getClass());
56
Jian Lid1ce10a2018-06-12 13:47:23 +090057 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090058 private static final String ADDRESS = "address";
59 private static final String PORT = "port";
60 private static final String ENDPOINT = "endpoint";
61 private static final String METHOD = "method";
62 private static final String REQUEST_MEDIA_TYPE = "requestMediaType";
63 private static final String RESPONSE_MEDIA_TYPE = "responseMediaType";
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected ComponentConfigService componentConfigService;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected RestTelemetryAdminService restTelemetryAdminService;
70
71 @Property(name = ADDRESS, value = DEFAULT_REST_SERVER_IP,
72 label = "Default IP address to establish initial connection to REST server")
73 protected String address = DEFAULT_REST_SERVER_IP;
74
75 @Property(name = PORT, intValue = DEFAULT_REST_SERVER_PORT,
76 label = "Default port number to establish initial connection to REST server")
77 protected Integer port = DEFAULT_REST_SERVER_PORT;
78
79 @Property(name = ENDPOINT, value = DEFAULT_REST_ENDPOINT,
80 label = "Endpoint of REST server")
81 protected String endpoint = DEFAULT_REST_ENDPOINT;
82
83 @Property(name = METHOD, value = DEFAULT_REST_METHOD,
84 label = "HTTP method of REST server")
85 protected String method = DEFAULT_REST_METHOD;
86
87 @Property(name = REQUEST_MEDIA_TYPE, value = DEFAULT_REST_REQUEST_MEDIA_TYPE,
88 label = "Request media type of REST server")
89 protected String requestMediaType = DEFAULT_REST_REQUEST_MEDIA_TYPE;
90
91 @Property(name = RESPONSE_MEDIA_TYPE, value = DEFAULT_REST_RESPONSE_MEDIA_TYPE,
92 label = "Response media type of REST server")
93 protected String responseMediaType = DEFAULT_REST_RESPONSE_MEDIA_TYPE;
94
Jian Lid1ce10a2018-06-12 13:47:23 +090095 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
96 label = "Specify the default behavior of telemetry service")
97 protected Boolean enableService = DEFAULT_DISABLE;
98
Jian Li4df75b12018-06-07 22:11:04 +090099 @Activate
100 protected void activate(ComponentContext context) {
101 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +0900102
103 if (enableService) {
104 restTelemetryAdminService.start(getConfig());
105 }
Jian Li4df75b12018-06-07 22:11:04 +0900106 log.info("Started");
107 }
108
109 @Deactivate
110 protected void deactivate() {
111 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900112
113 if (enableService) {
114 restTelemetryAdminService.stop();
115 }
Jian Li4df75b12018-06-07 22:11:04 +0900116 log.info("Stopped");
117 }
118
119 @Modified
120 private void modified(ComponentContext context) {
121 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900122 initTelemetryService(restTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900123 log.info("Modified");
124 }
125
Jian Lib9fe3492018-06-07 17:19:07 +0900126 @Override
127 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900128 return new DefaultRestTelemetryConfig.DefaultBuilder()
129 .withAddress(address)
130 .withPort(port)
131 .withEndpoint(endpoint)
132 .withMethod(method)
133 .withRequestMediaType(requestMediaType)
134 .withResponseMediaType(responseMediaType)
135 .build();
136 }
137
138 /**
139 * Extracts properties from the component configuration context.
140 *
141 * @param context the component context
142 */
143 private void readComponentConfiguration(ComponentContext context) {
144 Dictionary<?, ?> properties = context.getProperties();
145
146 String addressStr = Tools.get(properties, ADDRESS);
147 address = addressStr != null ? addressStr : DEFAULT_REST_SERVER_IP;
148 log.info("Configured. REST server address is {}", address);
149
150 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
151 if (portConfigured == null) {
152 port = DEFAULT_REST_SERVER_PORT;
153 log.info("REST server port is NOT configured, default value is {}", port);
154 } else {
155 port = portConfigured;
156 log.info("Configured. REST server port is {}", port);
157 }
158
159 String endpointStr = Tools.get(properties, ENDPOINT);
160 endpoint = endpointStr != null ? endpointStr : DEFAULT_REST_ENDPOINT;
161 log.info("Configured. REST server endpoint is {}", endpoint);
162
163 String methodStr = Tools.get(properties, METHOD);
164 method = methodStr != null ? methodStr : DEFAULT_REST_METHOD;
165 log.info("Configured. REST server default HTTP method is {}", method);
166
167 String requestMediaTypeStr = Tools.get(properties, REQUEST_MEDIA_TYPE);
168 requestMediaType = requestMediaTypeStr != null ?
169 requestMediaTypeStr : DEFAULT_REST_REQUEST_MEDIA_TYPE;
170 log.info("Configured. REST server request media type is {}", requestMediaType);
171
172 String responseMediaTypeStr = Tools.get(properties, RESPONSE_MEDIA_TYPE);
173 responseMediaType = responseMediaTypeStr != null ?
174 responseMediaTypeStr : DEFAULT_REST_RESPONSE_MEDIA_TYPE;
175 log.info("Configured. REST server response media type is {}", responseMediaType);
Jian Lid1ce10a2018-06-12 13:47:23 +0900176
177 Boolean enableServiceConfigured =
178 getBooleanProperty(properties, ENABLE_SERVICE);
179 if (enableServiceConfigured == null) {
180 enableService = DEFAULT_DISABLE;
181 log.info("REST service enable flag is NOT " +
182 "configured, default value is {}", enableService);
183 } else {
184 enableService = enableServiceConfigured;
185 log.info("Configured. REST service enable flag is {}", enableService);
186 }
Jian Lib9fe3492018-06-07 17:19:07 +0900187 }
188}