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 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;
 
  36 import java.io.IOException;
 
  37 import java.util.Collections;
 
  38 import java.util.List;
 
  40 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.*;
 
  41 import static org.onap.usecaseui.server.util.RestfulServices.create;
 
  43 @Service("PackageDistributionService")
 
  44 @org.springframework.context.annotation.Configuration
 
  45 @EnableAspectJAutoProxy
 
  46 public class DefaultPackageDistributionService implements PackageDistributionService {
 
  48     private static final Logger logger = LoggerFactory.getLogger(DefaultPackageDistributionService.class);
 
  50     private SDCCatalogService sdcCatalogService;
 
  52     private VfcService vfcService;
 
  54     public DefaultPackageDistributionService() {
 
  55         this(create(SDCCatalogService.class), create(VfcService.class));
 
  58     public DefaultPackageDistributionService(SDCCatalogService sdcCatalogService, VfcService vfcService) {
 
  59         this.sdcCatalogService = sdcCatalogService;
 
  60         this.vfcService = vfcService;
 
  64     public VfNsPackageInfo retrievePackageInfo() {
 
  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);
 
  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();
 
  79             logger.info(String.format("Can not get VF resources[code=%s, message=%s]", response.code(), response.message()));
 
  80             return Collections.emptyList();
 
  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();
 
  89             logger.info(String.format("Can not get NS services[code=%s, message=%s]", response.code(), response.message()));
 
  90             return Collections.emptyList();
 
  95     public DistributionResult postNsPackage(Csar csar) {
 
  97             Response<DistributionResult> response = vfcService.distributeNsPackage(csar).execute();
 
  98             if (response.isSuccessful()) {
 
  99                 return response.body();
 
 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!");
 
 104         } catch (IOException e) {
 
 105             throw new VfcException("VFC service is not available!", e);
 
 110     public Job postVfPackage(Csar csar) {
 
 112             Response<Job> response = vfcService.distributeVnfPackage(csar).execute();
 
 113             if (response.isSuccessful()) {
 
 114                 return response.body();
 
 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!");
 
 119         } catch (IOException e) {
 
 120             throw new VfcException("VFC service is not available!", e);
 
 125     public JobStatus getJobStatus(String jobId) {
 
 127             Response<JobStatus> response = vfcService.getJobStatus(jobId).execute();
 
 128             if (response.isSuccessful()) {
 
 129                 return response.body();
 
 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!");
 
 134         } catch (IOException e) {
 
 135             throw new VfcException("VFC service is not available!", e);
 
 140     public DistributionResult deleteNsPackage(String csarId) {
 
 142             Response<DistributionResult> response = vfcService.deleteNsPackage(csarId).execute();
 
 143             if (response.isSuccessful()) {
 
 144                 return response.body();
 
 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!");
 
 149         } catch (IOException e) {
 
 150             throw new VfcException("VFC service is not available!", e);
 
 155     public DistributionResult deleteVfPackage(String csarId) {
 
 157             Response<DistributionResult> response = vfcService.deleteVnfPackage(csarId).execute();
 
 158             if (response.isSuccessful()) {
 
 159                 return response.body();
 
 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!");
 
 164         } catch (IOException e) {
 
 165             throw new VfcException("VFC service is not available!", e);