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 | ce76700 | 2019-10-23 13:30:24 -0700 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 22 | "google.golang.org/grpc" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 23 | "google.golang.org/grpc/codes" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc/credentials" |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 25 | "google.golang.org/grpc/status" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 26 | "net" |
| 27 | ) |
| 28 | |
| 29 | /* |
| 30 | To add a GRPC server to your existing component simply follow these steps: |
| 31 | |
| 32 | 1. 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 | |
| 37 | 2. 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 | |
| 47 | 3. Add the service to the server |
| 48 | |
| 49 | e.g. |
| 50 | s.server.AddService(f) |
| 51 | |
| 52 | 4. Start the server |
| 53 | |
| 54 | s.server.Start(ctx) |
| 55 | */ |
| 56 | |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 57 | // Interface allows probes to be attached to server |
| 58 | // A probe must support the IsReady() method |
| 59 | type ReadyProbe interface { |
| 60 | IsReady() bool |
| 61 | } |
| 62 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 63 | type GrpcServer struct { |
| 64 | gs *grpc.Server |
| 65 | address string |
| 66 | port int |
| 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, |
| 79 | port int, |
| 80 | certs *GrpcSecurity, |
| 81 | secure bool, |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 82 | probe ReadyProbe, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 83 | ) *GrpcServer { |
| 84 | server := &GrpcServer{ |
| 85 | address: address, |
| 86 | port: port, |
| 87 | secure: secure, |
| 88 | GrpcSecurity: certs, |
Scott Baker | defa2bf | 2019-11-08 12:03:56 -0800 | [diff] [blame] | 89 | probe: probe, |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 90 | } |
| 91 | return server |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | Start prepares the GRPC server and starts servicing requests |
| 96 | */ |
| 97 | func (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 Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 111 | s.gs = grpc.NewServer(grpc.Creds(creds), |
| 112 | withServerUnaryInterceptor(s)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 113 | |
| 114 | } else { |
| 115 | log.Info("starting-insecure-grpc-server") |
Scott Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 116 | s.gs = grpc.NewServer(withServerUnaryInterceptor(s)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 117 | } |
| 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 Baker | 104b67d | 2019-10-29 15:56:27 -0700 | [diff] [blame] | 129 | func 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. |
| 137 | func 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 159 | /* |
| 160 | Stop servicing GRPC requests |
| 161 | */ |
| 162 | func (s *GrpcServer) Stop() { |
| 163 | if s.gs != nil { |
| 164 | s.gs.Stop() |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | AddService appends a generic service request function |
| 170 | */ |
| 171 | func (s *GrpcServer) AddService( |
| 172 | registerFunction func(*grpc.Server), |
| 173 | ) { |
| 174 | s.services = append(s.services, registerFunction) |
| 175 | } |