blob: 70b1d70227b96adcf5c346bfc4b8c23df2fb8eec [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.GrpcTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090021import org.onosproject.openstacktelemetry.api.GrpcTelemetryConfigService;
22import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090023import org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig;
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
36import static org.onlab.util.Tools.get;
37import static org.onlab.util.Tools.getIntegerProperty;
Ray Milkey8e406512018-10-24 15:56:50 -070038import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_ENABLE_SERVICE_DEFAULT;
39import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT;
40import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_SERVER_ADDRESS_DEFAULT;
41import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_SERVER_PORT_DEFAULT;
42import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_USE_PLAINTEXT_DEFAULT;
43import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_ENABLE_SERVICE;
44import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_MAX_INBOUND_MSG_SIZE;
45import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_SERVER_ADDRESS;
46import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_SERVER_PORT;
47import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_USE_PLAINTEXT;
Jian Li4df75b12018-06-07 22:11:04 +090048import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090049import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090050
51/**
52 * gRPC server configuration manager for publishing openstack telemetry.
53 */
Ray Milkey8e406512018-10-24 15:56:50 -070054@Component(
55 immediate = true,
56 service = GrpcTelemetryConfigService.class,
57 property = {
58 PROP_GRPC_ENABLE_SERVICE + ":Boolean=" + GRPC_ENABLE_SERVICE_DEFAULT,
59 PROP_GRPC_SERVER_ADDRESS + "=" + GRPC_SERVER_ADDRESS_DEFAULT,
60 PROP_GRPC_SERVER_PORT + ":Integer=" + GRPC_SERVER_PORT_DEFAULT,
61 PROP_GRPC_USE_PLAINTEXT + ":Boolean=" + GRPC_USE_PLAINTEXT_DEFAULT,
62 PROP_GRPC_MAX_INBOUND_MSG_SIZE + ":Integer=" + GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT
63 }
64)
Jian Lib9fe3492018-06-07 17:19:07 +090065public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService {
66
Jian Li4df75b12018-06-07 22:11:04 +090067 private final Logger log = LoggerFactory.getLogger(getClass());
68
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090070 protected ComponentConfigService componentConfigService;
71
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090073 protected GrpcTelemetryAdminService grpcTelemetryAdminService;
74
Ray Milkey8e406512018-10-24 15:56:50 -070075 /** Default IP address to establish initial connection to gRPC server. */
76 protected String address = GRPC_SERVER_ADDRESS_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090077
Ray Milkey8e406512018-10-24 15:56:50 -070078 /** Default port number to establish initial connection to gRPC server. */
79 protected Integer port = GRPC_SERVER_PORT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090080
Ray Milkey8e406512018-10-24 15:56:50 -070081 /** UsePlaintext flag value used for connecting to gRPC server. */
82 protected Boolean usePlaintext = GRPC_USE_PLAINTEXT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090083
Ray Milkey8e406512018-10-24 15:56:50 -070084 /** Maximum inbound message size used for communicating with gRPC server. */
85 protected Integer maxInboundMsgSize = GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +090086
Ray Milkey8e406512018-10-24 15:56:50 -070087 /** Specify the default behavior of telemetry service. */
88 protected Boolean enableService = GRPC_ENABLE_SERVICE_DEFAULT;
Jian Lid1ce10a2018-06-12 13:47:23 +090089
Jian Li4df75b12018-06-07 22:11:04 +090090 @Activate
91 protected void activate(ComponentContext context) {
92 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +090093
94 if (enableService) {
95 grpcTelemetryAdminService.start(getConfig());
96 }
Jian Li4df75b12018-06-07 22:11:04 +090097 log.info("Started");
98 }
99
100 @Deactivate
101 protected void deactivate() {
102 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900103
104 if (enableService) {
105 grpcTelemetryAdminService.stop();
106 }
Jian Li4df75b12018-06-07 22:11:04 +0900107 log.info("Stopped");
108 }
109
110 @Modified
111 private void modified(ComponentContext context) {
112 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900113 initTelemetryService(grpcTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900114 log.info("Modified");
115 }
116
Jian Lib9fe3492018-06-07 17:19:07 +0900117 @Override
118 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900119 return new DefaultGrpcTelemetryConfig.DefaultBuilder()
120 .withAddress(address)
121 .withPort(port)
122 .withUsePlaintext(usePlaintext)
123 .withMaxInboundMsgSize(maxInboundMsgSize)
124 .build();
Jian Lib9fe3492018-06-07 17:19:07 +0900125 }
Jian Li4df75b12018-06-07 22:11:04 +0900126
127 /**
128 * Extracts properties from the component configuration context.
129 *
130 * @param context the component context
131 */
132 private void readComponentConfiguration(ComponentContext context) {
133 Dictionary<?, ?> properties = context.getProperties();
134
Ray Milkey8e406512018-10-24 15:56:50 -0700135 String addressStr = get(properties, PROP_GRPC_SERVER_ADDRESS);
136 address = addressStr != null ? addressStr : GRPC_SERVER_ADDRESS_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900137 log.info("Configured. gRPC server address is {}", address);
138
Ray Milkey8e406512018-10-24 15:56:50 -0700139 Integer portConfigured = Tools.getIntegerProperty(properties, PROP_GRPC_SERVER_PORT);
Jian Li4df75b12018-06-07 22:11:04 +0900140 if (portConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700141 port = GRPC_SERVER_PORT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900142 log.info("gRPC server port is NOT configured, default value is {}", port);
143 } else {
144 port = portConfigured;
145 log.info("Configured. gRPC server port is {}", port);
146 }
147
148 Boolean usePlaintextConfigured =
Ray Milkey8e406512018-10-24 15:56:50 -0700149 getBooleanProperty(properties, PROP_GRPC_USE_PLAINTEXT);
Jian Li4df75b12018-06-07 22:11:04 +0900150 if (usePlaintextConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700151 usePlaintext = GRPC_USE_PLAINTEXT_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900152 log.info("gRPC server use plaintext flag is NOT " +
153 "configured, default value is {}", usePlaintext);
154 } else {
155 usePlaintext = usePlaintextConfigured;
156 log.info("Configured. gRPC server use plaintext flag is {}", usePlaintext);
157 }
158
159 Integer maxInboundMsgSizeConfigured =
Ray Milkey8e406512018-10-24 15:56:50 -0700160 getIntegerProperty(properties, PROP_GRPC_MAX_INBOUND_MSG_SIZE);
Jian Li4df75b12018-06-07 22:11:04 +0900161 if (maxInboundMsgSizeConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700162 maxInboundMsgSize = GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT;
Jian Li4df75b12018-06-07 22:11:04 +0900163 log.info("gRPC server max inbound message size is NOT " +
164 "configured, default value is {}", maxInboundMsgSize);
165 } else {
166 maxInboundMsgSize = maxInboundMsgSizeConfigured;
167 log.info("Configured. gRPC server max inbound message size is {}", maxInboundMsgSize);
168 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900169
170 Boolean enableServiceConfigured =
Ray Milkey8e406512018-10-24 15:56:50 -0700171 getBooleanProperty(properties, PROP_GRPC_ENABLE_SERVICE);
Jian Lid1ce10a2018-06-12 13:47:23 +0900172 if (enableServiceConfigured == null) {
Ray Milkey8e406512018-10-24 15:56:50 -0700173 enableService = GRPC_ENABLE_SERVICE_DEFAULT;
Jian Lid1ce10a2018-06-12 13:47:23 +0900174 log.info("gRPC service enable flag is NOT " +
175 "configured, default value is {}", enableService);
176 } else {
177 enableService = enableServiceConfigured;
178 log.info("Configured. gRPC service enable flag is {}", enableService);
179 }
Jian Li4df75b12018-06-07 22:11:04 +0900180 }
181
Jian Lib9fe3492018-06-07 17:19:07 +0900182}