blob: 8f68ef09031031e9f1f29917110addddd5611ed8 [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"
23 "github.com/opencord/voltha-lib-go/v2/pkg/log"
24 "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++ {
70 var client = ofa.addOFClient(toAdd[i])
71 go client.Run(context.Background())
72 }
73 for i := 0; i < len(toDel); i++ {
74 ofa.clientMap[toDel[i]].Stop()
75 ofa.mapLock.Lock()
76 delete(ofa.clientMap, toDel[i])
77 ofa.mapLock.Unlock()
78 }
79}
80
81func (ofa *OFAgent) addOFClient(deviceID string) *openflow.OFClient {
82 logger.Debugw("GrpcClient addClient called ", log.Fields{"device-id": deviceID})
83 ofa.mapLock.Lock()
84 ofc := ofa.clientMap[deviceID]
85 if ofc == nil {
86 ofc = openflow.NewOFClient(&openflow.OFClient{
87 DeviceID: deviceID,
88 OFControllerEndPoint: ofa.OFControllerEndPoint,
89 VolthaClient: ofa.volthaClient,
90 PacketOutChannel: ofa.packetOutChannel,
91 ConnectionMaxRetries: ofa.ConnectionMaxRetries,
92 ConnectionRetryDelay: ofa.ConnectionRetryDelay,
93 KeepRunning: true,
94 })
95 go ofc.Run(context.Background())
96 ofa.clientMap[deviceID] = ofc
97 }
98 ofa.mapLock.Unlock()
99 logger.Debugw("Finished with addClient", log.Fields{"deviceID": deviceID})
100 return ofc
101}
102
103func (ofa *OFAgent) getOFClient(deviceID string) *openflow.OFClient {
104 ofc := ofa.clientMap[deviceID]
105 if ofc == nil {
106 ofc = ofa.addOFClient(deviceID)
107 }
108 return ofc
109}