/
ports.go
268 lines (251 loc) · 8.53 KB
/
ports.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//(C) Copyright [2020] Hewlett Packard Enterprise Development LP
//
//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.
// Package caphandler ...
package caphandler
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/ODIM-Project/ODIM/lib-dmtf/model"
"github.com/ODIM-Project/ODIM/lib-utilities/response"
"github.com/ODIM-Project/PluginCiscoACI/capmodel"
"github.com/ODIM-Project/PluginCiscoACI/caputilities"
"github.com/ODIM-Project/PluginCiscoACI/config"
"github.com/ODIM-Project/PluginCiscoACI/db"
iris "github.com/kataras/iris/v12"
log "github.com/sirupsen/logrus"
)
// GetPortCollection fetches the ports which are linked to that switch
func GetPortCollection(ctx iris.Context) {
uri := ctx.Request().RequestURI
switchID := ctx.Params().Get("switchID")
// get all port which are store under that switch
portData, err := capmodel.GetSwitchPort(switchID)
if err != nil {
errMsg := fmt.Sprintf("failed to fetch port data for uri %s: %s", uri, err.Error())
createDbErrResp(ctx, err, errMsg, []interface{}{"Port", uri})
return
}
var members = []*model.Link{}
for i := 0; i < len(portData); i++ {
members = append(members, &model.Link{
Oid: uri + "/" + portData[i],
})
}
portCollectionResponse := model.Collection{
ODataContext: "/ODIM/v1/$metadata#PortCollection.PortCollection",
ODataID: uri,
ODataType: "#PortCollection.PortCollection",
Description: "PortCollection view",
Name: "Ports",
Members: members,
MembersCount: len(members),
}
ctx.StatusCode(http.StatusOK)
ctx.JSON(portCollectionResponse)
}
// GetPortInfo fetches the port info for given port id
func GetPortInfo(ctx iris.Context) {
uri := ctx.Request().RequestURI
switchID := ctx.Params().Get("switchID")
fabricID := ctx.Params().Get("id")
fabricData, err := capmodel.GetFabric(fabricID)
if err != nil {
errMsg := fmt.Sprintf("failed to fetch port data for uri %s: %s", uri, err.Error())
createDbErrResp(ctx, err, errMsg, []interface{}{"Fabric", fabricID})
return
}
portData := getPortData(ctx, uri)
if portData == nil {
return
}
getPortAddtionalAttributes(fabricData.PodID, switchID, portData)
ctx.StatusCode(http.StatusOK)
ctx.JSON(portData)
}
// PatchPort Update the given port with provied information
func PatchPort(ctx iris.Context) {
uri := ctx.Request().RequestURI
var port model.Port
err := ctx.ReadJSON(&port)
if err != nil {
errorMessage := "error while trying to get JSON body from the request: " + err.Error()
log.Error(errorMessage)
resp := updateErrorResponse(response.MalformedJSON, errorMessage, nil)
ctx.StatusCode(http.StatusBadRequest)
ctx.JSON(resp)
return
}
portData := getPortData(ctx, uri)
if portData == nil {
return
}
checkFlag := false
if port.Links != nil {
if port.Links.ConnectedPorts != nil {
if len(port.Links.ConnectedPorts) > 0 {
//Assuming we have only one connected port
ethernetURI := port.Links.ConnectedPorts[0].Oid
//Check on ODIM if ethernet is valid
reqURL := config.Data.ODIMConf.URL + ethernetURI
odimUsername := config.Data.ODIMConf.UserName
odimPassword := config.Data.ODIMConf.Password
for key, value := range config.Data.URLTranslation.SouthBoundURL {
reqURL = strings.Replace(reqURL, key, value, -1)
}
enigma, err := caputilities.NewEnigma(string(config.Data.KeyCertConf.RSAPrivateKeyPath))
if err != nil {
errMsg := fmt.Sprintf("Error while trying to read private key path %s ", err.Error())
log.Error(errMsg)
resp := updateErrorResponse(response.InternalError, errMsg, nil)
ctx.StatusCode(http.StatusServiceUnavailable)
ctx.JSON(resp)
return
}
//decrypting odim password
odimPwd := string(enigma.Decrypt(odimPassword))
checkFlag, err = caputilities.CheckValidityOfEthernet(reqURL, odimUsername, odimPwd)
if err != nil {
errMsg := fmt.Sprintf("Error while trying to contact ODIM")
log.Error(errMsg)
resp := updateErrorResponse(response.InternalError, errMsg, nil)
ctx.StatusCode(http.StatusServiceUnavailable)
ctx.JSON(resp)
return
}
if !checkFlag {
errMsg := fmt.Sprintf("Ethernet data for uri %s not found", reqURL)
log.Error(errMsg)
resp := updateErrorResponse(response.ResourceNotFound, errMsg, []interface{}{"Ethernet", reqURL})
ctx.StatusCode(http.StatusNotFound)
ctx.JSON(resp)
return
}
portData.Links = &model.PortLinks{}
portData.Links.ConnectedPorts = []model.Link{}
portData.Links.ConnectedPorts = append(portData.Links.ConnectedPorts, model.Link{Oid: ethernetURI})
} else {
portData.Links.ConnectedPorts = nil
}
} else {
portData.Links.ConnectedPorts = nil
}
}
if err := capmodel.UpdatePort(uri, portData); err != nil {
errMsg := fmt.Sprintf("failed to update port data for uri %s: %s", uri, err.Error())
createDbErrResp(ctx, err, errMsg, []interface{}{"Ports", uri})
return
}
ctx.StatusCode(http.StatusOK)
ctx.JSON(portData)
}
func getPortAddtionalAttributes(fabricID, switchID string, p *model.Port) {
switchIDData := strings.Split(switchID, ":")
PortInfoResponse, err := caputilities.GetPortInfo(fabricID, switchIDData[1], p.PortID)
if err != nil {
log.Error("Unable to get addtional port info " + err.Error())
return
}
portInfoData := PortInfoResponse.IMData[0].PhysicalInterface.Attributes
operationState := portInfoData["operSt"].(string)
if operationState == "up" {
p.LinkState = "Enabled"
p.LinkStatus = "LinkUp"
p.InterfaceEnabled = true
} else {
p.LinkState = "Disabled"
p.LinkStatus = "LinkDown"
p.InterfaceEnabled = false
}
curSpeedData := strings.Split(portInfoData["operSpeed"].(string), "G")
data, err := strconv.ParseFloat(curSpeedData[0], 64)
if err != nil {
log.Error("Unable to get current speed of port " + err.Error())
}
p.CurrentSpeedGbps = data
portsHealthResposne, err := caputilities.GetPortHealth(fabricID, switchIDData[1], p.PortID)
if err != nil {
log.Error("Unable to get Health of port " + err.Error())
return
}
Healthdata := portsHealthResposne.IMData[0].HealthData.Attributes
currentHealthValue := Healthdata["cur"].(string)
healthValue, err := strconv.Atoi(currentHealthValue)
if err != nil {
log.Error("Unable to convert current Health value:" + currentHealthValue + " go the error" + err.Error())
return
}
var portStatus = model.Status{
State: p.LinkState,
}
if healthValue > 90 {
portStatus.Health = "OK"
} else if healthValue <= 90 && healthValue < 30 {
portStatus.Health = "Warning"
} else {
portStatus.Health = "Critical"
}
p.Status = &portStatus
return
}
func updateErrorResponse(statusMsg, errMsg string, msgArgs []interface{}) interface{} {
args := response.Args{
Code: response.GeneralError,
Message: "",
ErrorArgs: []response.ErrArgs{
response.ErrArgs{
StatusMessage: statusMsg,
ErrorMessage: errMsg,
MessageArgs: msgArgs,
},
},
}
return args.CreateGenericErrorResponse()
}
func createDbErrResp(ctx iris.Context, err error, errMsg string, msgArgs []interface{}) (int, interface{}) {
var resp interface{}
var statusCode int
switch {
case errors.Is(err, db.ErrorKeyNotFound):
resp = updateErrorResponse(response.ResourceNotFound, errMsg, msgArgs)
statusCode = http.StatusNotFound
case errors.Is(err, db.ErrorServiceUnavailable):
resp = updateErrorResponse(response.CouldNotEstablishConnection, errMsg, nil)
statusCode = http.StatusServiceUnavailable
case errors.Is(err, db.ErrorKeyAlreadyExist):
resp = updateErrorResponse(response.ResourceAlreadyExists, errMsg, msgArgs)
statusCode = http.StatusConflict
default:
resp = updateErrorResponse(response.InternalError, errMsg, nil)
statusCode = http.StatusInternalServerError
}
log.Error(errMsg)
if ctx != nil {
ctx.StatusCode(statusCode)
ctx.JSON(resp)
}
return statusCode, resp
}
func getPortData(ctx iris.Context, portOID string) *model.Port {
log.Info("Port uri" + portOID)
portData, err := capmodel.GetPort(portOID)
if err != nil {
errMsg := fmt.Sprintf("failed to fetch port data for uri %s: %s", portOID, err.Error())
createDbErrResp(ctx, err, errMsg, []interface{}{"Ports", portOID})
return nil
}
return portData
}