3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023: Nordix Foundation
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
12 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
26 "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
29 var securityMethods []publishserviceapi.SecurityMethod
31 func (newContext *ServiceSecurity) PrepareNewSecurityContext(services []publishserviceapi.ServiceAPIDescription) error {
32 securityMethods = []publishserviceapi.SecurityMethod{}
33 for i, securityInfo := range newContext.SecurityInfo {
34 if securityInfo.InterfaceDetails != nil {
35 addSecurityMethodsFromInterfaceDetails(securityInfo.InterfaceDetails.SecurityMethods, &securityInfo.PrefSecurityMethods)
38 checkNil := securityInfo.ApiId != nil && securityInfo.AefId != nil
40 service := getServiceByApiId(&services, securityInfo.ApiId)
42 afpProfile := service.GetAefProfileById(securityInfo.AefId)
43 addSecurityMethodsFromAefProfile(afpProfile)
49 if isSecuryMethodsEmpty() {
50 return fmt.Errorf("not found compatible security method")
52 newContext.SecurityInfo[i].SelSecurityMethod = &securityMethods[0]
57 func isSecuryMethodsEmpty() bool {
58 return len(securityMethods) <= 0
61 func addSecurityMethodsFromInterfaceDetails(methodsFromInterface *[]publishserviceapi.SecurityMethod, prefMethods *[]publishserviceapi.SecurityMethod) {
62 if methodsFromInterface != nil {
63 securityMethods = append(securityMethods, *methodsFromInterface...)
65 if prefMethods != nil {
66 securityMethods = append(securityMethods, *prefMethods...)
70 func addSecurityMethodsFromAefProfile(aefProfile *publishserviceapi.AefProfile) {
71 if aefProfile.SecurityMethods != nil {
72 securityMethods = append(securityMethods, *aefProfile.SecurityMethods...)
76 func getServiceByApiId(services *[]publishserviceapi.ServiceAPIDescription, apiId *string) *publishserviceapi.ServiceAPIDescription {
78 for _, service := range *services {
79 if apiId != nil && strings.Compare(*service.ApiId, *apiId) == 0 {