fb0c2bc0317162709f4f4aa9ffccb6b7e8cf34d8
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / workflow / CreateAAIServiceTypeManager.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
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
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
12  */
13 package org.onap.nbi.apis.serviceorder.workflow;
14
15 import java.util.Date;
16 import org.onap.nbi.apis.serviceorder.MultiClient;
17 import org.onap.nbi.apis.serviceorder.model.ActionType;
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
19 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
20 import org.onap.nbi.apis.serviceorder.model.StateType;
21 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
22 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderItemInfo;
23 import org.onap.nbi.apis.serviceorder.repositories.ServiceOrderRepository;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.stereotype.Service;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30
31 @Service
32 public class CreateAAIServiceTypeManager {
33
34     @Autowired
35     private MultiClient serviceOrderConsumerService;
36
37     @Autowired
38     ServiceOrderRepository serviceOrderRepository;
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(CreateAAIServiceTypeManager.class);
41
42     public void createAAIServiceType(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {
43
44         LinkedHashMap servicesInAaiForCustomer = serviceOrderConsumerService
45             .getServicesInAaiForCustomer(serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId());
46
47         for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
48             if (ActionType.ADD == serviceOrderItem.getAction()) {
49                 ServiceOrderItemInfo serviceOrderItemInfo =
50                     serviceOrderInfo.getServiceOrderItemInfos().get(serviceOrderItem.getId());
51                 String sdcServiceName = (String) serviceOrderItemInfo.getCatalogResponse().get("name");
52                 if (!serviceNameExistsInAAI(servicesInAaiForCustomer, sdcServiceName)) {
53                     boolean serviceCreated = serviceOrderConsumerService.putServiceType(
54                         serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId(), sdcServiceName);
55                     if (!serviceCreated) {
56                         serviceOrder.setState(StateType.REJECTED);
57                         serviceOrder.setCompletionDateTime(new Date());
58                         serviceOrderRepository.save(serviceOrder);
59                         LOGGER.error("serviceOrder {0} rejected : cannot create customer", serviceOrder.getId());
60                     }
61                 }
62             }
63         }
64
65     }
66
67     private boolean serviceNameExistsInAAI(LinkedHashMap servicesInAaiForCustomer, String sdcServiceName) {
68
69         if (servicesInAaiForCustomer != null && servicesInAaiForCustomer.get("service-subscription") != null) {
70             List<LinkedHashMap> servicesInAAI =
71                 (List<LinkedHashMap>) servicesInAaiForCustomer.get("service-subscription");
72             for (LinkedHashMap service : servicesInAAI) {
73                 String serviceType = (String) service.get("service-type");
74                 if (sdcServiceName.equalsIgnoreCase(serviceType)) {
75                     return true;
76                 }
77
78             }
79         }
80
81         return false;
82
83     }
84
85 }