Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package grpc |
| 17 | |
| 18 | import ( |
| 19 | "context" |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 20 | grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" |
| 21 | grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing" |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | "google.golang.org/grpc" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc/codes" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 25 | "google.golang.org/grpc/credentials" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 26 | "google.golang.org/grpc/status" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 27 | "net" |
| 28 | ) |
| 29 | |
| 30 | /* |
| 31 | To add a GRPC server to your existing component simply follow these steps: |
| 32 | |
| 33 | 1. Create a server instance by passing the host and port where it should run and optionally add certificate information |
| 34 | |
| 35 | e.g. |
| 36 | s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false) |
| 37 | |
| 38 | 2. Create a function that will register your service with the GRPC server |
| 39 | |
| 40 | e.g. |
| 41 | f := func(gs *grpc.Server) { |
| 42 | voltha.RegisterVolthaReadOnlyServiceServer( |
| 43 | gs, |
| 44 | core.NewReadOnlyServiceHandler(s.root), |
| 45 | ) |
| 46 | } |
| 47 | |
| 48 | 3. Add the service to the server |
| 49 | |
| 50 | e.g. |
| 51 | s.server.AddService(f) |
| 52 | |
| 53 | 4. Start the server |
| 54 | |
| 55 | s.server.Start(ctx) |
| 56 | */ |
| 57 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 58 | // Interface allows probes to be attached to server |
| 59 | // A probe must support the IsReady() method |
| 60 | type ReadyProbe interface { |
| 61 | IsReady() bool |
| 62 | } |
| 63 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 64 | type GrpcServer struct { |
| 65 | gs *grpc.Server |
| 66 | address string |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 67 | secure bool |
| 68 | services []func(*grpc.Server) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 69 | probe ReadyProbe // optional |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 70 | |
| 71 | *GrpcSecurity |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | Instantiate a GRPC server data structure |
| 76 | */ |
| 77 | func NewGrpcServer( |
| 78 | address string, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 79 | certs *GrpcSecurity, |
| 80 | secure bool, |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 81 | probe ReadyProbe, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 82 | ) *GrpcServer { |
| 83 | server := &GrpcServer{ |
| 84 | address: address, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 85 | secure: secure, |
| 86 | GrpcSecurity: certs, |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 87 | probe: probe, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 88 | } |
| 89 | return server |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | Start prepares the GRPC server and starts servicing requests |
| 94 | */ |
| 95 | func (s *GrpcServer) Start(ctx context.Context) { |
| 96 | |
Neha Sharma | dd9af39 | 2020-04-28 09:03:57 +0000 | [diff] [blame] | 97 | lis, err := net.Listen("tcp", s.address) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 98 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 99 | logger.Fatalf(ctx, "failed to listen: %v", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 102 | // Use Intercepters to automatically inject and publish Open Tracing Spans by this GRPC server |
| 103 | serverOptions := []grpc.ServerOption{ |
| 104 | grpc.StreamInterceptor(grpc_middleware.ChainStreamServer( |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 105 | grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 106 | )), |
| 107 | grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer( |
Girish Kumar | e9d35bb | 2020-08-18 06:47:59 +0000 | [diff] [blame] | 108 | grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 109 | mkServerInterceptor(s), |
| 110 | ))} |
| 111 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 112 | if s.secure && s.GrpcSecurity != nil { |
| 113 | creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile) |
| 114 | if err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 115 | logger.Fatalf(ctx, "could not load TLS keys: %s", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 116 | } |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 117 | |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 118 | serverOptions = append(serverOptions, grpc.Creds(creds)) |
| 119 | s.gs = grpc.NewServer(serverOptions...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 120 | } else { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 121 | logger.Info(ctx, "starting-insecure-grpc-server") |
Girish Kumar | 7424065 | 2020-07-10 11:54:28 +0000 | [diff] [blame] | 122 | s.gs = grpc.NewServer(serverOptions...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Register all required services |
| 126 | for _, service := range s.services { |
| 127 | service(s.gs) |
| 128 | } |
| 129 | |
| 130 | if err := s.gs.Serve(lis); err != nil { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 131 | logger.Fatalf(ctx, "failed to serve: %v\n", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 135 | // Make a serverInterceptor for the given GrpcServer |
| 136 | // This interceptor will check whether there is an attached probe, |
| 137 | // and if that probe indicates NotReady, then an UNAVAILABLE |
| 138 | // response will be returned. |
| 139 | func mkServerInterceptor(s *GrpcServer) func(ctx context.Context, |
| 140 | req interface{}, |
| 141 | info *grpc.UnaryServerInfo, |
| 142 | handler grpc.UnaryHandler) (interface{}, error) { |
| 143 | |
| 144 | return func(ctx context.Context, |
| 145 | req interface{}, |
| 146 | info *grpc.UnaryServerInfo, |
| 147 | handler grpc.UnaryHandler) (interface{}, error) { |
| 148 | |
| 149 | if (s.probe != nil) && (!s.probe.IsReady()) { |
Neha Sharma | 94f16a9 | 2020-06-26 04:17:55 +0000 | [diff] [blame] | 150 | logger.Warnf(ctx, "Grpc request received while not ready %v", req) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 151 | return nil, status.Error(codes.Unavailable, "system is not ready") |
| 152 | } |
| 153 | |
| 154 | // Calls the handler |
| 155 | h, err := handler(ctx, req) |
| 156 | |
| 157 | return h, err |
| 158 | } |
| 159 | } |
| 160 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 161 | /* |
| 162 | Stop servicing GRPC requests |
| 163 | */ |
| 164 | func (s *GrpcServer) Stop() { |
| 165 | if s.gs != nil { |
| 166 | s.gs.Stop() |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | AddService appends a generic service request function |
| 172 | */ |
| 173 | func (s *GrpcServer) AddService( |
| 174 | registerFunction func(*grpc.Server), |
| 175 | ) { |
| 176 | s.services = append(s.services, registerFunction) |
| 177 | } |