tags:
- Statistics
summary: Fetch current statistics
- description: Provides current statistics of the Policy OPA PDP component
+ description: Provides current statistics of the Policy OPA PDP component
operationId: statistics
parameters:
- name: X-ONAP-RequestID
user: alice
action: read
object: id123
- type: dog
+ type: human
required:
- policyName
- policyFilter
user: alice
action: read
object: id123
- type: dog
+ type: human
+ required:
+ - policyName
+ - data
HealthCheckReport:
type: object
properties:
output:
type: object
additionalProperties: true
- required:
- - policyName
- - output
-
StatisticsReport:
type: object
properties:
data := requestBody.Data
log.Infof("data : %s", data)
policyId := requestBody.PolicyName
- if policyId == nil {
+ if policyId == ""{
errMsg := "Policy Id is nil"
sendErrorResponse(res, errMsg, http.StatusBadRequest)
return
}
- log.Infof("policy name : %s", *policyId)
- isExists := policymap.CheckIfPolicyAlreadyExists(*policyId)
+ log.Infof("policy name : %s", policyId)
+ isExists := policymap.CheckIfPolicyAlreadyExists(policyId)
if !isExists {
errMsg := "Policy associated with the patch request does not exists"
sendErrorResponse(res, errMsg, http.StatusBadRequest)
return
}
- matchFound := validatePolicyDataPathMatched(dirParts, *policyId, res)
+ matchFound := validatePolicyDataPathMatched(dirParts, policyId, res)
if !matchFound {
return
}
- patchInfos, err := getPatchInfo(requestBody.Data, dataDir, res)
+ patchInfos, err := getPatchInfo(&requestBody.Data, dataDir, res)
if err != nil {
log.Warnf("Failed to get Patch Info : %v", err)
return
func validateRequest(requestBody *oapicodegen.OPADataUpdateRequest) error {
validationErrors := utils.ValidateOPADataRequest(requestBody)
- if !utils.IsValidData(requestBody.Data) {
+ if !utils.IsValidData(&requestBody.Data) {
validationErrors = append(validationErrors, "Data is required and cannot be empty")
}
if len(validationErrors) > 0 {
OnapComponent: &onapComp,
OnapInstance: &onapIns,
OnapName: &onapName,
- PolicyName: &policyName,
- Data: &data,
+ PolicyName: policyName,
+ Data: data,
}
// Marshal the request to JSON
OnapComponent: &onapComp,
OnapInstance: &onapIns,
OnapName: &onapName,
- PolicyName: &policyName,
- Data: &data,
+ PolicyName: policyName,
+ Data: data,
}
// Marshal the request to JSON
requestBody, err := json.Marshal(validRequest)
OnapComponent: &onapComp,
OnapInstance: &onapIns,
OnapName: &onapName,
- PolicyName: &policyName,
- Data: &data, // Empty data
+ PolicyName: policyName,
+ Data: data, // Empty data
}
// Marshal the request to JSON
OnapComponent: &onapComp,
OnapInstance: &onapIns,
OnapName: &onapName,
-
- PolicyName: StringPointer("invalid-policy"),
- Data: &[]map[string]interface{}{{"test": "value"}},
+ PolicyName: "invalid-policy",
+ Data: []map[string]interface{}{{"test": "value"}},
}
bodyBytes, _ := json.Marshal(requestBody)
OnapInstance: &onapIns,
OnapName: &onapName,
- PolicyName: StringPointer("valid-policy"),
- Data: &[]map[string]interface{}{{"test": "value"}},
+ PolicyName: "valid-policy",
+ Data: []map[string]interface{}{{"test": "value"}},
}
bodyBytes, _ := json.Marshal(requestBody)
// creates a success decision response
func createSuccessDecisionResponse(policyName string, output map[string]interface{}) *oapicodegen.OPADecisionResponse {
return &oapicodegen.OPADecisionResponse{
- PolicyName: policyName,
- Output: output,
+ PolicyName: &policyName,
+ Output: &output,
}
}
// creates a decision response based on the provided parameters
func createSuccessDecisionResponseWithStatus(policyName string, output map[string]interface{}, statusMessage string) *oapicodegen.OPADecisionResponse {
return &oapicodegen.OPADecisionResponse{
- PolicyName: policyName,
- Output: output,
+ PolicyName: &policyName,
+ Output: &output,
StatusMessage: &statusMessage,
}
}
return
}
+ *policyId = decisionReq.PolicyName
// Validate the request body
validationErrors := utils.ValidateOPADataRequest(decisionReq)
}
log.Debugf("Validation successful for request fields")
// If validation passes, handle the decision request
-
decisionReq.PolicyName = strings.ReplaceAll(decisionReq.PolicyName, ".", "/")
handlePolicyValidation(res, decisionReq, errorDtls, httpStatus, policyId)
}
log.Warnf("Failed to unmarshal decision Request Input: %vg", err)
return
}
- if inputBytes == nil || len(inputBytes) == 0 {
+ if inputBytes == nil || len(inputBytes) == 0 || string(inputBytes) == "null" {
statusMessage := "{\"warning\":{\"code\":\"api_usage_warning\",\"message\":\"'input' key missing from the request\"}}"
decisionRes = createSuccessDecisionResponseWithStatus(decisionReq.PolicyName, nil, statusMessage)
} else {
}
// Utility function to return a pointer to a map
-func ptrMap(m map[string]interface{}) map[string]interface{} {
- return m
+func ptrMap(m map[string]interface{}) *map[string]interface{} {
+ return &m
}
// Utility function to return a pointer to a OPADecisionResponseDecision
rec := httptest.NewRecorder()
data := &oapicodegen.OPADecisionResponse{
- PolicyName: ptrString("test-policy"),
+ PolicyName: ptrStringEx("test-policy"),
Output: ptrMap(map[string]interface{}{"key": "value"}),
}
// Assertions
// Check the PolicyName field
- assert.Equal(t, response.PolicyName, policyName, "PolicyName should match")
+ assert.Equal(t, *response.PolicyName, policyName, "PolicyName should match")
// Check the Output field
- assert.Equal(t, response.Output, output, "Output should match")
+ assert.Equal(t, *response.Output, output, "Output should match")
}
// Test for policy filter
// Create a response object for error scenario
data := &oapicodegen.OPADecisionResponse{
- PolicyName: ptrString(policyName),
+ PolicyName: ptrStringEx(policyName),
Output: ptrMap(output),
}
func TestWriteOpaJSONResponse_Success(t *testing.T) {
// Prepare test data
decisionRes := oapicodegen.OPADecisionResponse{
- PolicyName: ptrString("TestPolicy"),
- Output: map[string]interface{}{"key": "value"},
+ PolicyName: ptrStringEx("TestPolicy"),
+ Output: &(map[string]interface{}{"key": "value"}),
}
// Create a mock HTTP response writer
// Prepare invalid test data to trigger JSON encoding error
decisionRes := oapicodegen.OPADecisionResponse{
// Introducing an invalid type to cause encoding failure
- Output: map[string]interface{}{"key": make(chan int)},
+ Output: &map[string]interface{}{"key": make(chan int)},
}
// Create a mock HTTP response writer
// Call the handler function that processes OPA decision
OpaDecision(res, req)
// Assert that the response status code is 200
- assert.Equal(t, 500, res.Code)
+ assert.Equal(t, 200, res.Code)
}
// Test for Invalid Decision error in Decision Result
// OPADataUpdateRequest defines model for OPADataUpdateRequest.
type OPADataUpdateRequest struct {
- CurrentDate *openapi_types.Date `json:"currentDate,omitempty"`
- CurrentDateTime *time.Time `json:"currentDateTime,omitempty"`
- CurrentTime *string `json:"currentTime,omitempty"`
- Data *[]map[string]interface{} `json:"data,omitempty"`
- OnapComponent *string `json:"onapComponent,omitempty"`
- OnapInstance *string `json:"onapInstance,omitempty"`
- OnapName *string `json:"onapName,omitempty"`
- PolicyName *string `json:"policyName,omitempty"`
+ CurrentDate *openapi_types.Date `json:"currentDate,omitempty"`
+ CurrentDateTime *time.Time `json:"currentDateTime,omitempty"`
+ CurrentTime *string `json:"currentTime,omitempty"`
+ Data []map[string]interface{} `json:"data"`
+ OnapComponent *string `json:"onapComponent,omitempty"`
+ OnapInstance *string `json:"onapInstance,omitempty"`
+ OnapName *string `json:"onapName,omitempty"`
+ PolicyName string `json:"policyName"`
// TimeOffset Time offset in hours and minutes, e.g., '+02:00' or '-05:00'
TimeOffset *string `json:"timeOffset,omitempty"`
// OPADecisionResponse defines model for OPADecisionResponse.
type OPADecisionResponse struct {
- Output map[string]interface{} `json:"output"`
- PolicyName string `json:"policyName"`
- StatusMessage *string `json:"statusMessage,omitempty"`
+ Output *map[string]interface{} `json:"output,omitempty"`
+ PolicyName *string `json:"policyName,omitempty"`
+ StatusMessage *string `json:"statusMessage,omitempty"`
}
// StatisticsReport defines model for StatisticsReport.
// Custom validation function for CurrentDate
func IsValidCurrentDate(currentDate *string) bool {
if currentDate == nil || strings.TrimSpace(*currentDate) == "" {
- return false
+ return true
}
re := regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`) // eg: "2025-01-17"
return re.MatchString(*currentDate)
OnapComponent: updateReq.OnapComponent,
OnapInstance: updateReq.OnapInstance,
OnapName: updateReq.OnapName,
- PolicyName: convertPtrToString(updateReq.PolicyName),
+ PolicyName: updateReq.PolicyName,
}
return validateCommonFields(commonFields)
var validationErrors []string
- if fields.CurrentDate == nil || !IsValidCurrentDate(fields.CurrentDate) {
- validationErrors = append(validationErrors, "CurrentDate is invalid or missing")
- }
- if fields.CurrentDateTime == nil || !IsValidTime(fields.CurrentDateTime) {
- validationErrors = append(validationErrors, "CurrentDateTime is invalid or missing")
- }
- if fields.CurrentTime == nil || !IsValidCurrentTime(fields.CurrentTime) {
- validationErrors = append(validationErrors, "CurrentTime is invalid or missing")
- }
- if fields.TimeOffset == nil || !IsValidTimeOffset(fields.TimeOffset) {
- validationErrors = append(validationErrors, "TimeOffset is invalid or missing")
+ if fields.CurrentDate != nil && !IsValidCurrentDate(fields.CurrentDate) {
+ validationErrors = append(validationErrors, "CurrentDate is invalid")
}
- if fields.TimeZone == nil || !IsValidTimeZone(fields.TimeZone) {
- validationErrors = append(validationErrors, "TimeZone is invalid or missing")
+ if fields.CurrentDateTime != nil && !IsValidTime(fields.CurrentDateTime) {
+ validationErrors = append(validationErrors, "CurrentDateTime is invalid")
}
- if !IsValidString(fields.OnapComponent) {
- validationErrors = append(validationErrors, "OnapComponent is required")
+ if fields.CurrentTime != nil && !IsValidCurrentTime(fields.CurrentTime) {
+ validationErrors = append(validationErrors, "CurrentTime is invalid")
}
- if !IsValidString(fields.OnapInstance) {
- validationErrors = append(validationErrors, "OnapInstance is required")
+ if fields.TimeOffset != nil && !IsValidTimeOffset(fields.TimeOffset) {
+ validationErrors = append(validationErrors, "TimeOffset is invalid")
}
- if !IsValidString(fields.OnapName) {
- validationErrors = append(validationErrors, "OnapName is required")
+ if fields.TimeZone != nil && !IsValidTimeZone(fields.TimeZone) {
+ validationErrors = append(validationErrors, "TimeZone is invalid")
}
if !IsValidString(fields.PolicyName) {
validationErrors = append(validationErrors, "PolicyName is required and cannot be empty")
}
func TestIsValidCurrentDate(t *testing.T) {
- validDates := []string{"2025-01-17", "1999-12-31"}
- invalidDates := []string{"20250117", "01-17-2025", "abcd-ef-gh", ""}
+ validDates := []string{"2025-01-17", "1999-12-31", ""}
+ invalidDates := []string{"20250117", "01-17-2025", "abcd-ef-gh"}
for _, date := range validDates {
if !IsValidCurrentDate(&date) {
OnapComponent: strPtr("Component"),
OnapInstance: strPtr("Instance"),
OnapName: strPtr("Onap"),
- PolicyName: strPtr("Policy1"),
+ PolicyName: "Policy1",
}
errors := ValidateOPADataRequest(updateReq)