blob: 275ba2e937ad1854bd6fd2a07c0d22bfef1fae50 [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"
21 "github.com/golang/protobuf/ptypes/empty"
22 "github.com/opencord/ofagent-go/internal/pkg/openflow"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080023 "github.com/opencord/voltha-lib-go/v3/pkg/log"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080024 "time"
25)
26
27func (ofa *OFAgent) synchronizeDeviceList(ctx context.Context) {
28 // Refresh once to get everything started
29 ofa.refreshDeviceList()
30
31 tick := time.NewTicker(ofa.DeviceListRefreshInterval)
32loop:
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
44func (ofa *OFAgent) refreshDeviceList() {
45 deviceList, err := ofa.volthaClient.ListLogicalDevices(context.Background(), &empty.Empty{})
46 if err != nil {
47 logger.Errorw("ofagent failed to query device list from voltha",
48 log.Fields{"error": err})
49 return
50 }
51 devices := deviceList.GetItems()
52
53 var toAdd []string
54 var toDel []string
55 var deviceIDMap = make(map[string]string)
56 for i := 0; i < len(devices); i++ {
57 deviceID := devices[i].GetId()
58 deviceIDMap[deviceID] = deviceID
59 if ofa.clientMap[deviceID] == nil {
60 toAdd = append(toAdd, deviceID)
61 }
62 }
63 for key := range ofa.clientMap {
64 if deviceIDMap[key] == "" {
65 toDel = append(toDel, key)
66 }
67 }
68 logger.Debugw("GrpcClient refreshDeviceList", log.Fields{"ToAdd": toAdd, "ToDel": toDel})
69 for i := 0; i < len(toAdd); i++ {
David K. Bainbridge55376262020-01-22 23:28:27 -080070 ofa.addOFClient(toAdd[i]) // client is started in addOFClient
David K. Bainbridge157bdab2020-01-16 14:38:05 -080071 }
72 for i := 0; i < len(toDel); i++ {
73 ofa.clientMap[toDel[i]].Stop()
74 ofa.mapLock.Lock()
75 delete(ofa.clientMap, toDel[i])
76 ofa.mapLock.Unlock()
77 }
78}
79
80func (ofa *OFAgent) addOFClient(deviceID string) *openflow.OFClient {
81 logger.Debugw("GrpcClient addClient called ", log.Fields{"device-id": deviceID})
82 ofa.mapLock.Lock()
83 ofc := ofa.clientMap[deviceID]
84 if ofc == nil {
85 ofc = openflow.NewOFClient(&openflow.OFClient{
86 DeviceID: deviceID,
87 OFControllerEndPoint: ofa.OFControllerEndPoint,
88 VolthaClient: ofa.volthaClient,
89 PacketOutChannel: ofa.packetOutChannel,
90 ConnectionMaxRetries: ofa.ConnectionMaxRetries,
91 ConnectionRetryDelay: ofa.ConnectionRetryDelay,
David K. Bainbridge157bdab2020-01-16 14:38:05 -080092 })
93 go ofc.Run(context.Background())
94 ofa.clientMap[deviceID] = ofc
95 }
96 ofa.mapLock.Unlock()
97 logger.Debugw("Finished with addClient", log.Fields{"deviceID": deviceID})
98 return ofc
99}
100
101func (ofa *OFAgent) getOFClient(deviceID string) *openflow.OFClient {
102 ofc := ofa.clientMap[deviceID]
103 if ofc == nil {
104 ofc = ofa.addOFClient(deviceID)
105 }
106 return ofc
107}