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