blob: d8af7cc646e506bd2d142aea6b20d8d089c0cb42 [file] [log] [blame]
Prince Pereirac1c21d62021-04-22 08:38:15 +00001/*
2 * Copyright 2020-present Open Networking Foundation
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
17// Package nbi holds rpc server apis implemented
18package nbi
19
20import (
21 "context"
22 "errors"
23 "net"
24 "strconv"
25 "strings"
26
27 "github.com/opencord/device-management-interface/go/dmi"
28 dev "github.com/opencord/opendevice-manager/pkg/models/device"
29
30 "github.com/opencord/voltha-lib-go/v4/pkg/log"
31)
32
33/* validateUri() verifies if the ip and port are valid and already registered then return the truth value of the desired state specified by the following 2 switches,
34 wantRegistered: 'true' if the fact of an ip is registered is the desired state
35 includePort: 'true' further checks if <ip>:<port#> does exist in the devicemap in case an ip is found registered
36*/
37func validateUri(ctx context.Context, uri string) (ok bool, err error) {
38 ok = false
39 if !strings.Contains(uri, ":") {
40 logger.Errorw(ctx, "Invalid-uri", log.Fields{"uri-received": uri, "expected-uri": "ip:port"})
41 err = errors.New("incorrect IP address format (<ip>:<port #>)")
42 return
43 }
44 splits := strings.Split(uri, ":")
45 ip, port := splits[0], splits[1]
46 if net.ParseIP(ip) == nil {
47 // also check to see if it's a valid hostname
48 if _, err2 := net.LookupIP(ip); err2 != nil {
49 logger.Errorw(ctx, "Invalid-ip", log.Fields{"uri-received": uri, "ip": ip})
50 err = errors.New("invalid IP address " + ip)
51 return
52 }
53 }
54 if _, err2 := strconv.Atoi(port); err2 != nil {
55 logger.Errorw(ctx, "Invalid-port", log.Fields{"uri-received": uri, "port": port})
56 err = errors.New("Port number " + port + " needs to be an integer")
57 return
58 }
59 ok = true
60 return
61}
62
63// validateStartManagingDeviceReq validates the 'StartManagingDevice' request is proper or not
64func validateStartManagingDeviceReq(ctx context.Context, req *dmi.ModifiableComponent) (resp *dmi.StartManagingDeviceResponse, ok bool) {
65 resp = new(dmi.StartManagingDeviceResponse)
66 resp.DeviceUuid = new(dmi.Uuid)
67 resp.Status = dmi.Status_ERROR_STATUS
68 if ok1, err := validateUri(ctx, req.Uri.Uri); !ok1 {
69 logger.Errorw(ctx, "validation-failed-for-StartManagingDevice-request", log.Fields{"error": err, "req": req})
70 resp.Reason = dmi.StartManagingDeviceResponse_INVALID_PARAMS
71 resp.ReasonDetail = err.Error()
72 return
73 }
74
75 if rec, _ := dev.DBGetByName(ctx, req.Name); rec != nil {
76 logger.Errorw(ctx, "validation-failed-for-StartManagingDevice-request-record-already-exists", log.Fields{"req": req, "rec": rec})
77 resp.Reason = dmi.StartManagingDeviceResponse_DEVICE_ALREADY_MANAGED
78 resp.ReasonDetail = "device already exists and managed with uuid " + rec.Uuid + " and uri " + rec.Uri
79 return
80 }
81
82 ok = true
83
84 return
85}
86
87// isValidSetLogLevel check is valid set loglevel request
88func isValidSetLogLevel(ctx context.Context, listEntities []*dmi.EntitiesLogLevel) (bool, error) {
89
90 if len(listEntities) == 0 {
91 // atleast one entities is required for set loglevel
92 logger.Errorw(ctx, "found-empty-entities", log.Fields{"entities": listEntities})
93 return false, errors.New("found empty entities")
94 }
95
96 if len(listEntities) > 1 {
97 // if set Entities more than 1, atleast 1 entity in nested struct
98 for _, entities := range listEntities {
99 if len(entities.Entities) == 0 {
100 logger.Errorw(ctx, "entities-has-empty-entries", log.Fields{"entities": entities})
101 return false, errors.New("set-empty-entries-not-allowed")
102 }
103 }
104 }
105
106 logger.Debug(ctx, "valid-set-log-request")
107 return true, nil
108}