modify e2e serviceInstancelist
[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.Collections;
20 import java.util.List;
21
22 import org.onap.usecaseui.server.service.lcm.ServiceInstanceService;
23 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
24 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.ServiceInstanceRsp;
25 import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;
26 import org.onap.usecaseui.server.util.RestfulServices;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
30 import org.springframework.stereotype.Service;
31
32 import okhttp3.ResponseBody;
33 import retrofit2.Response;
34
35 @Service("ServiceInstanceService")
36 @org.springframework.context.annotation.Configuration
37 @EnableAspectJAutoProxy
38 public class DefaultServiceInstanceService implements ServiceInstanceService {
39
40     private static final Logger logger = LoggerFactory.getLogger(DefaultServiceInstanceService.class);
41
42     private AAIService aaiService;
43
44     public DefaultServiceInstanceService() {
45         this(RestfulServices.create(AAIService.class));
46     }
47
48     public DefaultServiceInstanceService(AAIService aaiService) {
49         this.aaiService = aaiService;
50     }
51
52     @Override
53     public List<String> listServiceInstances(String customerId, String serviceType) {
54         try {
55             Response<ServiceInstanceRsp> response = aaiService.listServiceInstances(customerId, serviceType).execute();
56             if (response.isSuccessful()) {
57                 return response.body().getServiceInstances();
58             } else {
59                 logger.info(String.format("Can not get service instances[code=%s, message=%s]", response.code(), response.message()));
60                 return Collections.emptyList();
61             }
62         } catch (IOException e) {
63             logger.error("list services instances occur exception");
64             throw new AAIException("AAI is not available.", e);
65         }
66     }
67
68         @Override
69         public String getRelationShipData(String customerId, String serviceType, String serviceId) {
70         try {
71             Response<ResponseBody> response = aaiService.getAAIServiceInstance(customerId, serviceType,serviceId).execute();
72             if (response.isSuccessful()) {
73                 String result=new String(response.body().bytes());
74                 return result;
75             } else {
76                 logger.info(String.format("Can not get service instances[code=%s, message=%s]", response.code(), response.message()));
77                 return "";
78             }
79         } catch (IOException e) {
80             logger.error("list services instances occur exception");
81             throw new AAIException("AAI is not available.", e);
82         }
83         }
84 }