David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 the original author or authors. |
| 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 | */ |
| 16 | |
| 17 | package ofagent |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "github.com/golang/protobuf/ptypes/empty" |
| 22 | "github.com/opencord/ofagent-go/internal/pkg/openflow" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 24 | "time" |
| 25 | ) |
| 26 | |
| 27 | func (ofa *OFAgent) synchronizeDeviceList(ctx context.Context) { |
| 28 | // Refresh once to get everything started |
| 29 | ofa.refreshDeviceList() |
| 30 | |
| 31 | tick := time.NewTicker(ofa.DeviceListRefreshInterval) |
| 32 | loop: |
| 33 | for { |
| 34 | select { |
| 35 | case <-ctx.Done(): |
| 36 | break loop |
| 37 | case <-tick.C: |
| 38 | ofa.refreshDeviceList() |
| 39 | } |
| 40 | } |
| 41 | tick.Stop() |
| 42 | } |
| 43 | |
| 44 | func (ofa *OFAgent) refreshDeviceList() { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 45 | // If we exit, assume disconnected |
| 46 | if ofa.volthaClient == nil { |
| 47 | logger.Error("no-voltha-connection") |
| 48 | ofa.events <- ofaEventVolthaDisconnected |
| 49 | return |
| 50 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 51 | deviceList, err := ofa.volthaClient.ListLogicalDevices(context.Background(), &empty.Empty{}) |
| 52 | if err != nil { |
| 53 | logger.Errorw("ofagent failed to query device list from voltha", |
| 54 | log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 55 | ofa.events <- ofaEventVolthaDisconnected |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 56 | return |
| 57 | } |
| 58 | devices := deviceList.GetItems() |
| 59 | |
| 60 | var toAdd []string |
| 61 | var toDel []string |
| 62 | var deviceIDMap = make(map[string]string) |
| 63 | for i := 0; i < len(devices); i++ { |
| 64 | deviceID := devices[i].GetId() |
| 65 | deviceIDMap[deviceID] = deviceID |
| 66 | if ofa.clientMap[deviceID] == nil { |
| 67 | toAdd = append(toAdd, deviceID) |
| 68 | } |
| 69 | } |
| 70 | for key := range ofa.clientMap { |
| 71 | if deviceIDMap[key] == "" { |
| 72 | toDel = append(toDel, key) |
| 73 | } |
| 74 | } |
| 75 | logger.Debugw("GrpcClient refreshDeviceList", log.Fields{"ToAdd": toAdd, "ToDel": toDel}) |
| 76 | for i := 0; i < len(toAdd); i++ { |
David K. Bainbridge | 5537626 | 2020-01-22 23:28:27 -0800 | [diff] [blame] | 77 | ofa.addOFClient(toAdd[i]) // client is started in addOFClient |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 78 | } |
| 79 | for i := 0; i < len(toDel); i++ { |
| 80 | ofa.clientMap[toDel[i]].Stop() |
| 81 | ofa.mapLock.Lock() |
| 82 | delete(ofa.clientMap, toDel[i]) |
| 83 | ofa.mapLock.Unlock() |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | func (ofa *OFAgent) addOFClient(deviceID string) *openflow.OFClient { |
| 88 | logger.Debugw("GrpcClient addClient called ", log.Fields{"device-id": deviceID}) |
| 89 | ofa.mapLock.Lock() |
| 90 | ofc := ofa.clientMap[deviceID] |
| 91 | if ofc == nil { |
| 92 | ofc = openflow.NewOFClient(&openflow.OFClient{ |
| 93 | DeviceID: deviceID, |
| 94 | OFControllerEndPoint: ofa.OFControllerEndPoint, |
| 95 | VolthaClient: ofa.volthaClient, |
| 96 | PacketOutChannel: ofa.packetOutChannel, |
| 97 | ConnectionMaxRetries: ofa.ConnectionMaxRetries, |
| 98 | ConnectionRetryDelay: ofa.ConnectionRetryDelay, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 99 | }) |
| 100 | go ofc.Run(context.Background()) |
| 101 | ofa.clientMap[deviceID] = ofc |
| 102 | } |
| 103 | ofa.mapLock.Unlock() |
| 104 | logger.Debugw("Finished with addClient", log.Fields{"deviceID": deviceID}) |
| 105 | return ofc |
| 106 | } |
| 107 | |
| 108 | func (ofa *OFAgent) getOFClient(deviceID string) *openflow.OFClient { |
| 109 | ofc := ofa.clientMap[deviceID] |
| 110 | if ofc == nil { |
| 111 | ofc = ofa.addOFClient(deviceID) |
| 112 | } |
| 113 | return ofc |
| 114 | } |