2 * Copyright 2016-2017 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.service.lcm.impl;
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;
25 import javax.annotation.Resource;
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;
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;
49 import okhttp3.ResponseBody;
50 import retrofit2.Response;
52 @Service("ServiceInstanceService")
53 @org.springframework.context.annotation.Configuration
54 @EnableAspectJAutoProxy
55 public class DefaultServiceInstanceService implements ServiceInstanceService {
57 private static final Logger logger = LoggerFactory.getLogger(DefaultServiceInstanceService.class);
59 private AAIService aaiService;
61 @Resource(name="ServiceLcmService")
62 private ServiceLcmService serviceLcmService;
64 @Resource(name="CustomerService")
65 private CustomerService customerService;
67 public void setCustomerService(CustomerService customerService) {
68 this.customerService = customerService;
71 public void setServiceLcmService(ServiceLcmService serviceLcmService) {
72 this.serviceLcmService = serviceLcmService;
75 public DefaultServiceInstanceService() {
76 this(RestfulServices.create(AAIService.class));
79 public DefaultServiceInstanceService(AAIService aaiService) {
80 this.aaiService = aaiService;
84 public List<String> listServiceInstances(String customerId, String serviceType) {
85 List<String> result = new ArrayList<>();
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);
96 logger.info(String.format("Can not get service instances[code=%s, message=%s]", response.code(), response.message()));
97 return Collections.emptyList();
99 } catch (IOException e) {
100 logger.error("list services instances occur exception"+e.getMessage());
101 return Collections.emptyList();
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)){
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);
131 object.put("childServiceInstances",parentServiceInstances);
132 result.add(object.toString());
139 public String getRelationShipData(String customerId, String serviceType, String serviceId) {
141 Response<ResponseBody> response = aaiService.getAAIServiceInstance(customerId, serviceType,serviceId).execute();
142 if (response.isSuccessful()) {
143 String result=new String(response.body().bytes());
146 logger.info(String.format("Can not get service instances[code=%s, message=%s]", response.code(), response.message()));
149 } catch (IOException e) {
150 logger.error("list services instances occur exception:"+e.getMessage());
156 public String serviceNumByCustomer() throws JsonProcessingException{
157 Map<String,Object> result = new HashMap();
158 ObjectMapper omAlarm = new ObjectMapper();
159 List<AAICustomer> customers = customerService.listCustomer();
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>();
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();
174 customerMap.put("name",customer.getSubscriberName());
175 customerMap.put("value",customerNum);
176 list.add(customerMap);
179 result.put("serviceTotalNum", total);
180 result.put("customerServiceList", list);
181 return omAlarm.writeValueAsString(result);
185 public String serviceNumByServiceType(String customerId) throws JsonProcessingException{
187 List<AAIServiceSubscription> serviceTypes = customerService.listServiceSubscriptions(customerId);
189 List<Map<String,Object>> list = new ArrayList<>();
191 ObjectMapper omAlarm = new ObjectMapper();
193 for (AAIServiceSubscription aaiServiceSubscription : serviceTypes) {
195 Map<String,Object> serviceTypeMap = new HashMap<String,Object>();
197 List<String> serviceInstances =this.listServiceInstances(customerId, aaiServiceSubscription.getServiceType());
199 serviceTypeMap.put(aaiServiceSubscription.getServiceType(),serviceInstances.size());
201 list.add(serviceTypeMap);
203 return omAlarm.writeValueAsString(list);