blob: d5685f1d412127d09341b3d7bc732b7f5a405957 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
Stephane Barbarie7dfae952018-08-13 11:43:17 -040016package grpc
17
18import (
Stephane Barbarie7dfae952018-08-13 11:43:17 -040019 "context"
khenaidoo5aadea02018-11-07 14:30:11 -050020 "fmt"
khenaidoobf6e7bb2018-08-14 22:27:29 -040021 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040022 "google.golang.org/grpc"
23 "google.golang.org/grpc/credentials"
khenaidoobf6e7bb2018-08-14 22:27:29 -040024 "net"
Stephane Barbarie7dfae952018-08-13 11:43:17 -040025)
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)
khenaidoobf6e7bb2018-08-14 22:27:29 -040053*/
Stephane Barbarie7dfae952018-08-13 11:43:17 -040054
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) {
khenaidoo5aadea02018-11-07 14:30:11 -050087
88 host := fmt.Sprintf("%s:%d", s.address, s.port)
Stephane Barbarie7dfae952018-08-13 11:43:17 -040089
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 s.gs.Stop()
122}
123
124/*
125AddService appends a generic service request function
126*/
127func (s *GrpcServer) AddService(
128 registerFunction func(*grpc.Server),
129) {
130 s.services = append(s.services, registerFunction)
131}