Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [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 | |
| 17 | package api |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "net/http" |
| 22 | "sync" |
| 23 | |
| 24 | "github.com/grpc-ecosystem/grpc-gateway/runtime" |
| 25 | "github.com/opencord/bbsim/api/legacy" |
| 26 | "github.com/opencord/bbsim/internal/bbsim/devices" |
| 27 | "google.golang.org/grpc" |
| 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
| 30 | ) |
| 31 | |
| 32 | type BBSimLegacyServer struct { |
| 33 | } |
| 34 | |
| 35 | // Response Constants |
| 36 | const ( |
| 37 | RequestAccepted = "API request accepted" |
| 38 | OLTNotEnabled = "OLT not enabled" |
| 39 | RequestFailed = "API request failed" |
| 40 | SoftReboot = "soft-reboot" |
| 41 | HardReboot = "hard-reboot" |
| 42 | DeviceTypeOlt = "olt" |
| 43 | DeviceTypeOnu = "onu" |
| 44 | ) |
| 45 | |
| 46 | // OLTStatus method returns the OLT status. |
| 47 | func (s BBSimLegacyServer) OLTStatus(ctx context.Context, in *legacy.Empty) (*legacy.OLTStatusResponse, error) { |
| 48 | logger.Trace("OLTStatus request received") |
| 49 | |
| 50 | olt := devices.GetOLT() |
| 51 | oltInfo := &legacy.OLTStatusResponse{ |
| 52 | Olt: &legacy.OLTInfo{ |
| 53 | OltId: int64(olt.ID), |
| 54 | OltVendor: "BBSIM", |
| 55 | OltSerial: olt.SerialNumber, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 56 | //OltIp: getOltIP().String(), |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 57 | OltState: olt.OperState.Current(), |
| 58 | }, |
| 59 | } |
| 60 | |
| 61 | for _, nni := range olt.Nnis { |
| 62 | nniPortInfo := &legacy.PortInfo{ |
| 63 | PortType: "nni", |
| 64 | PortId: uint32(nni.ID), |
| 65 | PortState: nni.OperState.Current(), |
| 66 | } |
| 67 | oltInfo.Ports = append(oltInfo.Ports, nniPortInfo) |
| 68 | } |
| 69 | |
| 70 | for _, pon := range olt.Pons { |
| 71 | ponPortInfo := &legacy.PortInfo{ |
| 72 | PortType: "pon", |
| 73 | PortId: uint32(pon.ID), |
| 74 | PonPortMaxOnus: uint32(olt.NumOnuPerPon), |
| 75 | PonPortActiveOnus: uint32(olt.NumPon), |
| 76 | PortState: pon.OperState.Current(), |
| 77 | } |
| 78 | oltInfo.Ports = append(oltInfo.Ports, ponPortInfo) |
| 79 | } |
| 80 | |
| 81 | return oltInfo, nil |
| 82 | } |
| 83 | |
| 84 | // PortStatus method returns Port status. |
| 85 | func (s BBSimLegacyServer) PortStatus(ctx context.Context, in *legacy.PortInfo) (*legacy.Ports, error) { |
| 86 | logger.Trace("PortStatus() invoked") |
| 87 | ports := &legacy.Ports{} |
| 88 | switch portType := in.GetPortType(); portType { |
| 89 | case "pon": |
| 90 | portInfo, err := s.fetchPortDetail(in.PortId, portType) |
| 91 | if err != nil { |
| 92 | return ports, err |
| 93 | } |
| 94 | ports.Ports = append(ports.Ports, portInfo) |
| 95 | case "nni": |
| 96 | portInfo, _ := s.fetchPortDetail(in.PortId, portType) |
| 97 | ports.Ports = append(ports.Ports, portInfo) |
| 98 | default: |
| 99 | return &legacy.Ports{}, status.Errorf(codes.InvalidArgument, "Invalid port type") |
| 100 | } |
| 101 | |
| 102 | return ports, nil |
| 103 | } |
| 104 | |
| 105 | // ONUStatus method returns ONU status. |
| 106 | func (s BBSimLegacyServer) ONUStatus(ctx context.Context, in *legacy.ONURequest) (*legacy.ONUs, error) { |
| 107 | logger.Trace("ONUStatus request received") |
| 108 | onuInfo := &legacy.ONUs{} |
| 109 | |
| 110 | if in.GetOnu() != nil { |
| 111 | logger.Debugf("Received single ONU: %+v, %d\n", in.GetOnu().OnuId, in.GetOnu().PonPortId) |
| 112 | return s.handleONUStatusRequest(in.GetOnu()) |
| 113 | } |
| 114 | |
| 115 | logger.Debug("Received all ONUs status request") |
| 116 | |
| 117 | // Get status of all ONUs |
| 118 | olt := devices.GetOLT() |
| 119 | for _, p := range olt.Pons { |
| 120 | for _, o := range p.Onus { |
| 121 | onuInfo.Onus = append(onuInfo.Onus, copyONUInfo(o)) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return onuInfo, nil |
| 126 | } |
| 127 | |
| 128 | // ONUActivate method handles ONU activate requests from user. |
| 129 | func (s BBSimLegacyServer) ONUActivate(ctx context.Context, in *legacy.ONURequest) (*legacy.BBSimResponse, error) { |
| 130 | logger.Trace("ONUActivate request received") |
| 131 | logger.Error("Not implemented") |
| 132 | |
| 133 | return &legacy.BBSimResponse{StatusMsg: RequestAccepted}, nil |
| 134 | } |
| 135 | |
| 136 | // ONUDeactivate method handles ONU deactivation request. |
| 137 | func (s BBSimLegacyServer) ONUDeactivate(ctx context.Context, in *legacy.ONURequest) (*legacy.BBSimResponse, error) { |
| 138 | logger.Info("ONUDeactivate request received") |
| 139 | logger.Error("Not implemented") |
| 140 | |
| 141 | return &legacy.BBSimResponse{StatusMsg: RequestAccepted}, nil |
| 142 | } |
| 143 | |
| 144 | // GenerateONUAlarm RPC generates alarm for the onu |
| 145 | func (s BBSimLegacyServer) GenerateONUAlarm(ctx context.Context, in *legacy.ONUAlarmRequest) (*legacy.BBSimResponse, error) { |
| 146 | logger.Trace("GenerateONUAlarms() invoked") |
| 147 | logger.Error("Not implemented") |
| 148 | |
| 149 | return nil, nil |
| 150 | } |
| 151 | |
| 152 | // GenerateOLTAlarm RPC generates alarm for the OLT |
| 153 | func (s BBSimLegacyServer) GenerateOLTAlarm(ctx context.Context, in *legacy.OLTAlarmRequest) (*legacy.BBSimResponse, error) { |
| 154 | logger.Trace("GenerateOLTAlarm() invoked") |
| 155 | logger.Error("Not implemented") |
| 156 | |
| 157 | return &legacy.BBSimResponse{StatusMsg: RequestAccepted}, nil |
| 158 | } |
| 159 | |
| 160 | // PerformDeviceAction rpc take the device request and performs OLT and ONU hard and soft reboot |
| 161 | func (s BBSimLegacyServer) PerformDeviceAction(ctx context.Context, in *legacy.DeviceAction) (*legacy.BBSimResponse, error) { |
| 162 | logger.Trace("PerformDeviceAction() invoked") |
| 163 | logger.Error("Not implemented") |
| 164 | |
| 165 | return &legacy.BBSimResponse{StatusMsg: RequestAccepted}, nil |
| 166 | } |
| 167 | |
| 168 | // GetFlows returns all flows or flows for specified ONU |
| 169 | func (s BBSimLegacyServer) GetFlows(ctx context.Context, in *legacy.ONUInfo) (*legacy.Flows, error) { |
| 170 | logger.Info("GetFlow request received") |
| 171 | logger.Error("Not implemented") |
| 172 | |
| 173 | return &legacy.Flows{}, nil |
| 174 | } |
| 175 | |
| 176 | // StartRestGatewayService method starts REST server for BBSim. |
| 177 | func StartRestGatewayService(channel chan bool, group *sync.WaitGroup, grpcAddress string, hostandport string) { |
| 178 | ctx := context.Background() |
| 179 | ctx, cancel := context.WithCancel(ctx) |
| 180 | defer cancel() |
| 181 | |
| 182 | mux := runtime.NewServeMux() |
| 183 | opts := []grpc.DialOption{grpc.WithInsecure()} |
| 184 | |
| 185 | // Register REST endpoints |
| 186 | err := legacy.RegisterBBSimServiceHandlerFromEndpoint(ctx, mux, grpcAddress, opts) |
| 187 | if err != nil { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 188 | logger.Errorf("%v", err) |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 189 | return |
| 190 | } |
| 191 | |
| 192 | s := &http.Server{Addr: hostandport, Handler: mux} |
| 193 | |
| 194 | go func() { |
Zdravko Bozakov | 958d81c | 2019-12-13 22:09:48 +0100 | [diff] [blame] | 195 | logger.Infof("legacy REST API server listening on %s", hostandport) |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 196 | if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 197 | logger.Errorf("Could not start legacy API server: %v", err) |
| 198 | return |
| 199 | } |
| 200 | }() |
| 201 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 202 | x := <-channel |
| 203 | if x { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 204 | logger.Warnf("Stopping legacy API REST server") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 205 | if err := s.Shutdown(ctx); err != nil { |
| 206 | logger.Errorf("Could not stop server: %v", err) |
| 207 | } |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 208 | group.Done() |
| 209 | } |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 210 | } |