blob: 308ee49349ccdedd3e79d22c2d131626698a200a [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.GrpcTelemetryAdminService;
Jian Lib9fe3492018-06-07 17:19:07 +090027import org.onosproject.openstacktelemetry.api.GrpcTelemetryConfigService;
28import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li4df75b12018-06-07 22:11:04 +090029import org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig;
30import org.osgi.service.component.ComponentContext;
31import 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;
38import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
39import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_IP;
40import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_PORT;
41import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_USE_PLAINTEXT;
42import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty;
Jian Lib9fe3492018-06-07 17:19:07 +090043
44/**
45 * gRPC server configuration manager for publishing openstack telemetry.
46 */
47public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService {
48
Jian Li4df75b12018-06-07 22:11:04 +090049 private final Logger log = LoggerFactory.getLogger(getClass());
50
51 private static final String ADDRESS = "address";
52 private static final String PORT = "port";
53 private static final String USE_PLAINTEXT = "usePlaintext";
54 private static final String MAX_INBOUND_MSG_SIZE = "maxInboundMsgSize";
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected ComponentConfigService componentConfigService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected GrpcTelemetryAdminService grpcTelemetryAdminService;
61
62 @Property(name = ADDRESS, value = DEFAULT_GRPC_SERVER_IP,
63 label = "Default IP address to establish initial connection to gRPC server")
64 protected String address = DEFAULT_GRPC_SERVER_IP;
65
66 @Property(name = PORT, intValue = DEFAULT_GRPC_SERVER_PORT,
67 label = "Default port number to establish initial connection to gRPC server")
68 protected Integer port = DEFAULT_GRPC_SERVER_PORT;
69
70 @Property(name = USE_PLAINTEXT, boolValue = DEFAULT_GRPC_USE_PLAINTEXT,
71 label = "UsePlaintext flag value used for connecting to gRPC server")
72 protected Boolean usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
73
74 @Property(name = MAX_INBOUND_MSG_SIZE, intValue = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE,
75 label = "Maximum inbound message size used for communicating with gRPC server")
76 protected Integer maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
77
78 @Activate
79 protected void activate(ComponentContext context) {
80 componentConfigService.registerProperties(getClass());
81 grpcTelemetryAdminService.start(getConfig());
82 log.info("Started");
83 }
84
85 @Deactivate
86 protected void deactivate() {
87 componentConfigService.unregisterProperties(getClass(), false);
88 grpcTelemetryAdminService.stop();
89 log.info("Stopped");
90 }
91
92 @Modified
93 private void modified(ComponentContext context) {
94 readComponentConfiguration(context);
95 grpcTelemetryAdminService.restart(getConfig());
96 log.info("Modified");
97 }
98
Jian Lib9fe3492018-06-07 17:19:07 +090099 @Override
100 public TelemetryConfig getConfig() {
Jian Li4df75b12018-06-07 22:11:04 +0900101 return new DefaultGrpcTelemetryConfig.DefaultBuilder()
102 .withAddress(address)
103 .withPort(port)
104 .withUsePlaintext(usePlaintext)
105 .withMaxInboundMsgSize(maxInboundMsgSize)
106 .build();
Jian Lib9fe3492018-06-07 17:19:07 +0900107 }
Jian Li4df75b12018-06-07 22:11:04 +0900108
109 /**
110 * Extracts properties from the component configuration context.
111 *
112 * @param context the component context
113 */
114 private void readComponentConfiguration(ComponentContext context) {
115 Dictionary<?, ?> properties = context.getProperties();
116
117 String addressStr = get(properties, ADDRESS);
118 address = addressStr != null ? addressStr : DEFAULT_GRPC_SERVER_IP;
119 log.info("Configured. gRPC server address is {}", address);
120
121 Integer portConfigured = Tools.getIntegerProperty(properties, PORT);
122 if (portConfigured == null) {
123 port = DEFAULT_GRPC_SERVER_PORT;
124 log.info("gRPC server port is NOT configured, default value is {}", port);
125 } else {
126 port = portConfigured;
127 log.info("Configured. gRPC server port is {}", port);
128 }
129
130 Boolean usePlaintextConfigured =
131 getBooleanProperty(properties, USE_PLAINTEXT);
132 if (usePlaintextConfigured == null) {
133 usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT;
134 log.info("gRPC server use plaintext flag is NOT " +
135 "configured, default value is {}", usePlaintext);
136 } else {
137 usePlaintext = usePlaintextConfigured;
138 log.info("Configured. gRPC server use plaintext flag is {}", usePlaintext);
139 }
140
141 Integer maxInboundMsgSizeConfigured =
142 getIntegerProperty(properties, MAX_INBOUND_MSG_SIZE);
143 if (maxInboundMsgSizeConfigured == null) {
144 maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE;
145 log.info("gRPC server max inbound message size is NOT " +
146 "configured, default value is {}", maxInboundMsgSize);
147 } else {
148 maxInboundMsgSize = maxInboundMsgSizeConfigured;
149 log.info("Configured. gRPC server max inbound message size is {}", maxInboundMsgSize);
150 }
151 }
152
Jian Lib9fe3492018-06-07 17:19:07 +0900153}