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