blob: 43cbb86cd9e5427fe3d72bbcd11506106f663f24 [file] [log] [blame]
David K. Bainbridge157bdab2020-01-16 14:38:05 -08001/*
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
17package ofagent
18
19import (
20 "context"
David Bainbridgef8ce7d22020-04-08 12:49:41 -070021 "time"
22
David K. Bainbridge157bdab2020-01-16 14:38:05 -080023 "github.com/golang/protobuf/ptypes/empty"
24 "github.com/opencord/ofagent-go/internal/pkg/openflow"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/log"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080026)
27
28func (ofa *OFAgent) synchronizeDeviceList(ctx context.Context) {
29 // Refresh once to get everything started
Rohan Agrawalc32d9932020-06-15 11:01:47 +000030 ofa.refreshDeviceList(ctx)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080031
32 tick := time.NewTicker(ofa.DeviceListRefreshInterval)
33loop:
34 for {
35 select {
36 case <-ctx.Done():
37 break loop
38 case <-tick.C:
Rohan Agrawalc32d9932020-06-15 11:01:47 +000039 ofa.refreshDeviceList(ctx)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080040 }
41 }
42 tick.Stop()
43}
44
Rohan Agrawalc32d9932020-06-15 11:01:47 +000045func (ofa *OFAgent) refreshDeviceList(ctx context.Context) {
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080046 // If we exit, assume disconnected
47 if ofa.volthaClient == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000048 logger.Error(ctx, "no-voltha-connection")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080049 ofa.events <- ofaEventVolthaDisconnected
50 return
51 }
David Bainbridgef8ce7d22020-04-08 12:49:41 -070052 deviceList, err := ofa.volthaClient.Get().ListLogicalDevices(context.Background(), &empty.Empty{})
David K. Bainbridge157bdab2020-01-16 14:38:05 -080053 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000054 logger.Errorw(ctx, "ofagent failed to query device list from voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080055 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080056 ofa.events <- ofaEventVolthaDisconnected
David K. Bainbridge157bdab2020-01-16 14:38:05 -080057 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 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000076 logger.Debugw(ctx, "GrpcClient refreshDeviceList", log.Fields{"ToAdd": toAdd, "ToDel": toDel})
David K. Bainbridge157bdab2020-01-16 14:38:05 -080077 for i := 0; i < len(toAdd); i++ {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000078 ofa.addOFClient(ctx, toAdd[i]) // client is started in addOFClient
David K. Bainbridge157bdab2020-01-16 14:38:05 -080079 }
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
Rohan Agrawalc32d9932020-06-15 11:01:47 +000088func (ofa *OFAgent) addOFClient(ctx context.Context, deviceID string) *openflow.OFClient {
89 logger.Debugw(ctx, "GrpcClient addClient called ", log.Fields{"device-id": deviceID})
David K. Bainbridge157bdab2020-01-16 14:38:05 -080090 ofa.mapLock.Lock()
91 ofc := ofa.clientMap[deviceID]
92 if ofc == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000093 ofc = openflow.NewOFClient(ctx, &openflow.OFClient{
Jonathan Hart4b110f62020-03-13 17:36:19 -070094 DeviceID: deviceID,
95 OFControllerEndPoints: ofa.OFControllerEndPoints,
96 VolthaClient: ofa.volthaClient,
97 PacketOutChannel: ofa.packetOutChannel,
98 ConnectionMaxRetries: ofa.ConnectionMaxRetries,
99 ConnectionRetryDelay: ofa.ConnectionRetryDelay,
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800100 })
Jonathan Hart4b110f62020-03-13 17:36:19 -0700101 ofc.Run(context.Background())
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800102 ofa.clientMap[deviceID] = ofc
103 }
104 ofa.mapLock.Unlock()
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000105 logger.Debugw(ctx, "Finished with addClient", log.Fields{"deviceID": deviceID})
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800106 return ofc
107}
108
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000109func (ofa *OFAgent) getOFClient(ctx context.Context, deviceID string) *openflow.OFClient {
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800110 ofc := ofa.clientMap[deviceID]
111 if ofc == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000112 ofc = ofa.addOFClient(ctx, deviceID)
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800113 }
114 return ofc
115}