2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
13 package org.onap.nbi.apis.serviceorder.workflow;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import org.onap.nbi.apis.serviceorder.SoClient;
20 import org.onap.nbi.apis.serviceorder.model.ServiceCharacteristic;
21 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
22 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
23 import org.onap.nbi.apis.serviceorder.model.StateType;
24 import org.onap.nbi.apis.serviceorder.model.consumer.CloudConfiguration;
25 import org.onap.nbi.apis.serviceorder.model.consumer.CreateE2EServiceInstanceResponse;
26 import org.onap.nbi.apis.serviceorder.model.consumer.CreateServiceInstanceResponse;
27 import org.onap.nbi.apis.serviceorder.model.consumer.MSOE2EPayload;
28 import org.onap.nbi.apis.serviceorder.model.consumer.MSOPayload;
29 import org.onap.nbi.apis.serviceorder.model.consumer.ModelInfo;
30 import org.onap.nbi.apis.serviceorder.model.consumer.OwningEntity;
31 import org.onap.nbi.apis.serviceorder.model.consumer.ParametersModel;
32 import org.onap.nbi.apis.serviceorder.model.consumer.Project;
33 import org.onap.nbi.apis.serviceorder.model.consumer.RequestDetails;
34 import org.onap.nbi.apis.serviceorder.model.consumer.RequestInfo;
35 import org.onap.nbi.apis.serviceorder.model.consumer.RequestParameters;
36 import org.onap.nbi.apis.serviceorder.model.consumer.ResourceModel;
37 import org.onap.nbi.apis.serviceorder.model.consumer.ServiceModel;
38 import org.onap.nbi.apis.serviceorder.model.consumer.SubscriberInfo;
39 import org.onap.nbi.apis.serviceorder.model.consumer.UserParams;
40 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
41 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.beans.factory.annotation.Value;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.stereotype.Service;
48 import org.springframework.util.CollectionUtils;
51 public class PostSoProcessor {
53 private static final Logger LOGGER = LoggerFactory.getLogger(PostSoProcessor.class);
55 @Value("${onap.lcpCloudRegionId}")
56 private String lcpCloudRegionId;
58 @Value("${onap.tenantId}")
59 private String tenantId;
61 @Value("${so.owning.entity.id}")
62 private String soOwningEntityId;
64 @Value("${so.owning.entity.name}")
65 private String soOwningEntityName;
67 @Value("${so.project.name}")
68 private String soProjectName;
72 private ServiceOrderService serviceOrderService;
75 private SoClient soClient;
79 public ResponseEntity<CreateServiceInstanceResponse> postServiceOrderItem(ServiceOrderInfo serviceOrderInfo,
80 ServiceOrderItem serviceOrderItem) {
81 ResponseEntity<CreateServiceInstanceResponse> response = null;
83 response = postSORequest(serviceOrderItem,serviceOrderInfo);
84 } catch (NullPointerException e) {
85 LOGGER.error("Unable to create service instance for serviceOrderItem.id=" + serviceOrderItem.getId(), e);
91 public ResponseEntity<CreateE2EServiceInstanceResponse> postE2EServiceOrderItem(ServiceOrderInfo serviceOrderInfo,
92 ServiceOrderItem serviceOrderItem, ServiceOrder serviceOrder) {
93 ResponseEntity<CreateE2EServiceInstanceResponse> response;
95 response = postE2ESORequest(serviceOrderItem, serviceOrderInfo, serviceOrder);
96 } catch (NullPointerException e) {
97 LOGGER.error("Unable to create service instance for serviceOrderItem.id=" + serviceOrderItem.getId(), e);
103 private ResponseEntity<CreateServiceInstanceResponse> postSORequest(ServiceOrderItem serviceOrderItem,
104 ServiceOrderInfo serviceOrderInfo) {
105 RequestDetails requestDetails = buildSoRequest(serviceOrderItem,
106 serviceOrderInfo.getServiceOrderItemInfos().get(serviceOrderItem.getId()).getCatalogResponse(),
107 serviceOrderInfo.getSubscriberInfo());
108 MSOPayload msoPayload = new MSOPayload(requestDetails);
109 ResponseEntity<CreateServiceInstanceResponse> response = null;
111 switch (serviceOrderItem.getAction()) {
113 response = soClient.callCreateServiceInstance(msoPayload);
116 response = soClient.callDeleteServiceInstance(msoPayload,serviceOrderItem.getService().getId());
119 if(StateType.INPROGRESS_MODIFY_ITEM_TO_CREATE ==serviceOrderItem.getState()) {
120 response = soClient.callCreateServiceInstance(msoPayload);
122 if(StateType.ACKNOWLEDGED==serviceOrderItem.getState()) {
123 response = soClient.callDeleteServiceInstance(msoPayload,serviceOrderItem.getService().getId());
132 private ResponseEntity<CreateE2EServiceInstanceResponse> postE2ESORequest(ServiceOrderItem serviceOrderItem,
133 ServiceOrderInfo serviceOrderInfo, ServiceOrder serviceOrder) {
134 ServiceModel service = buildE2ESoRequest(serviceOrderItem, serviceOrderInfo.getServiceOrderItemInfos().get(serviceOrderItem.getId()).getCatalogResponse(), serviceOrderInfo.getSubscriberInfo(), serviceOrder);
135 MSOE2EPayload msoE2EPayload = new MSOE2EPayload(service);
136 ResponseEntity<CreateE2EServiceInstanceResponse> response = null;
137 switch (serviceOrderItem.getAction()) {
139 response = soClient.callE2ECreateServiceInstance(msoE2EPayload);
142 response = soClient.callE2EDeleteServiceInstance(service.getGlobalSubscriberId(), service.getServiceType(),serviceOrderItem.getService().getId());
145 if(StateType.INPROGRESS_MODIFY_ITEM_TO_CREATE ==serviceOrderItem.getState()) {
146 response = soClient.callE2ECreateServiceInstance(msoE2EPayload);
148 if(StateType.ACKNOWLEDGED==serviceOrderItem.getState()) {
149 response = soClient.callE2EDeleteServiceInstance(service.getGlobalSubscriberId(), service.getServiceType(),serviceOrderItem.getService().getId());
161 * Build SO CREATE request from the ServiceOrder and catalog informations from SDC
165 * @param subscriberInfo
168 private RequestDetails buildSoRequest(ServiceOrderItem orderItem, Map<String, Object> sdcInfos,
169 SubscriberInfo subscriberInfo) {
170 RequestDetails requestDetails = new RequestDetails();
172 requestDetails.setSubscriberInfo(subscriberInfo);
174 ModelInfo modelInfo = new ModelInfo();
175 modelInfo.setModelType("service");
176 modelInfo.setModelInvariantId((String) sdcInfos.get("invariantUUID"));
177 modelInfo.setModelNameVersionId(orderItem.getService().getServiceSpecification().getId());
178 modelInfo.setModelVersionId(orderItem.getService().getServiceSpecification().getId());
179 modelInfo.setModelName((String) sdcInfos.get("name"));
180 modelInfo.setModelVersion((String) sdcInfos.get("version"));
181 requestDetails.setModelInfo(modelInfo);
183 RequestInfo requestInfo = new RequestInfo();
184 requestInfo.setInstanceName(orderItem.getService().getName());
185 requestInfo.setSource("VID");
186 requestInfo.setSuppressRollback(false);
187 requestInfo.setRequestorId("NBI");
188 requestDetails.setRequestInfo(requestInfo);
190 RequestParameters requestParameters = new RequestParameters();
191 requestParameters.setSubscriptionServiceType((String) sdcInfos.get("name"));
192 requestParameters.setUserParams(
193 retrieveUserParamsFromServiceCharacteristics(orderItem.getService().getServiceCharacteristic()));
194 requestParameters.setaLaCarte(true);
195 requestDetails.setRequestParameters(requestParameters);
197 CloudConfiguration cloudConfiguration = new CloudConfiguration(lcpCloudRegionId, tenantId);
198 requestDetails.setCloudConfiguration(cloudConfiguration);
200 OwningEntity owningEntity = new OwningEntity();
201 owningEntity.setOwningEntityId(soOwningEntityId);
202 owningEntity.setOwningEntityName(soOwningEntityName);
203 requestDetails.setOwningEntity(owningEntity);
205 Project project = new Project();
206 project.setProjectName(soProjectName);
208 requestDetails.setProject(project);
210 return requestDetails;
214 * Build E2E SO CREATE request from the ServiceOrder and catalog informations from SDC
216 * @param serviceOrderItem
217 * @param serviceOrder
221 //ServiceOrderItem serviceOrderItem --> orderItem?
222 private ServiceModel buildE2ESoRequest(ServiceOrderItem serviceOrderItem, Map<String, Object> sdcInfos,
223 SubscriberInfo subscriberInfo, ServiceOrder serviceOrder) {
225 subscriberInfo.getGlobalSubscriberId();
226 ServiceModel service = new ServiceModel();
227 service.setName(serviceOrderItem.getService().getName());
228 service.setDescription(serviceOrder.getDescription());
229 service.setServiceUuid(serviceOrderItem.getService().getServiceSpecification().getId());
230 service.setServiceInvariantUuid((String) sdcInfos.get("invariantUUID"));
231 service.setGlobalSubscriberId(subscriberInfo.getGlobalSubscriberId());
232 service.setServiceType((String) sdcInfos.get("name"));
234 ParametersModel parameters = new ParametersModel();
235 ArrayList<ResourceModel> resources = new ArrayList();
237 ArrayList<Object> resourceObjects = (ArrayList<Object>)sdcInfos.get("resourceSpecification");
239 for(int i = 0; i < resourceObjects.size(); i++) {
241 ResourceModel resourceModel = new ResourceModel((Map<String, Object>)resourceObjects.get(i));
242 ParametersModel resourceParameters = new ParametersModel();
243 resourceModel.setParameters(resourceParameters);
244 resources.add(resourceModel);
247 parameters.setResources(resources);
248 List<UserParams> userParams = retrieveUserParamsFromServiceCharacteristics(serviceOrderItem.getService().getServiceCharacteristic());
250 // If there are ServiceCharacteristics add them to requestInputs
251 if (!userParams.isEmpty()){
252 Map<String, String> requestInputs = new HashMap<String, String>();
253 for (int i = 0; i < userParams.size(); i++) {
254 requestInputs.put(userParams.get(i).getName(), userParams.get(i).getValue());
257 parameters.setRequestInputs(requestInputs);
259 service.setParameters(parameters);
265 * Build a list of UserParams for the SO request by browsing a list of ServiceCharacteristics from SDC
267 private List<UserParams> retrieveUserParamsFromServiceCharacteristics(List<ServiceCharacteristic> characteristics) {
268 List<UserParams> userParams = new ArrayList<>();
270 if (!CollectionUtils.isEmpty(characteristics)) {
271 for (ServiceCharacteristic characteristic : characteristics) {
272 UserParams userParam = new UserParams(characteristic.getName(),
273 characteristic.getValue().getServiceCharacteristicValue());
274 userParams.add(userParam);