Updates for G Maintenance release
[nonrtric/plt/sme.git] / capifcore / internal / eventsapi / typevalidation.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package eventsapi
22
23 import (
24         "errors"
25         "fmt"
26         "net/url"
27         "strings"
28 )
29
30 func (es EventSubscription) Validate() error {
31         if len(es.Events) == 0 {
32                 return errors.New("required attribute EventSubscription:events must contain at least one element")
33         }
34
35         for _, event := range es.Events {
36                 if err := validateEvent(event); err != nil {
37                         return errors.New("EventSubscription events contains invalid event")
38                 }
39         }
40
41         if len(strings.TrimSpace(string(es.NotificationDestination))) == 0 {
42                 return errors.New("EventSubscription missing required notificationDestination")
43         }
44         if _, err := url.ParseRequestURI(string(es.NotificationDestination)); err != nil {
45                 return fmt.Errorf("APIInvokerEnrolmentDetails has invalid notificationDestination, err=%s", err)
46         }
47
48         return nil
49 }
50
51 func validateEvent(event CAPIFEvent) error {
52         switch event {
53         case CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE:
54         case CAPIFEventACCESSCONTROLPOLICYUPDATE:
55         case CAPIFEventAPIINVOKERAUTHORIZATIONREVOKED:
56         case CAPIFEventAPIINVOKEROFFBOARDED:
57         case CAPIFEventAPIINVOKERONBOARDED:
58         case CAPIFEventAPIINVOKERUPDATED:
59         case CAPIFEventAPITOPOLOGYHIDINGCREATED:
60         case CAPIFEventAPITOPOLOGYHIDINGREVOKED:
61         case CAPIFEventSERVICEAPIAVAILABLE:
62         case CAPIFEventSERVICEAPIINVOCATIONFAILURE:
63         case CAPIFEventSERVICEAPIINVOCATIONSUCCESS:
64         case CAPIFEventSERVICEAPIUNAVAILABLE:
65         case CAPIFEventSERVICEAPIUPDATE:
66         default:
67                 return errors.New("wrong event type")
68         }
69         return nil
70 }