blob: 832c60b4bbb4723b81b13f11935699cb4bcb0fac [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;
19import org.apache.felix.scr.annotations.Deactivate;
20import org.apache.felix.scr.annotations.Modified;
21import org.apache.felix.scr.annotations.Property;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onlab.util.Tools;
25import org.onosproject.cfg.ComponentConfigService;
26import org.onosproject.openstacktelemetry.api.RestTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090027import org.onosproject.openstacktelemetry.api.RestTelemetryConfigService;
28import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090029import org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig;
30import org.osgi.service.component.ComponentContext;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.Dictionary;
35
36import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_ENDPOINT;
37import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_METHOD;
38import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_REQUEST_MEDIA_TYPE;
39import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_RESPONSE_MEDIA_TYPE;
40import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_IP;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_REST_SERVER_PORT;
Jian Lib9fe3492018-06-07 17:19:07 +090042
43/**
44 * REST server configuration manager for publishing openstack telemetry.
45 */
46public class RestTelemetryConfigManager implements RestTelemetryConfigService {
47
Jian Li4df75b12018-06-07 22:11:04 +090048 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 Lib9fe3492018-06-07 17:19:07 +0900108 @Override
109 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900110 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 Lib9fe3492018-06-07 17:19:07 +0900158 }
159}