blob: 4c95aa175c7b96c4f5875c0bffc22955940b6d55 [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"
23 "google.golang.org/grpc/credentials"
24 "net"
25)
26
27/*
28To add a GRPC server to your existing component simply follow these steps:
29
301. Create a server instance by passing the host and port where it should run and optionally add certificate information
31
32 e.g.
33 s.server = server.NewGrpcServer(s.config.GrpcHost, s.config.GrpcPort, nil, false)
34
352. Create a function that will register your service with the GRPC server
36
37 e.g.
38 f := func(gs *grpc.Server) {
39 voltha.RegisterVolthaReadOnlyServiceServer(
40 gs,
41 core.NewReadOnlyServiceHandler(s.root),
42 )
43 }
44
453. Add the service to the server
46
47 e.g.
48 s.server.AddService(f)
49
504. Start the server
51
52 s.server.Start(ctx)
53*/
54
55type GrpcServer struct {
56 gs *grpc.Server
57 address string
58 port int
59 secure bool
60 services []func(*grpc.Server)
61
62 *GrpcSecurity
63}
64
65/*
66Instantiate a GRPC server data structure
67*/
68func NewGrpcServer(
69 address string,
70 port int,
71 certs *GrpcSecurity,
72 secure bool,
73) *GrpcServer {
74 server := &GrpcServer{
75 address: address,
76 port: port,
77 secure: secure,
78 GrpcSecurity: certs,
79 }
80 return server
81}
82
83/*
84Start prepares the GRPC server and starts servicing requests
85*/
86func (s *GrpcServer) Start(ctx context.Context) {
87
88 host := fmt.Sprintf("%s:%d", s.address, s.port)
89
90 lis, err := net.Listen("tcp", host)
91 if err != nil {
92 log.Fatalf("failed to listen: %v", err)
93 }
94
95 if s.secure && s.GrpcSecurity != nil {
96 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
97 if err != nil {
98 log.Fatalf("could not load TLS keys: %s", err)
99 }
100 s.gs = grpc.NewServer(grpc.Creds(creds))
101
102 } else {
103 log.Info("starting-insecure-grpc-server")
104 s.gs = grpc.NewServer()
105 }
106
107 // Register all required services
108 for _, service := range s.services {
109 service(s.gs)
110 }
111
112 if err := s.gs.Serve(lis); err != nil {
113 log.Fatalf("failed to serve: %v\n", err)
114 }
115}
116
117/*
118Stop servicing GRPC requests
119*/
120func (s *GrpcServer) Stop() {
121 if s.gs != nil {
122 s.gs.Stop()
123 }
124}
125
126/*
127AddService appends a generic service request function
128*/
129func (s *GrpcServer) AddService(
130 registerFunction func(*grpc.Server),
131) {
132 s.services = append(s.services, registerFunction)
133}