2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END========================================================================
19 package com.oransc.rappmanager.sme.service;
21 import com.fasterxml.jackson.core.type.TypeReference;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.oransc.rappmanager.models.RappDeployer;
24 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
25 import com.oransc.rappmanager.models.rapp.Rapp;
26 import com.oransc.rappmanager.models.rapp.RappEvent;
27 import com.oransc.rappmanager.models.rappinstance.RappInstance;
28 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
29 import com.oransc.rappmanager.sme.invoker.data.APIInvokerEnrolmentDetails;
30 import com.oransc.rappmanager.sme.provider.data.APIProviderEnrolmentDetails;
31 import com.oransc.rappmanager.sme.provider.data.APIProviderFunctionDetails;
32 import com.oransc.rappmanager.sme.provider.data.ApiProviderFuncRole;
33 import com.oransc.rappmanager.sme.provider.data.RegistrationInformation;
34 import com.oransc.rappmanager.sme.publishservice.data.ServiceAPIDescription;
35 import java.util.List;
36 import java.util.Optional;
37 import lombok.RequiredArgsConstructor;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Service;
43 @RequiredArgsConstructor
44 public class SmeDeployer implements RappDeployer {
46 Logger logger = LoggerFactory.getLogger(SmeDeployer.class);
48 private final com.oransc.rappmanager.sme.provider.rest.DefaultApiClient providerDefaultApiClient;
51 private final com.oransc.rappmanager.sme.publishservice.rest.DefaultApiClient publishServiceDefaultApiClient;
53 private final com.oransc.rappmanager.sme.invoker.rest.DefaultApiClient invokerDefaultApiClient;
55 private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
57 private final ObjectMapper objectMapper;
59 private final RappInstanceStateMachine rappInstanceStateMachine;
61 private String amfRegistrationId;
64 APIProviderEnrolmentDetails createAMF() {
65 APIProviderEnrolmentDetails responseApiEnrollmentDetails = null;
67 APIProviderFunctionDetails apiProviderFunctionDetails = new APIProviderFunctionDetails();
68 apiProviderFunctionDetails.setApiProvFuncRole(ApiProviderFuncRole.AMF);
69 apiProviderFunctionDetails.setApiProvFuncInfo("Rapp Manager as AMF");
70 apiProviderFunctionDetails.setRegInfo(new RegistrationInformation().apiProvPubKey("asd"));
72 APIProviderEnrolmentDetails apiProviderEnrolmentDetails = new APIProviderEnrolmentDetails();
73 apiProviderEnrolmentDetails.setRegSec("PSK");
74 apiProviderEnrolmentDetails.setApiProvDomInfo("Rapp Manager as AMF");
75 apiProviderEnrolmentDetails.setApiProvFuncs(List.of(apiProviderFunctionDetails));
76 responseApiEnrollmentDetails = providerDefaultApiClient.postRegistrations(apiProviderEnrolmentDetails);
77 amfRegistrationId = responseApiEnrollmentDetails.getApiProvDomId();
78 } catch (Exception e) {
79 logger.warn("Error in creating AMF", e);
81 return responseApiEnrollmentDetails;
85 deleteProviderFunc(amfRegistrationId);
89 public boolean deployRappInstance(Rapp rapp, RappInstance rappInstance) {
90 logger.debug("Deploying SME functions for RappInstance {}", rappInstance.getRappInstanceId());
91 if (rappInstance.isSMEEnabled()) {
92 if (createProviderDomain(rapp, rappInstance) && createPublishApi(rapp, rappInstance) && createInvoker(rapp,
94 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYED);
97 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYFAILED);
98 rappInstance.setReason("Unable to deploy SME");
105 public boolean undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
106 logger.debug("Undeploying SME functions for Rapp {}", rapp.getName());
107 if (rappInstance.isSMEEnabled()) {
109 rappInstance.getSme().getInvokerIds().forEach(this::deleteInvoker);
110 rappInstance.getSme().getServiceApiIds()
111 .forEach(s -> deletePublishApi(s, rappInstance.getSme().getApfId()));
112 rappInstance.getSme().getProviderFunctionIds().forEach(this::deleteProviderFunc);
113 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYED);
115 } catch (Exception e) {
116 logger.warn("Failed to Undeploy SME functions for Rapp {}", rapp.getName(), e);
118 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYFAILED);
119 rappInstance.setReason("Unable to undeploy SME");
126 public boolean primeRapp(Rapp rapp) {
127 //If there is any priming operations on SME
132 public boolean deprimeRapp(Rapp rapp) {
133 //If there is any depriming operations
137 boolean createProviderDomain(Rapp rapp, RappInstance rappInstance) {
138 logger.debug("Creating provider domain for Rapp {}", rapp.getName());
140 String providerDomainPayload =
141 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappInstance.getSme());
142 logger.debug("Provider domain payload {}", providerDomainPayload);
143 if (providerDomainPayload != null) {
144 APIProviderEnrolmentDetails apiProviderEnrolmentDetails =
145 objectMapper.readValue(providerDomainPayload, APIProviderEnrolmentDetails.class);
146 apiProviderEnrolmentDetails.setRegSec(
147 apiProviderEnrolmentDetails.getRegSec() + rappInstance.getRappInstanceId());
148 APIProviderEnrolmentDetails responseApiEnrollmentDetails =
149 providerDefaultApiClient.postRegistrations(apiProviderEnrolmentDetails);
150 if (responseApiEnrollmentDetails.getApiProvFuncs() != null) {
151 rappInstance.getSme().setProviderFunctionIds(responseApiEnrollmentDetails.getApiProvFuncs().stream()
152 .map(APIProviderFunctionDetails::getApiProvFuncId)
155 getProviderFuncId(responseApiEnrollmentDetails.getApiProvFuncs(),
156 ApiProviderFuncRole.APF).ifPresent(apiProviderFunctionDetails -> rappInstance.getSme()
158 apiProviderFunctionDetails.getApiProvFuncId()));
159 getProviderFuncId(responseApiEnrollmentDetails.getApiProvFuncs(),
160 ApiProviderFuncRole.AEF).ifPresent(apiProviderFunctionDetails -> rappInstance.getSme()
162 apiProviderFunctionDetails.getApiProvFuncId()));
166 } catch (Exception e) {
167 logger.warn("Error in creating provider domain", e);
172 Optional<APIProviderFunctionDetails> getProviderFuncId(List<APIProviderFunctionDetails> apiProviderFunctionDetails,
173 ApiProviderFuncRole apiProviderFuncRole) {
174 return apiProviderFunctionDetails.stream()
175 .filter(apiProviderFunctionDetail -> apiProviderFunctionDetail.getApiProvFuncRole()
176 .equals(apiProviderFuncRole)).findFirst();
179 void deleteProviderFunc(String registrationId) {
180 providerDefaultApiClient.deleteRegistrationsRegistrationId(registrationId);
184 boolean createPublishApi(Rapp rapp, RappInstance rappInstance) {
185 logger.debug("Creating publish api for Rapp {}", rapp.getName());
187 String providerApiPayload =
188 rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappInstance.getSme());
189 if (providerApiPayload != null) {
190 ServiceAPIDescription serviceAPIDescription =
191 objectMapper.readValue(providerApiPayload, ServiceAPIDescription.class);
192 serviceAPIDescription.getAefProfiles()
193 .forEach(aefProfile -> aefProfile.setAefId(rappInstance.getSme().getAefId()));
194 ServiceAPIDescription serviceAPIDescriptionResponse =
195 publishServiceDefaultApiClient.postApfIdServiceApis(rappInstance.getSme().getApfId(),
196 serviceAPIDescription);
198 if (serviceAPIDescriptionResponse.getAefProfiles() != null) {
199 rappInstance.getSme().setServiceApiIds(List.of(serviceAPIDescriptionResponse.getApiId()));
203 } catch (Exception e) {
204 logger.warn("Error in creating publish api", e);
209 void deletePublishApi(String serviceApiId, String apfId) {
210 publishServiceDefaultApiClient.deleteApfIdServiceApisServiceApiId(serviceApiId, apfId);
213 boolean createInvoker(Rapp rapp, RappInstance rappInstance) {
214 logger.debug("Creating provider domain for Rapp {}", rapp.getName());
216 String invokerPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappInstance.getSme());
217 if (invokerPayload != null) {
218 List<APIInvokerEnrolmentDetails> apiInvokerEnrolmentDetails =
219 objectMapper.readValue(invokerPayload, new TypeReference<>() { });
220 apiInvokerEnrolmentDetails.forEach(apiInvokerEnrolmentDetail -> {
221 APIInvokerEnrolmentDetails apiInvokerEnrolmentDetailsResponse =
222 invokerDefaultApiClient.postOnboardedInvokers(apiInvokerEnrolmentDetail);
223 if (apiInvokerEnrolmentDetailsResponse.getApiList() != null) {
224 rappInstance.getSme()
225 .setInvokerIds(List.of(apiInvokerEnrolmentDetailsResponse.getApiInvokerId()));
230 } catch (Exception e) {
231 logger.warn("Error in creating invoker", e);
236 void deleteInvoker(String invokerId) {
237 invokerDefaultApiClient.deleteOnboardedInvokersOnboardingId(invokerId);