fix NPE serviceOrder
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / workflow / CheckOrderConsistenceManager.java
1 /**
2  * Copyright (c) 2018 Orange
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.nbi.apis.serviceorder.workflow;
17
18 import org.onap.nbi.apis.serviceorder.MultiClient;
19 import org.onap.nbi.apis.serviceorder.model.RelatedParty;
20 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
21 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
22 import org.onap.nbi.apis.serviceorder.model.StateType;
23 import org.onap.nbi.apis.serviceorder.model.consumer.SubscriberInfo;
24 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
25 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderItemInfo;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.stereotype.Service;
30 import org.springframework.util.StringUtils;
31
32 import java.util.LinkedHashMap;
33
34 @Service
35 public class CheckOrderConsistenceManager {
36
37
38     @Autowired
39     private MultiClient serviceOrderConsumerService;
40
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;
47
48         for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
49             ServiceOrderItemInfo serviceOrderItemInfo = new ServiceOrderItemInfo();
50             serviceOrderItemInfo.setId(serviceOrderItem.getId());
51             handleServiceFromCatalog(serviceOrderItem, serviceOrderItemInfo);
52             switch (serviceOrderItem.getAction()) {
53                 case ADD:
54                     if (existServiceInCatalog(serviceOrderItemInfo)
55                             && StringUtils.isEmpty(serviceOrderItem.getService().getId())
56                             && serviceOrderConsumerService.isTenantIdPresentInAAI()) {
57                         serviceOrderInfo.addServiceOrderItemInfos(serviceOrderItem.getId(), serviceOrderItemInfo);
58                     } else {
59                         isServiceOrderRejected = true;
60                         serviceOrderItem.setState(StateType.REJECTED);
61                     }
62                     break;
63                 case MODIFY:
64                 case DELETE:
65                     isAllItemsInAdd = false;
66                     if (isCustomerFromServiceOrderPresentInInventory(serviceOrderInfo)
67                             && existServiceInInventory(serviceOrderItem, serviceOrderItemInfo,
68                             serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId())) {
69                         serviceOrderInfo.addServiceOrderItemInfos(serviceOrderItem.getId(), serviceOrderItemInfo);
70                     } else {
71                         isServiceOrderRejected = true;
72                         serviceOrderItem.setState(StateType.REJECTED);
73                     }
74                     break;
75                 case NOCHANGE:
76                     isAllItemsInAdd = false;
77                     serviceOrderItem.setState(StateType.COMPLETED);
78                     nbItemsCompleted++;
79                     break;
80             }
81         }
82         if (serviceOrder.getOrderItem().size() != nbItemsCompleted) {
83             serviceOrderInfo.setAllItemsCompleted(false);
84         } else {
85             serviceOrderInfo.setAllItemsCompleted(true);
86         }
87         serviceOrderInfo.setAllItemsInAdd(isAllItemsInAdd);
88         serviceOrderInfo.setIsServiceOrderRejected(isServiceOrderRejected);
89
90         return serviceOrderInfo;
91
92     }
93
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);
101         } else {
102             subscriberInfo.setSubscriberName("generic");
103             subscriberInfo.setGlobalSubscriberId("generic");
104             serviceOrderInfo.setUseServiceOrderCustomer(false);
105         }
106         serviceOrderInfo.setSubscriberInfo(subscriberInfo);
107
108     }
109
110
111     private RelatedParty getCustomerFromServiceOrder(ServiceOrder serviceOrder) {
112         if (serviceOrder.getRelatedParty() != null) {
113             for (RelatedParty relatedParty : serviceOrder.getRelatedParty()) {
114                 if ("ONAPcustomer".equalsIgnoreCase(relatedParty.getRole())) {
115                     return relatedParty;
116                 }
117             }
118         }
119         return null;
120     }
121
122     private boolean isCustomerFromServiceOrderPresentInInventory(ServiceOrderInfo serviceOrderInfo) {
123
124         if (serviceOrderInfo.isUseServiceOrderCustomer()) {
125
126             boolean customerPresentInAAI = serviceOrderConsumerService
127                     .isCustomerPresentInAAI(serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId());
128             return customerPresentInAAI;
129         }
130         return true;
131     }
132
133     private boolean existServiceInInventory(ServiceOrderItem serviceOrderItem,
134                                             ServiceOrderItemInfo serviceOrderItemInfo, String globalSubscriberId) {
135         if (!StringUtils.isEmpty(serviceOrderItem.getService().getId())) {
136             String serviceName = (String) serviceOrderItemInfo.getCatalogResponse().get("name");
137             boolean serviceExistInInventory = serviceOrderConsumerService.doesServiceExistInServiceInventory(
138                     serviceOrderItem.getService().getId(), serviceName, globalSubscriberId);
139             if (serviceExistInInventory) {
140                 return true;
141             }
142         }
143         return false;
144     }
145
146     private boolean existServiceInCatalog(ServiceOrderItemInfo serviceOrderItemInfo) {
147         return serviceOrderItemInfo.getCatalogResponse() != null;
148     }
149
150     private void handleServiceFromCatalog(ServiceOrderItem serviceOrderItem,
151                                           ServiceOrderItemInfo serviceOrderItemInfo) {
152         ResponseEntity<Object> response = serviceOrderConsumerService
153                 .getServiceCatalog(serviceOrderItem.getService().getServiceSpecification().getId());
154         if (response != null && (response.getStatusCode().equals(HttpStatus.OK)
155                 || response.getStatusCode().equals(HttpStatus.PARTIAL_CONTENT))) {
156             LinkedHashMap body = (LinkedHashMap) response.getBody();
157             serviceOrderItemInfo.setCatalogResponse(body);
158         }
159     }
160
161
162 }