khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | /* |
Joey Armstrong | 5f51f2e | 2023-01-17 17:06:26 -0500 | [diff] [blame] | 2 | * Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 17 | package mocks |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 22 | "strconv" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 23 | "strings" |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 24 | "time" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 25 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 26 | "github.com/golang/protobuf/ptypes/empty" |
| 27 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
| 28 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v5/go/adapter_service" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 30 | "github.com/opencord/voltha-protos/v5/go/common" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 31 | ca "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 32 | "github.com/opencord/voltha-protos/v5/go/extension" |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 33 | "github.com/opencord/voltha-protos/v5/go/health" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 34 | "github.com/phayes/freeport" |
| 35 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 36 | "github.com/gogo/protobuf/proto" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 37 | com "github.com/opencord/voltha-lib-go/v7/pkg/adapters/common" |
| 38 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 39 | "github.com/opencord/voltha-protos/v5/go/omci" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 40 | of "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 41 | "github.com/opencord/voltha-protos/v5/go/voltha" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 42 | ) |
| 43 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 44 | // ONUAdapter represent ONU adapter attributes |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 45 | type ONUAdapter struct { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 46 | *Adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 47 | grpcServer *vgrpc.GrpcServer |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 48 | } |
| 49 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 50 | // NewONUAdapter creates ONU adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 51 | func NewONUAdapter(ctx context.Context, coreEndpoint string, deviceType string, vendor string) *ONUAdapter { |
| 52 | // Get an available port |
| 53 | grpcPort, err := freeport.GetFreePort() |
| 54 | if err != nil { |
| 55 | logger.Fatalw(ctx, "no-free-port", log.Fields{"error": err}) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 56 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 57 | listeningAddress := fmt.Sprintf("127.0.0.1:%s", strconv.Itoa(grpcPort)) |
| 58 | onuAdapter := &ONUAdapter{Adapter: NewAdapter(listeningAddress, coreEndpoint, deviceType, vendor)} |
| 59 | |
| 60 | onuAdapter.start(ctx) |
| 61 | return onuAdapter |
| 62 | } |
| 63 | |
| 64 | func (onuA *ONUAdapter) onuRestarted(ctx context.Context, endPoint string) error { |
| 65 | logger.Errorw(ctx, "remote-restarted", log.Fields{"endpoint": endPoint}) |
| 66 | return nil |
| 67 | } |
| 68 | |
| 69 | func (onuA *ONUAdapter) start(ctx context.Context) { |
| 70 | |
| 71 | // Set up the probe service |
| 72 | onuA.Probe = &probe.Probe{} |
| 73 | probePort, err := freeport.GetFreePort() |
| 74 | if err != nil { |
| 75 | logger.Fatal(ctx, "Cannot get a freeport for probePort") |
| 76 | } |
| 77 | probeAddress := "127.0.0.1:" + strconv.Itoa(probePort) |
| 78 | go onuA.Probe.ListenAndServe(ctx, probeAddress) |
| 79 | |
| 80 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, onuA.Probe) |
| 81 | |
| 82 | onuA.Probe.RegisterService(ctx, "onu-grpc-service", onuA.coreEnpoint) |
| 83 | |
| 84 | // start gRPC handler |
| 85 | onuA.grpcServer = vgrpc.NewGrpcServer(onuA.serviceEndpoint, nil, false, nil) |
| 86 | |
| 87 | logger.Debugw(ctx, "ONUAdapter-address", log.Fields{"address": onuA.serviceEndpoint}) |
| 88 | |
| 89 | go onuA.startGRPCService(ctx, onuA.grpcServer, onuA, "onu-grpc-service") |
| 90 | |
| 91 | // Establish grpc connection to Core |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 92 | if onuA.coreClient, err = vgrpc.NewClient( |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 93 | "mock-onu-endpoint", |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 94 | onuA.coreEnpoint, |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 95 | "core_service.CoreService", |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 96 | onuA.onuRestarted); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 97 | logger.Fatal(ctx, "grpc-client-not-created") |
| 98 | } |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 99 | go onuA.coreClient.Start(probeCtx, setCoreServiceHandler) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 100 | |
| 101 | logger.Debugw(ctx, "ONUAdapter-started", log.Fields{"grpc-address": onuA.serviceEndpoint}) |
| 102 | } |
| 103 | |
| 104 | // Stop brings down core services |
| 105 | func (onuA *ONUAdapter) StopGrpcClient() { |
| 106 | // Stop the grpc clients |
| 107 | onuA.coreClient.Stop(context.Background()) |
| 108 | } |
| 109 | |
| 110 | func (onuA *ONUAdapter) Stop() { |
| 111 | if onuA.grpcServer != nil { |
| 112 | onuA.grpcServer.Stop() |
| 113 | } |
| 114 | logger.Debugw(context.Background(), "ONUAdapter-stopped", log.Fields{"grpc-address": onuA.serviceEndpoint}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 115 | } |
| 116 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 117 | // Adopt_device creates new handler for added device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 118 | func (onuA *ONUAdapter) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { |
| 119 | logger.Debugw(ctx, "AdoptDevice", log.Fields{"device": device.AdapterEndpoint, "device-type": onuA.DeviceType}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 120 | go func() { |
| 121 | d := proto.Clone(device).(*voltha.Device) |
| 122 | d.Root = false |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 123 | d.Vendor = onuA.vendor |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 124 | d.Model = "go-mock" |
| 125 | d.SerialNumber = com.GetRandomSerialNumber() |
| 126 | d.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 127 | onuA.storeDevice(d) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 128 | |
| 129 | c, err := onuA.GetCoreClient() |
| 130 | if err != nil { |
| 131 | return |
| 132 | } |
| 133 | if _, err := c.DeviceUpdate(context.TODO(), d); err != nil { |
| 134 | logger.Fatalf(ctx, "deviceUpdate-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 135 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 136 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 137 | d.ConnectStatus = common.ConnectStatus_REACHABLE |
| 138 | d.OperStatus = common.OperStatus_DISCOVERED |
khenaidoo | 9318152 | 2020-01-23 12:43:21 -0500 | [diff] [blame] | 139 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 140 | if _, err = c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{DeviceId: d.Id, OperStatus: d.OperStatus, ConnStatus: d.ConnectStatus}); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 141 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 142 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 143 | |
| 144 | uniPortNo := uint32(2) |
| 145 | if device.ProxyAddress != nil { |
| 146 | if device.ProxyAddress.ChannelId != 0 { |
| 147 | uniPortNo = device.ProxyAddress.ChannelId |
| 148 | } |
| 149 | } |
| 150 | |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 151 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 152 | uniPort := &voltha.Port{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 153 | DeviceId: d.Id, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 154 | PortNo: uniPortNo, |
| 155 | Label: fmt.Sprintf("uni-%d", uniPortNo), |
| 156 | Type: voltha.Port_ETHERNET_UNI, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 157 | OperStatus: common.OperStatus_ACTIVE, |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 158 | OfpPort: &of.OfpPort{ |
| 159 | HwAddr: macAddressToUint32Array("12:12:12:12:12:12"), |
| 160 | Config: 0, |
| 161 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 162 | Curr: capability, |
| 163 | Advertised: capability, |
| 164 | Peer: capability, |
| 165 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 166 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 167 | }, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 168 | } |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 169 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 170 | if _, err = c.PortCreated(context.TODO(), uniPort); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 171 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | ponPortNo := uint32(1) |
| 175 | if device.ParentPortNo != 0 { |
| 176 | ponPortNo = device.ParentPortNo |
| 177 | } |
| 178 | |
| 179 | ponPort := &voltha.Port{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 180 | DeviceId: d.Id, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 181 | PortNo: ponPortNo, |
| 182 | Label: fmt.Sprintf("pon-%d", ponPortNo), |
| 183 | Type: voltha.Port_PON_ONU, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 184 | OperStatus: common.OperStatus_ACTIVE, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 185 | Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 186 | PortNo: device.ParentPortNo}}, // Peer port is parent's port number |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 187 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 188 | |
| 189 | if _, err = c.PortCreated(context.TODO(), ponPort); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 190 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 191 | } |
| 192 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 193 | d.ConnectStatus = common.ConnectStatus_REACHABLE |
| 194 | d.OperStatus = common.OperStatus_ACTIVE |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 195 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 196 | if _, err = c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{DeviceId: d.Id, OperStatus: d.OperStatus, ConnStatus: d.ConnectStatus}); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 197 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 198 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 199 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 200 | //Get the latest device data from the Core |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 201 | if d, err = c.GetDevice(context.TODO(), &common.ID{Id: d.Id}); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 202 | logger.Fatalf(ctx, "getting-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 203 | } |
| 204 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 205 | onuA.updateDevice(d) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 206 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 207 | return &empty.Empty{}, nil |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame] | 210 | // Single_get_value_request retrieves a single value. |
| 211 | func (onuA *ONUAdapter) Single_get_value_request(ctx context.Context, // nolint |
| 212 | request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) { |
| 213 | logger.Fatalf(ctx, "Single_get_value_request unimplemented") |
| 214 | return nil, nil |
| 215 | } |
| 216 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 217 | // Disable_device disables device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 218 | func (onuA *ONUAdapter) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 219 | go func() { |
| 220 | if d := onuA.getDevice(device.Id); d == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 221 | logger.Fatalf(ctx, "device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 222 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 223 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 224 | cloned := proto.Clone(device).(*voltha.Device) |
| 225 | // Update the all ports state on that device to disable |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 226 | c, err := onuA.GetCoreClient() |
| 227 | if err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 228 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 229 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 230 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 231 | if _, err := c.PortsStateUpdate(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 232 | &ca.PortStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 233 | DeviceId: cloned.Id, |
| 234 | PortTypeFilter: 0, |
| 235 | OperStatus: common.OperStatus_UNKNOWN, |
| 236 | }); err != nil { |
| 237 | logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err}) |
| 238 | } |
| 239 | |
| 240 | //Update the device operational state |
| 241 | cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE |
| 242 | cloned.OperStatus = common.OperStatus_UNKNOWN |
| 243 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 244 | if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 245 | DeviceId: cloned.Id, |
| 246 | OperStatus: cloned.OperStatus, |
| 247 | ConnStatus: cloned.ConnectStatus, |
| 248 | }); err != nil { |
| 249 | // Device may already have been deleted in the core |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 250 | logger.Warnw(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 251 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 252 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 253 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 254 | onuA.updateDevice(cloned) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 255 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 256 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 257 | return &empty.Empty{}, nil |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 258 | } |
| 259 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 260 | func (onuA *ONUAdapter) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 261 | go func() { |
| 262 | if d := onuA.getDevice(device.Id); d == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 263 | logger.Fatalf(ctx, "device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | cloned := proto.Clone(device).(*voltha.Device) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 267 | |
| 268 | c, err := onuA.GetCoreClient() |
| 269 | if err != nil { |
| 270 | return |
| 271 | } |
| 272 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 273 | // Update the all ports state on that device to enable |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 274 | if _, err := c.PortsStateUpdate(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 275 | &ca.PortStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 276 | DeviceId: cloned.Id, |
| 277 | PortTypeFilter: 0, |
| 278 | OperStatus: common.OperStatus_ACTIVE, |
| 279 | }); err != nil { |
| 280 | logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | //Update the device state |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 284 | cloned.ConnectStatus = common.ConnectStatus_REACHABLE |
| 285 | cloned.OperStatus = common.OperStatus_ACTIVE |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 286 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 287 | if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 288 | DeviceId: cloned.Id, |
| 289 | OperStatus: cloned.OperStatus, |
| 290 | ConnStatus: cloned.ConnectStatus, |
| 291 | }); err != nil { |
| 292 | // Device may already have been deleted in the core |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 293 | logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 294 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 295 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 296 | onuA.updateDevice(cloned) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 297 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 298 | return &empty.Empty{}, nil |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 299 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 300 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 301 | func (onuA *ONUAdapter) StartOmciTest(ctx context.Context, _ *ca.OMCITest) (*omci.TestResponse, error) { // nolint |
| 302 | return &omci.TestResponse{Result: omci.TestResponse_SUCCESS}, nil |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 303 | } |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame] | 304 | |
| 305 | func (onuA *ONUAdapter) GetHealthStatus(stream adapter_service.AdapterService_GetHealthStatusServer) error { |
| 306 | ctx := context.Background() |
| 307 | logger.Debugw(ctx, "receive-stream-connection", log.Fields{"stream": stream}) |
| 308 | |
| 309 | if stream == nil { |
| 310 | return fmt.Errorf("conn-is-nil %v", stream) |
| 311 | } |
| 312 | initialRequestTime := time.Now() |
| 313 | var remoteClient *common.Connection |
| 314 | var tempClient *common.Connection |
| 315 | var err error |
| 316 | loop: |
| 317 | for { |
| 318 | tempClient, err = stream.Recv() |
| 319 | if err != nil { |
| 320 | logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 321 | break loop |
| 322 | } |
| 323 | err = stream.Send(&health.HealthStatus{State: health.HealthStatus_HEALTHY}) |
| 324 | if err != nil { |
| 325 | logger.Warnw(ctx, "sending-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 326 | break loop |
| 327 | } |
| 328 | remoteClient = tempClient |
| 329 | logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient}) |
| 330 | |
| 331 | select { |
| 332 | case <-stream.Context().Done(): |
| 333 | logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()}) |
| 334 | break loop |
| 335 | default: |
| 336 | } |
| 337 | } |
| 338 | logger.Errorw(ctx, "connection-down", log.Fields{"remote-client": remoteClient, "error": err, "initial-conn-time": initialRequestTime}) |
| 339 | return err |
| 340 | } |