blob: 75ef63491faf7db4fda5549f8a32f0ce62d07612 [file] [log] [blame]
onkarkundargi72cfd362020-02-27 12:34:37 +05301/*
Scott Bakerf04b6392020-03-20 08:29:46 -07002 * Copyright 2018-present Open Networking Foundation
onkarkundargi72cfd362020-02-27 12:34:37 +05303 *
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 *
Scott Bakerf04b6392020-03-20 08:29:46 -07008 * http://www.apache.org/licenses/LICENSE-2.0
onkarkundargi72cfd362020-02-27 12:34:37 +05309 *
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.
onkarkundargi72cfd362020-02-27 12:34:37 +053015 */
16
Scott Bakerf04b6392020-03-20 08:29:46 -070017// Implements a server for nem-ondemand-proxy
18package proxy
onkarkundargi72cfd362020-02-27 12:34:37 +053019
20import (
21 "context"
22 "github.com/opencord/nem-ondemand-proxy/protos/nem_ondemand_api"
23 "google.golang.org/grpc"
24 "google.golang.org/grpc/reflection"
25 "log"
26 "net"
27)
28
onkarkundargi72cfd362020-02-27 12:34:37 +053029//server is used to implement the grpc server for the proxy
30type server struct {
Scott Bakerf04b6392020-03-20 08:29:46 -070031 handler *OnDemandHandler
32 grpcServer *grpc.Server
33}
34
35func NewOnDemandServer(handler *OnDemandHandler) *server {
36 s := &server{}
37
38 s.grpcServer = grpc.NewServer()
39 s.handler = handler
40
41 nem_ondemand_api.RegisterNemServiceServer(s.grpcServer, s)
42 reflection.Register(s.grpcServer)
43
44 return s
45}
46
47func (s *server) StartServing() error {
48 lis, err := net.Listen("tcp", GlobalConfig.Local)
49 if err != nil {
50 log.Fatalf("failed to listen: %v", err)
51 }
52
53 if err := s.grpcServer.Serve(lis); err != nil {
54 return err
55 }
56
57 return nil
onkarkundargi72cfd362020-02-27 12:34:37 +053058}
59
60func (s *server) OmciTest(ctx context.Context, id *nem_ondemand_api.OnuID) (*nem_ondemand_api.ResponseTest, error) {
61 log.Printf("Request Received from operator client: %s", id.Id)
Scott Bakerf04b6392020-03-20 08:29:46 -070062 resp, err := s.handler.HandleRequest(&id.Id)
onkarkundargi72cfd362020-02-27 12:34:37 +053063 if err != nil {
64 log.Printf("%s", err)
65 return nil, err
66 }
67 log.Printf("Result received from voltha-grpc-client: %s", resp.String())
68 if len(resp.String()) > 0 {
69 return &nem_ondemand_api.ResponseTest{Result: resp.String()}, nil
70 }
71 return &nem_ondemand_api.ResponseTest{Result: "FAILURE"}, nil
72}