blob: 1e25b84f021e605fac5a7c60807555ad9737c7a2 [file] [log] [blame]
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -07001/*
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
17package org.onosproject.protobuf.registry;
18
19import com.google.common.collect.Maps;
20import io.grpc.BindableService;
21import io.grpc.Server;
22import io.grpc.ServerBuilder;
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -070023import org.onosproject.protobuf.api.GrpcServiceRegistry;
24import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Deactivate;
28import org.osgi.service.component.annotations.Modified;
29import org.osgi.service.component.annotations.Property;
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -070030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.io.IOException;
34import java.util.Dictionary;
35import java.util.Map;
36import java.util.concurrent.TimeUnit;
Jian Li10d99622017-08-26 00:58:24 +090037import java.util.concurrent.atomic.AtomicBoolean;
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -070038
39import 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 Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(service = GrpcServiceRegistry.class)
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -070050public class GrpcServiceRegistryImpl implements GrpcServiceRegistry {
51
52 private static final int DEFAULT_SERVER_PORT = 64000;
53 private static final int DEFAULT_SHUTDOWN_TIME = 1;
Jian Li10d99622017-08-26 00:58:24 +090054 private static final AtomicBoolean SERVICES_MODIFIED_SINCE_START = new AtomicBoolean(false);
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -070055
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 */
68 @Property(name = PORT_PROPERTY_NAME, intValue = DEFAULT_SERVER_PORT,
69 label = "The port number which ONOS will use to host gRPC services.")
70 private int listeningPort = DEFAULT_SERVER_PORT;
71
72 @Activate
73 public void activate() {
74 log.info("Started");
75 }
76
77 @Deactivate
78 public void deactivate() {
79 attemptGracefulShutdownThenForce(DEFAULT_SHUTDOWN_TIME);
80 log.info("Stopped");
81 }
82
83 @Modified
84 public void modified(ComponentContext context) {
85 if (context != null) {
86 setProperties(context);
87 }
88 log.info("Connection was restarted to allow service to be added, " +
89 "this is a temporary workaround");
90 restartServer(listeningPort);
91 }
92
93 @Override
94 public boolean register(BindableService service) {
95 synchronized (registeredServices) {
96 if (!registeredServices.containsKey(service.getClass())) {
97 registeredServices.put(service.getClass(), service);
98 } else {
99 log.warn("The specified class \"{}\" was not added becuase an " +
100 "instance of the class is already registered.",
101 service.getClass().toString());
102 return false;
103 }
104 }
105 return restartServer(listeningPort);
106 }
107
108 @Override
109 public boolean unregister(BindableService service) {
110 synchronized (registeredServices) {
111 if (registeredServices.containsKey(service.getClass())) {
112 registeredServices.remove(service.getClass());
113 } else {
114 log.warn("The specified class \"{}\" was not removed because it " +
115 "was not present.", service.getClass().toString());
116 return false;
117 }
118 }
119 return restartServer(listeningPort);
120 }
121
122 @Override
123 public boolean containsService(Class<BindableService> serviceClass) {
124 return registeredServices.containsKey(serviceClass);
125 }
126
127 private void setProperties(ComponentContext context) {
128 Dictionary<String, Object> properties = context.getProperties();
129 String listeningPort = get(properties, PORT_PROPERTY_NAME);
130 this.listeningPort = listeningPort == null ? DEFAULT_SERVER_PORT :
131 Integer.parseInt(listeningPort.trim());
132 }
133
134 /**
135 * Attempts a graceful shutdown allowing {@code timeLimitSeconds} to elapse
136 * before forcing a shutdown.
137 *
138 * @param timeLimitSeconds time before a shutdown is forced in seconds
139 * @return true if the server is terminated, false otherwise
140 */
141 private boolean attemptGracefulShutdownThenForce(int timeLimitSeconds) {
142 if (!server.isShutdown()) {
143 server.shutdown();
144 }
145 try {
146 /*This is not conditional in case the server is shutdown but
147 handling requests submitted before shutdown was called.*/
148 server.awaitTermination(timeLimitSeconds, TimeUnit.SECONDS);
149 } catch (InterruptedException e) {
150 log.error("Awaiting server termination failed with error {}",
151 e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800152 Thread.currentThread().interrupt();
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -0700153 }
154 if (!server.isTerminated()) {
155 server.shutdownNow();
156 try {
157 server.awaitTermination(10, TimeUnit.MILLISECONDS);
158 } catch (InterruptedException e) {
159 log.error("Server failed to terminate as expected with error" +
160 " {}", e.getMessage());
Ray Milkey5c7d4882018-02-05 14:50:39 -0800161 Thread.currentThread().interrupt();
Aaron Kruglikovae7e3b82017-05-03 14:13:53 -0700162 }
163 }
164 return server.isTerminated();
165 }
166
167 private boolean restartServer(int port) {
168 if (!attemptGracefulShutdownThenForce(DEFAULT_SHUTDOWN_TIME)) {
169 log.error("Shutdown failed, the previous server may still be" +
170 " active.");
171 }
172 return createServerAndStart(port);
173 }
174
175 /**
176 * Creates a server with the set of registered services on the specified
177 * port.
178 *
179 * @param port the port on which this server will listen
180 * @return true if the server was started successfully, false otherwise
181 */
182 private boolean createServerAndStart(int port) {
183
184 ServerBuilder serverBuilder =
185 ServerBuilder.forPort(port);
186 synchronized (registeredServices) {
187 registeredServices.values().forEach(
188 service -> serverBuilder.addService(service));
189 }
190 server = serverBuilder.build();
191 try {
192 server.start();
193 } catch (IllegalStateException e) {
194 log.error("The server could not be started because an existing " +
195 "server is already running: {}", e.getMessage());
196 return false;
197 } catch (IOException e) {
198 log.error("The server could not be started due to a failure to " +
199 "bind: {} ", e.getMessage());
200 return false;
201 }
202 return true;
203 }
204}