|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// // |
| 3 | +// Copyright 2019 Broadcom. The term Broadcom refers to Broadcom Inc. and/or // |
| 4 | +// its subsidiaries. // |
| 5 | +// // |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); // |
| 7 | +// you may not use this file except in compliance with the License. // |
| 8 | +// You may obtain a copy of the License at // |
| 9 | +// // |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 // |
| 11 | +// // |
| 12 | +// Unless required by applicable law or agreed to in writing, software // |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, // |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // |
| 15 | +// See the License for the specific language governing permissions and // |
| 16 | +// limitations under the License. // |
| 17 | +// // |
| 18 | +//////////////////////////////////////////////////////////////////////////////// |
| 19 | + |
| 20 | +package custom_validation |
| 21 | + |
| 22 | +import ( |
| 23 | + "reflect" |
| 24 | + "github.com/antchfx/xmlquery" |
| 25 | + "github.com/go-redis/redis/v7" |
| 26 | + "github.com/Azure/sonic-mgmt-common/cvl/internal/util" |
| 27 | + "github.com/Azure/sonic-mgmt-common/cvl/internal/yparser" |
| 28 | + ) |
| 29 | + |
| 30 | +type CustomValidation struct {} |
| 31 | + |
| 32 | +type CVLValidateType uint |
| 33 | +const ( |
| 34 | + VALIDATE_NONE CVLValidateType = iota //Data is used as dependent data |
| 35 | + VALIDATE_SYNTAX //Syntax is checked and data is used as dependent data |
| 36 | + VALIDATE_SEMANTICS //Semantics is checked |
| 37 | + VALIDATE_ALL //Syntax and Semantics are checked |
| 38 | +) |
| 39 | + |
| 40 | +type CVLOperation uint |
| 41 | +const ( |
| 42 | + OP_NONE CVLOperation = 0 //Used to just validate the config without any operation |
| 43 | + OP_CREATE = 1 << 0//For Create operation |
| 44 | + OP_UPDATE = 1 << 1//For Update operation |
| 45 | + OP_DELETE = 1 << 2//For Delete operation |
| 46 | +) |
| 47 | + |
| 48 | +//CVLRetCode CVL Error codes |
| 49 | +type CVLRetCode int |
| 50 | +const ( |
| 51 | + CVL_SUCCESS CVLRetCode = iota |
| 52 | + CVL_ERROR |
| 53 | + CVL_NOT_IMPLEMENTED |
| 54 | + CVL_INTERNAL_UNKNOWN |
| 55 | + CVL_FAILURE |
| 56 | + CVL_SYNTAX_ERROR = CVLRetCode(yparser.YP_SYNTAX_ERROR) |
| 57 | + CVL_SEMANTIC_ERROR = CVLRetCode(yparser.YP_SEMANTIC_ERROR) |
| 58 | + CVL_SYNTAX_MISSING_FIELD = CVLRetCode(yparser.YP_SYNTAX_MISSING_FIELD) |
| 59 | + CVL_SYNTAX_INVALID_FIELD = CVLRetCode(yparser.YP_SYNTAX_INVALID_FIELD) /* Invalid Field */ |
| 60 | + CVL_SYNTAX_INVALID_INPUT_DATA = CVLRetCode(yparser.YP_SYNTAX_INVALID_INPUT_DATA) /*Invalid Input Data */ |
| 61 | + CVL_SYNTAX_MULTIPLE_INSTANCE = CVLRetCode(yparser.YP_SYNTAX_MULTIPLE_INSTANCE) /* Multiple Field Instances */ |
| 62 | + CVL_SYNTAX_DUPLICATE = CVLRetCode(yparser.YP_SYNTAX_DUPLICATE) /* Duplicate Fields */ |
| 63 | + CVL_SYNTAX_ENUM_INVALID = CVLRetCode(yparser.YP_SYNTAX_ENUM_INVALID) /* Invalid enum value */ |
| 64 | + CVL_SYNTAX_ENUM_INVALID_NAME = CVLRetCode(yparser.YP_SYNTAX_ENUM_INVALID_NAME) /* Invalid enum name */ |
| 65 | + CVL_SYNTAX_ENUM_WHITESPACE = CVLRetCode(yparser.YP_SYNTAX_ENUM_WHITESPACE) /* Enum name with leading/trailing whitespaces */ |
| 66 | + CVL_SYNTAX_OUT_OF_RANGE = CVLRetCode(yparser.YP_SYNTAX_OUT_OF_RANGE) /* Value out of range/length/pattern (data) */ |
| 67 | + CVL_SYNTAX_MINIMUM_INVALID = CVLRetCode(yparser.YP_SYNTAX_MINIMUM_INVALID) /* min-elements constraint not honored */ |
| 68 | + CVL_SYNTAX_MAXIMUM_INVALID = CVLRetCode(yparser.YP_SYNTAX_MAXIMUM_INVALID) /* max-elements constraint not honored */ |
| 69 | + CVL_SEMANTIC_DEPENDENT_DATA_MISSING = CVLRetCode(yparser.YP_SEMANTIC_DEPENDENT_DATA_MISSING) /* Dependent Data is missing */ |
| 70 | + CVL_SEMANTIC_MANDATORY_DATA_MISSING = CVLRetCode(yparser.YP_SEMANTIC_MANDATORY_DATA_MISSING) /* Mandatory Data is missing */ |
| 71 | + CVL_SEMANTIC_KEY_ALREADY_EXIST = CVLRetCode(yparser.YP_SEMANTIC_KEY_ALREADY_EXIST) /* Key already existing. */ |
| 72 | + CVL_SEMANTIC_KEY_NOT_EXIST = CVLRetCode(yparser.YP_SEMANTIC_KEY_NOT_EXIST) /* Key is missing. */ |
| 73 | + CVL_SEMANTIC_KEY_DUPLICATE = CVLRetCode(yparser.YP_SEMANTIC_KEY_DUPLICATE) /* Duplicate key. */ |
| 74 | + CVL_SEMANTIC_KEY_INVALID = CVLRetCode(yparser.YP_SEMANTIC_KEY_INVALID) |
| 75 | +) |
| 76 | + |
| 77 | +//CVLEditConfigData Strcture for key and data in API |
| 78 | +type CVLEditConfigData struct { |
| 79 | + VType CVLValidateType //Validation type |
| 80 | + VOp CVLOperation //Operation type |
| 81 | + Key string //Key format : "PORT|Ethernet4" |
| 82 | + Data map[string]string //Value : {"alias": "40GE0/28", "mtu" : 9100, "admin_status": down} |
| 83 | +} |
| 84 | + |
| 85 | +//CVLErrorInfo CVL Error Structure |
| 86 | +type CVLErrorInfo struct { |
| 87 | + TableName string /* Table having error */ |
| 88 | + ErrCode CVLRetCode /* CVL Error return Code. */ |
| 89 | + CVLErrDetails string /* CVL Error Message details. */ |
| 90 | + Keys []string /* Keys of the Table having error. */ |
| 91 | + Value string /* Field Value throwing error */ |
| 92 | + Field string /* Field Name throwing error . */ |
| 93 | + Msg string /* Detailed error message. */ |
| 94 | + ConstraintErrMsg string /* Constraint error message. */ |
| 95 | + ErrAppTag string |
| 96 | +} |
| 97 | + |
| 98 | +type CustValidationCache struct { |
| 99 | + Data interface{} |
| 100 | +} |
| 101 | + |
| 102 | +//CustValidationCtxt Custom validation context passed to custom validation function |
| 103 | +type CustValidationCtxt struct { |
| 104 | + ReqData []CVLEditConfigData //All request data |
| 105 | + CurCfg *CVLEditConfigData //Current request data for which validation should be done |
| 106 | + YNodeName string //YANG node name |
| 107 | + YNodeVal string //YANG node value, leaf-list will have "," separated value |
| 108 | + YCur *xmlquery.Node //YANG data tree |
| 109 | + SessCache *CustValidationCache //Session cache, can be used for storing data, persistent in session |
| 110 | + RClient *redis.Client //Redis client |
| 111 | +} |
| 112 | + |
| 113 | +//InvokeCustomValidation Common function to invoke custom validation |
| 114 | +//TBD should we do this using GO plugin feature ? |
| 115 | +func InvokeCustomValidation(cv *CustomValidation, name string, args... interface{}) CVLErrorInfo { |
| 116 | + inputs := make([]reflect.Value, len(args)) |
| 117 | + for i := range args { |
| 118 | + inputs[i] = reflect.ValueOf(args[i]) |
| 119 | + } |
| 120 | + |
| 121 | + f := reflect.ValueOf(cv).MethodByName(name) |
| 122 | + if !f.IsNil() { |
| 123 | + v := f.Call(inputs) |
| 124 | + util.TRACE_LEVEL_LOG(util.TRACE_SEMANTIC, |
| 125 | + "InvokeCustomValidation: %s(), CVLErrorInfo: %v", name, v[0]) |
| 126 | + |
| 127 | + return (v[0].Interface()).(CVLErrorInfo) |
| 128 | + } |
| 129 | + |
| 130 | + return CVLErrorInfo{ErrCode: CVL_SUCCESS} |
| 131 | +} |
| 132 | + |
0 commit comments