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.JsonProcessingException;
22 import com.fasterxml.jackson.core.type.TypeReference;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.oransc.rappmanager.models.RappDeployer;
25 import com.oransc.rappmanager.models.csar.RappCsarConfigurationHandler;
26 import com.oransc.rappmanager.models.rapp.Rapp;
27 import com.oransc.rappmanager.models.rapp.RappEvent;
28 import com.oransc.rappmanager.models.rappinstance.RappInstance;
29 import com.oransc.rappmanager.models.statemachine.RappInstanceStateMachine;
30 import com.oransc.rappmanager.sme.invoker.data.APIInvokerEnrolmentDetails;
31 import com.oransc.rappmanager.sme.provider.data.APIProviderEnrolmentDetails;
32 import com.oransc.rappmanager.sme.provider.data.APIProviderFunctionDetails;
33 import com.oransc.rappmanager.sme.provider.data.ApiProviderFuncRole;
34 import com.oransc.rappmanager.sme.provider.data.RegistrationInformation;
35 import com.oransc.rappmanager.sme.publishservice.data.ServiceAPIDescription;
36 import java.util.List;
37 import java.util.Optional;
38 import lombok.RequiredArgsConstructor;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.stereotype.Service;
44 @RequiredArgsConstructor
45 public class SmeDeployer implements RappDeployer {
47 Logger logger = LoggerFactory.getLogger(SmeDeployer.class);
49 private final com.oransc.rappmanager.sme.provider.rest.DefaultApiClient providerDefaultApiClient;
52 private final com.oransc.rappmanager.sme.publishservice.rest.DefaultApiClient publishServiceDefaultApiClient;
54 private final com.oransc.rappmanager.sme.invoker.rest.DefaultApiClient invokerDefaultApiClient;
56 private final RappCsarConfigurationHandler rappCsarConfigurationHandler;
58 private final ObjectMapper objectMapper;
60 private final RappInstanceStateMachine rappInstanceStateMachine;
62 private String amfRegistrationId;
65 APIProviderEnrolmentDetails createAMF() {
66 APIProviderEnrolmentDetails responseApiEnrollmentDetails = null;
68 APIProviderFunctionDetails apiProviderFunctionDetails = new APIProviderFunctionDetails();
69 apiProviderFunctionDetails.setApiProvFuncRole(ApiProviderFuncRole.AMF);
70 apiProviderFunctionDetails.setApiProvFuncInfo("Rapp Manager as AMF");
71 apiProviderFunctionDetails.setRegInfo(new RegistrationInformation().apiProvPubKey("asd"));
73 APIProviderEnrolmentDetails apiProviderEnrolmentDetails = new APIProviderEnrolmentDetails();
74 apiProviderEnrolmentDetails.setRegSec("PSK");
75 apiProviderEnrolmentDetails.setApiProvDomInfo("Rapp Manager as AMF");
76 apiProviderEnrolmentDetails.setApiProvFuncs(List.of(apiProviderFunctionDetails));
77 responseApiEnrollmentDetails = providerDefaultApiClient.postRegistrations(apiProviderEnrolmentDetails);
78 amfRegistrationId = responseApiEnrollmentDetails.getApiProvDomId();
79 } catch (Exception e) {
80 logger.warn("Error in creating AMF", e);
82 return responseApiEnrollmentDetails;
86 deleteProviderFunc(amfRegistrationId);
90 public boolean deployRappInstance(Rapp rapp, RappInstance rappInstance) {
91 logger.debug("Deploying SME functions for RappInstance {}", rappInstance.getRappInstanceId());
94 createProviderDomain(rapp, rappInstance) && createPublishApi(rapp, rappInstance) && createInvoker(
97 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYED);
99 rappInstance.setReason("Unable to deploy SME");
100 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYFAILED);
103 } catch (JsonProcessingException e) {
104 logger.warn("Failed to deploy SME functions for Rapp {}", rapp.getName(), e);
106 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEDEPLOYFAILED);
107 rappInstance.setReason("Unable to deploy SME");
112 public boolean undeployRappInstance(Rapp rapp, RappInstance rappInstance) {
113 logger.debug("Undeploying SME functions for Rapp {}", rapp.getName());
115 rappInstance.getSme().getInvokerIds().forEach(this::deleteInvoker);
116 rappInstance.getSme().getServiceApiIds()
117 .forEach(s -> deletePublishApi(s, rappInstance.getSme().getApfId()));
118 rappInstance.getSme().getProviderFunctionIds().forEach(this::deleteProviderFunc);
119 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYED);
121 } catch (Exception e) {
122 logger.warn("Failed to Undeploy SME functions for Rapp {}", rapp.getName(), e);
124 rappInstanceStateMachine.sendRappInstanceEvent(rappInstance, RappEvent.SMEUNDEPLOYFAILED);
125 rappInstance.setReason("Unable to undeploy SME");
130 public boolean primeRapp(Rapp rapp) {
131 //If there is any priming operations on SME
136 public boolean deprimeRapp(Rapp rapp) {
137 //If there is any depriming operations
141 boolean createProviderDomain(Rapp rapp, RappInstance rappInstance) throws JsonProcessingException {
142 logger.debug("Creating provider domain for Rapp {}", rapp.getName());
144 String providerDomainPayload =
145 rappCsarConfigurationHandler.getSmeProviderDomainPayload(rapp, rappInstance.getSme());
146 logger.debug("Provider domain payload {}", providerDomainPayload);
147 if (providerDomainPayload != null) {
148 APIProviderEnrolmentDetails apiProviderEnrolmentDetails =
149 objectMapper.readValue(providerDomainPayload, APIProviderEnrolmentDetails.class);
150 apiProviderEnrolmentDetails.setRegSec(
151 apiProviderEnrolmentDetails.getRegSec() + rappInstance.getRappInstanceId());
152 APIProviderEnrolmentDetails responseApiEnrollmentDetails =
153 providerDefaultApiClient.postRegistrations(apiProviderEnrolmentDetails);
154 if (responseApiEnrollmentDetails.getApiProvFuncs() != null) {
155 rappInstance.getSme().setProviderFunctionIds(responseApiEnrollmentDetails.getApiProvFuncs().stream()
156 .map(APIProviderFunctionDetails::getApiProvFuncId)
159 getProviderFuncId(responseApiEnrollmentDetails.getApiProvFuncs(),
160 ApiProviderFuncRole.APF).ifPresent(apiProviderFunctionDetails -> rappInstance.getSme()
162 apiProviderFunctionDetails.getApiProvFuncId()));
163 getProviderFuncId(responseApiEnrollmentDetails.getApiProvFuncs(),
164 ApiProviderFuncRole.AEF).ifPresent(apiProviderFunctionDetails -> rappInstance.getSme()
166 apiProviderFunctionDetails.getApiProvFuncId()));
170 } catch (Exception e) {
171 logger.warn("Error in creating provider domain", e);
176 Optional<APIProviderFunctionDetails> getProviderFuncId(List<APIProviderFunctionDetails> apiProviderFunctionDetails,
177 ApiProviderFuncRole apiProviderFuncRole) {
178 return apiProviderFunctionDetails.stream()
179 .filter(apiProviderFunctionDetail -> apiProviderFunctionDetail.getApiProvFuncRole()
180 .equals(apiProviderFuncRole)).findFirst();
183 void deleteProviderFunc(String registrationId) {
184 providerDefaultApiClient.deleteRegistrationsRegistrationId(registrationId);
188 boolean createPublishApi(Rapp rapp, RappInstance rappInstance) throws JsonProcessingException {
189 logger.debug("Creating publish api for Rapp {}", rapp.getName());
191 String providerApiPayload =
192 rappCsarConfigurationHandler.getSmeProviderApiPayload(rapp, rappInstance.getSme());
193 if (providerApiPayload != null) {
194 ServiceAPIDescription serviceAPIDescription =
195 objectMapper.readValue(providerApiPayload, ServiceAPIDescription.class);
196 serviceAPIDescription.getAefProfiles()
197 .forEach(aefProfile -> aefProfile.setAefId(rappInstance.getSme().getAefId()));
198 ServiceAPIDescription serviceAPIDescriptionResponse =
199 publishServiceDefaultApiClient.postApfIdServiceApis(rappInstance.getSme().getApfId(),
200 serviceAPIDescription);
202 if (serviceAPIDescriptionResponse.getAefProfiles() != null) {
203 rappInstance.getSme().setServiceApiIds(List.of(serviceAPIDescriptionResponse.getApiId()));
207 } catch (Exception e) {
208 logger.warn("Error in creating publish api", e);
213 void deletePublishApi(String serviceApiId, String apfId) {
214 publishServiceDefaultApiClient.deleteApfIdServiceApisServiceApiId(serviceApiId, apfId);
217 boolean createInvoker(Rapp rapp, RappInstance rappInstance) throws JsonProcessingException {
218 logger.debug("Creating provider domain for Rapp {}", rapp.getName());
220 String invokerPayload = rappCsarConfigurationHandler.getSmeInvokerPayload(rapp, rappInstance.getSme());
221 if (invokerPayload != null) {
222 List<APIInvokerEnrolmentDetails> apiInvokerEnrolmentDetails =
223 objectMapper.readValue(invokerPayload, new TypeReference<>() { });
224 apiInvokerEnrolmentDetails.forEach(apiInvokerEnrolmentDetail -> {
225 APIInvokerEnrolmentDetails apiInvokerEnrolmentDetailsResponse =
226 invokerDefaultApiClient.postOnboardedInvokers(apiInvokerEnrolmentDetail);
227 if (apiInvokerEnrolmentDetailsResponse.getApiList() != null) {
228 rappInstance.getSme()
229 .setInvokerIds(List.of(apiInvokerEnrolmentDetailsResponse.getApiInvokerId()));
234 } catch (Exception e) {
235 logger.warn("Error in creating invoker", e);
240 void deleteInvoker(String invokerId) {
241 invokerDefaultApiClient.deleteOnboardedInvokersOnboardingId(invokerId);