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.apache.felix.scr.annotations.Activate; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Component; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
| 27 | import org.onosproject.cfg.ComponentConfigService; |
| 28 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryConfigService; |
| 30 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 31 | import org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig; |
| 32 | import org.osgi.service.component.ComponentContext; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Dictionary; |
| 37 | |
| 38 | import static org.onlab.util.Tools.get; |
| 39 | import static org.onlab.util.Tools.getIntegerProperty; |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 40 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_DISABLE; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 41 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE; |
| 42 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_IP; |
| 43 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_PORT; |
| 44 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_USE_PLAINTEXT; |
| 45 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * gRPC server configuration manager for publishing openstack telemetry. |
| 49 | */ |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 50 | @Component(immediate = true) |
| 51 | @Service |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 52 | public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService { |
| 53 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 54 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 55 | |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 56 | private static final String ENABLE_SERVICE = "enableService"; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 57 | 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 Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 84 | @Property(name = ENABLE_SERVICE, boolValue = DEFAULT_DISABLE, |
| 85 | label = "Specify the default behavior of telemetry service") |
| 86 | protected Boolean enableService = DEFAULT_DISABLE; |
| 87 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 88 | @Activate |
| 89 | protected void activate(ComponentContext context) { |
| 90 | componentConfigService.registerProperties(getClass()); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 91 | |
| 92 | if (enableService) { |
| 93 | grpcTelemetryAdminService.start(getConfig()); |
| 94 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 95 | log.info("Started"); |
| 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | protected void deactivate() { |
| 100 | componentConfigService.unregisterProperties(getClass(), false); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 101 | |
| 102 | if (enableService) { |
| 103 | grpcTelemetryAdminService.stop(); |
| 104 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
| 108 | @Modified |
| 109 | private void modified(ComponentContext context) { |
| 110 | readComponentConfiguration(context); |
Jian Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 111 | |
| 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 Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 123 | log.info("Modified"); |
| 124 | } |
| 125 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 126 | @Override |
| 127 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 128 | return new DefaultGrpcTelemetryConfig.DefaultBuilder() |
| 129 | .withAddress(address) |
| 130 | .withPort(port) |
| 131 | .withUsePlaintext(usePlaintext) |
| 132 | .withMaxInboundMsgSize(maxInboundMsgSize) |
| 133 | .build(); |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 134 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 135 | |
| 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 Li | d1ce10a | 2018-06-12 13:47:23 +0900 | [diff] [blame] | 178 | |
| 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 Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 189 | } |
| 190 | |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 191 | } |