Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package vpagent |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "time" |
| 21 | |
| 22 | "voltha-go-controller/internal/pkg/intf" |
| 23 | |
| 24 | "github.com/golang/protobuf/ptypes/empty" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 25 | "voltha-go-controller/log" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 27 | ) |
| 28 | |
| 29 | func (vpa *VPAgent) synchronizeDeviceList(ctx context.Context) { |
| 30 | // Send reconnection indication to the devices already known |
| 31 | for _, vpc := range vpa.clientMap { |
| 32 | vpc.ConnectInd(context.TODO(), intf.DeviceReDisc) |
| 33 | } |
| 34 | |
| 35 | // Refresh once to get everything started |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 36 | vpa.refreshDeviceList(ctx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 37 | |
| 38 | tick := time.NewTicker(vpa.DeviceListRefreshInterval) |
| 39 | loop: |
| 40 | for { |
| 41 | select { |
| 42 | case <-ctx.Done(): |
| 43 | logger.Errorw(ctx, "Context Done", log.Fields{"Context": ctx}) |
| 44 | break loop |
| 45 | case <-tick.C: |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 46 | vpa.refreshDeviceList(ctx) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | tick.Stop() |
| 50 | } |
| 51 | |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 52 | func (vpa *VPAgent) refreshDeviceList(cntx context.Context) { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 53 | // If we exit, assume disconnected |
| 54 | if vpa.volthaClient == nil { |
| 55 | logger.Error(ctx, "no-voltha-connection") |
| 56 | vpa.events <- vpaEventVolthaDisconnected |
| 57 | return |
| 58 | } |
| 59 | deviceList, err := vpa.volthaClient.Get().ListLogicalDevices(context.Background(), &empty.Empty{}) |
| 60 | if err != nil { |
| 61 | logger.Errorw(ctx, "vpagent failed to query device list from voltha", |
| 62 | log.Fields{"error": err}) |
| 63 | vpa.events <- vpaEventVolthaDisconnected |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | var toAdd []int |
| 68 | var toDel []string |
| 69 | var deviceIDMap = make(map[string]string) |
| 70 | for index, d := range deviceList.Items { |
| 71 | deviceID := d.Id |
| 72 | deviceIDMap[deviceID] = deviceID |
| 73 | if vpa.clientMap[deviceID] == nil { |
| 74 | toAdd = append(toAdd, index) |
| 75 | } |
| 76 | } |
| 77 | for key := range vpa.clientMap { |
| 78 | deviceID, ok := deviceIDMap[key] |
| 79 | if !ok || (ok && deviceID == "") { |
| 80 | toDel = append(toDel, key) |
| 81 | } |
| 82 | } |
| 83 | logger.Debugw(ctx, "Device Refresh", log.Fields{"ToAdd": toAdd, "ToDel": toDel}) |
| 84 | for i := 0; i < len(toAdd); i++ { |
| 85 | device := deviceList.Items[toAdd[i]] |
| 86 | serialNum := device.Desc.SerialNum |
| 87 | // If the blocked device list contain device serial number, do not add VPClient. |
| 88 | if vpa.VPClientAgent.IsBlockedDevice(serialNum) { |
| 89 | logger.Debugw(ctx, "Device Serial Number is present in the blocked device list", log.Fields{"device-serial-number": serialNum}) |
| 90 | } else { |
| 91 | vpa.addVPClient(device) // client is started in addVPClient |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | for i := 0; i < len(toDel); i++ { |
Tinoj Joseph | 07cc537 | 2022-07-18 22:53:51 +0530 | [diff] [blame] | 96 | vpa.VPClientAgent.DelDevice(cntx, toDel[i]) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 97 | vpa.mapLock.Lock() |
| 98 | delete(vpa.clientMap, toDel[i]) |
| 99 | vpa.mapLock.Unlock() |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func (vpa *VPAgent) addVPClient(device *voltha.LogicalDevice) intf.IVPClient { |
| 104 | logger.Warnw(ctx, "GrpcClient addClient called ", log.Fields{"device-id": device.Id}) |
| 105 | vpa.mapLock.Lock() |
| 106 | defer vpa.mapLock.Unlock() |
| 107 | var serialNum = "Unknown" |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 108 | var mfrDesc = "Unknown" |
| 109 | var hwDesc = "Unknown" |
| 110 | var swDesc = "Unknown" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 111 | if device.Desc != nil { |
| 112 | serialNum = device.Desc.SerialNum |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 113 | mfrDesc = device.Desc.MfrDesc |
| 114 | hwDesc = device.Desc.HwDesc |
| 115 | swDesc = device.Desc.SwDesc |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 116 | } |
| 117 | vpc := vpa.clientMap[device.Id] |
| 118 | if vpc == nil { |
| 119 | vpa.VPClientAgent.AddNewDevice(&intf.VPClientCfg{ |
| 120 | DeviceID: device.Id, |
| 121 | SerialNum: serialNum, |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 122 | MfrDesc: mfrDesc, |
| 123 | HwDesc: hwDesc, |
| 124 | SwDesc: swDesc, |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 125 | SouthBoundID: device.RootDeviceId, |
Tinoj Joseph | 429b9d9 | 2022-11-16 18:51:05 +0530 | [diff] [blame] | 126 | TimeStamp: time.Now(), |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 127 | VolthaClient: vpa.volthaClient, |
| 128 | PacketOutChannel: vpa.packetOutChannel, |
| 129 | }) |
| 130 | |
| 131 | } |
| 132 | logger.Debugw(ctx, "Finished with addClient", log.Fields{"deviceID": device.Id}) |
| 133 | return vpc |
| 134 | } |
| 135 | |
| 136 | //AddClientToClientMap - called by controller once device obj is created |
| 137 | func (vpa *VPAgent) AddClientToClientMap(deviceID string, vpc intf.IVPClient) { |
| 138 | vpa.mapLock.Lock() |
| 139 | defer vpa.mapLock.Unlock() |
| 140 | |
| 141 | if vpc != nil { |
| 142 | vpa.clientMap[deviceID] = vpc |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func (vpa *VPAgent) getVPClient(deviceID string) intf.IVPClient { |
| 147 | if vpc, ok := vpa.clientMap[deviceID]; ok { |
| 148 | return vpc |
| 149 | } |
| 150 | return nil |
| 151 | } |