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; |
| 19 | import org.apache.felix.scr.annotations.Deactivate; |
| 20 | import org.apache.felix.scr.annotations.Modified; |
| 21 | import org.apache.felix.scr.annotations.Property; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.util.Tools; |
| 25 | import org.onosproject.cfg.ComponentConfigService; |
| 26 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryAdminService; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 27 | import org.onosproject.openstacktelemetry.api.GrpcTelemetryConfigService; |
| 28 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig; |
| 30 | import org.osgi.service.component.ComponentContext; |
| 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; |
| 38 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_MAX_INBOUND_MSG_SIZE; |
| 39 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_IP; |
| 40 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_SERVER_PORT; |
| 41 | import static org.onosproject.openstacktelemetry.api.Constants.DEFAULT_GRPC_USE_PLAINTEXT; |
| 42 | import static org.onosproject.openstacktelemetry.util.OpenstackTelemetryUtil.getBooleanProperty; |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * gRPC server configuration manager for publishing openstack telemetry. |
| 46 | */ |
| 47 | public class GrpcTelemetryConfigManager implements GrpcTelemetryConfigService { |
| 48 | |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 49 | 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 Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 99 | @Override |
| 100 | public TelemetryConfig getConfig() { |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 101 | return new DefaultGrpcTelemetryConfig.DefaultBuilder() |
| 102 | .withAddress(address) |
| 103 | .withPort(port) |
| 104 | .withUsePlaintext(usePlaintext) |
| 105 | .withMaxInboundMsgSize(maxInboundMsgSize) |
| 106 | .build(); |
Jian Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 107 | } |
Jian Li | 4df75b1 | 2018-06-07 22:11:04 +0900 | [diff] [blame] | 108 | |
| 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 Li | b9fe349 | 2018-06-07 17:19:07 +0900 | [diff] [blame] | 153 | } |