blob: 0ab2b5ee7f6cf376d39375e5a55ba2c56a8a8d1b [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 Lib9fe3492018-06-07 17:19:07 +090046
47/**
48 * gRPC 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 GrpcTelemetryConfigManager implements GrpcTelemetryConfigService {
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 USE_PLAINTEXT = "usePlaintext";
60 private static final String MAX_INBOUND_MSG_SIZE = "maxInboundMsgSize";
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ComponentConfigService componentConfigService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected GrpcTelemetryAdminService grpcTelemetryAdminService;
67
68 @Property(name = ADDRESS, value = DEFAULT_GRPC_SERVER_IP,
69 label = "Default IP address to establish initial connection to gRPC server")
70 protected String address = DEFAULT_GRPC_SERVER_IP;
71
72 @Property(name = PORT, intValue = DEFAULT_GRPC_SERVER_PORT,
73 label = "Default port number to establish initial connection to gRPC server")
74 protected Integer port = DEFAULT_GRPC_SERVER_PORT;
75
76 @Property(name = USE_PLAINTEXT, boolValue = DEFAULT_GRPC_USE_PLAINTEXT,
77 label = "UsePlaintext flag value used for connecting to gRPC server")
78 protected Boolean usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
79
80 @Property(name = MAX_INBOUND_MSG_SIZE, intValue = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE,
81 label = "Maximum inbound message size used for communicating with gRPC server")
82 protected Integer maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
83
Jian Lid1ce10a2018-06-12 13:47:23 +090084 @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
85 label = "Specify the default behavior of telemetry service")
86 protected Boolean enableService = DEFAULT_DISABLE;
87
Jian Li4df75b12018-06-07 22:11:04 +090088 @Activate
89 protected void activate(ComponentContext context) {
90 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +090091
92 if (enableService) {
93 grpcTelemetryAdminService.start(getConfig());
94 }
Jian Li4df75b12018-06-07 22:11:04 +090095 log.info("Started");
96 }
97
98 @Deactivate
99 protected void deactivate() {
100 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +0900101
102 if (enableService) {
103 grpcTelemetryAdminService.stop();
104 }
Jian Li4df75b12018-06-07 22:11:04 +0900105 log.info("Stopped");
106 }
107
108 @Modified
109 private void modified(ComponentContext context) {
110 readComponentConfiguration(context);
Jian Lid1ce10a2018-06-12 13:47:23 +0900111
112 if (enableService) {
113 if (grpcTelemetryAdminService.isRunning()) {
114 grpcTelemetryAdminService.restart(getConfig());
115 } else {
116 grpcTelemetryAdminService.start(getConfig());
117 }
118 } else {
119 if (grpcTelemetryAdminService.isRunning()) {
120 grpcTelemetryAdminService.stop();
121 }
122 }
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 DefaultGrpcTelemetryConfig.DefaultBuilder()
129 .withAddress(address)
130 .withPort(port)
131 .withUsePlaintext(usePlaintext)
132 .withMaxInboundMsgSize(maxInboundMsgSize)
133 .build();
Jian Lib9fe3492018-06-07 17:19:07 +0900134 }
Jian Li4df75b12018-06-07 22:11:04 +0900135
136 /**
137 * Extracts properties from the component configuration context.
138 *
139 * @param context the component context
140 */
141 private void readComponentConfiguration(ComponentContext context) {
142 Dictionary<?, ?> properties = context.getProperties();
143
144 String addressStr = get(properties, ADDRESS);
145 address = addressStr != null ? addressStr : DEFAULT_GRPC_SERVER_IP;
146 log.info("Configured. gRPC server address is {}", address);
147
148 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
149 if (portConfigured == null) {
150 port = DEFAULT_GRPC_SERVER_PORT;
151 log.info("gRPC server port is NOT configured, default value is {}", port);
152 } else {
153 port = portConfigured;
154 log.info("Configured. gRPC server port is {}", port);
155 }
156
157 Boolean usePlaintextConfigured =
158 getBooleanProperty(properties, USE_PLAINTEXT);
159 if (usePlaintextConfigured == null) {
160 usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
161 log.info("gRPC server use plaintext flag is NOT " +
162 "configured, default value is {}", usePlaintext);
163 } else {
164 usePlaintext = usePlaintextConfigured;
165 log.info("Configured. gRPC server use plaintext flag is {}", usePlaintext);
166 }
167
168 Integer maxInboundMsgSizeConfigured =
169 getIntegerProperty(properties, MAX_INBOUND_MSG_SIZE);
170 if (maxInboundMsgSizeConfigured == null) {
171 maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
172 log.info("gRPC server max inbound message size is NOT " +
173 "configured, default value is {}", maxInboundMsgSize);
174 } else {
175 maxInboundMsgSize = maxInboundMsgSizeConfigured;
176 log.info("Configured. gRPC server max inbound message size is {}", maxInboundMsgSize);
177 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900178
179 Boolean enableServiceConfigured =
180 getBooleanProperty(properties, ENABLE_SERVICE);
181 if (enableServiceConfigured == null) {
182 enableService = DEFAULT_DISABLE;
183 log.info("gRPC service enable flag is NOT " +
184 "configured, default value is {}", enableService);
185 } else {
186 enableService = enableServiceConfigured;
187 log.info("Configured. gRPC service enable flag is {}", enableService);
188 }
Jian Li4df75b12018-06-07 22:11:04 +0900189 }
190
Jian Lib9fe3492018-06-07 17:19:07 +0900191}