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; |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 38 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_ENABLE_SERVICE_DEFAULT; |
| 39 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT; |
| 40 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_SERVER_ADDRESS_DEFAULT; |
| 41 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_SERVER_PORT_DEFAULT; |
| 42 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.GRPC_USE_PLAINTEXT_DEFAULT; |
| 43 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_ENABLE_SERVICE; |
| 44 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_MAX_INBOUND_MSG_SIZE; |
| 45 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_SERVER_ADDRESS; |
| 46 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_SERVER_PORT; |
| 47 | import static org.onosproject.openstacktelemetry.impl.OsgiPropertyConstants.PROP_GRPC_USE_PLAINTEXT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 48 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 49 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.initTelemetryService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * gRPC server configuration manager for publishing openstack telemetry. |
| 53 | */ |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 54 | @Component( |
| 55 | immediate = true, |
| 56 | service = GrpcTelemetryConfigService.class, |
| 57 | property = { |
| 58 | PROP_GRPC_ENABLE_SERVICE + ":Boolean=" + GRPC_ENABLE_SERVICE_DEFAULT, |
| 59 | PROP_GRPC_SERVER_ADDRESS + "=" + GRPC_SERVER_ADDRESS_DEFAULT, |
| 60 | PROP_GRPC_SERVER_PORT + ":Integer=" + GRPC_SERVER_PORT_DEFAULT, |
| 61 | PROP_GRPC_USE_PLAINTEXT + ":Boolean=" + GRPC_USE_PLAINTEXT_DEFAULT, |
| 62 | PROP_GRPC_MAX_INBOUND_MSG_SIZE + ":Integer=" + GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT |
| 63 | } |
| 64 | ) |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 65 | public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService { |
| 66 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 67 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 68 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 70 | protected ComponentConfigService componentConfigService; |
| 71 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 73 | protected GrpcTelemetryAdminService grpcTelemetryAdminService; |
| 74 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 75 | /** Default IP address to establish initial connection to gRPC server. */ |
| 76 | protected String address = GRPC_SERVER_ADDRESS_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 77 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 78 | /** Default port number to establish initial connection to gRPC server. */ |
| 79 | protected Integer port = GRPC_SERVER_PORT_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 80 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 81 | /** UsePlaintext flag value used for connecting to gRPC server. */ |
| 82 | protected Boolean usePlaintext = GRPC_USE_PLAINTEXT_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 83 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 84 | /** Maximum inbound message size used for communicating with gRPC server. */ |
| 85 | protected Integer maxInboundMsgSize = GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 86 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 87 | /** Specify the default behavior of telemetry service. */ |
| 88 | protected Boolean enableService = GRPC_ENABLE_SERVICE_DEFAULT; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 89 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 90 | @Activate |
| 91 | protected void activate(ComponentContext context) { |
| 92 | componentConfigService.registerProperties(getClass()); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 93 | |
| 94 | if (enableService) { |
| 95 | grpcTelemetryAdminService.start(getConfig()); |
| 96 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 97 | log.info("Started"); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | protected void deactivate() { |
| 102 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 103 | |
| 104 | if (enableService) { |
| 105 | grpcTelemetryAdminService.stop(); |
| 106 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 107 | log.info("Stopped"); |
| 108 | } |
| 109 | |
| 110 | @Modified |
| 111 | private void modified(ComponentContext context) { |
| 112 | readComponentConfiguration(context); |
Jian Li | 3ed7f30 | 2018-08-27 17:16:27 +0900 | [diff] [blame] | 113 | initTelemetryService(grpcTelemetryAdminService, getConfig(), enableService); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 114 | log.info("Modified"); |
| 115 | } |
| 116 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 117 | @Override |
| 118 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 119 | return new DefaultGrpcTelemetryConfig.DefaultBuilder() |
| 120 | .withAddress(address) |
| 121 | .withPort(port) |
| 122 | .withUsePlaintext(usePlaintext) |
| 123 | .withMaxInboundMsgSize(maxInboundMsgSize) |
| 124 | .build(); |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 125 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Extracts properties from the component configuration context. |
| 129 | * |
| 130 | * @param context the component context |
| 131 | */ |
| 132 | private void readComponentConfiguration(ComponentContext context) { |
| 133 | Dictionary<?, ?> properties = context.getProperties(); |
| 134 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 135 | String addressStr = get(properties, PROP_GRPC_SERVER_ADDRESS); |
| 136 | address = addressStr != null ? addressStr : GRPC_SERVER_ADDRESS_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 137 | log.info("Configured. gRPC server address is {}", address); |
| 138 | |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 139 | Integer portConfigured = Tools.getIntegerProperty(properties, PROP_GRPC_SERVER_PORT); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 140 | if (portConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 141 | port = GRPC_SERVER_PORT_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 142 | log.info("gRPC server port is NOT configured, default value is {}", port); |
| 143 | } else { |
| 144 | port = portConfigured; |
| 145 | log.info("Configured. gRPC server port is {}", port); |
| 146 | } |
| 147 | |
| 148 | Boolean usePlaintextConfigured = |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 149 | getBooleanProperty(properties, PROP_GRPC_USE_PLAINTEXT); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 150 | if (usePlaintextConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 151 | usePlaintext = GRPC_USE_PLAINTEXT_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 152 | log.info("gRPC server use plaintext flag is NOT " + |
| 153 | "configured, default value is {}", usePlaintext); |
| 154 | } else { |
| 155 | usePlaintext = usePlaintextConfigured; |
| 156 | log.info("Configured. gRPC server use plaintext flag is {}", usePlaintext); |
| 157 | } |
| 158 | |
| 159 | Integer maxInboundMsgSizeConfigured = |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 160 | getIntegerProperty(properties, PROP_GRPC_MAX_INBOUND_MSG_SIZE); |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 161 | if (maxInboundMsgSizeConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 162 | maxInboundMsgSize = GRPC_MAX_INBOUND_MSG_SIZE_DEFAULT; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 163 | log.info("gRPC server max inbound message size is NOT " + |
| 164 | "configured, default value is {}", maxInboundMsgSize); |
| 165 | } else { |
| 166 | maxInboundMsgSize = maxInboundMsgSizeConfigured; |
| 167 | log.info("Configured. gRPC server max inbound message size is {}", maxInboundMsgSize); |
| 168 | } |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 169 | |
| 170 | Boolean enableServiceConfigured = |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 171 | getBooleanProperty(properties, PROP_GRPC_ENABLE_SERVICE); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 172 | if (enableServiceConfigured == null) { |
Ray Milkey | 8e40651 | 2018-10-24 15:56:50 -0700 | [diff] [blame] | 173 | enableService = GRPC_ENABLE_SERVICE_DEFAULT; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 174 | log.info("gRPC service enable flag is NOT " + |
| 175 | "configured, default value is {}", enableService); |
| 176 | } else { |
| 177 | enableService = enableServiceConfigured; |
| 178 | log.info("Configured. gRPC service enable flag is {}", enableService); |
| 179 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 180 | } |
| 181 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 182 | } |