a0e07948f50ff5815b9417750121fe61e636abcb
[usecase-ui/server.git] /
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 org.onap.usecaseui.server.bean.lcm.VfNsPackageInfo;
19 import org.onap.usecaseui.server.service.lcm.PackageDistributionService;
20 import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
21 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
22 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.Vnf;
23 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
24 import org.onap.usecaseui.server.service.lcm.domain.vfc.VfcService;
25 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Csar;
26 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.DistributionResult;
27 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.Job;
28 import org.onap.usecaseui.server.service.lcm.domain.vfc.beans.JobStatus;
29 import org.onap.usecaseui.server.service.lcm.domain.vfc.exceptions.VfcException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.context.annotation.EnableAspectJAutoProxy;
33 import org.springframework.stereotype.Service;
34 import retrofit2.Response;
35
36 import java.io.IOException;
37 import java.util.Collections;
38 import java.util.List;
39
40 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.*;
41 import static org.onap.usecaseui.server.util.RestfulServices.create;
42
43 @Service("PackageDistributionService")
44 @org.springframework.context.annotation.Configuration
45 @EnableAspectJAutoProxy
46 public class DefaultPackageDistributionService implements PackageDistributionService {
47
48     private static final Logger logger = LoggerFactory.getLogger(DefaultPackageDistributionService.class);
49
50     private SDCCatalogService sdcCatalogService;
51
52     private VfcService vfcService;
53
54     public DefaultPackageDistributionService() {
55         this(create(SDCCatalogService.class), create(VfcService.class));
56     }
57
58     public DefaultPackageDistributionService(SDCCatalogService sdcCatalogService, VfcService vfcService) {
59         this.sdcCatalogService = sdcCatalogService;
60         this.vfcService = vfcService;
61     }
62
63     @Override
64     public VfNsPackageInfo retrievePackageInfo() {
65         try {
66             List<SDCServiceTemplate> nsTemplate = getNsTemplate();
67             List<Vnf> vnf = getVFResource();
68             return new VfNsPackageInfo(nsTemplate, vnf);
69         } catch (IOException e) {
70             throw new SDCCatalogException("SDC Service is not available!", e);
71         }
72     }
73
74     private List<Vnf> getVFResource() throws IOException {
75         Response<List<Vnf>> response = sdcCatalogService.listResources(RESOURCETYPE_VF).execute();
76         if (response.isSuccessful()) {
77             return response.body();
78         } else {
79             logger.info(String.format("Can not get VF resources[code=%s, message=%s]", response.code(), response.message()));
80             return Collections.emptyList();
81         }
82     }
83
84     private List<SDCServiceTemplate> getNsTemplate() throws IOException {
85         Response<List<SDCServiceTemplate>> response = sdcCatalogService.listServices(CATEGORY_NS, DISTRIBUTION_STATUS_DISTRIBUTED).execute();
86         if (response.isSuccessful()) {
87             return response.body();
88         } else {
89             logger.info(String.format("Can not get NS services[code=%s, message=%s]", response.code(), response.message()));
90             return Collections.emptyList();
91         }
92     }
93
94     @Override
95     public DistributionResult postNsPackage(Csar csar) {
96         try {
97             Response<DistributionResult> response = vfcService.distributeNsPackage(csar).execute();
98             if (response.isSuccessful()) {
99                 return response.body();
100             } else {
101                 logger.info(String.format("Can not post NS packages[code=%s, message=%s]", response.code(), response.message()));
102                 throw new VfcException("VFC service is not available!");
103             }
104         } catch (IOException e) {
105             throw new VfcException("VFC service is not available!", e);
106         }
107     }
108
109     @Override
110     public Job postVfPackage(Csar csar) {
111         try {
112             Response<Job> response = vfcService.distributeVnfPackage(csar).execute();
113             if (response.isSuccessful()) {
114                 return response.body();
115             } else {
116                 logger.info(String.format("Can not get VF packages[code=%s, message=%s]", response.code(), response.message()));
117                 throw new VfcException("VFC service is not available!");
118             }
119         } catch (IOException e) {
120             throw new VfcException("VFC service is not available!", e);
121         }
122     }
123
124     @Override
125     public JobStatus getJobStatus(String jobId) {
126         try {
127             Response<JobStatus> response = vfcService.getJobStatus(jobId).execute();
128             if (response.isSuccessful()) {
129                 return response.body();
130             } else {
131                 logger.info(String.format("Can not get Job status[code=%s, message=%s]", response.code(), response.message()));
132                 throw new VfcException("VFC service is not available!");
133             }
134         } catch (IOException e) {
135             throw new VfcException("VFC service is not available!", e);
136         }
137     }
138
139     @Override
140     public DistributionResult deleteNsPackage(String csarId) {
141         try {
142             Response<DistributionResult> response = vfcService.deleteNsPackage(csarId).execute();
143             if (response.isSuccessful()) {
144                 return response.body();
145             } else {
146                 logger.info(String.format("Can not delete NS packages[code=%s, message=%s]", response.code(), response.message()));
147                 throw new VfcException("VFC service is not available!");
148             }
149         } catch (IOException e) {
150             throw new VfcException("VFC service is not available!", e);
151         }
152     }
153
154     @Override
155     public DistributionResult deleteVfPackage(String csarId) {
156         try {
157             Response<DistributionResult> response = vfcService.deleteVnfPackage(csarId).execute();
158             if (response.isSuccessful()) {
159                 return response.body();
160             } else {
161                 logger.info(String.format("Can not delete VF packages[code=%s, message=%s]", response.code(), response.message()));
162                 throw new VfcException("VFC service is not available!");
163             }
164         } catch (IOException e) {
165             throw new VfcException("VFC service is not available!", e);
166         }
167     }
168 }