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