khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | */ |
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" |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 21 | "errors" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 22 | "fmt" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 23 | "strconv" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 24 | "strings" |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame^] | 25 | "time" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 26 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 27 | "github.com/golang/protobuf/ptypes/empty" |
| 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" |
| 31 | "github.com/opencord/voltha-protos/v5/go/extension" |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame^] | 32 | "github.com/opencord/voltha-protos/v5/go/health" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 33 | "github.com/opencord/voltha-protos/v5/go/omci" |
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 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
| 39 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 40 | ca "github.com/opencord/voltha-protos/v5/go/core_adapter" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 41 | of "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 42 | "github.com/opencord/voltha-protos/v5/go/voltha" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 43 | ) |
| 44 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 45 | // OLTAdapter represent OLT adapter |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 46 | type OLTAdapter struct { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 47 | *Adapter |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 48 | ChildDeviceType string |
| 49 | childVendor string |
| 50 | grpcServer *vgrpc.GrpcServer |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 51 | } |
| 52 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 53 | // NewOLTAdapter - creates OLT adapter instance |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 54 | func NewOLTAdapter(ctx context.Context, coreEndpoint string, deviceType string, vendor string, childDeviceType, childVendor string) *OLTAdapter { |
| 55 | // Get an available port |
| 56 | grpcPort, err := freeport.GetFreePort() |
| 57 | if err != nil { |
| 58 | logger.Fatalw(ctx, "no-free-port", log.Fields{"error": err}) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 59 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 60 | |
| 61 | // start gRPC handler |
| 62 | listeningAddress := fmt.Sprintf("127.0.0.1:%s", strconv.Itoa(grpcPort)) |
| 63 | oltAdapter := &OLTAdapter{Adapter: NewAdapter(listeningAddress, coreEndpoint, deviceType, vendor), |
| 64 | ChildDeviceType: childDeviceType, childVendor: childVendor} |
| 65 | |
| 66 | oltAdapter.start(ctx) |
| 67 | return oltAdapter |
| 68 | } |
| 69 | |
| 70 | func (oltA *OLTAdapter) oltRestarted(ctx context.Context, endPoint string) error { |
| 71 | logger.Errorw(ctx, "remote-restarted", log.Fields{"endpoint": endPoint}) |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | func (oltA *OLTAdapter) start(ctx context.Context) { |
| 76 | |
| 77 | // Set up the probe service |
| 78 | oltA.Probe = &probe.Probe{} |
| 79 | probePort, err := freeport.GetFreePort() |
| 80 | if err != nil { |
| 81 | logger.Fatal(ctx, "Cannot get a freeport for probePort") |
| 82 | } |
| 83 | probeAddress := "127.0.0.1:" + strconv.Itoa(probePort) |
| 84 | go oltA.Probe.ListenAndServe(ctx, probeAddress) |
| 85 | |
| 86 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, oltA.Probe) |
| 87 | |
| 88 | oltA.Probe.RegisterService(ctx, "olt-grpc-service", oltA.coreEnpoint) |
| 89 | |
| 90 | oltA.grpcServer = vgrpc.NewGrpcServer(oltA.serviceEndpoint, nil, false, nil) |
| 91 | |
| 92 | logger.Debugw(ctx, "OLTAdapter-address", log.Fields{"address": oltA.serviceEndpoint}) |
| 93 | |
| 94 | go oltA.startGRPCService(ctx, oltA.grpcServer, oltA, "olt-grpc-service") |
| 95 | |
| 96 | // Establish grpc connection to Core |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 97 | if oltA.coreClient, err = vgrpc.NewClient( |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame^] | 98 | "mock-olt-endpoint", |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 99 | oltA.coreEnpoint, |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame^] | 100 | "core_service.CoreService", |
khenaidoo | 25057da | 2021-12-08 14:40:45 -0500 | [diff] [blame] | 101 | oltA.oltRestarted); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 102 | logger.Fatal(ctx, "grpc-client-not-created") |
| 103 | } |
| 104 | |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame^] | 105 | go oltA.coreClient.Start(probeCtx, setCoreServiceHandler) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 106 | |
| 107 | logger.Debugw(ctx, "OLTAdapter-started", log.Fields{"grpc-address": oltA.serviceEndpoint}) |
| 108 | |
| 109 | } |
| 110 | |
| 111 | // Stop brings down core services |
| 112 | func (oltA *OLTAdapter) StopGrpcClient() { |
| 113 | // Stop the grpc clients |
| 114 | oltA.coreClient.Stop(context.Background()) |
| 115 | } |
| 116 | |
| 117 | // Stop brings down core services |
| 118 | func (oltA *OLTAdapter) Stop() { |
| 119 | |
| 120 | // Stop the grpc |
| 121 | if oltA.grpcServer != nil { |
| 122 | oltA.grpcServer.Stop() |
| 123 | } |
| 124 | logger.Debugw(context.Background(), "OLTAdapter-stopped", log.Fields{"grpc-address": oltA.serviceEndpoint}) |
| 125 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 126 | } |
| 127 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 128 | // Adopt_device creates new handler for added device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 129 | func (oltA *OLTAdapter) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { |
| 130 | logger.Debugw(ctx, "AdoptDevice", log.Fields{"device": device.AdapterEndpoint, "device-type": oltA.DeviceType}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 131 | go func() { |
| 132 | d := proto.Clone(device).(*voltha.Device) |
| 133 | d.Root = true |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 134 | d.Vendor = oltA.vendor |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 135 | d.Model = "go-mock" |
| 136 | d.SerialNumber = com.GetRandomSerialNumber() |
| 137 | d.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 138 | oltA.storeDevice(d) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 139 | c, err := oltA.GetCoreClient() |
| 140 | if err != nil { |
| 141 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 142 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 143 | if _, err := c.DeviceUpdate(context.TODO(), d); err != nil { |
| 144 | logger.Fatalf(ctx, "deviceUpdate-failed-%s", err) |
| 145 | } |
| 146 | |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 147 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 148 | nniPort := &voltha.Port{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 149 | DeviceId: device.Id, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 150 | PortNo: 2, |
| 151 | Label: fmt.Sprintf("nni-%d", 2), |
| 152 | Type: voltha.Port_ETHERNET_NNI, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 153 | OperStatus: common.OperStatus_ACTIVE, |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 154 | OfpPort: &of.OfpPort{ |
| 155 | HwAddr: macAddressToUint32Array("11:22:33:44:55:66"), |
| 156 | Config: 0, |
| 157 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 158 | Curr: capability, |
| 159 | Advertised: capability, |
| 160 | Peer: capability, |
| 161 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 162 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 163 | }, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 164 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 165 | if _, err = c.PortCreated(context.TODO(), nniPort); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 166 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | ponPort := &voltha.Port{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 170 | DeviceId: device.Id, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 171 | PortNo: 1, |
| 172 | Label: fmt.Sprintf("pon-%d", 1), |
| 173 | Type: voltha.Port_PON_OLT, |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 174 | OperStatus: common.OperStatus_ACTIVE, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 175 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 176 | if _, err = c.PortCreated(context.TODO(), ponPort); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 177 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 178 | } |
| 179 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 180 | d.ConnectStatus = common.ConnectStatus_REACHABLE |
| 181 | d.OperStatus = common.OperStatus_ACTIVE |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 182 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 183 | 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] | 184 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | //Get the latest device data from the Core |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 188 | 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] | 189 | logger.Fatalf(ctx, "getting-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 190 | } |
| 191 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 192 | oltA.updateDevice(d) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 193 | |
| 194 | // Register Child devices |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 195 | initialUniPortNo := startingUNIPortNo |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 196 | for i := 0; i < numONUPerOLT; i++ { |
| 197 | go func(seqNo int) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 198 | if _, err := c.ChildDeviceDetected(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 199 | &ca.DeviceDiscovery{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 200 | ParentId: d.Id, |
| 201 | ParentPortNo: 1, |
| 202 | ChildDeviceType: oltA.ChildDeviceType, |
| 203 | ChannelId: uint32(initialUniPortNo + seqNo), |
| 204 | VendorId: oltA.childVendor, |
| 205 | SerialNumber: com.GetRandomSerialNumber(), |
| 206 | OnuId: uint32(seqNo), |
| 207 | }); err != nil { |
| 208 | logger.Fatalw(ctx, "failure-sending-child-device", log.Fields{"error": err, "parent-id": d.Id, "child-device-type": oltA.ChildDeviceType}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 209 | } |
| 210 | }(i) |
| 211 | } |
| 212 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 213 | return &empty.Empty{}, nil |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame] | 216 | // Single_get_value_request retrieves a single value. |
| 217 | func (oltA *OLTAdapter) Single_get_value_request(ctx context.Context, // nolint |
| 218 | request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) { |
| 219 | logger.Fatalf(ctx, "Single_get_value_request unimplemented") |
| 220 | return nil, nil |
| 221 | } |
| 222 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 223 | // Get_ofp_device_info returns ofp device info |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 224 | func (oltA *OLTAdapter) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 225 | if d := oltA.getDevice(device.Id); d == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 226 | logger.Fatalf(ctx, "device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 227 | } |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 228 | return &ca.SwitchCapability{ |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 229 | Desc: &of.OfpDesc{ |
| 230 | HwDesc: "olt_adapter_mock", |
| 231 | SwDesc: "olt_adapter_mock", |
| 232 | SerialNum: "12345678", |
| 233 | }, |
| 234 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 235 | NBuffers: 256, |
| 236 | NTables: 2, |
| 237 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 238 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 239 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 240 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 241 | }, |
| 242 | }, nil |
| 243 | } |
| 244 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 245 | // Disable_device disables device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 246 | func (oltA *OLTAdapter) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 247 | go func() { |
| 248 | if d := oltA.getDevice(device.Id); d == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 249 | logger.Fatalf(ctx, "device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | cloned := proto.Clone(device).(*voltha.Device) |
| 253 | // Update the all ports state on that device to disable |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 254 | c, err := oltA.GetCoreClient() |
| 255 | if err != nil { |
| 256 | return |
| 257 | } |
| 258 | |
| 259 | if _, err := c.PortsStateUpdate(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 260 | &ca.PortStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 261 | DeviceId: cloned.Id, |
| 262 | PortTypeFilter: 0, |
| 263 | OperStatus: common.OperStatus_UNKNOWN, |
| 264 | }); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 265 | 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] | 266 | } |
| 267 | |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 268 | //Update the device operational state |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 269 | cloned.OperStatus = common.OperStatus_UNKNOWN |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 270 | // The device is still reachable after it has been disabled, so the connection status should not be changed. |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 271 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 272 | if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 273 | DeviceId: cloned.Id, |
| 274 | OperStatus: cloned.OperStatus, |
| 275 | ConnStatus: cloned.ConnectStatus, |
| 276 | }); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 277 | // Device may already have been deleted in the core |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 278 | 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] | 279 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 280 | } |
| 281 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 282 | oltA.updateDevice(cloned) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 283 | |
| 284 | // Tell the Core that all child devices have been disabled (by default it's an action already taken by the Core |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 285 | if _, err := c.ChildDevicesLost(context.TODO(), &common.ID{Id: cloned.Id}); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 286 | // Device may already have been deleted in the core |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 287 | logger.Warnw(ctx, "lost-notif-of-child-devices-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 288 | } |
| 289 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 290 | return &empty.Empty{}, nil |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 291 | } |
| 292 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 293 | // Reenable_device reenables device |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 294 | func (oltA *OLTAdapter) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 295 | go func() { |
| 296 | if d := oltA.getDevice(device.Id); d == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 297 | logger.Fatalf(ctx, "device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | cloned := proto.Clone(device).(*voltha.Device) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 301 | |
| 302 | c, err := oltA.GetCoreClient() |
| 303 | if err != nil { |
| 304 | return |
| 305 | } |
| 306 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 307 | // Update the all ports state on that device to enable |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 308 | if _, err := c.PortsStateUpdate(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 309 | &ca.PortStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 310 | DeviceId: cloned.Id, |
| 311 | PortTypeFilter: 0, |
| 312 | OperStatus: common.OperStatus_ACTIVE, |
| 313 | }); err != nil { |
| 314 | 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] | 315 | } |
| 316 | |
| 317 | //Update the device state |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 318 | cloned.OperStatus = common.OperStatus_ACTIVE |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 319 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 320 | if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 321 | DeviceId: cloned.Id, |
| 322 | OperStatus: cloned.OperStatus, |
| 323 | ConnStatus: cloned.ConnectStatus, |
| 324 | }); err != nil { |
| 325 | // Device may already have been deleted in the core |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 326 | 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] | 327 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // Tell the Core that all child devices have been enabled |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 331 | if _, err := c.ChildDevicesDetected(context.TODO(), &common.ID{Id: cloned.Id}); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 332 | logger.Fatalf(ctx, "detection-notif-of-child-devices-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 333 | } |
| 334 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 335 | return &empty.Empty{}, nil |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 336 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 337 | |
| 338 | // Enable_port - |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 339 | func (oltA *OLTAdapter) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { //nolint |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 340 | go func() { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 341 | c, err := oltA.GetCoreClient() |
| 342 | if err != nil { |
| 343 | return |
| 344 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 345 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 346 | if port.Type == voltha.Port_PON_OLT { |
| 347 | if _, err := c.PortStateUpdate(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 348 | &ca.PortState{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 349 | DeviceId: port.DeviceId, |
| 350 | PortType: voltha.Port_ETHERNET_NNI, |
| 351 | PortNo: port.PortNo, |
| 352 | OperStatus: common.OperStatus_ACTIVE, |
| 353 | }); err != nil { |
| 354 | logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": port.DeviceId, "error": err}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 359 | return &empty.Empty{}, nil |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | // Disable_port - |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 363 | func (oltA *OLTAdapter) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { //nolint |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 364 | go func() { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 365 | c, err := oltA.GetCoreClient() |
| 366 | if err != nil { |
| 367 | return |
| 368 | } |
| 369 | if port.Type == voltha.Port_PON_OLT { |
| 370 | if _, err := c.PortStateUpdate(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 371 | &ca.PortState{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 372 | DeviceId: port.DeviceId, |
| 373 | PortType: voltha.Port_PON_OLT, |
| 374 | PortNo: port.PortNo, |
| 375 | OperStatus: common.OperStatus_DISCOVERED, |
| 376 | }); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 377 | // Corresponding device may have been deleted |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 378 | logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": port.DeviceId, "error": err}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 382 | return &empty.Empty{}, nil |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 383 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 384 | |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 385 | // Reboot_device - |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 386 | func (oltA *OLTAdapter) RebootDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 387 | logger.Infow(ctx, "reboot-device", log.Fields{"device-id": device.Id}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 388 | |
| 389 | go func() { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 390 | c, err := oltA.GetCoreClient() |
| 391 | if err != nil { |
| 392 | return |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 393 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 394 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 395 | if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 396 | DeviceId: device.Id, |
| 397 | OperStatus: common.OperStatus_UNKNOWN, |
| 398 | ConnStatus: common.ConnectStatus_UNREACHABLE, |
| 399 | }); err != nil { |
| 400 | logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err}) |
| 401 | return |
| 402 | } |
| 403 | |
| 404 | if _, err := c.PortsStateUpdate(context.TODO(), |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 405 | &ca.PortStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 406 | DeviceId: device.Id, |
| 407 | PortTypeFilter: 0, |
| 408 | OperStatus: common.OperStatus_UNKNOWN, |
| 409 | }); err != nil { |
| 410 | logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err}) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 411 | } |
| 412 | }() |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 413 | return &empty.Empty{}, nil |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 416 | // TODO: REMOVE Start_omci_test begins an omci self-test |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 417 | func (oltA *OLTAdapter) StartOmciTest(ctx context.Context, test *ca.OMCITest) (*omci.TestResponse, error) { // nolint |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 418 | return nil, errors.New("start-omci-test-not-implemented") |
| 419 | } |
| 420 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 421 | // Helper for test only |
| 422 | func (oltA *OLTAdapter) SetDeviceActive(deviceID string) { |
| 423 | c, err := oltA.GetCoreClient() |
| 424 | if err != nil { |
| 425 | return |
| 426 | } |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 427 | |
khenaidoo | 9beaaf1 | 2021-10-19 17:32:01 -0400 | [diff] [blame] | 428 | if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{ |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 429 | DeviceId: deviceID, |
| 430 | OperStatus: common.OperStatus_ACTIVE, |
| 431 | ConnStatus: common.ConnectStatus_REACHABLE, |
| 432 | }); err != nil { |
| 433 | logger.Warnw(context.Background(), "device-state-update-failed", log.Fields{"device-id": deviceID, "error": err}) |
| 434 | return |
| 435 | } |
ssiddiqui | f076cb8 | 2021-04-23 10:47:04 +0530 | [diff] [blame] | 436 | |
yasin sapli | 5458a1c | 2021-06-14 22:24:38 +0000 | [diff] [blame] | 437 | } |
khenaidoo | a46458b | 2021-12-15 16:50:44 -0500 | [diff] [blame^] | 438 | |
| 439 | func (oltA *OLTAdapter) GetHealthStatus(stream adapter_service.AdapterService_GetHealthStatusServer) error { |
| 440 | ctx := context.Background() |
| 441 | logger.Debugw(ctx, "receive-stream-connection", log.Fields{"stream": stream}) |
| 442 | |
| 443 | if stream == nil { |
| 444 | return fmt.Errorf("conn-is-nil %v", stream) |
| 445 | } |
| 446 | initialRequestTime := time.Now() |
| 447 | var remoteClient *common.Connection |
| 448 | var tempClient *common.Connection |
| 449 | var err error |
| 450 | loop: |
| 451 | for { |
| 452 | tempClient, err = stream.Recv() |
| 453 | if err != nil { |
| 454 | logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 455 | break loop |
| 456 | } |
| 457 | err = stream.Send(&health.HealthStatus{State: health.HealthStatus_HEALTHY}) |
| 458 | if err != nil { |
| 459 | logger.Warnw(ctx, "sending-stream-error", log.Fields{"remote-client": remoteClient, "error": err}) |
| 460 | break loop |
| 461 | } |
| 462 | remoteClient = tempClient |
| 463 | logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient}) |
| 464 | |
| 465 | select { |
| 466 | case <-stream.Context().Done(): |
| 467 | logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()}) |
| 468 | break loop |
| 469 | default: |
| 470 | } |
| 471 | } |
| 472 | logger.Errorw(ctx, "connection-down", log.Fields{"remote-client": remoteClient, "error": err, "initial-conn-time": initialRequestTime}) |
| 473 | return err |
| 474 | } |