Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.protobuf.registry; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import io.grpc.BindableService; |
| 21 | import io.grpc.Server; |
| 22 | import io.grpc.ServerBuilder; |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 23 | import org.onosproject.protobuf.api.GrpcServiceRegistry; |
| 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.Property; |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | import java.util.Dictionary; |
| 35 | import java.util.Map; |
| 36 | import java.util.concurrent.TimeUnit; |
Jian Li | 10d9962 | 2017-08-26 00:58:24 +0900 | [diff] [blame] | 37 | import java.util.concurrent.atomic.AtomicBoolean; |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 38 | |
| 39 | import static org.onlab.util.Tools.get; |
| 40 | |
| 41 | /** |
| 42 | * A basic implementation of {@link GrpcServiceRegistry} designed for use with |
| 43 | * built in gRPC services. |
| 44 | * |
| 45 | * NOTE: this is an early implementation in which the addition of any new |
| 46 | * service forces a restart of the server, this is sufficient for testing but |
| 47 | * inappropriate for deployment. |
| 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | @Component(service = GrpcServiceRegistry.class) |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 50 | public class GrpcServiceRegistryImpl implements GrpcServiceRegistry { |
| 51 | |
| 52 | private static final int DEFAULT_SERVER_PORT = 64000; |
| 53 | private static final int DEFAULT_SHUTDOWN_TIME = 1; |
Jian Li | 10d9962 | 2017-08-26 00:58:24 +0900 | [diff] [blame] | 54 | private static final AtomicBoolean SERVICES_MODIFIED_SINCE_START = new AtomicBoolean(false); |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 55 | |
| 56 | private static final String PORT_PROPERTY_NAME = "listeningPort"; |
| 57 | |
| 58 | private final Map<Class<? extends BindableService>, BindableService> registeredServices = |
| 59 | Maps.newHashMap(); |
| 60 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 61 | |
| 62 | private Server server; |
| 63 | |
| 64 | /* It is currently the responsibility of the administrator to notify |
| 65 | clients of nonstandard port usage as there is no mechanism available to |
| 66 | discover the port hosting gRPC services. |
| 67 | */ |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 68 | /** The port number which ONOS will use to host gRPC services. */ |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 69 | private int listeningPort = DEFAULT_SERVER_PORT; |
| 70 | |
| 71 | @Activate |
| 72 | public void activate() { |
| 73 | log.info("Started"); |
| 74 | } |
| 75 | |
| 76 | @Deactivate |
| 77 | public void deactivate() { |
| 78 | attemptGracefulShutdownThenForce(DEFAULT_SHUTDOWN_TIME); |
| 79 | log.info("Stopped"); |
| 80 | } |
| 81 | |
| 82 | @Modified |
| 83 | public void modified(ComponentContext context) { |
| 84 | if (context != null) { |
| 85 | setProperties(context); |
| 86 | } |
| 87 | log.info("Connection was restarted to allow service to be added, " + |
| 88 | "this is a temporary workaround"); |
| 89 | restartServer(listeningPort); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean register(BindableService service) { |
| 94 | synchronized (registeredServices) { |
| 95 | if (!registeredServices.containsKey(service.getClass())) { |
| 96 | registeredServices.put(service.getClass(), service); |
| 97 | } else { |
| 98 | log.warn("The specified class \"{}\" was not added becuase an " + |
| 99 | "instance of the class is already registered.", |
| 100 | service.getClass().toString()); |
| 101 | return false; |
| 102 | } |
| 103 | } |
| 104 | return restartServer(listeningPort); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean unregister(BindableService service) { |
| 109 | synchronized (registeredServices) { |
| 110 | if (registeredServices.containsKey(service.getClass())) { |
| 111 | registeredServices.remove(service.getClass()); |
| 112 | } else { |
| 113 | log.warn("The specified class \"{}\" was not removed because it " + |
| 114 | "was not present.", service.getClass().toString()); |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | return restartServer(listeningPort); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean containsService(Class<BindableService> serviceClass) { |
| 123 | return registeredServices.containsKey(serviceClass); |
| 124 | } |
| 125 | |
| 126 | private void setProperties(ComponentContext context) { |
| 127 | Dictionary<String, Object> properties = context.getProperties(); |
| 128 | String listeningPort = get(properties, PORT_PROPERTY_NAME); |
| 129 | this.listeningPort = listeningPort == null ? DEFAULT_SERVER_PORT : |
| 130 | Integer.parseInt(listeningPort.trim()); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Attempts a graceful shutdown allowing {@code timeLimitSeconds} to elapse |
| 135 | * before forcing a shutdown. |
| 136 | * |
| 137 | * @param timeLimitSeconds time before a shutdown is forced in seconds |
| 138 | * @return true if the server is terminated, false otherwise |
| 139 | */ |
| 140 | private boolean attemptGracefulShutdownThenForce(int timeLimitSeconds) { |
| 141 | if (!server.isShutdown()) { |
| 142 | server.shutdown(); |
| 143 | } |
| 144 | try { |
| 145 | /*This is not conditional in case the server is shutdown but |
| 146 | handling requests submitted before shutdown was called.*/ |
| 147 | server.awaitTermination(timeLimitSeconds, TimeUnit.SECONDS); |
| 148 | } catch (InterruptedException e) { |
| 149 | log.error("Awaiting server termination failed with error {}", |
| 150 | e.getMessage()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 151 | Thread.currentThread().interrupt(); |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 152 | } |
| 153 | if (!server.isTerminated()) { |
| 154 | server.shutdownNow(); |
| 155 | try { |
| 156 | server.awaitTermination(10, TimeUnit.MILLISECONDS); |
| 157 | } catch (InterruptedException e) { |
| 158 | log.error("Server failed to terminate as expected with error" + |
| 159 | " {}", e.getMessage()); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 160 | Thread.currentThread().interrupt(); |
Aaron Kruglikov | ae7e3b8 | 2017-05-03 14:13:53 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | return server.isTerminated(); |
| 164 | } |
| 165 | |
| 166 | private boolean restartServer(int port) { |
| 167 | if (!attemptGracefulShutdownThenForce(DEFAULT_SHUTDOWN_TIME)) { |
| 168 | log.error("Shutdown failed, the previous server may still be" + |
| 169 | " active."); |
| 170 | } |
| 171 | return createServerAndStart(port); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Creates a server with the set of registered services on the specified |
| 176 | * port. |
| 177 | * |
| 178 | * @param port the port on which this server will listen |
| 179 | * @return true if the server was started successfully, false otherwise |
| 180 | */ |
| 181 | private boolean createServerAndStart(int port) { |
| 182 | |
| 183 | ServerBuilder serverBuilder = |
| 184 | ServerBuilder.forPort(port); |
| 185 | synchronized (registeredServices) { |
| 186 | registeredServices.values().forEach( |
| 187 | service -> serverBuilder.addService(service)); |
| 188 | } |
| 189 | server = serverBuilder.build(); |
| 190 | try { |
| 191 | server.start(); |
| 192 | } catch (IllegalStateException e) { |
| 193 | log.error("The server could not be started because an existing " + |
| 194 | "server is already running: {}", e.getMessage()); |
| 195 | return false; |
| 196 | } catch (IOException e) { |
| 197 | log.error("The server could not be started due to a failure to " + |
| 198 | "bind: {} ", e.getMessage()); |
| 199 | return false; |
| 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | } |