blob: 9543a7ea76f3c3f5715168179bcfe5bd45da4deb [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 (
Rohan Agrawal31f21802020-06-12 05:38:46 +000020 "context"
khenaidoo8b4abbf2020-04-24 17:04:30 -040021 "fmt"
npujar1d86a522019-11-14 17:11:16 +053022 "strconv"
23 "strings"
24 "sync"
khenaidood948f772021-08-11 17:49:24 -040025 "time"
npujar1d86a522019-11-14 17:11:16 +053026
khenaidood948f772021-08-11 17:49:24 -040027 "github.com/golang/protobuf/ptypes/empty"
28 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
29 "github.com/opencord/voltha-lib-go/v7/pkg/log"
30 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
31 "github.com/opencord/voltha-protos/v5/go/adapter_services"
32 "github.com/opencord/voltha-protos/v5/go/common"
33 "github.com/opencord/voltha-protos/v5/go/core"
34 "google.golang.org/grpc"
35
36 "github.com/opencord/voltha-protos/v5/go/extension"
37 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
38 "github.com/opencord/voltha-protos/v5/go/openflow_13"
39 "github.com/opencord/voltha-protos/v5/go/voltha"
40)
41
42const (
43 numONUPerOLT = 4
44 startingUNIPortNo = 100
khenaidooab1f7bd2019-11-14 14:00:27 -050045)
46
47func macAddressToUint32Array(mac string) []uint32 {
48 slist := strings.Split(mac, ":")
49 result := make([]uint32, len(slist))
50 var err error
51 var tmp int64
52 for index, val := range slist {
53 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
54 return []uint32{1, 2, 3, 4, 5, 6}
55 }
56 result[index] = uint32(tmp)
57 }
58 return result
59}
60
khenaidood948f772021-08-11 17:49:24 -040061// GetNumONUPerOLT returns number of ONUs per OLT
62func GetNumONUPerOLT() int {
63 return numONUPerOLT
64}
65
66// Returns the starting UNI port number
67func GetStartingUNIPortNo() int {
68 return startingUNIPortNo
69}
70
npujar1d86a522019-11-14 17:11:16 +053071// Adapter represents adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050072type Adapter struct {
khenaidood948f772021-08-11 17:49:24 -040073 flows map[string]map[uint64]*openflow_13.OfpFlowStats
74 flowLock sync.RWMutex
75 devices map[string]*voltha.Device
76 deviceLock sync.RWMutex
77 failFlowAdd map[string]bool
78 failFlowAddLock sync.RWMutex
79 failFlowDelete map[string]bool
80 failFlowDeleteLock sync.RWMutex
81 failDeleteDevice map[string]bool
82 failDeleteDeviceLock sync.RWMutex
83 coreEnpoint string
84 coreClient *vgrpc.Client
85 serviceEndpoint string
86 DeviceType string
87 vendor string
88 Probe *probe.Probe
khenaidooab1f7bd2019-11-14 14:00:27 -050089}
90
npujar1d86a522019-11-14 17:11:16 +053091// NewAdapter creates adapter instance
khenaidood948f772021-08-11 17:49:24 -040092func NewAdapter(serviceEndpoint, coreEndpoint, deviceType, vendor string) *Adapter {
khenaidooab1f7bd2019-11-14 14:00:27 -050093 return &Adapter{
khenaidood948f772021-08-11 17:49:24 -040094 flows: map[string]map[uint64]*openflow_13.OfpFlowStats{},
95 devices: map[string]*voltha.Device{},
96 failFlowAdd: map[string]bool{},
97 failFlowDelete: map[string]bool{},
98 failDeleteDevice: map[string]bool{},
99 coreEnpoint: coreEndpoint,
100 serviceEndpoint: serviceEndpoint,
101 DeviceType: deviceType,
102 vendor: vendor,
khenaidooab1f7bd2019-11-14 14:00:27 -0500103 }
104}
105
khenaidood948f772021-08-11 17:49:24 -0400106func (ta *Adapter) IsReady() bool {
107 return ta.Probe.IsReady()
108}
109
khenaidooab1f7bd2019-11-14 14:00:27 -0500110func (ta *Adapter) storeDevice(d *voltha.Device) {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400111 ta.deviceLock.Lock()
112 defer ta.deviceLock.Unlock()
khenaidooab1f7bd2019-11-14 14:00:27 -0500113 if d != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400114 ta.devices[d.Id] = d
khenaidooab1f7bd2019-11-14 14:00:27 -0500115 }
116}
117
khenaidooab1f7bd2019-11-14 14:00:27 -0500118func (ta *Adapter) getDevice(id string) *voltha.Device {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400119 ta.deviceLock.RLock()
120 defer ta.deviceLock.RUnlock()
121 return ta.devices[id]
khenaidooab1f7bd2019-11-14 14:00:27 -0500122}
123
khenaidoo8b4abbf2020-04-24 17:04:30 -0400124func (ta *Adapter) updateDevice(d *voltha.Device) {
125 ta.storeDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500126}
127
khenaidood948f772021-08-11 17:49:24 -0400128func (ta *Adapter) GetEndPoint() string {
129 return ta.serviceEndpoint
khenaidooab1f7bd2019-11-14 14:00:27 -0500130}
npujar1d86a522019-11-14 17:11:16 +0530131
khenaidood948f772021-08-11 17:49:24 -0400132func (ta *Adapter) GetCoreClient() (core.CoreServiceClient, error) {
133 // Wait until the Core is up and running
134 for {
135 if ta.coreClient != nil {
136 client, err := ta.coreClient.GetClient()
137 if err != nil {
138 logger.Infow(context.Background(), "got-error-core-client", log.Fields{"error": err})
139 time.Sleep(1 * time.Second)
140 continue
141 }
142 c, ok := client.(core.CoreServiceClient)
143 if ok {
144 logger.Debug(context.Background(), "got-valid-client")
145 return c, nil
146 }
147 }
148 logger.Info(context.Background(), "waiting-for-grpc-core-client")
149 time.Sleep(1 * time.Second)
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530150 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500151}
152
khenaidood948f772021-08-11 17:49:24 -0400153// Helper methods
154// startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
155func (ta *Adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_services.AdapterServiceServer, serviceName string) {
156 logger.Infow(ctx, "service-created", log.Fields{"service": serviceName})
157
158 server.AddService(func(gs *grpc.Server) { adapter_services.RegisterAdapterServiceServer(gs, handler) })
159 logger.Infow(ctx, "service-added", log.Fields{"service": serviceName})
160
161 ta.Probe.UpdateStatus(ctx, serviceName, probe.ServiceStatusRunning)
162 logger.Infow(ctx, "service-started", log.Fields{"service": serviceName})
163
164 // Note that there is a small window here in which the core could return its status as ready,
165 // when it really isn't. This is unlikely to cause issues, as the delay is incredibly short.
166 server.Start(ctx)
167 ta.Probe.UpdateStatus(ctx, serviceName, probe.ServiceStatusStopped)
khenaidooab1f7bd2019-11-14 14:00:27 -0500168}
169
khenaidood948f772021-08-11 17:49:24 -0400170func setAndTestCoreServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
171 svc := core.NewCoreServiceClient(conn)
172 if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != voltha.HealthStatus_HEALTHY {
173 return nil
174 }
175 return svc
khenaidooab1f7bd2019-11-14 14:00:27 -0500176}
177
khenaidood948f772021-08-11 17:49:24 -0400178// gRPC service
179func (ta *Adapter) GetHealthStatus(ctx context.Context, empty *empty.Empty) (*voltha.HealthStatus, error) {
180 return &voltha.HealthStatus{State: voltha.HealthStatus_HEALTHY}, nil
181}
182
183// Device
184
185func (ta *Adapter) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
186 return &empty.Empty{}, nil
187}
188
189func (ta *Adapter) ReconcileDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
190 return &empty.Empty{}, nil
191}
192
193func (ta *Adapter) DeleteDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
194 ta.failDeleteDeviceLock.RLock()
195 haveToFail, ok := ta.failDeleteDevice[device.Id]
196 if ok && haveToFail {
197 ta.failDeleteDeviceLock.RUnlock()
198 return nil, fmt.Errorf("delete-device-failure")
199 }
200 ta.failDeleteDeviceLock.RUnlock()
201 if ok {
202 ta.RemoveDevice(device.Id)
203 }
204 logger.Debugw(ctx, "device-deleted-in-adapter", log.Fields{"device-id": device.Id})
205 return &empty.Empty{}, nil
206}
207
208func (ta *Adapter) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
209 return &empty.Empty{}, nil
210}
211
212func (ta *Adapter) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
213 return &empty.Empty{}, nil
214}
215
216func (ta *Adapter) RebootDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
217 return &empty.Empty{}, nil
218}
219
220func (ta *Adapter) SelfTestDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
221 return &empty.Empty{}, nil
222}
223
224func (ta *Adapter) ChildDeviceLost(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
225 return &empty.Empty{}, nil
226}
227
228func (ta *Adapter) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error) {
229 return nil, nil
230}
231
232// Ports
233
234func (ta *Adapter) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
235 return &empty.Empty{}, nil
236}
237
238func (ta *Adapter) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
239 return &empty.Empty{}, nil
240}
241
242// Flows
243func (ta *Adapter) UpdateFlowsBulk(ctx context.Context, flows *ic.BulkFlows) (*empty.Empty, error) {
244 return &empty.Empty{}, nil
245}
246
247func (ta *Adapter) UpdateFlowsIncrementally(ctx context.Context, incrFlows *ic.IncrementalFlows) (*empty.Empty, error) {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400248 ta.flowLock.Lock()
249 defer ta.flowLock.Unlock()
250
khenaidood948f772021-08-11 17:49:24 -0400251 if _, ok := ta.flows[incrFlows.Device.Id]; !ok {
252 ta.flows[incrFlows.Device.Id] = map[uint64]*openflow_13.OfpFlowStats{}
253 }
254
255 if incrFlows.Flows.ToAdd != nil && len(incrFlows.Flows.ToAdd.Items) > 0 {
256 ta.failFlowAddLock.RLock()
257 if haveToFail, ok := ta.failFlowAdd[incrFlows.Device.Id]; ok && haveToFail {
258 ta.failFlowAddLock.RUnlock()
259 return nil, fmt.Errorf("flow-add-error")
khenaidoo8b4abbf2020-04-24 17:04:30 -0400260 }
khenaidood948f772021-08-11 17:49:24 -0400261 ta.failFlowAddLock.RUnlock()
262 for _, f := range incrFlows.Flows.ToAdd.Items {
263 ta.flows[incrFlows.Device.Id][f.Id] = f
khenaidoo8b4abbf2020-04-24 17:04:30 -0400264 }
265 }
khenaidood948f772021-08-11 17:49:24 -0400266 if incrFlows.Flows.ToRemove != nil && len(incrFlows.Flows.ToRemove.Items) > 0 {
267 ta.failFlowDeleteLock.RLock()
268 if haveToFail, ok := ta.failFlowDelete[incrFlows.Device.Id]; ok && haveToFail {
269 ta.failFlowDeleteLock.RUnlock()
270 return nil, fmt.Errorf("flow-delete-error")
khenaidoo8b4abbf2020-04-24 17:04:30 -0400271 }
khenaidood948f772021-08-11 17:49:24 -0400272 ta.failFlowDeleteLock.RUnlock()
273 for _, f := range incrFlows.Flows.ToRemove.Items {
274 delete(ta.flows[incrFlows.Device.Id], f.Id)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400275 }
276 }
khenaidood948f772021-08-11 17:49:24 -0400277 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500278}
279
khenaidood948f772021-08-11 17:49:24 -0400280//Packets
281func (ta *Adapter) SendPacketOut(ctx context.Context, packet *ic.PacketOut) (*empty.Empty, error) {
282 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500283}
284
khenaidood948f772021-08-11 17:49:24 -0400285// PM
286func (ta *Adapter) UpdatePmConfig(ctx context.Context, configs *ic.PmConfigsInfo) (*empty.Empty, error) {
287 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500288}
289
khenaidood948f772021-08-11 17:49:24 -0400290// Image
291func (ta *Adapter) DownloadOnuImage(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) {
292 return &voltha.DeviceImageResponse{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500293}
294
khenaidood948f772021-08-11 17:49:24 -0400295func (ta *Adapter) GetOnuImageStatus(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
296 return &voltha.DeviceImageResponse{}, nil
npujar1d86a522019-11-14 17:11:16 +0530297}
298
khenaidood948f772021-08-11 17:49:24 -0400299func (ta *Adapter) AbortOnuImageUpgrade(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
300 return &voltha.DeviceImageResponse{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500301}
302
khenaidood948f772021-08-11 17:49:24 -0400303func (ta *Adapter) GetOnuImages(ctx context.Context, id *common.ID) (*voltha.OnuImages, error) {
304 return &voltha.OnuImages{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500305}
306
khenaidood948f772021-08-11 17:49:24 -0400307func (ta *Adapter) ActivateOnuImage(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
308 return &voltha.DeviceImageResponse{}, nil
309}
310
311func (ta *Adapter) CommitOnuImage(ctx context.Context, request *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
312 return &voltha.DeviceImageResponse{}, nil
313}
314
315// Deprecated image APIs
316func (ta *Adapter) DownloadImage(ctx context.Context, in *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
317 return &voltha.ImageDownload{}, nil
318}
319
320func (ta *Adapter) GetImageDownloadStatus(ctx context.Context, in *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
321 return &voltha.ImageDownload{}, nil
322}
323
324func (ta *Adapter) CancelImageDownload(ctx context.Context, in *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
325 return &voltha.ImageDownload{}, nil
326}
327
328func (ta *Adapter) ActivateImageUpdate(ctx context.Context, in *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
329 return &voltha.ImageDownload{}, nil
330}
331
332func (ta *Adapter) RevertImageUpdate(ctx context.Context, in *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
333 return &voltha.ImageDownload{}, nil
334}
335
336// OMCI test
337func (ta *Adapter) StartOmciTest(ctx context.Context, test *ic.OMCITest) (*voltha.TestResponse, error) {
khenaidooab1f7bd2019-11-14 14:00:27 -0500338 return nil, nil
339}
340
khenaidood948f772021-08-11 17:49:24 -0400341// Events
342func (ta *Adapter) SuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
343 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500344}
345
khenaidood948f772021-08-11 17:49:24 -0400346func (ta *Adapter) UnSuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
347 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500348}
349
khenaidood948f772021-08-11 17:49:24 -0400350func (ta *Adapter) SimulateAlarm(context.Context, *ic.SimulateAlarmMessage) (*common.OperationResp, error) {
351 return &common.OperationResp{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500352}
353
khenaidood948f772021-08-11 17:49:24 -0400354func (ta *Adapter) GetExtValue(context.Context, *ic.GetExtValueMessage) (*common.ReturnValues, error) {
355 return &common.ReturnValues{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500356}
kesavandbc2d1622020-01-21 00:42:01 -0500357
khenaidood948f772021-08-11 17:49:24 -0400358func (ta *Adapter) SetExtValue(context.Context, *ic.SetExtValueMessage) (*empty.Empty, error) {
359 return &empty.Empty{}, nil
kesavandbc2d1622020-01-21 00:42:01 -0500360}
361
khenaidood948f772021-08-11 17:49:24 -0400362func (ta *Adapter) GetSingleValue(context.Context, *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
363 return &extension.SingleGetValueResponse{}, nil
kesavandbc2d1622020-01-21 00:42:01 -0500364}
Scott Baker0e78ba22020-02-24 17:58:47 -0800365
khenaidood948f772021-08-11 17:49:24 -0400366func (ta *Adapter) SetSingleValue(context.Context, *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) {
367 return &extension.SingleSetValueResponse{}, nil
Scott Baker0e78ba22020-02-24 17:58:47 -0800368}
Scott Baker432f9be2020-03-26 11:56:30 -0700369
khenaidood948f772021-08-11 17:49:24 -0400370// APIs for test ONLY
khenaidoo8b4abbf2020-04-24 17:04:30 -0400371// GetFlowCount returns the total number of flows presently under this adapter
khenaidood948f772021-08-11 17:49:24 -0400372func (ta *Adapter) GetFlowCount(deviceID string) int {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400373 ta.flowLock.RLock()
374 defer ta.flowLock.RUnlock()
375
khenaidood948f772021-08-11 17:49:24 -0400376 if _, ok := ta.flows[deviceID]; ok {
377 return len(ta.flows[deviceID])
378 }
379 return 0
khenaidoo8b4abbf2020-04-24 17:04:30 -0400380}
381
khenaidood948f772021-08-11 17:49:24 -0400382// RemoveDevice removes all flows in this adapter
383func (ta *Adapter) RemoveDevice(deviceID string) {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400384 ta.flowLock.Lock()
385 defer ta.flowLock.Unlock()
khenaidood948f772021-08-11 17:49:24 -0400386 ta.failFlowAddLock.Lock()
387 defer ta.failFlowAddLock.Unlock()
388 ta.failFlowDeleteLock.Lock()
389 defer ta.failFlowDeleteLock.Unlock()
khenaidoo8b4abbf2020-04-24 17:04:30 -0400390
khenaidood948f772021-08-11 17:49:24 -0400391 delete(ta.flows, deviceID)
392 delete(ta.failFlowAdd, deviceID)
393 delete(ta.failFlowDelete, deviceID)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400394}
395
396// SetFlowAction sets the adapter action on addition and deletion of flows
khenaidood948f772021-08-11 17:49:24 -0400397func (ta *Adapter) SetFlowAction(deviceID string, failFlowAdd, failFlowDelete bool) {
398 ta.failFlowAddLock.Lock()
399 defer ta.failFlowAddLock.Unlock()
400 ta.failFlowDeleteLock.Lock()
401 defer ta.failFlowDeleteLock.Unlock()
402 ta.failFlowAdd[deviceID] = failFlowAdd
403 ta.failFlowDelete[deviceID] = failFlowDelete
khenaidoo8b4abbf2020-04-24 17:04:30 -0400404}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530405
406// SetDeleteAction sets the adapter action on delete device
khenaidood948f772021-08-11 17:49:24 -0400407func (ta *Adapter) SetDeleteAction(deviceID string, failDeleteDevice bool) {
408 ta.failDeleteDeviceLock.Lock()
409 defer ta.failDeleteDeviceLock.Unlock()
410 ta.failDeleteDevice[deviceID] = failDeleteDevice
yasin sapli5458a1c2021-06-14 22:24:38 +0000411}