Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacktelemetry.impl; |
| 17 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryConfigService; |
| 22 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig; |
| 24 | import org.osgi.service.component.ComponentContext; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Modified; |
| 29 | import org.osgi.service.component.annotations.Reference; |
| 30 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Dictionary; |
| 35 | |
| 36 | import static org.onlab.util.Tools.get; |
| 37 | import static org.onlab.util.Tools.getIntegerProperty; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 38 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 39 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE; |
| 40 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_IP; |
| 41 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_PORT; |
| 42 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_USE_PLAINTEXT; |
| 43 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 44 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * gRPC server configuration manager for publishing openstack telemetry. |
| 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 49 | @Component(immediate = true, service = GrpcTelemetryConfigService.class) |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 50 | public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService { |
| 51 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 54 | private static final String ENABLE_SERVICE = "enableService"; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 55 | 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 Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 61 | protected ComponentConfigService componentConfigService; |
| 62 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 64 | protected GrpcTelemetryAdminService grpcTelemetryAdminService; |
| 65 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 66 | //@Property(name = ADDRESS, value = DEFAULT_GRPC_SERVER_IP, |
| 67 | // label = "Default IP address to establish initial connection to gRPC server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 68 | protected String address = DEFAULT_GRPC_SERVER_IP; |
| 69 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 70 | //@Property(name = PORT, intValue = DEFAULT_GRPC_SERVER_PORT, |
| 71 | // label = "Default port number to establish initial connection to gRPC server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 72 | protected Integer port = DEFAULT_GRPC_SERVER_PORT; |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 74 | //@Property(name = USE_PLAINTEXT, boolValue = DEFAULT_GRPC_USE_PLAINTEXT, |
| 75 | // label = "UsePlaintext flag value used for connecting to gRPC server") |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 76 | protected Boolean usePlaintext = DEFAULT_GRPC_USE_PLAINTEXT; |
| 77 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 78 | //@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 Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 80 | protected Integer maxInboundMsgSize = DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE; |
| 81 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 82 | //@Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE, |
| 83 | // label = "Specify the default behavior of telemetry service") |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 84 | protected Boolean enableService = DEFAULT_DISABLE; |
| 85 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 86 | @Activate |
| 87 | protected void activate(ComponentContext context) { |
| 88 | componentConfigService.registerProperties(getClass()); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 89 | |
| 90 | if (enableService) { |
| 91 | grpcTelemetryAdminService.start(getConfig()); |
| 92 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 93 | log.info("Started"); |
| 94 | } |
| 95 | |
| 96 | @Deactivate |
| 97 | protected void deactivate() { |
| 98 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 99 | |
| 100 | if (enableService) { |
| 101 | grpcTelemetryAdminService.stop(); |
| 102 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 103 | log.info("Stopped"); |
| 104 | } |
| 105 | |
| 106 | @Modified |
| 107 | private void modified(ComponentContext context) { |
| 108 | readComponentConfiguration(context); |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 109 | initTelemetryService(grpcTelemetryAdminService, getConfig(), enableService); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 110 | log.info("Modified"); |
| 111 | } |
| 112 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 113 | @Override |
| 114 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 115 | return new DefaultGrpcTelemetryConfig.DefaultBuilder() |
| 116 | .withAddress(address) |
| 117 | .withPort(port) |
| 118 | .withUsePlaintext(usePlaintext) |
| 119 | .withMaxInboundMsgSize(maxInboundMsgSize) |
| 120 | .build(); |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 121 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 122 | |
| 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 Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 165 | |
| 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 Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 176 | } |
| 177 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 178 | } |