baf7e7fd4e99d9559ff69375849c483046eaf333
[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 org.onap.nbi.apis.serviceorder.MultiClient;
16 import org.onap.nbi.apis.serviceorder.model.ActionType;
17 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
19 import org.onap.nbi.apis.serviceorder.model.StateType;
20 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;
21 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderItemInfo;
22 import org.onap.nbi.apis.serviceorder.service.ServiceOrderService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.stereotype.Service;
27
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 @Service
33 public class CreateAAIServiceTypeManager {
34
35     @Autowired
36     private MultiClient serviceOrderConsumerService;
37
38     @Autowired
39     ServiceOrderService serviceOrderService;
40
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(CreateAAIServiceTypeManager.class);
43
44     public void createAAIServiceType(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {
45
46         Map servicesInAaiForCustomer = serviceOrderConsumerService
47             .getServicesInAaiForCustomer(serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId(), serviceOrder);
48
49         for (ServiceOrderItem serviceOrderItem : serviceOrder.getOrderItem()) {
50             if (ActionType.ADD == serviceOrderItem.getAction()) {
51                 ServiceOrderItemInfo serviceOrderItemInfo =
52                     serviceOrderInfo.getServiceOrderItemInfos().get(serviceOrderItem.getId());
53                 String sdcServiceName = (String) serviceOrderItemInfo.getCatalogResponse().get("name");
54                 if (!serviceNameExistsInAAI(servicesInAaiForCustomer, sdcServiceName)) {
55                     boolean serviceCreated = serviceOrderConsumerService.putServiceType(
56                         serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId(), sdcServiceName, serviceOrder);
57                     if (!serviceCreated) {
58                         serviceOrderService.updateOrderState(serviceOrder,StateType.REJECTED);
59                         LOGGER.warn("serviceOrder {} rejected : cannot create service type {} for customer {}",
60                             serviceOrder.getId(), sdcServiceName,
61                             serviceOrderInfo.getSubscriberInfo().getGlobalSubscriberId());
62                         serviceOrderService.addOrderMessage(serviceOrder, "501");
63
64                     }
65                 }
66             }
67         }
68
69     }
70
71     private boolean serviceNameExistsInAAI(Map servicesInAaiForCustomer, String sdcServiceName) {
72
73         if (servicesInAaiForCustomer != null && servicesInAaiForCustomer.get("service-subscription") != null) {
74             List<LinkedHashMap> servicesInAAI =
75                 (List<LinkedHashMap>) servicesInAaiForCustomer.get("service-subscription");
76             for (LinkedHashMap service : servicesInAAI) {
77                 String serviceType = (String) service.get("service-type");
78                 if (sdcServiceName.equalsIgnoreCase(serviceType)) {
79                     return true;
80                 }
81
82             }
83         }
84         return false;
85
86     }
87
88 }