blob: 7796b83995883f86ad1e15bf4a0bbdee97b2fffc [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 */
16package grpc
17
18import (
19 "context"
20 "fmt"
Scott Bakerce767002019-10-23 13:30:24 -070021 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Baker2c1c4822019-10-16 11:02:41 -070022 "google.golang.org/grpc"
Scott Baker104b67d2019-10-29 15:56:27 -070023 "google.golang.org/grpc/codes"
Scott Baker2c1c4822019-10-16 11:02:41 -070024 "google.golang.org/grpc/credentials"
Scott Baker104b67d2019-10-29 15:56:27 -070025 "google.golang.org/grpc/status"
Scott Baker2c1c4822019-10-16 11:02:41 -070026 "net"
27)
28
29/*
30To add a GRPC server to your existing component simply follow these steps:
31
321. Create a server instance by passing the host and port where it should run and optionally add certificate information
33
34 e.g.
35 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
36
372. Create a function that will register your service with the GRPC server
38
39 e.g.
40 f := func(gs *grpc.Server) {
41 voltha.RegisterVolthaReadOnlyServiceServer(
42 gs,
43 core.NewReadOnlyServiceHandler(s.root),
44 )
45 }
46
473. Add the service to the server
48
49 e.g.
50 s.server.AddService(f)
51
524. Start the server
53
54 s.server.Start(ctx)
55*/
56
Scott Baker104b67d2019-10-29 15:56:27 -070057// Interface allows probes to be attached to server
58// A probe must support the IsReady() method
59type ReadyProbe interface {
60 IsReady() bool
61}
62
Scott Baker2c1c4822019-10-16 11:02:41 -070063type GrpcServer struct {
64 gs *grpc.Server
65 address string
66 port int
67 secure bool
68 services []func(*grpc.Server)
Scott Baker104b67d2019-10-29 15:56:27 -070069 probe ReadyProbe // optional
Scott Baker2c1c4822019-10-16 11:02:41 -070070
71 *GrpcSecurity
72}
73
74/*
75Instantiate a GRPC server data structure
76*/
77func NewGrpcServer(
78 address string,
79 port int,
80 certs *GrpcSecurity,
81 secure bool,
Scott Bakerdefa2bf2019-11-08 12:03:56 -080082 probe ReadyProbe,
Scott Baker2c1c4822019-10-16 11:02:41 -070083) *GrpcServer {
84 server := &GrpcServer{
85 address: address,
86 port: port,
87 secure: secure,
88 GrpcSecurity: certs,
Scott Bakerdefa2bf2019-11-08 12:03:56 -080089 probe: probe,
Scott Baker2c1c4822019-10-16 11:02:41 -070090 }
91 return server
92}
93
94/*
95Start prepares the GRPC server and starts servicing requests
96*/
97func (s *GrpcServer) Start(ctx context.Context) {
98
99 host := fmt.Sprintf("%s:%d", s.address, s.port)
100
101 lis, err := net.Listen("tcp", host)
102 if err != nil {
103 log.Fatalf("failed to listen: %v", err)
104 }
105
106 if s.secure && s.GrpcSecurity != nil {
107 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
108 if err != nil {
109 log.Fatalf("could not load TLS keys: %s", err)
110 }
Scott Baker104b67d2019-10-29 15:56:27 -0700111 s.gs = grpc.NewServer(grpc.Creds(creds),
112 withServerUnaryInterceptor(s))
Scott Baker2c1c4822019-10-16 11:02:41 -0700113
114 } else {
115 log.Info("starting-insecure-grpc-server")
Scott Baker104b67d2019-10-29 15:56:27 -0700116 s.gs = grpc.NewServer(withServerUnaryInterceptor(s))
Scott Baker2c1c4822019-10-16 11:02:41 -0700117 }
118
119 // Register all required services
120 for _, service := range s.services {
121 service(s.gs)
122 }
123
124 if err := s.gs.Serve(lis); err != nil {
125 log.Fatalf("failed to serve: %v\n", err)
126 }
127}
128
Scott Baker104b67d2019-10-29 15:56:27 -0700129func withServerUnaryInterceptor(s *GrpcServer) grpc.ServerOption {
130 return grpc.UnaryInterceptor(mkServerInterceptor(s))
131}
132
133// Make a serverInterceptor for the given GrpcServer
134// This interceptor will check whether there is an attached probe,
135// and if that probe indicates NotReady, then an UNAVAILABLE
136// response will be returned.
137func mkServerInterceptor(s *GrpcServer) func(ctx context.Context,
138 req interface{},
139 info *grpc.UnaryServerInfo,
140 handler grpc.UnaryHandler) (interface{}, error) {
141
142 return func(ctx context.Context,
143 req interface{},
144 info *grpc.UnaryServerInfo,
145 handler grpc.UnaryHandler) (interface{}, error) {
146
147 if (s.probe != nil) && (!s.probe.IsReady()) {
148 log.Warnf("Grpc request received while not ready %v", req)
149 return nil, status.Error(codes.Unavailable, "system is not ready")
150 }
151
152 // Calls the handler
153 h, err := handler(ctx, req)
154
155 return h, err
156 }
157}
158
Scott Baker2c1c4822019-10-16 11:02:41 -0700159/*
160Stop servicing GRPC requests
161*/
162func (s *GrpcServer) Stop() {
163 if s.gs != nil {
164 s.gs.Stop()
165 }
166}
167
168/*
169AddService appends a generic service request function
170*/
171func (s *GrpcServer) AddService(
172 registerFunction func(*grpc.Server),
173) {
174 s.services = append(s.services, registerFunction)
175}