Implementation for PUT trustedInvokers endpoint
[nonrtric/plt/sme.git] / capifcore / internal / securityapi / typeupdate.go
1 // -
2 //
3 //      ========================LICENSE_START=================================
4 //      O-RAN-SC
5 //      %%
6 //      Copyright (C) 2023: Nordix Foundation
7 //      %%
8 //      Licensed under the Apache License, Version 2.0 (the "License");
9 //      you may not use this file except in compliance with the License.
10 //      You may obtain a copy of the License at
11 //
12 //           http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //      Unless required by applicable law or agreed to in writing, software
15 //      distributed under the License is distributed on an "AS IS" BASIS,
16 //      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //      See the License for the specific language governing permissions and
18 //      limitations under the License.
19 //      ========================LICENSE_END===================================
20 package securityapi
21
22 import (
23         "fmt"
24         "strings"
25
26         "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
27 )
28
29 var securityMethods []publishserviceapi.SecurityMethod
30
31 func (newContext *ServiceSecurity) PrepareNewSecurityContext(services []publishserviceapi.ServiceAPIDescription) error {
32         securityMethods = []publishserviceapi.SecurityMethod{}
33         for i, securityInfo := range newContext.SecurityInfo {
34
35                 if securityInfo.InterfaceDetails != nil {
36                         addSecurityMethodsFromInterfaceDetails(securityInfo.InterfaceDetails.SecurityMethods, &securityInfo.PrefSecurityMethods)
37
38                 } else {
39                         checkNil := securityInfo.ApiId != nil && securityInfo.AefId != nil
40                         if checkNil {
41                                 service := getServiceByApiId(&services, securityInfo.ApiId)
42                                 afpProfile := service.GetAefProfileById(securityInfo.AefId)
43
44                                 addSecurityMethodsFromAefProfile(afpProfile)
45                         }
46                 }
47
48                 if isSecuryMethodsEmpty() {
49                         return fmt.Errorf("not found compatible security method")
50                 }
51                 newContext.SecurityInfo[i].SelSecurityMethod = &securityMethods[0]
52         }
53         return nil
54 }
55
56 func isSecuryMethodsEmpty() bool {
57         return len(securityMethods) <= 0
58 }
59
60 func addSecurityMethodsFromInterfaceDetails(methodsFromInterface *[]publishserviceapi.SecurityMethod, prefMethods *[]publishserviceapi.SecurityMethod) {
61
62         if methodsFromInterface != nil {
63                 securityMethods = append(securityMethods, *methodsFromInterface...)
64         }
65         if prefMethods != nil {
66                 securityMethods = append(securityMethods, *prefMethods...)
67         }
68 }
69
70 func addSecurityMethodsFromAefProfile(afpProfile *publishserviceapi.AefProfile) {
71         if afpProfile.SecurityMethods != nil {
72                 securityMethods = append(securityMethods, *afpProfile.SecurityMethods...)
73         }
74 }
75
76 func getServiceByApiId(services *[]publishserviceapi.ServiceAPIDescription, apiId *string) *publishserviceapi.ServiceAPIDescription {
77
78         for _, service := range *services {
79                 if apiId != nil && strings.Compare(*service.ApiId, *apiId) == 0 {
80                         return &service
81                 }
82         }
83         return nil
84 }