blob: 6c952a1f8ede4e484585dedfc4802a5b377a50e5 [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001/*
2 * Copyright 2017-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 Barbarie35595062018-02-08 08:34:39 -050016package grpc
17
18import (
19 "net"
20
21 "context"
22 "github.com/opencord/voltha/ponsim/v2/common"
23 "github.com/opencord/voltha/ponsim/v2/core"
24 "github.com/opencord/voltha/ponsim/v2/grpc/nbi"
25 "github.com/opencord/voltha/ponsim/v2/grpc/sbi"
26 "github.com/opencord/voltha/protos/go/bal"
27 "github.com/opencord/voltha/protos/go/ponsim"
28 "github.com/opencord/voltha/protos/go/voltha"
29 "google.golang.org/grpc"
30 "google.golang.org/grpc/credentials"
31 "strconv"
32 "strings"
33)
34
35type GrpcServer struct {
36 gs *grpc.Server
37 address string
38 port int32
39 secure bool
40 services []func(*grpc.Server)
41
42 *GrpcSecurity
43}
44
45/*
46Instantiate a GRPC server data structure
47*/
48func NewGrpcServer(
49 address string,
50 port int32,
51 certs *GrpcSecurity,
52 secure bool,
53) *GrpcServer {
54 server := &GrpcServer{
55 address: address,
56 port: port,
57 secure: secure,
58 GrpcSecurity: certs,
59 }
60 return server
61}
62
63/*
64Start prepares the GRPC server and starts servicing requests
65*/
66func (s *GrpcServer) Start(ctx context.Context) {
67 host := strings.Join([]string{
68 s.address,
69 strconv.Itoa(int(s.port)),
70 }, ":")
71
72 lis, err := net.Listen("tcp", host)
73 if err != nil {
74 common.Logger().Fatalf("failed to listen: %v", err)
75 }
76
77 if s.secure {
78 creds, err := credentials.NewServerTLSFromFile(s.CertFile, s.KeyFile)
79 if err != nil {
80 common.Logger().Fatalf("could not load TLS keys: %s", err)
81 }
82 s.gs = grpc.NewServer(grpc.Creds(creds))
83
84 } else {
85 common.Logger().Println("In DEFAULT\n")
86 s.gs = grpc.NewServer()
87 }
88
89 // Register all required services
90 for _, service := range s.services {
91 service(s.gs)
92 }
93
94 if err := s.gs.Serve(lis); err != nil {
95 common.Logger().Fatalf("failed to serve: %v\n", err)
96 }
97}
98
99/*
100Stop servicing GRPC requests
101*/
102func (s *GrpcServer) Stop() {
103 s.gs.Stop()
104}
105
106/*
107AddService appends a generic service request function
108*/
109func (s *GrpcServer) AddService(
110 registerFunction func(*grpc.Server, interface{}),
111 handler interface{},
112) {
113 s.services = append(s.services, func(gs *grpc.Server) { registerFunction(gs, handler) })
114}
115
116/*
117AddPonSimService appends service request functions for PonSim devices
118*/
119func (s *GrpcServer) AddPonSimService(device core.PonSimInterface) {
120 s.services = append(
121 s.services,
122 func(gs *grpc.Server) {
123 voltha.RegisterPonSimServer(gs, nbi.NewPonSimHandler(device))
124 },
125 )
126}
127
128/*
129AddCommonService appends service request functions common to all PonSim devices
130*/
131func (s *GrpcServer) AddCommonService(device core.PonSimInterface) {
132 s.services = append(
133 s.services,
134 func(gs *grpc.Server) {
135 ponsim.RegisterPonSimCommonServer(gs, sbi.NewPonSimCommonHandler(device))
136 },
137 )
138}
139
140/*
141AddOltService appends service request functions specific to OLT devices
142*/
143func (s *GrpcServer) AddOltService(device core.PonSimInterface) {
144 s.services = append(
145 s.services,
146 func(gs *grpc.Server) {
147 ponsim.RegisterPonSimOltServer(
148 gs,
149 sbi.NewPonSimOltHandler(device.(*core.PonSimOltDevice)),
150 )
151 },
152 )
153}
154
155/*
156AddXPonService appends service request functions specific to XPonSim
157*/
158func (s *GrpcServer) AddXPonService() {
159 s.services = append(
160 s.services,
161 func(gs *grpc.Server) {
162 voltha.RegisterXPonSimServer(gs, nbi.NewXPonSimHandler())
163 },
164 )
165}
166
167/*
168AddBalService appends service request functions specific to BAL
169*/
170func (s *GrpcServer) AddBalService() {
171 s.services = append(
172 s.services,
173 func(gs *grpc.Server) {
174 bal.RegisterBalServer(gs, nbi.NewBalHandler())
175 },
176 )
177}