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();
198 requestParameters.setSubscriptionServiceType((String) sdcInfos.get("name"));
199 requestParameters.setUserParams(
200 retrieveUserParamsFromServiceCharacteristics(orderItem.getService().getServiceCharacteristic()));
201 requestParameters.setaLaCarte(true);
202 requestParameters.setTestApi("GR_API");
203 requestDetails.setRequestParameters(requestParameters);
205 CloudConfiguration cloudConfiguration = new CloudConfiguration(lcpCloudRegionId, tenantId, cloudOwner);
206 requestDetails.setCloudConfiguration(cloudConfiguration);
208 OwningEntity owningEntity = new OwningEntity();
209 owningEntity.setOwningEntityId(serviceOrderInfo.getOwningEntityId());
210 owningEntity.setOwningEntityName(soOwningEntityName);
211 requestDetails.setOwningEntity(owningEntity);
213 Project project = new Project();
214 project.setProjectName(soProjectName);
216 requestDetails.setProject(project);
218 return requestDetails;
222 * Build E2E SO CREATE request from the ServiceOrder and catalog informations from SDC
224 * @param serviceOrderItem
225 * @param serviceOrder
229 // ServiceOrderItem serviceOrderItem --> orderItem?
230 private ServiceModel buildE2ESoRequest(ServiceOrderItem serviceOrderItem, Map<String, Object> sdcInfos,
231 SubscriberInfo subscriberInfo, ServiceOrder serviceOrder) {
233 subscriberInfo.getGlobalSubscriberId();
234 ServiceModel service = new ServiceModel();
235 service.setName(serviceOrderItem.getService().getName());
236 service.setDescription(serviceOrder.getDescription());
237 service.setServiceUuid(serviceOrderItem.getService().getServiceSpecification().getId());
238 service.setServiceInvariantUuid((String) sdcInfos.get("invariantUUID"));
239 service.setGlobalSubscriberId(subscriberInfo.getGlobalSubscriberId());
240 service.setServiceType((String) sdcInfos.get("name"));
242 ParametersModel parameters = new ParametersModel();
243 ArrayList<ResourceModel> resources = new ArrayList();
245 ArrayList<Object> resourceObjects = (ArrayList<Object>) sdcInfos.get("resourceSpecification");
247 for (int i = 0; i < resourceObjects.size(); i++) {
249 ResourceModel resourceModel = new ResourceModel((Map<String, Object>) resourceObjects.get(i));
250 ParametersModel resourceParameters = new ParametersModel();
251 resourceModel.setParameters(resourceParameters);
252 resources.add(resourceModel);
255 parameters.setResources(resources);
256 List<UserParams> userParams =
257 retrieveUserParamsFromServiceCharacteristics(serviceOrderItem.getService().getServiceCharacteristic());
259 // If there are ServiceCharacteristics add them to requestInputs
260 if (!userParams.isEmpty()) {
261 Map<String, String> requestInputs = new HashMap<String, String>();
262 for (int i = 0; i < userParams.size(); i++) {
263 requestInputs.put(userParams.get(i).getName(), userParams.get(i).getValue());
266 parameters.setRequestInputs(requestInputs);
268 service.setParameters(parameters);
274 * Build a list of UserParams for the SO request by browsing a list of ServiceCharacteristics from
277 private List<UserParams> retrieveUserParamsFromServiceCharacteristics(List<ServiceCharacteristic> characteristics) {
278 List<UserParams> userParams = new ArrayList<>();
279 UserParams userParam;
281 if (!CollectionUtils.isEmpty(characteristics)) {
282 for (ServiceCharacteristic characteristic : characteristics) {
283 // Check is the characteristic is of type object, if proceed as before to allow for
284 // backwards compatibility.
285 if (characteristic.getValueType() != null && !characteristic.getValueType().isEmpty()
286 && characteristic.getValueType().equals("object")) {
287 ObjectMapper mapper = new ObjectMapper();
288 JsonNode jsonNode = null;
290 jsonNode = mapper.readTree(characteristic.getValue().getServiceCharacteristicValue());
291 } catch (IOException e) {
292 LOGGER.error("Failed to read object json {} , exception is ",
293 characteristic.getValue().getServiceCharacteristicValue(), e.getMessage());
295 ObjectNode objectNode = (ObjectNode) jsonNode;
296 Iterator<Map.Entry<String, JsonNode>> iter = objectNode.fields();
297 while (iter.hasNext()) {
298 Map.Entry<String, JsonNode> entry = iter.next();
299 if (!entry.getValue().isArray()) {
300 userParam = new UserParams(entry.getKey(), entry.getValue().asText());
302 ArrayNode arrayNode = (ArrayNode) entry.getValue();
303 String arrayNodeValueString = arrayNode.toString();
304 userParam = new UserParams(entry.getKey(), arrayNodeValueString);
306 userParams.add(userParam);
309 // as UserParams for all other types, boolean, string, integer etc
311 userParam = new UserParams(characteristic.getName(),
312 characteristic.getValue().getServiceCharacteristicValue());
313 userParams.add(userParam);