forked from FlexSNR/l2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
174 lines (157 loc) · 5.88 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
//Copyright [2016] [SnapRoute Inc]
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// _______ __ __________ ___ _______.____ __ ____ __ .___________. ______ __ __
// | ____|| | | ____\ \ / / / |\ \ / \ / / | | | | / || | | |
// | |__ | | | |__ \ V / | (----` \ \/ \/ / | | `---| |----`| ,----'| |__| |
// | __| | | | __| > < \ \ \ / | | | | | | | __ |
// | | | `----.| |____ / . \ .----) | \ /\ / | | | | | `----.| | | |
// |__| |_______||_______/__/ \__\ |_______/ \__/ \__/ |__| |__| \______||__| |__|
//
package api
import (
"errors"
"l2/lldp/config"
"l2/lldp/server"
"l2/lldp/utils"
"sync"
)
type ApiLayer struct {
server *server.LLDPServer
}
var lldpapi *ApiLayer = nil
var once sync.Once
/* Singleton instance should be accesible only within api
*/
func getInstance() *ApiLayer {
once.Do(func() {
lldpapi = &ApiLayer{}
})
return lldpapi
}
func Init(svr *server.LLDPServer) {
lldpapi = getInstance()
lldpapi.server = svr
}
//@TODO: Create for LLDP Interface will only be called during auto-create if an entry is not present in the DB
// If it is present then we will read it during restart
// During update we need to check whether there is any entry in the runtime information or not
func validateExistingIntfConfig(intfRef string) (int32, bool, error) {
ifIndex, exists := lldpapi.server.EntryExist(intfRef)
if !exists {
return ifIndex, exists, errors.New("Update cannot be performed for " + intfRef +
" as LLDP Server doesn't have any info for the ifIndex")
}
return ifIndex, exists, nil
}
func SendIntfConfig(intfRef, txrxMode string, enable bool) (bool, error) {
var txrxModeEnum uint8
// Validate ifIndex before sending the config to server
ifIndex, proceed, err := validateExistingIntfConfig(intfRef)
if !proceed {
return proceed, err
}
switch txrxMode {
case config.TX_RX_MODE_TxRx:
txrxModeEnum = config.TXRX
case config.TX_RX_MODE_TxOnly:
txrxModeEnum = config.TX_ONLY
case config.TX_RX_MODE_RxOnly:
txrxModeEnum = config.RX_ONLY
default:
return false, errors.New("Invalid TxRxMode string provided")
}
lldpapi.server.IntfCfgCh <- &config.IntfConfig{ifIndex, enable, txrxModeEnum}
return proceed, err
}
func UpdateIntfConfig(intfRef, txrxMode string, enable bool) (bool, error) {
var txrxModeEnum uint8
ifIndex, proceed, err := validateExistingIntfConfig(intfRef)
if !proceed {
return proceed, err
}
switch txrxMode {
case config.TX_RX_MODE_TxRx:
txrxModeEnum = config.TXRX
case config.TX_RX_MODE_TxOnly:
txrxModeEnum = config.TX_ONLY
case config.TX_RX_MODE_RxOnly:
txrxModeEnum = config.RX_ONLY
default:
return false, errors.New("Invalid TxRxMode string provided")
}
lldpapi.server.IntfCfgCh <- &config.IntfConfig{ifIndex, enable, txrxModeEnum}
return proceed, err
}
func SendGlobalConfig(vrf, txrxMode string, enable, snoopAndDrop bool, tranmitInterval int32) (bool, error) {
var txrxModeEnum uint8
if lldpapi.server.Global != nil {
return false, errors.New("Create/Delete on Global Object is not allowed, please do Update")
}
switch txrxMode {
case config.TX_RX_MODE_TxRx:
txrxModeEnum = config.TXRX
case config.TX_RX_MODE_TxOnly:
txrxModeEnum = config.TX_ONLY
case config.TX_RX_MODE_RxOnly:
txrxModeEnum = config.RX_ONLY
default:
return false, errors.New("Invalid TxRxMode string provided")
}
debug.Logger.Debug("LLDP API received auto-create global config:", vrf, enable, tranmitInterval, txrxMode, snoopAndDrop)
lldpapi.server.GblCfgCh <- &config.Global{vrf, enable, tranmitInterval, txrxModeEnum, snoopAndDrop}
debug.Logger.Debug("LLDP API pushed the global config on channel and returning true to confgMgr for create")
return true, nil
}
func UpdateGlobalConfig(vrf, txrxMode string, enable, snoopAndDrop bool, tranmitInterval int32) (bool, error) {
var txrxModeEnum uint8
if lldpapi.server.Global == nil {
return false, errors.New("Update can only be performed if the global object for LLDP is created")
}
switch txrxMode {
case config.TX_RX_MODE_TxRx:
txrxModeEnum = config.TXRX
case config.TX_RX_MODE_TxOnly:
txrxModeEnum = config.TX_ONLY
case config.TX_RX_MODE_RxOnly:
txrxModeEnum = config.RX_ONLY
default:
return false, errors.New("Invalid TxRxMode string provided")
}
debug.Logger.Debug("LLDP API received global config:", vrf, enable, tranmitInterval, txrxModeEnum, snoopAndDrop)
lldpapi.server.GblCfgCh <- &config.Global{vrf, enable, tranmitInterval, txrxModeEnum, snoopAndDrop}
debug.Logger.Debug("LLDP API pushed the global config on channel and returning true to confgMgr")
return true, nil
}
func SendPortStateChange(ifIndex int32, state string) {
lldpapi.server.IfStateCh <- &config.PortState{ifIndex, state}
}
func GetIntfs(idx int, cnt int) (int, int, []config.Intf) {
n, c, result := lldpapi.server.GetIntfs(idx, cnt)
return n, c, result
}
func GetIntfStates(idx int, cnt int) (int, int, []config.IntfState) {
n, c, result := lldpapi.server.GetIntfStates(idx, cnt)
return n, c, result
}
func GetIntfState(intfRef string) *config.IntfState {
return lldpapi.server.GetIntfState(intfRef)
}
func UpdateCache(sysInfo *config.SystemInfo) {
lldpapi.server.UpdateCacheCh <- sysInfo
}
func GetLLDPGlobalState(vrf string) (*config.GlobalState, error) {
return lldpapi.server.GetGlobalState(vrf), nil
}