Merge "Modify dockerfile to add postgreSQL PGP key"
[usecase-ui/server.git] / server / src / main / java / org / onap / usecaseui / server / service / lcm / impl / DefaultServiceInstanceService.java
1 /**
2  * Copyright 2016-2017 ZTE Corporation.
3  *
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  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.usecaseui.server.service.lcm.impl;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.annotation.Resource;
26
27 import org.onap.usecaseui.server.bean.ServiceBean;
28 import org.onap.usecaseui.server.bean.ServiceInstanceOperations;
29 import org.onap.usecaseui.server.service.lcm.CustomerService;
30 import org.onap.usecaseui.server.service.lcm.ServiceInstanceService;
31 import org.onap.usecaseui.server.service.lcm.ServiceLcmService;
32 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
33 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAICustomer;
34 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAIServiceSubscription;
35 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.ServiceInstanceRsp;
36 import org.onap.usecaseui.server.util.RestfulServices;
37 import org.onap.usecaseui.server.util.UuiCommonUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.context.annotation.EnableAspectJAutoProxy;
41 import org.springframework.stereotype.Service;
42
43 import com.alibaba.fastjson.JSON;
44 import com.alibaba.fastjson.JSONArray;
45 import com.alibaba.fastjson.JSONObject;
46 import com.fasterxml.jackson.core.JsonProcessingException;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48
49 import okhttp3.ResponseBody;
50 import retrofit2.Response;
51
52 @Service("ServiceInstanceService")
53 @org.springframework.context.annotation.Configuration
54 @EnableAspectJAutoProxy
55 public class DefaultServiceInstanceService implements ServiceInstanceService {
56
57     private static final Logger logger = LoggerFactory.getLogger(DefaultServiceInstanceService.class);
58
59     private AAIService aaiService;
60     
61     @Resource(name="ServiceLcmService")
62     private ServiceLcmService serviceLcmService;
63     
64     @Resource(name="CustomerService")
65     private CustomerService customerService;
66
67     public void setCustomerService(CustomerService customerService) {
68         this.customerService = customerService;
69     }
70     
71         public void setServiceLcmService(ServiceLcmService serviceLcmService) {
72                 this.serviceLcmService = serviceLcmService;
73         }
74
75     public DefaultServiceInstanceService() {
76         this(RestfulServices.create(AAIService.class));
77     }
78
79     public DefaultServiceInstanceService(AAIService aaiService) {
80         this.aaiService = aaiService;
81     }
82
83     @Override
84     public List<String> listServiceInstances(String customerId, String serviceType) {
85         List<String> result = new ArrayList<>();
86         try {
87             Response<ResponseBody> response = aaiService.listServiceInstances(customerId, serviceType).execute();
88             if (response.isSuccessful()) {
89                 String resultStr=new String(response.body().bytes());
90                 JSONObject object = JSONObject.parseObject(resultStr);
91                 if(UuiCommonUtil.isNotNullOrEmpty(object)){
92                         result=this.parseServiceInstance(object, customerId, serviceType);
93                 }
94                 return result;
95             } else {
96                 logger.info(String.format("Can not get service instances[code=%s, message=%s]", response.code(), response.message()));
97                 return Collections.emptyList();
98             }
99         } catch (IOException e) {
100             logger.error("list services instances occur exception"+e.getMessage());
101             return Collections.emptyList();
102         }
103     }
104     
105         private List<String> parseServiceInstance(JSONObject objects,String customerId,String serviceType) throws JsonProcessingException{
106         ObjectMapper mapper = new ObjectMapper();
107         List<String> result = new ArrayList<>();
108         JSONArray serviceInstances=objects.getJSONArray("service-instance");
109         for(Object serviceInstance:serviceInstances){
110                         JSONObject object =  JSON.parseObject(serviceInstance+"");
111                         String serviceInstanceId=object.get("service-instance-id").toString();
112                         ServiceBean serviceBean = serviceLcmService.getServiceBeanByServiceInStanceId(serviceInstanceId);
113                         ServiceInstanceOperations serviceInstanceOperations = serviceLcmService.getServiceInstanceOperationById(serviceInstanceId);
114                         if(!UuiCommonUtil.isNotNullOrEmpty(serviceBean)){
115                                 continue;
116                         }
117                         String serviceDomain = serviceBean.getServiceDomain();
118                                 object.put("serviceDomain",serviceDomain);
119                                 object.put("operationResult",serviceInstanceOperations.getOperationResult());
120                                 object.put("operationId",serviceInstanceOperations.getOperationId());
121                                 object.put("operationType",serviceInstanceOperations.getOperationType());
122                                 if("SOTN".equals(serviceDomain)||"CCVPN".equals(serviceDomain)||"E2E Service".equals(serviceDomain)||"Network Service".equals(serviceDomain)){
123                                         List<String> parentIds = serviceLcmService.getServiceInstanceIdByParentId(serviceInstanceId);
124                                         List<String> parentServiceInstances = new ArrayList<>();
125                                         if(parentIds.size()>0){
126                                                 for(String id:parentIds){
127                                                         String parentServiceInstance=this.getRelationShipData(customerId, serviceType, id);
128                                                         parentServiceInstances.add(parentServiceInstance);
129                                                 }
130                                         }
131                                         object.put("childServiceInstances",parentServiceInstances);
132                                         result.add(object.toString());
133                                 }
134         }
135         return result;
136     }
137         
138         @Override
139         public String getRelationShipData(String customerId, String serviceType, String serviceId) {
140         try {
141             Response<ResponseBody> response = aaiService.getAAIServiceInstance(customerId, serviceType,serviceId).execute();
142             if (response.isSuccessful()) {
143                 String result=new String(response.body().bytes());
144                 return result;
145             } else {
146                 logger.info(String.format("Can not get service instances[code=%s, message=%s]", response.code(), response.message()));
147                 return "";
148             }
149         } catch (IOException e) {
150             logger.error("list services instances occur exception:"+e.getMessage());
151             return "";
152         }
153         }
154         
155         @Override
156         public String serviceNumByCustomer() throws JsonProcessingException{
157                 Map<String,Object> result = new HashMap();
158                 ObjectMapper omAlarm = new ObjectMapper();
159                 List<AAICustomer> customers = customerService.listCustomer();
160                 int total =0;
161                 List<Map<String,Object>> list = new ArrayList<>();
162                 if(customers.size()>0){
163                         for(AAICustomer customer : customers){
164                                 Map<String,Object> customerMap = new HashMap<String,Object>();
165                                 int customerNum = 0;
166                                 List<AAIServiceSubscription> serviceSubscriptions = customerService.listServiceSubscriptions(customer.getGlobalCustomerId());
167                                 if(serviceSubscriptions.size()>0){
168                                         for(AAIServiceSubscription serviceSubscription:serviceSubscriptions){
169                                                 List<String> serviceInstances =this.listServiceInstances(customer.getGlobalCustomerId(), serviceSubscription.getServiceType());
170                                                 total+=serviceInstances.size();
171                                                 customerNum+=serviceInstances.size();
172                                         }
173                                 }
174                                 customerMap.put("name",customer.getSubscriberName());
175                                 customerMap.put("value",customerNum);
176                                 list.add(customerMap);
177                         }
178                 }
179                 result.put("serviceTotalNum", total);
180                 result.put("customerServiceList", list);
181                 return omAlarm.writeValueAsString(result);
182         }
183         
184         @Override
185         public String serviceNumByServiceType(String customerId) throws JsonProcessingException{
186                 
187                 List<AAIServiceSubscription> serviceTypes = customerService.listServiceSubscriptions(customerId);
188                 
189                 List<Map<String,Object>> list = new ArrayList<>();
190                 
191                 ObjectMapper omAlarm = new ObjectMapper();
192                 
193                 for (AAIServiceSubscription aaiServiceSubscription : serviceTypes) {
194                         
195                         Map<String,Object> serviceTypeMap = new HashMap<String,Object>();
196                         
197                         List<String> serviceInstances =this.listServiceInstances(customerId, aaiServiceSubscription.getServiceType());
198                         
199                         serviceTypeMap.put(aaiServiceSubscription.getServiceType(),serviceInstances.size());
200                         
201                         list.add(serviceTypeMap);
202                 }
203                 return omAlarm.writeValueAsString(list);
204         }
205 }