blob: 7712b233db52994732c45ca5651b142964ae0472 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidooab1f7bd2019-11-14 14:00:27 -050017package mocks
18
19import (
20 "context"
21 "fmt"
khenaidood948f772021-08-11 17:49:24 -040022 "strconv"
Kent Hagerman2a07b862020-06-19 15:23:07 -040023 "strings"
24
khenaidood948f772021-08-11 17:49:24 -040025 "github.com/golang/protobuf/ptypes/empty"
26 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
27 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
28 "github.com/opencord/voltha-protos/v5/go/common"
khenaidoo9beaaf12021-10-19 17:32:01 -040029 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidood948f772021-08-11 17:49:24 -040030 "github.com/opencord/voltha-protos/v5/go/extension"
31 "github.com/phayes/freeport"
32
khenaidooab1f7bd2019-11-14 14:00:27 -050033 "github.com/gogo/protobuf/proto"
khenaidood948f772021-08-11 17:49:24 -040034 com "github.com/opencord/voltha-lib-go/v7/pkg/adapters/common"
35 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo9beaaf12021-10-19 17:32:01 -040036 "github.com/opencord/voltha-protos/v5/go/omci"
khenaidood948f772021-08-11 17:49:24 -040037 of "github.com/opencord/voltha-protos/v5/go/openflow_13"
38 "github.com/opencord/voltha-protos/v5/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050039)
40
npujar1d86a522019-11-14 17:11:16 +053041// ONUAdapter represent ONU adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050042type ONUAdapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040043 *Adapter
khenaidood948f772021-08-11 17:49:24 -040044 grpcServer *vgrpc.GrpcServer
khenaidooab1f7bd2019-11-14 14:00:27 -050045}
46
npujar1d86a522019-11-14 17:11:16 +053047// NewONUAdapter creates ONU adapter
khenaidood948f772021-08-11 17:49:24 -040048func NewONUAdapter(ctx context.Context, coreEndpoint string, deviceType string, vendor string) *ONUAdapter {
49 // Get an available port
50 grpcPort, err := freeport.GetFreePort()
51 if err != nil {
52 logger.Fatalw(ctx, "no-free-port", log.Fields{"error": err})
khenaidoo67b22152020-03-02 16:01:25 -050053 }
khenaidood948f772021-08-11 17:49:24 -040054 listeningAddress := fmt.Sprintf("127.0.0.1:%s", strconv.Itoa(grpcPort))
55 onuAdapter := &ONUAdapter{Adapter: NewAdapter(listeningAddress, coreEndpoint, deviceType, vendor)}
56
57 onuAdapter.start(ctx)
58 return onuAdapter
59}
60
61func (onuA *ONUAdapter) onuRestarted(ctx context.Context, endPoint string) error {
62 logger.Errorw(ctx, "remote-restarted", log.Fields{"endpoint": endPoint})
63 return nil
64}
65
66func (onuA *ONUAdapter) start(ctx context.Context) {
67
68 // Set up the probe service
69 onuA.Probe = &probe.Probe{}
70 probePort, err := freeport.GetFreePort()
71 if err != nil {
72 logger.Fatal(ctx, "Cannot get a freeport for probePort")
73 }
74 probeAddress := "127.0.0.1:" + strconv.Itoa(probePort)
75 go onuA.Probe.ListenAndServe(ctx, probeAddress)
76
77 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, onuA.Probe)
78
79 onuA.Probe.RegisterService(ctx, "onu-grpc-service", onuA.coreEnpoint)
80
81 // start gRPC handler
82 onuA.grpcServer = vgrpc.NewGrpcServer(onuA.serviceEndpoint, nil, false, nil)
83
84 logger.Debugw(ctx, "ONUAdapter-address", log.Fields{"address": onuA.serviceEndpoint})
85
86 go onuA.startGRPCService(ctx, onuA.grpcServer, onuA, "onu-grpc-service")
87
88 // Establish grpc connection to Core
89 if onuA.coreClient, err = vgrpc.NewClient(onuA.coreEnpoint,
90 onuA.onuRestarted,
91 vgrpc.ActivityCheck(true)); err != nil {
92 logger.Fatal(ctx, "grpc-client-not-created")
93 }
94 go onuA.coreClient.Start(probeCtx, setAndTestCoreServiceHandler)
95
96 logger.Debugw(ctx, "ONUAdapter-started", log.Fields{"grpc-address": onuA.serviceEndpoint})
97}
98
99// Stop brings down core services
100func (onuA *ONUAdapter) StopGrpcClient() {
101 // Stop the grpc clients
102 onuA.coreClient.Stop(context.Background())
103}
104
105func (onuA *ONUAdapter) Stop() {
106 if onuA.grpcServer != nil {
107 onuA.grpcServer.Stop()
108 }
109 logger.Debugw(context.Background(), "ONUAdapter-stopped", log.Fields{"grpc-address": onuA.serviceEndpoint})
khenaidooab1f7bd2019-11-14 14:00:27 -0500110}
111
npujar1d86a522019-11-14 17:11:16 +0530112// Adopt_device creates new handler for added device
khenaidood948f772021-08-11 17:49:24 -0400113func (onuA *ONUAdapter) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
114 logger.Debugw(ctx, "AdoptDevice", log.Fields{"device": device.AdapterEndpoint, "device-type": onuA.DeviceType})
khenaidooab1f7bd2019-11-14 14:00:27 -0500115 go func() {
116 d := proto.Clone(device).(*voltha.Device)
117 d.Root = false
khenaidood948f772021-08-11 17:49:24 -0400118 d.Vendor = onuA.vendor
khenaidooab1f7bd2019-11-14 14:00:27 -0500119 d.Model = "go-mock"
120 d.SerialNumber = com.GetRandomSerialNumber()
121 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
122 onuA.storeDevice(d)
khenaidood948f772021-08-11 17:49:24 -0400123
124 c, err := onuA.GetCoreClient()
125 if err != nil {
126 return
127 }
128 if _, err := c.DeviceUpdate(context.TODO(), d); err != nil {
129 logger.Fatalf(ctx, "deviceUpdate-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500130 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500131
khenaidood948f772021-08-11 17:49:24 -0400132 d.ConnectStatus = common.ConnectStatus_REACHABLE
133 d.OperStatus = common.OperStatus_DISCOVERED
khenaidoo93181522020-01-23 12:43:21 -0500134
khenaidoo9beaaf12021-10-19 17:32:01 -0400135 if _, err = c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{DeviceId: d.Id, OperStatus: d.OperStatus, ConnStatus: d.ConnectStatus}); err != nil {
khenaidood948f772021-08-11 17:49:24 -0400136 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidoo442e7c72020-03-10 16:13:48 -0400137 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500138
139 uniPortNo := uint32(2)
140 if device.ProxyAddress != nil {
141 if device.ProxyAddress.ChannelId != 0 {
142 uniPortNo = device.ProxyAddress.ChannelId
143 }
144 }
145
khenaidooc6c7bda2020-06-17 17:20:18 -0400146 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -0500147 uniPort := &voltha.Port{
khenaidood948f772021-08-11 17:49:24 -0400148 DeviceId: d.Id,
khenaidooab1f7bd2019-11-14 14:00:27 -0500149 PortNo: uniPortNo,
150 Label: fmt.Sprintf("uni-%d", uniPortNo),
151 Type: voltha.Port_ETHERNET_UNI,
khenaidood948f772021-08-11 17:49:24 -0400152 OperStatus: common.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -0400153 OfpPort: &of.OfpPort{
154 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
155 Config: 0,
156 State: uint32(of.OfpPortState_OFPPS_LIVE),
157 Curr: capability,
158 Advertised: capability,
159 Peer: capability,
160 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
161 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
162 },
khenaidooab1f7bd2019-11-14 14:00:27 -0500163 }
khenaidooc6c7bda2020-06-17 17:20:18 -0400164
khenaidood948f772021-08-11 17:49:24 -0400165 if _, err = c.PortCreated(context.TODO(), uniPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000166 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500167 }
168
169 ponPortNo := uint32(1)
170 if device.ParentPortNo != 0 {
171 ponPortNo = device.ParentPortNo
172 }
173
174 ponPort := &voltha.Port{
khenaidood948f772021-08-11 17:49:24 -0400175 DeviceId: d.Id,
khenaidooab1f7bd2019-11-14 14:00:27 -0500176 PortNo: ponPortNo,
177 Label: fmt.Sprintf("pon-%d", ponPortNo),
178 Type: voltha.Port_PON_ONU,
khenaidood948f772021-08-11 17:49:24 -0400179 OperStatus: common.OperStatus_ACTIVE,
khenaidooab1f7bd2019-11-14 14:00:27 -0500180 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500181 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500182 }
khenaidood948f772021-08-11 17:49:24 -0400183
184 if _, err = c.PortCreated(context.TODO(), ponPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000185 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500186 }
187
khenaidood948f772021-08-11 17:49:24 -0400188 d.ConnectStatus = common.ConnectStatus_REACHABLE
189 d.OperStatus = common.OperStatus_ACTIVE
khenaidooab1f7bd2019-11-14 14:00:27 -0500190
khenaidoo9beaaf12021-10-19 17:32:01 -0400191 if _, err = c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{DeviceId: d.Id, OperStatus: d.OperStatus, ConnStatus: d.ConnectStatus}); err != nil {
khenaidood948f772021-08-11 17:49:24 -0400192 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500193 }
khenaidood948f772021-08-11 17:49:24 -0400194
khenaidooab1f7bd2019-11-14 14:00:27 -0500195 //Get the latest device data from the Core
khenaidood948f772021-08-11 17:49:24 -0400196 if d, err = c.GetDevice(context.TODO(), &common.ID{Id: d.Id}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000197 logger.Fatalf(ctx, "getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500198 }
199
khenaidoo8b4abbf2020-04-24 17:04:30 -0400200 onuA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500201 }()
khenaidood948f772021-08-11 17:49:24 -0400202 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500203}
204
Andrea Campanella025667e2021-01-14 11:50:07 +0100205// Single_get_value_request retrieves a single value.
206func (onuA *ONUAdapter) Single_get_value_request(ctx context.Context, // nolint
207 request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
208 logger.Fatalf(ctx, "Single_get_value_request unimplemented")
209 return nil, nil
210}
211
npujar1d86a522019-11-14 17:11:16 +0530212// Disable_device disables device
khenaidood948f772021-08-11 17:49:24 -0400213func (onuA *ONUAdapter) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500214 go func() {
215 if d := onuA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000216 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500217 }
khenaidood948f772021-08-11 17:49:24 -0400218
khenaidooab1f7bd2019-11-14 14:00:27 -0500219 cloned := proto.Clone(device).(*voltha.Device)
220 // Update the all ports state on that device to disable
khenaidood948f772021-08-11 17:49:24 -0400221 c, err := onuA.GetCoreClient()
222 if err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400223 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500224 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500225
khenaidood948f772021-08-11 17:49:24 -0400226 if _, err := c.PortsStateUpdate(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400227 &ca.PortStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400228 DeviceId: cloned.Id,
229 PortTypeFilter: 0,
230 OperStatus: common.OperStatus_UNKNOWN,
231 }); err != nil {
232 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
233 }
234
235 //Update the device operational state
236 cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
237 cloned.OperStatus = common.OperStatus_UNKNOWN
238
khenaidoo9beaaf12021-10-19 17:32:01 -0400239 if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400240 DeviceId: cloned.Id,
241 OperStatus: cloned.OperStatus,
242 ConnStatus: cloned.ConnectStatus,
243 }); err != nil {
244 // Device may already have been deleted in the core
divyadesaicb8b59d2020-08-18 09:55:47 +0000245 logger.Warnw(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400246 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500247 }
khenaidood948f772021-08-11 17:49:24 -0400248
khenaidoo8b4abbf2020-04-24 17:04:30 -0400249 onuA.updateDevice(cloned)
khenaidood948f772021-08-11 17:49:24 -0400250
khenaidooab1f7bd2019-11-14 14:00:27 -0500251 }()
khenaidood948f772021-08-11 17:49:24 -0400252 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500253}
254
khenaidood948f772021-08-11 17:49:24 -0400255func (onuA *ONUAdapter) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500256 go func() {
257 if d := onuA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000258 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500259 }
260
261 cloned := proto.Clone(device).(*voltha.Device)
khenaidood948f772021-08-11 17:49:24 -0400262
263 c, err := onuA.GetCoreClient()
264 if err != nil {
265 return
266 }
267
khenaidooab1f7bd2019-11-14 14:00:27 -0500268 // Update the all ports state on that device to enable
khenaidood948f772021-08-11 17:49:24 -0400269 if _, err := c.PortsStateUpdate(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400270 &ca.PortStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400271 DeviceId: cloned.Id,
272 PortTypeFilter: 0,
273 OperStatus: common.OperStatus_ACTIVE,
274 }); err != nil {
275 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500276 }
277
278 //Update the device state
khenaidood948f772021-08-11 17:49:24 -0400279 cloned.ConnectStatus = common.ConnectStatus_REACHABLE
280 cloned.OperStatus = common.OperStatus_ACTIVE
khenaidooab1f7bd2019-11-14 14:00:27 -0500281
khenaidoo9beaaf12021-10-19 17:32:01 -0400282 if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400283 DeviceId: cloned.Id,
284 OperStatus: cloned.OperStatus,
285 ConnStatus: cloned.ConnectStatus,
286 }); err != nil {
287 // Device may already have been deleted in the core
divyadesaicb8b59d2020-08-18 09:55:47 +0000288 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidood948f772021-08-11 17:49:24 -0400289 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500290 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400291 onuA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500292 }()
khenaidood948f772021-08-11 17:49:24 -0400293 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500294}
khenaidoo67b22152020-03-02 16:01:25 -0500295
khenaidoo9beaaf12021-10-19 17:32:01 -0400296func (onuA *ONUAdapter) StartOmciTest(ctx context.Context, _ *ca.OMCITest) (*omci.TestResponse, error) { // nolint
297 return &omci.TestResponse{Result: omci.TestResponse_SUCCESS}, nil
yasin sapli5458a1c2021-06-14 22:24:38 +0000298}