2 * Copyright (c) 2018 Orange
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with 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
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
15 package org.onap.nbi.apis.serviceorder.workflow;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
23 import org.onap.nbi.apis.serviceorder.SoClient;
24 import org.onap.nbi.apis.serviceorder.model.ServiceCharacteristic;
25 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
26 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
27 import org.onap.nbi.apis.serviceorder.model.StateType;
28 import org.onap.nbi.apis.serviceorder.model.consumer.CloudConfiguration;
29 import org.onap.nbi.apis.serviceorder.model.consumer.CreateE2EServiceInstanceResponse;
30 import org.onap.nbi.apis.serviceorder.model.consumer.CreateServiceInstanceResponse;
31 import org.onap.nbi.apis.serviceorder.model.consumer.MSOE2EPayload;
32 import org.onap.nbi.apis.serviceorder.model.consumer.MSOPayload;
33 import org.onap.nbi.apis.serviceorder.model.consumer.ModelInfo;
34 import org.onap.nbi.apis.serviceorder.model.consumer.OwningEntity;
35 import org.onap.nbi.apis.serviceorder.model.consumer.ParametersModel;
36 import org.onap.nbi.apis.serviceorder.model.consumer.Project;
37 import org.onap.nbi.apis.serviceorder.model.consumer.RequestDetails;
38 import org.onap.nbi.apis.serviceorder.model.consumer.RequestInfo;
39 import org.onap.nbi.apis.serviceorder.model.consumer.RequestParameters;
40 import org.onap.nbi.apis.serviceorder.model.consumer.ResourceModel;
41 import org.onap.nbi.apis.serviceorder.model.consumer.ServiceModel;
42 import org.onap.nbi.apis.serviceorder.model.consumer.SubscriberInfo;
43 import org.onap.nbi.apis.serviceorder.model.consumer.UserParams;
44 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
45 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.beans.factory.annotation.Value;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.stereotype.Service;
52 import org.springframework.util.CollectionUtils;
53 import com.fasterxml.jackson.databind.JsonNode;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55 import com.fasterxml.jackson.databind.node.ArrayNode;
56 import com.fasterxml.jackson.databind.node.ObjectNode;
59 public class PostSoProcessor {
61 private static final Logger LOGGER = LoggerFactory.getLogger(PostSoProcessor.class);
63 @Value("${onap.lcpCloudRegionId}")
64 private String lcpCloudRegionId;
66 @Value("${onap.tenantId}")
67 private String tenantId;
69 @Value("${so.owning.entity.id}")
70 private String soOwningEntityId;
72 @Value("${so.owning.entity.name}")
73 private String soOwningEntityName;
75 @Value("${so.project.name}")
76 private String soProjectName;
78 @Value("${onap.cloudOwner}")
79 private String cloudOwner;
82 private ServiceOrderService serviceOrderService;
85 private SoClient soClient;
87 public ResponseEntity<CreateServiceInstanceResponse> postServiceOrderItem(ServiceOrderInfo serviceOrderInfo,
88 ServiceOrderItem serviceOrderItem) {
89 ResponseEntity<CreateServiceInstanceResponse> response = null;
91 response = postSORequest(serviceOrderItem, serviceOrderInfo);
92 } catch (NullPointerException e) {
93 LOGGER.error("Unable to create service instance for serviceOrderItem.id=" + serviceOrderItem.getId(), e);
99 public ResponseEntity<CreateE2EServiceInstanceResponse> postE2EServiceOrderItem(ServiceOrderInfo serviceOrderInfo,
100 ServiceOrderItem serviceOrderItem, ServiceOrder serviceOrder) {
101 ResponseEntity<CreateE2EServiceInstanceResponse> response;
103 response = postE2ESORequest(serviceOrderItem, serviceOrderInfo, serviceOrder);
104 } catch (NullPointerException e) {
105 LOGGER.error("Unable to create service instance for serviceOrderItem.id=" + serviceOrderItem.getId(), e);
111 private ResponseEntity<CreateServiceInstanceResponse> postSORequest(ServiceOrderItem serviceOrderItem,
112 ServiceOrderInfo serviceOrderInfo) {
113 RequestDetails requestDetails = buildSoRequest(serviceOrderItem, serviceOrderInfo);
114 MSOPayload msoPayload = new MSOPayload(requestDetails);
115 ResponseEntity<CreateServiceInstanceResponse> response = null;
117 switch (serviceOrderItem.getAction()) {
119 response = soClient.callCreateServiceInstance(msoPayload);
122 response = soClient.callDeleteServiceInstance(msoPayload, serviceOrderItem.getService().getId());
125 if (StateType.INPROGRESS_MODIFY_ITEM_TO_CREATE == serviceOrderItem.getState()) {
126 response = soClient.callCreateServiceInstance(msoPayload);
128 if (StateType.ACKNOWLEDGED == serviceOrderItem.getState()) {
129 response = soClient.callDeleteServiceInstance(msoPayload, serviceOrderItem.getService().getId());
138 private ResponseEntity<CreateE2EServiceInstanceResponse> postE2ESORequest(ServiceOrderItem serviceOrderItem,
139 ServiceOrderInfo serviceOrderInfo, ServiceOrder serviceOrder) {
140 ServiceModel service = buildE2ESoRequest(serviceOrderItem,
141 serviceOrderInfo.getServiceOrderItemInfos().get(serviceOrderItem.getId()).getCatalogResponse(),
142 serviceOrderInfo.getSubscriberInfo(), serviceOrder);
143 MSOE2EPayload msoE2EPayload = new MSOE2EPayload(service);
144 ResponseEntity<CreateE2EServiceInstanceResponse> response = null;
145 switch (serviceOrderItem.getAction()) {
147 response = soClient.callE2ECreateServiceInstance(msoE2EPayload);
150 response = soClient.callE2EDeleteServiceInstance(service.getGlobalSubscriberId(),
151 service.getServiceType(), serviceOrderItem.getService().getId());
154 if (StateType.INPROGRESS_MODIFY_ITEM_TO_CREATE == serviceOrderItem.getState()) {
155 response = soClient.callE2ECreateServiceInstance(msoE2EPayload);
157 if (StateType.ACKNOWLEDGED == serviceOrderItem.getState()) {
158 response = soClient.callE2EDeleteServiceInstance(service.getGlobalSubscriberId(),
159 service.getServiceType(), serviceOrderItem.getService().getId());
169 * Build SO CREATE request from the ServiceOrder and catalog informations from SDC
172 * @param serviceOrderInfo
175 private RequestDetails buildSoRequest(ServiceOrderItem orderItem, ServiceOrderInfo serviceOrderInfo) {
176 RequestDetails requestDetails = new RequestDetails();
178 requestDetails.setSubscriberInfo(serviceOrderInfo.getSubscriberInfo());
179 Map<String, Object> sdcInfos =
180 serviceOrderInfo.getServiceOrderItemInfos().get(orderItem.getId()).getCatalogResponse();
181 ModelInfo modelInfo = new ModelInfo();
182 modelInfo.setModelType("service");
183 modelInfo.setModelInvariantId((String) sdcInfos.get("invariantUUID"));
184 modelInfo.setModelNameVersionId(orderItem.getService().getServiceSpecification().getId());
185 modelInfo.setModelVersionId(orderItem.getService().getServiceSpecification().getId());
186 modelInfo.setModelName((String) sdcInfos.get("name"));
187 modelInfo.setModelVersion((String) sdcInfos.get("version"));
188 requestDetails.setModelInfo(modelInfo);
190 RequestInfo requestInfo = new RequestInfo();
191 requestInfo.setInstanceName(orderItem.getService().getName());
192 requestInfo.setSource("VID");
193 requestInfo.setSuppressRollback(false);
194 requestInfo.setRequestorId("NBI");
195 requestDetails.setRequestInfo(requestInfo);
197 RequestParameters requestParameters = new RequestParameters();
199 String serviceTypeFromJson = orderItem.getService().getServicetype();
200 requestParameters.setSubscriptionServiceType(serviceTypeFromJson != null ? serviceTypeFromJson:(String) sdcInfos.get("name"));
201 requestParameters.setUserParams(
202 retrieveUserParamsFromServiceCharacteristics(orderItem.getService().getServiceCharacteristic()));
203 requestParameters.setaLaCarte(true);
204 requestParameters.setTestApi("GR_API");
205 requestDetails.setRequestParameters(requestParameters);
207 CloudConfiguration cloudConfiguration = new CloudConfiguration(lcpCloudRegionId, tenantId, cloudOwner);
208 requestDetails.setCloudConfiguration(cloudConfiguration);
210 OwningEntity owningEntity = new OwningEntity();
211 owningEntity.setOwningEntityId(serviceOrderInfo.getOwningEntityId());
212 owningEntity.setOwningEntityName(soOwningEntityName);
213 requestDetails.setOwningEntity(owningEntity);
215 Project project = new Project();
216 project.setProjectName(soProjectName);
218 requestDetails.setProject(project);
220 return requestDetails;
224 * Build E2E SO CREATE request from the ServiceOrder and catalog informations from SDC
226 * @param serviceOrderItem
227 * @param serviceOrder
231 // ServiceOrderItem serviceOrderItem --> orderItem?
232 private ServiceModel buildE2ESoRequest(ServiceOrderItem serviceOrderItem, Map<String, Object> sdcInfos,
233 SubscriberInfo subscriberInfo, ServiceOrder serviceOrder) {
235 subscriberInfo.getGlobalSubscriberId();
236 ServiceModel service = new ServiceModel();
237 service.setName(serviceOrderItem.getService().getName());
238 service.setDescription(serviceOrder.getDescription());
239 service.setServiceUuid(serviceOrderItem.getService().getServiceSpecification().getId());
240 service.setServiceInvariantUuid((String) sdcInfos.get("invariantUUID"));
241 service.setGlobalSubscriberId(subscriberInfo.getGlobalSubscriberId());
242 String serviceTypeFromJson = serviceOrderItem.getService().getServicetype();
243 service.setServiceType(serviceTypeFromJson != null ? serviceTypeFromJson : (String) sdcInfos.get("name"));
244 ParametersModel parameters = new ParametersModel();
245 ArrayList<ResourceModel> resources = new ArrayList();
247 ArrayList<Object> resourceObjects = (ArrayList<Object>) sdcInfos.get("resourceSpecification");
249 for (int i = 0; i < resourceObjects.size(); i++) {
251 ResourceModel resourceModel = new ResourceModel((Map<String, Object>) resourceObjects.get(i));
252 ParametersModel resourceParameters = new ParametersModel();
253 resourceModel.setParameters(resourceParameters);
254 resources.add(resourceModel);
257 parameters.setResources(resources);
258 List<UserParams> userParams =
259 retrieveUserParamsFromServiceCharacteristics(serviceOrderItem.getService().getServiceCharacteristic());
261 // If there are ServiceCharacteristics add them to requestInputs
262 if (!userParams.isEmpty()) {
263 Map<String, String> requestInputs = new HashMap<String, String>();
264 for (int i = 0; i < userParams.size(); i++) {
265 requestInputs.put(userParams.get(i).getName(), userParams.get(i).getValue());
268 parameters.setRequestInputs(requestInputs);
270 service.setParameters(parameters);
276 * Build a list of UserParams for the SO request by browsing a list of ServiceCharacteristics from
279 private List<UserParams> retrieveUserParamsFromServiceCharacteristics(List<ServiceCharacteristic> characteristics) {
280 List<UserParams> userParams = new ArrayList<>();
281 UserParams userParam;
283 if (!CollectionUtils.isEmpty(characteristics)) {
284 for (ServiceCharacteristic characteristic : characteristics) {
285 // Check is the characteristic is of type object, if proceed as before to allow for
286 // backwards compatibility.
287 if (characteristic.getValueType() != null && !characteristic.getValueType().isEmpty()
288 && characteristic.getValueType().equals("object")) {
289 ObjectMapper mapper = new ObjectMapper();
290 JsonNode jsonNode = null;
292 jsonNode = mapper.readTree(characteristic.getValue().getServiceCharacteristicValue());
293 } catch (IOException e) {
294 LOGGER.error("Failed to read object json {} , exception is ",
295 characteristic.getValue().getServiceCharacteristicValue(), e.getMessage());
297 ObjectNode objectNode = (ObjectNode) jsonNode;
298 Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
299 while (iter.hasNext()) {
300 Map.Entry<String, JsonNode> entry = iter.next();
301 if (!entry.getValue().isArray()) {
302 userParam = new UserParams(entry.getKey(), entry.getValue().asText());
304 ArrayNode arrayNode = (ArrayNode) entry.getValue();
305 String arrayNodeValueString = arrayNode.toString();
306 userParam = new UserParams(entry.getKey(), arrayNodeValueString);
308 userParams.add(userParam);
311 // as UserParams for all other types, boolean, string, integer etc
313 userParam = new UserParams(characteristic.getName(),
314 characteristic.getValue().getServiceCharacteristicValue());
315 userParams.add(userParam);