Updates for G Maintenance release
[nonrtric/plt/sme.git] / capifcore / internal / providermanagementapi / 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 providermanagementapi
22
23 import (
24         "errors"
25         "fmt"
26         "strings"
27 )
28
29 func (ri RegistrationInformation) Validate() error {
30         if len(strings.TrimSpace(ri.ApiProvPubKey)) == 0 {
31                 return errors.New("RegistrationInformation missing required apiProvPubKey")
32         }
33         return nil
34 }
35
36 func (fd APIProviderFunctionDetails) Validate() error {
37         if len(strings.TrimSpace(string(fd.ApiProvFuncRole))) == 0 {
38                 return errors.New("APIProviderFunctionDetails missing required apiProvFuncRole")
39         }
40         switch role := fd.ApiProvFuncRole; role {
41         case ApiProviderFuncRoleAEF:
42         case ApiProviderFuncRoleAPF:
43         case ApiProviderFuncRoleAMF:
44         default:
45                 return errors.New("APIProviderFunctionDetails has invalid apiProvFuncRole")
46         }
47
48         return fd.RegInfo.Validate()
49 }
50
51 func (pd APIProviderEnrolmentDetails) Validate() error {
52         if len(strings.TrimSpace(pd.RegSec)) == 0 {
53                 return errors.New("APIProviderEnrolmentDetails missing required regSec")
54         }
55         if pd.ApiProvFuncs != nil {
56                 return pd.validateFunctions()
57         }
58         return nil
59 }
60
61 func (pd APIProviderEnrolmentDetails) validateFunctions() error {
62         for _, function := range *pd.ApiProvFuncs {
63                 err := function.Validate()
64                 if err != nil {
65                         return fmt.Errorf("apiProvFuncs contains invalid function: %s", err)
66                 }
67         }
68         return nil
69 }
70
71 func (pd APIProviderEnrolmentDetails) ValidateAlreadyRegistered(otherProvider APIProviderEnrolmentDetails) error {
72         if pd.RegSec == otherProvider.RegSec {
73                 return errors.New("provider with identical regSec already registered")
74         }
75         return nil
76 }