blob: 97ddcb15804140f65c74b6349f469cdca2f918cf [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;
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_GRPC_MAX_INBOUND_MSG_SIZE;
40import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_IP;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_PORT;
42import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_USE_PLAINTEXT;
43import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Li3ed7f302018-08-27 17:16:27 +090044import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService;
Jian Lib9fe3492018-06-07 17:19:07 +090045
46/**
47 * gRPC server configuration manager for publishing openstack telemetry.
48 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(immediate = true, service = GrpcTelemetryConfigService.class)
Jian Lib9fe3492018-06-07 17:19:07 +090050public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService {
51
Jian Li4df75b12018-06-07 22:11:04 +090052 private final Logger log = LoggerFactory.getLogger(getClass());
53
Jian Lid1ce10a2018-06-12 13:47:23 +090054 private static final String ENABLE_SERVICE = "enableService";
Jian Li4df75b12018-06-07 22:11:04 +090055 private static final String ADDRESS = "address";
56 private static final String PORT = "port";
57 private static final String USE_PLAINTEXT = "usePlaintext";
58 private static final String MAX_INBOUND_MSG_SIZE = "maxInboundMsgSize";
59
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090061 protected ComponentConfigService componentConfigService;
62
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jian Li4df75b12018-06-07 22:11:04 +090064 protected GrpcTelemetryAdminService grpcTelemetryAdminService;
65
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 //@Property(name = ADDRESS, value = DEFAULT_GRPC_SERVER_IP,
67 // label = "Default IP address to establish initial connection to gRPC server")
Jian Li4df75b12018-06-07 22:11:04 +090068 protected String address = DEFAULT_GRPC_SERVER_IP;
69
Ray Milkeyd84f89b2018-08-17 14:54:17 -070070 //@Property(name = PORT, intValue = DEFAULT_GRPC_SERVER_PORT,
71 // label = "Default port number to establish initial connection to gRPC server")
Jian Li4df75b12018-06-07 22:11:04 +090072 protected Integer port = DEFAULT_GRPC_SERVER_PORT;
73
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 //@Property(name = USE_PLAINTEXT, boolValue = DEFAULT_GRPC_USE_PLAINTEXT,
75 // label = "UsePlaintext flag value used for connecting to gRPC server")
Jian Li4df75b12018-06-07 22:11:04 +090076 protected Boolean usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
77
Ray Milkeyd84f89b2018-08-17 14:54:17 -070078 //@Property(name = MAX_INBOUND_MSG_SIZE, intValue = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE,
79 // label = "Maximum inbound message size used for communicating with gRPC server")
Jian Li4df75b12018-06-07 22:11:04 +090080 protected Integer maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
81
Ray Milkeyd84f89b2018-08-17 14:54:17 -070082 //@Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE,
83 // label = "Specify the default behavior of telemetry service")
Jian Lid1ce10a2018-06-12 13:47:23 +090084 protected Boolean enableService = DEFAULT_DISABLE;
85
Jian Li4df75b12018-06-07 22:11:04 +090086 @Activate
87 protected void activate(ComponentContext context) {
88 componentConfigService.registerProperties(getClass());
Jian Lid1ce10a2018-06-12 13:47:23 +090089
90 if (enableService) {
91 grpcTelemetryAdminService.start(getConfig());
92 }
Jian Li4df75b12018-06-07 22:11:04 +090093 log.info("Started");
94 }
95
96 @Deactivate
97 protected void deactivate() {
98 componentConfigService.unregisterProperties(getClass(), false);
Jian Lid1ce10a2018-06-12 13:47:23 +090099
100 if (enableService) {
101 grpcTelemetryAdminService.stop();
102 }
Jian Li4df75b12018-06-07 22:11:04 +0900103 log.info("Stopped");
104 }
105
106 @Modified
107 private void modified(ComponentContext context) {
108 readComponentConfiguration(context);
Jian Li3ed7f302018-08-27 17:16:27 +0900109 initTelemetryService(grpcTelemetryAdminService, getConfig(), enableService);
Jian Li4df75b12018-06-07 22:11:04 +0900110 log.info("Modified");
111 }
112
Jian Lib9fe3492018-06-07 17:19:07 +0900113 @Override
114 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900115 return new DefaultGrpcTelemetryConfig.DefaultBuilder()
116 .withAddress(address)
117 .withPort(port)
118 .withUsePlaintext(usePlaintext)
119 .withMaxInboundMsgSize(maxInboundMsgSize)
120 .build();
Jian Lib9fe3492018-06-07 17:19:07 +0900121 }
Jian Li4df75b12018-06-07 22:11:04 +0900122
123 /**
124 * Extracts properties from the component configuration context.
125 *
126 * @param context the component context
127 */
128 private void readComponentConfiguration(ComponentContext context) {
129 Dictionary<?, ?> properties = context.getProperties();
130
131 String addressStr = get(properties, ADDRESS);
132 address = addressStr != null ? addressStr : DEFAULT_GRPC_SERVER_IP;
133 log.info("Configured. gRPC server address is {}", address);
134
135 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
136 if (portConfigured == null) {
137 port = DEFAULT_GRPC_SERVER_PORT;
138 log.info("gRPC server port is NOT configured, default value is {}", port);
139 } else {
140 port = portConfigured;
141 log.info("Configured. gRPC server port is {}", port);
142 }
143
144 Boolean usePlaintextConfigured =
145 getBooleanProperty(properties, USE_PLAINTEXT);
146 if (usePlaintextConfigured == null) {
147 usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
148 log.info("gRPC server use plaintext flag is NOT " +
149 "configured, default value is {}", usePlaintext);
150 } else {
151 usePlaintext = usePlaintextConfigured;
152 log.info("Configured. gRPC server use plaintext flag is {}", usePlaintext);
153 }
154
155 Integer maxInboundMsgSizeConfigured =
156 getIntegerProperty(properties, MAX_INBOUND_MSG_SIZE);
157 if (maxInboundMsgSizeConfigured == null) {
158 maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
159 log.info("gRPC server max inbound message size is NOT " +
160 "configured, default value is {}", maxInboundMsgSize);
161 } else {
162 maxInboundMsgSize = maxInboundMsgSizeConfigured;
163 log.info("Configured. gRPC server max inbound message size is {}", maxInboundMsgSize);
164 }
Jian Lid1ce10a2018-06-12 13:47:23 +0900165
166 Boolean enableServiceConfigured =
167 getBooleanProperty(properties, ENABLE_SERVICE);
168 if (enableServiceConfigured == null) {
169 enableService = DEFAULT_DISABLE;
170 log.info("gRPC service enable flag is NOT " +
171 "configured, default value is {}", enableService);
172 } else {
173 enableService = enableServiceConfigured;
174 log.info("Configured. gRPC service enable flag is {}", enableService);
175 }
Jian Li4df75b12018-06-07 22:11:04 +0900176 }
177
Jian Lib9fe3492018-06-07 17:19:07 +0900178}