2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package providermanagement
29 "github.com/labstack/echo/v4"
31 "oransc.org/nonrtric/capifcore/internal/common29122"
32 provapi "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
34 log "github.com/sirupsen/logrus"
37 //go:generate mockery --name ServiceRegister
38 type ServiceRegister interface {
39 IsFunctionRegistered(aefId string) bool
42 type ProviderManager struct {
43 onboardedProviders map[string]provapi.APIProviderEnrolmentDetails
47 func NewProviderManager() *ProviderManager {
48 return &ProviderManager{
49 onboardedProviders: make(map[string]provapi.APIProviderEnrolmentDetails),
53 func (pm *ProviderManager) IsFunctionRegistered(aefId string) bool {
56 for _, provider := range pm.onboardedProviders {
57 for _, registeredFunc := range *provider.ApiProvFuncs {
58 if *registeredFunc.ApiProvFuncId == aefId {
68 func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error {
69 var newProvider provapi.APIProviderEnrolmentDetails
70 err := ctx.Bind(&newProvider)
72 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for provider")
75 if newProvider.ApiProvDomInfo == nil || *newProvider.ApiProvDomInfo == "" {
76 return sendCoreError(ctx, http.StatusBadRequest, "Provider missing required ApiProvDomInfo")
80 defer pm.lock.Unlock()
82 newProvider.ApiProvDomId = pm.getDomainId(newProvider.ApiProvDomInfo)
84 pm.registerFunctions(newProvider.ApiProvFuncs)
85 pm.onboardedProviders[*newProvider.ApiProvDomId] = newProvider
87 uri := ctx.Request().Host + ctx.Request().URL.String()
88 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newProvider.ApiProvDomId))
89 err = ctx.JSON(http.StatusCreated, newProvider)
91 // Something really bad happened, tell Echo that our handler failed
98 func (pm *ProviderManager) DeleteRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
100 defer pm.lock.Unlock()
102 log.Debug(pm.onboardedProviders)
103 if _, ok := pm.onboardedProviders[registrationId]; ok {
104 log.Debug("Deleting provider", registrationId)
105 delete(pm.onboardedProviders, registrationId)
108 return ctx.NoContent(http.StatusNoContent)
111 func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
112 registeredProvider, ok := pm.onboardedProviders[registrationId]
114 return sendCoreError(ctx, http.StatusBadRequest, "Provider must be onboarded before updating it")
118 var updatedProvider provapi.APIProviderEnrolmentDetails
119 err := ctx.Bind(&updatedProvider)
121 return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for provider")
124 for _, function := range *updatedProvider.ApiProvFuncs {
125 if function.ApiProvFuncId == nil {
126 function.ApiProvFuncId = pm.getFuncId(function.ApiProvFuncRole, function.ApiProvFuncInfo)
127 registeredFuncs := *registeredProvider.ApiProvFuncs
128 newFuncs := append(registeredFuncs, function)
129 registeredProvider.ApiProvFuncs = &newFuncs
130 pm.onboardedProviders[*registeredProvider.ApiProvDomId] = registeredProvider
134 err = ctx.JSON(http.StatusOK, registeredProvider)
136 // Something really bad happened, tell Echo that our handler failed
143 func (pm *ProviderManager) ModifyIndApiProviderEnrolment(ctx echo.Context, registrationId string) error {
144 return ctx.NoContent(http.StatusNotImplemented)
147 func (pm *ProviderManager) registerFunctions(provFuncs *[]provapi.APIProviderFunctionDetails) {
148 if provFuncs == nil {
151 for i, provFunc := range *provFuncs {
152 (*provFuncs)[i].ApiProvFuncId = pm.getFuncId(provFunc.ApiProvFuncRole, provFunc.ApiProvFuncInfo)
156 func (pm *ProviderManager) getDomainId(domainInfo *string) *string {
157 idAsString := "domain_id_" + strings.ReplaceAll(*domainInfo, " ", "_")
161 func (pm *ProviderManager) getFuncId(role provapi.ApiProviderFuncRole, funcInfo *string) *string {
164 case provapi.ApiProviderFuncRoleAPF:
166 case provapi.ApiProviderFuncRoleAMF:
168 case provapi.ApiProviderFuncRoleAEF:
171 idPrefix = "function_id_"
173 idAsString := idPrefix + strings.ReplaceAll(*funcInfo, " ", "_")
177 // This function wraps sending of an error in the Error format, and
178 // handling the failure to marshal that.
179 func sendCoreError(ctx echo.Context, code int, message string) error {
180 pd := common29122.ProblemDetails{
184 err := ctx.JSON(code, pd)