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 | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v7/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 |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 30 | ofa.refreshDeviceList(ctx) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 31 | |
| 32 | tick := time.NewTicker(ofa.DeviceListRefreshInterval) |
| 33 | loop: |
| 34 | for { |
| 35 | select { |
| 36 | case <-ctx.Done(): |
| 37 | break loop |
| 38 | case <-tick.C: |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 39 | ofa.refreshDeviceList(ctx) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | tick.Stop() |
| 43 | } |
| 44 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 45 | func (ofa *OFAgent) refreshDeviceList(ctx context.Context) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 46 | span, ctx := log.CreateChildSpan(ctx, "refresh-device-list") |
| 47 | defer span.Finish() |
| 48 | |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 49 | // If we exit, assume disconnected |
| 50 | if ofa.volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 51 | logger.Error(ctx, "no-voltha-connection") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 52 | ofa.events <- ofaEventVolthaDisconnected |
| 53 | return |
| 54 | } |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 55 | vc := ofa.volthaClient.Get() |
| 56 | if vc == nil { |
| 57 | logger.Error(ctx, "No client found to query device list from voltha") |
| 58 | ofa.events <- ofaEventVolthaDisconnected |
| 59 | return |
| 60 | } |
| 61 | deviceList, err := vc.ListLogicalDevices(log.WithSpanFromContext(context.Background(), ctx), &empty.Empty{}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 62 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 63 | logger.Errorw(ctx, "ofagent failed to query device list from voltha", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 64 | log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 65 | ofa.events <- ofaEventVolthaDisconnected |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 66 | return |
| 67 | } |
| 68 | devices := deviceList.GetItems() |
| 69 | |
| 70 | var toAdd []string |
| 71 | var toDel []string |
| 72 | var deviceIDMap = make(map[string]string) |
| 73 | for i := 0; i < len(devices); i++ { |
| 74 | deviceID := devices[i].GetId() |
| 75 | deviceIDMap[deviceID] = deviceID |
| 76 | if ofa.clientMap[deviceID] == nil { |
| 77 | toAdd = append(toAdd, deviceID) |
| 78 | } |
| 79 | } |
| 80 | for key := range ofa.clientMap { |
| 81 | if deviceIDMap[key] == "" { |
| 82 | toDel = append(toDel, key) |
| 83 | } |
| 84 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 85 | logger.Debugw(ctx, "GrpcClient refreshDeviceList", log.Fields{"ToAdd": toAdd, "ToDel": toDel}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 86 | for i := 0; i < len(toAdd); i++ { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 87 | ofa.addOFClient(ctx, toAdd[i]) // client is started in addOFClient |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 88 | } |
| 89 | for i := 0; i < len(toDel); i++ { |
| 90 | ofa.clientMap[toDel[i]].Stop() |
| 91 | ofa.mapLock.Lock() |
| 92 | delete(ofa.clientMap, toDel[i]) |
| 93 | ofa.mapLock.Unlock() |
| 94 | } |
| 95 | } |
| 96 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 97 | func (ofa *OFAgent) addOFClient(ctx context.Context, deviceID string) *openflow.OFClient { |
| 98 | logger.Debugw(ctx, "GrpcClient addClient called ", log.Fields{"device-id": deviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 99 | ofa.mapLock.Lock() |
| 100 | ofc := ofa.clientMap[deviceID] |
| 101 | if ofc == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 102 | ofc = openflow.NewOFClient(ctx, &openflow.OFClient{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 103 | DeviceID: deviceID, |
| 104 | OFControllerEndPoints: ofa.OFControllerEndPoints, |
| 105 | VolthaClient: ofa.volthaClient, |
| 106 | PacketOutChannel: ofa.packetOutChannel, |
| 107 | ConnectionMaxRetries: ofa.ConnectionMaxRetries, |
| 108 | ConnectionRetryDelay: ofa.ConnectionRetryDelay, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 109 | }) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 110 | ofc.Run(context.Background()) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 111 | ofa.clientMap[deviceID] = ofc |
| 112 | } |
| 113 | ofa.mapLock.Unlock() |
divyadesai | 9a2c9b0 | 2020-08-18 06:40:51 +0000 | [diff] [blame] | 114 | logger.Debugw(ctx, "Finished with addClient", log.Fields{"device-id": deviceID}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 115 | return ofc |
| 116 | } |
| 117 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 118 | func (ofa *OFAgent) getOFClient(ctx context.Context, deviceID string) *openflow.OFClient { |
khenaidoo | 927391f | 2021-06-18 17:06:52 -0400 | [diff] [blame] | 119 | ofa.mapLock.Lock() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 120 | ofc := ofa.clientMap[deviceID] |
khenaidoo | 927391f | 2021-06-18 17:06:52 -0400 | [diff] [blame] | 121 | ofa.mapLock.Unlock() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 122 | if ofc == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 123 | ofc = ofa.addOFClient(ctx, deviceID) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 124 | } |
| 125 | return ofc |
| 126 | } |
khenaidoo | 927391f | 2021-06-18 17:06:52 -0400 | [diff] [blame] | 127 | |
| 128 | //clearAllOFClient clears all OF connections for all clients |
| 129 | func (ofa *OFAgent) clearAllOFClient() { |
| 130 | logger.Debug(context.Background(), "stopping-all-of-connections...") |
| 131 | ofa.mapLock.Lock() |
| 132 | for deviceID := range ofa.clientMap { |
| 133 | ofa.clientMap[deviceID].Stop() |
| 134 | delete(ofa.clientMap, deviceID) |
| 135 | } |
| 136 | ofa.mapLock.Unlock() |
| 137 | } |