blob: d50b3ec6fb810c58f0dfe4ad46a259e9ed998956 [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() {
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080045 // If we exit, assume disconnected
46 if ofa.volthaClient == nil {
47 logger.Error("no-voltha-connection")
48 ofa.events <- ofaEventVolthaDisconnected
49 return
50 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080051 deviceList, err := ofa.volthaClient.ListLogicalDevices(context.Background(), &empty.Empty{})
52 if err != nil {
53 logger.Errorw("ofagent failed to query device list from voltha",
54 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080055 ofa.events <- ofaEventVolthaDisconnected
David K. Bainbridge157bdab2020-01-16 14:38:05 -080056 return
57 }
58 devices := deviceList.GetItems()
59
60 var toAdd []string
61 var toDel []string
62 var deviceIDMap = make(map[string]string)
63 for i := 0; i < len(devices); i++ {
64 deviceID := devices[i].GetId()
65 deviceIDMap[deviceID] = deviceID
66 if ofa.clientMap[deviceID] == nil {
67 toAdd = append(toAdd, deviceID)
68 }
69 }
70 for key := range ofa.clientMap {
71 if deviceIDMap[key] == "" {
72 toDel = append(toDel, key)
73 }
74 }
75 logger.Debugw("GrpcClient refreshDeviceList", log.Fields{"ToAdd": toAdd, "ToDel": toDel})
76 for i := 0; i < len(toAdd); i++ {
David K. Bainbridge55376262020-01-22 23:28:27 -080077 ofa.addOFClient(toAdd[i]) // client is started in addOFClient
David K. Bainbridge157bdab2020-01-16 14:38:05 -080078 }
79 for i := 0; i < len(toDel); i++ {
80 ofa.clientMap[toDel[i]].Stop()
81 ofa.mapLock.Lock()
82 delete(ofa.clientMap, toDel[i])
83 ofa.mapLock.Unlock()
84 }
85}
86
87func (ofa *OFAgent) addOFClient(deviceID string) *openflow.OFClient {
88 logger.Debugw("GrpcClient addClient called ", log.Fields{"device-id": deviceID})
89 ofa.mapLock.Lock()
90 ofc := ofa.clientMap[deviceID]
91 if ofc == nil {
92 ofc = openflow.NewOFClient(&openflow.OFClient{
93 DeviceID: deviceID,
94 OFControllerEndPoint: ofa.OFControllerEndPoint,
95 VolthaClient: ofa.volthaClient,
96 PacketOutChannel: ofa.packetOutChannel,
97 ConnectionMaxRetries: ofa.ConnectionMaxRetries,
98 ConnectionRetryDelay: ofa.ConnectionRetryDelay,
David K. Bainbridge157bdab2020-01-16 14:38:05 -080099 })
100 go ofc.Run(context.Background())
101 ofa.clientMap[deviceID] = ofc
102 }
103 ofa.mapLock.Unlock()
104 logger.Debugw("Finished with addClient", log.Fields{"deviceID": deviceID})
105 return ofc
106}
107
108func (ofa *OFAgent) getOFClient(deviceID string) *openflow.OFClient {
109 ofc := ofa.clientMap[deviceID]
110 if ofc == nil {
111 ofc = ofa.addOFClient(deviceID)
112 }
113 return ofc
114}