blob: 60e851e56321b46f08ac22235ae6170e377525f7 [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.GrpcTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090029import org.onosproject.openstacktelemetry.api.GrpcTelemetryConfigService;
30import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090031import org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig;
32import org.osgi.service.component.ComponentContext;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Dictionary;
37
38import static org.onlab.util.Tools.get;
39import static org.onlab.util.Tools.getIntegerProperty;
Jian Lid1ce10a2018-06-12 13:47:23 +090040import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE;
Jian Li4df75b12018-06-07 22:11:04 +090041import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
42import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_IP;
43import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_PORT;
44import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_USE_PLAINTEXT;
45import 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 * gRPC 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 GrpcTelemetryConfigManager implements GrpcTelemetryConfigService {
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 USE_PLAINTEXT = "usePlaintext";
61 private static final String MAX_INBOUND_MSG_SIZE = "maxInboundMsgSize";
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ComponentConfigService componentConfigService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected GrpcTelemetryAdminService grpcTelemetryAdminService;
68
69 @Property(name = ADDRESS, value = DEFAULT_GRPC_SERVER_IP,
70 label = "Default IP address to establish initial connection to gRPC server")
71 protected String address = DEFAULT_GRPC_SERVER_IP;
72
73 @Property(name = PORT, intValue = DEFAULT_GRPC_SERVER_PORT,
74 label = "Default port number to establish initial connection to gRPC server")
75 protected Integer port = DEFAULT_GRPC_SERVER_PORT;
76
77 @Property(name = USE_PLAINTEXT, boolValue = DEFAULT_GRPC_USE_PLAINTEXT,
78 label = "UsePlaintext flag value used for connecting to gRPC server")
79 protected Boolean usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
80
81 @Property(name = MAX_INBOUND_MSG_SIZE, intValue = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE,
82 label = "Maximum inbound message size used for communicating with gRPC server")
83 protected Integer maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
84
Jian Lid1ce10a2018-06-12 13:47:23 +090085 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
86 label = "Specify the default behavior of telemetry service")
87 protected Boolean enableService = DEFAULT_DISABLE;
88
Jian Li4df75b12018-06-07 22:11:04 +090089 @Activate
90 protected void activate(ComponentContext context) {
91 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +090092
93 if (enableService) {
94 grpcTelemetryAdminService.start(getConfig());
95 }
Jian Li4df75b12018-06-07 22:11:04 +090096 log.info("Started");
97 }
98
99 @Deactivate
100 protected void deactivate() {
101 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900102
103 if (enableService) {
104 grpcTelemetryAdminService.stop();
105 }
Jian Li4df75b12018-06-07 22:11:04 +0900106 log.info("Stopped");
107 }
108
109 @Modified
110 private void modified(ComponentContext context) {
111 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900112 initTelemetryService(grpcTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900113 log.info("Modified");
114 }
115
Jian Lib9fe3492018-06-07 17:19:07 +0900116 @Override
117 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900118 return new DefaultGrpcTelemetryConfig.DefaultBuilder()
119 .withAddress(address)
120 .withPort(port)
121 .withUsePlaintext(usePlaintext)
122 .withMaxInboundMsgSize(maxInboundMsgSize)
123 .build();
Jian Lib9fe3492018-06-07 17:19:07 +0900124 }
Jian Li4df75b12018-06-07 22:11:04 +0900125
126 /**
127 * Extracts properties from the component configuration context.
128 *
129 * @param context the component context
130 */
131 private void readComponentConfiguration(ComponentContext context) {
132 Dictionary<?, ?> properties = context.getProperties();
133
134 String addressStr = get(properties, ADDRESS);
135 address = addressStr != null ? addressStr : DEFAULT_GRPC_SERVER_IP;
136 log.info("Configured. gRPC server address is {}", address);
137
138 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
139 if (portConfigured == null) {
140 port = DEFAULT_GRPC_SERVER_PORT;
141 log.info("gRPC server port is NOT configured, default value is {}", port);
142 } else {
143 port = portConfigured;
144 log.info("Configured. gRPC server port is {}", port);
145 }
146
147 Boolean usePlaintextConfigured =
148 getBooleanProperty(properties, USE_PLAINTEXT);
149 if (usePlaintextConfigured == null) {
150 usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
151 log.info("gRPC server use plaintext flag is NOT " +
152 "configured, default value is {}", usePlaintext);
153 } else {
154 usePlaintext = usePlaintextConfigured;
155 log.info("Configured. gRPC server use plaintext flag is {}", usePlaintext);
156 }
157
158 Integer maxInboundMsgSizeConfigured =
159 getIntegerProperty(properties, MAX_INBOUND_MSG_SIZE);
160 if (maxInboundMsgSizeConfigured == null) {
161 maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
162 log.info("gRPC server max inbound message size is NOT " +
163 "configured, default value is {}", maxInboundMsgSize);
164 } else {
165 maxInboundMsgSize = maxInboundMsgSizeConfigured;
166 log.info("Configured. gRPC server max inbound message size is {}", maxInboundMsgSize);
167 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900168
169 Boolean enableServiceConfigured =
170 getBooleanProperty(properties, ENABLE_SERVICE);
171 if (enableServiceConfigured == null) {
172 enableService = DEFAULT_DISABLE;
173 log.info("gRPC service enable flag is NOT " +
174 "configured, default value is {}", enableService);
175 } else {
176 enableService = enableServiceConfigured;
177 log.info("Configured. gRPC service enable flag is {}", enableService);
178 }
Jian Li4df75b12018-06-07 22:11:04 +0900179 }
180
Jian Lib9fe3492018-06-07 17:19:07 +0900181}