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" |
| 20 | "fmt" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 21 | "google.golang.org/grpc" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 22 | "google.golang.org/grpc/codes" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 23 | "google.golang.org/grpc/credentials" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc/status" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 25 | "net" |
| 26 | ) |
| 27 | |
| 28 | /* |
| 29 | To add a GRPC server to your existing component simply follow these steps: |
| 30 | |
| 31 | 1. 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 | |
| 36 | 2. 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 | |
| 46 | 3. Add the service to the server |
| 47 | |
| 48 | e.g. |
| 49 | s.server.AddService(f) |
| 50 | |
| 51 | 4. Start the server |
| 52 | |
| 53 | s.server.Start(ctx) |
| 54 | */ |
| 55 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 56 | // Interface allows probes to be attached to server |
| 57 | // A probe must support the IsReady() method |
| 58 | type ReadyProbe interface { |
| 59 | IsReady() bool |
| 60 | } |
| 61 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 62 | type GrpcServer struct { |
| 63 | gs *grpc.Server |
| 64 | address string |
| 65 | port int |
| 66 | secure bool |
| 67 | services []func(*grpc.Server) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 68 | probe ReadyProbe // optional |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 69 | |
| 70 | *GrpcSecurity |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | Instantiate a GRPC server data structure |
| 75 | */ |
| 76 | func NewGrpcServer( |
| 77 | address string, |
| 78 | port int, |
| 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, |
| 85 | port: port, |
| 86 | secure: secure, |
| 87 | GrpcSecurity: certs, |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 88 | probe: probe, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 89 | } |
| 90 | return server |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | Start prepares the GRPC server and starts servicing requests |
| 95 | */ |
| 96 | func (s *GrpcServer) Start(ctx context.Context) { |
| 97 | |
| 98 | host := fmt.Sprintf("%s:%d", s.address, s.port) |
| 99 | |
| 100 | lis, err := net.Listen("tcp", host) |
| 101 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 102 | logger.Fatalf("failed to listen: %v", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | if s.secure && s.GrpcSecurity != nil { |
| 106 | creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile) |
| 107 | if err != nil { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 108 | logger.Fatalf("could not load TLS keys: %s", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 109 | } |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 110 | s.gs = grpc.NewServer(grpc.Creds(creds), |
| 111 | withServerUnaryInterceptor(s)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 112 | |
| 113 | } else { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 114 | logger.Info("starting-insecure-grpc-server") |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 115 | s.gs = grpc.NewServer(withServerUnaryInterceptor(s)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 116 | } |
| 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 { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 124 | logger.Fatalf("failed to serve: %v\n", err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 128 | func 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. |
| 136 | func 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()) { |
Girish Kumar | e6f45e8 | 2020-03-20 10:46:54 +0000 | [diff] [blame] | 147 | logger.Warnf("Grpc request received while not ready %v", req) |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 148 | 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 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 158 | /* |
| 159 | Stop servicing GRPC requests |
| 160 | */ |
| 161 | func (s *GrpcServer) Stop() { |
| 162 | if s.gs != nil { |
| 163 | s.gs.Stop() |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | AddService appends a generic service request function |
| 169 | */ |
| 170 | func (s *GrpcServer) AddService( |
| 171 | registerFunction func(*grpc.Server), |
| 172 | ) { |
| 173 | s.services = append(s.services, registerFunction) |
| 174 | } |