blob: 43f2912e028771f9ae0329fc38529ea8f6182ef7 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
Stephane Barbarie7dfae952018-08-13 11:43:17 -040016package grpc
17
18import (
Stephane Barbarie7dfae952018-08-13 11:43:17 -040019 "context"
khenaidoo5aadea02018-11-07 14:30:11 -050020 "fmt"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040021 "google.golang.org/grpc"
Scott Bakeree6a0872019-10-29 15:59:52 -070022 "google.golang.org/grpc/codes"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040023 "google.golang.org/grpc/credentials"
Scott Bakeree6a0872019-10-29 15:59:52 -070024 "google.golang.org/grpc/status"
khenaidoobf6e7bb2018-08-14 22:27:29 -040025 "net"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040026)
27
28/*
29To add a GRPC server to your existing component simply follow these steps:
30
311. Create a server instance by passing the host and port where it should run and optionally add certificate information
32
33 e.g.
34 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
35
362. Create a function that will register your service with the GRPC server
37
38 e.g.
39 f := func(gs *grpc.Server) {
40 voltha.RegisterVolthaReadOnlyServiceServer(
41 gs,
42 core.NewReadOnlyServiceHandler(s.root),
43 )
44 }
45
463. Add the service to the server
47
48 e.g.
49 s.server.AddService(f)
50
514. Start the server
52
53 s.server.Start(ctx)
khenaidoobf6e7bb2018-08-14 22:27:29 -040054*/
Stephane Barbarie7dfae952018-08-13 11:43:17 -040055
Scott Bakeree6a0872019-10-29 15:59:52 -070056// Interface allows probes to be attached to server
57// A probe must support the IsReady() method
58type ReadyProbe interface {
59 IsReady() bool
60}
61
Stephane Barbarie7dfae952018-08-13 11:43:17 -040062type GrpcServer struct {
63 gs *grpc.Server
64 address string
65 port int
66 secure bool
67 services []func(*grpc.Server)
Scott Bakeree6a0872019-10-29 15:59:52 -070068 probe ReadyProbe // optional
Stephane Barbarie7dfae952018-08-13 11:43:17 -040069
70 *GrpcSecurity
71}
72
73/*
74Instantiate a GRPC server data structure
75*/
76func NewGrpcServer(
77 address string,
78 port int,
79 certs *GrpcSecurity,
80 secure bool,
Scott Bakeree6a0872019-10-29 15:59:52 -070081 probe ReadyProbe,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040082) *GrpcServer {
83 server := &GrpcServer{
84 address: address,
85 port: port,
86 secure: secure,
87 GrpcSecurity: certs,
Scott Bakeree6a0872019-10-29 15:59:52 -070088 probe: probe,
Stephane Barbarie7dfae952018-08-13 11:43:17 -040089 }
90 return server
91}
92
93/*
94Start prepares the GRPC server and starts servicing requests
95*/
96func (s *GrpcServer) Start(ctx context.Context) {
khenaidoo5aadea02018-11-07 14:30:11 -050097
98 host := fmt.Sprintf("%s:%d", s.address, s.port)
Stephane Barbarie7dfae952018-08-13 11:43:17 -040099
100 lis, err := net.Listen("tcp", host)
101 if err != nil {
Scott Baker432f9be2020-03-26 11:56:30 -0700102 logger.Fatalf("failed to listen: %v", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400103 }
104
105 if s.secure && s.GrpcSecurity != nil {
106 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
107 if err != nil {
Scott Baker432f9be2020-03-26 11:56:30 -0700108 logger.Fatalf("could not load TLS keys: %s", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400109 }
Scott Bakeree6a0872019-10-29 15:59:52 -0700110 s.gs = grpc.NewServer(grpc.Creds(creds),
111 withServerUnaryInterceptor(s))
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400112
113 } else {
Scott Baker432f9be2020-03-26 11:56:30 -0700114 logger.Info("starting-insecure-grpc-server")
Scott Bakeree6a0872019-10-29 15:59:52 -0700115 s.gs = grpc.NewServer(withServerUnaryInterceptor(s))
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400116 }
117
118 // Register all required services
119 for _, service := range s.services {
120 service(s.gs)
121 }
122
123 if err := s.gs.Serve(lis); err != nil {
Scott Baker432f9be2020-03-26 11:56:30 -0700124 logger.Fatalf("failed to serve: %v\n", err)
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400125 }
126}
127
Scott Bakeree6a0872019-10-29 15:59:52 -0700128func withServerUnaryInterceptor(s *GrpcServer) grpc.ServerOption {
129 return grpc.UnaryInterceptor(mkServerInterceptor(s))
130}
131
132// Make a serverInterceptor for the given GrpcServer
133// This interceptor will check whether there is an attached probe,
134// and if that probe indicates NotReady, then an UNAVAILABLE
135// response will be returned.
136func mkServerInterceptor(s *GrpcServer) func(ctx context.Context,
137 req interface{},
138 info *grpc.UnaryServerInfo,
139 handler grpc.UnaryHandler) (interface{}, error) {
140
141 return func(ctx context.Context,
142 req interface{},
143 info *grpc.UnaryServerInfo,
144 handler grpc.UnaryHandler) (interface{}, error) {
145
146 if (s.probe != nil) && (!s.probe.IsReady()) {
Scott Baker432f9be2020-03-26 11:56:30 -0700147 logger.Warnf("Grpc request received while not ready %v", req)
Scott Bakeree6a0872019-10-29 15:59:52 -0700148 return nil, status.Error(codes.Unavailable, "system is not ready")
149 }
150
151 // Calls the handler
152 h, err := handler(ctx, req)
153
154 return h, err
155 }
156}
157
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400158/*
159Stop servicing GRPC requests
160*/
161func (s *GrpcServer) Stop() {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700162 if s.gs != nil {
163 s.gs.Stop()
164 }
Stephane Barbarie7dfae952018-08-13 11:43:17 -0400165}
166
167/*
168AddService appends a generic service request function
169*/
170func (s *GrpcServer) AddService(
171 registerFunction func(*grpc.Server),
172) {
173 s.services = append(s.services, registerFunction)
174}