3 * Copyright (c) 2017 Orange. All rights reserved.
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.
17 package org.onap.nbi.apis.serviceorder.workflow;
19 import org.onap.nbi.apis.serviceorder.MultiClient;
20 import org.onap.nbi.apis.serviceorder.model.RelatedParty;
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.SubscriberInfo;
25 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
26 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderItemInfo;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.stereotype.Service;
31 import org.springframework.util.StringUtils;
32 import java.util.LinkedHashMap;
35 public class CheckOrderConsistenceManager {
39 private MultiClient serviceOrderConsumerService;
41 public ServiceOrderInfo checkServiceOrder(ServiceOrder serviceOrder) {
42 ServiceOrderInfo serviceOrderInfo = new ServiceOrderInfo();
43 manageCustomer(serviceOrder, serviceOrderInfo);
44 int nbItemsCompleted = 0;
45 boolean isServiceOrderRejected = false;
46 boolean isAllItemsInAdd = true;
48 for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
49 ServiceOrderItemInfo serviceOrderItemInfo = new ServiceOrderItemInfo();
50 serviceOrderItemInfo.setId(serviceOrderItem.getId());
51 handleServiceFromCatalog(serviceOrderItem, serviceOrderItemInfo);
52 switch (serviceOrderItem.getAction()) {
54 if (existServiceInCatalog(serviceOrderItemInfo)
55 && StringUtils.isEmpty(serviceOrderItem.getService().getId())
56 && serviceOrderConsumerService.isTenantIdPresentInAAI()) {
57 serviceOrderInfo.addServiceOrderItemInfos(serviceOrderItem.getId(), serviceOrderItemInfo);
59 isServiceOrderRejected = true;
60 serviceOrderItem.setState(StateType.REJECTED);
65 isAllItemsInAdd = false;
66 if (isCustomerFromServiceOrderPresentInInventory(serviceOrderInfo)
67 && existServiceInInventory(serviceOrderItem, serviceOrderItemInfo,
68 serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId())) {
69 serviceOrderInfo.addServiceOrderItemInfos(serviceOrderItem.getId(), serviceOrderItemInfo);
71 isServiceOrderRejected = true;
72 serviceOrderItem.setState(StateType.REJECTED);
76 isAllItemsInAdd = false;
77 serviceOrderItem.setState(StateType.COMPLETED);
82 if (serviceOrder.getOrderItem().size() != nbItemsCompleted) {
83 serviceOrderInfo.setAllItemsCompleted(false);
85 serviceOrderInfo.setAllItemsCompleted(true);
87 serviceOrderInfo.setAllItemsInAdd(isAllItemsInAdd);
88 serviceOrderInfo.setIsServiceOrderRejected(isServiceOrderRejected);
90 return serviceOrderInfo;
94 private void manageCustomer(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {
95 RelatedParty customerFromServiceOrder = getCustomerFromServiceOrder(serviceOrder);
96 SubscriberInfo subscriberInfo = new SubscriberInfo();
97 if (customerFromServiceOrder != null) {
98 subscriberInfo.setSubscriberName(customerFromServiceOrder.getName());
99 subscriberInfo.setGlobalSubscriberId(customerFromServiceOrder.getId());
100 serviceOrderInfo.setUseServiceOrderCustomer(true);
102 subscriberInfo.setSubscriberName("generic");
103 subscriberInfo.setGlobalSubscriberId("generic");
104 serviceOrderInfo.setUseServiceOrderCustomer(false);
106 serviceOrderInfo.setSubscriberInfo(subscriberInfo);
111 private RelatedParty getCustomerFromServiceOrder(ServiceOrder serviceOrder) {
112 for (RelatedParty relatedParty : serviceOrder.getRelatedParty()) {
113 if ("ONAPcustomer".equalsIgnoreCase(relatedParty.getRole())) {
120 private boolean isCustomerFromServiceOrderPresentInInventory(ServiceOrderInfo serviceOrderInfo) {
122 if (serviceOrderInfo.isUseServiceOrderCustomer()) {
124 boolean customerPresentInAAI = serviceOrderConsumerService
125 .isCustomerPresentInAAI(serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId());
126 return customerPresentInAAI;
131 private boolean existServiceInInventory(ServiceOrderItem serviceOrderItem,
132 ServiceOrderItemInfo serviceOrderItemInfo, String globalSubscriberId) {
133 if (!StringUtils.isEmpty(serviceOrderItem.getService().getId())) {
134 String serviceName = (String) serviceOrderItemInfo.getCatalogResponse().get("name");
135 boolean serviceExistInInventory = serviceOrderConsumerService.doesServiceExistInServiceInventory(
136 serviceOrderItem.getService().getId(), serviceName, globalSubscriberId);
137 if (serviceExistInInventory) {
144 private boolean existServiceInCatalog(ServiceOrderItemInfo serviceOrderItemInfo) {
145 return serviceOrderItemInfo.getCatalogResponse() != null;
148 private void handleServiceFromCatalog(ServiceOrderItem serviceOrderItem,
149 ServiceOrderItemInfo serviceOrderItemInfo) {
150 ResponseEntity<Object> response = serviceOrderConsumerService
151 .getServiceCatalog(serviceOrderItem.getService().getServiceSpecification().getId());
152 if (response != null && (response.getStatusCode().equals(HttpStatus.OK)
153 || response.getStatusCode().equals(HttpStatus.PARTIAL_CONTENT))) {
154 LinkedHashMap body = (LinkedHashMap) response.getBody();
155 serviceOrderItemInfo.setCatalogResponse(body);