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 com.google.common.io.Files;
19 import okhttp3.ResponseBody;
20 import org.onap.usecaseui.server.bean.lcm.ServiceTemplateInput;
21 import org.onap.usecaseui.server.bean.lcm.ServiceTemplateInputRsp;
22 import org.onap.usecaseui.server.bean.lcm.TemplateInput;
23 import org.onap.usecaseui.server.service.lcm.ServiceTemplateService;
24 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
25 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.VimInfo;
26 import org.onap.usecaseui.server.service.lcm.domain.sdc.SDCCatalogService;
27 import org.onap.usecaseui.server.service.lcm.domain.sdc.bean.SDCServiceTemplate;
28 import org.onap.usecaseui.server.service.lcm.domain.sdc.exceptions.SDCCatalogException;
29 import org.onap.usecaseui.server.util.RestfulServices;
30 import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
31 import org.openecomp.sdc.toscaparser.api.common.JToscaException;
32 import org.openecomp.sdc.toscaparser.api.parameters.Input;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.context.annotation.EnableAspectJAutoProxy;
36 import org.springframework.stereotype.Service;
39 import java.io.IOException;
40 import java.util.ArrayList;
41 import java.util.List;
43 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.CATEGORY_E2E_SERVICE;
44 import static org.onap.usecaseui.server.service.lcm.domain.sdc.consts.SDCConsts.DISTRIBUTION_STATUS_DISTRIBUTED;
46 @Service("ServiceTemplateService")
47 @org.springframework.context.annotation.Configuration
48 @EnableAspectJAutoProxy
49 public class DefaultServiceTemplateService implements ServiceTemplateService {
51 private static final Logger logger = LoggerFactory.getLogger(DefaultServiceTemplateService.class);
53 private SDCCatalogService sdcCatalog;
55 private AAIService aaiService;
57 public DefaultServiceTemplateService() {
58 this(RestfulServices.create(SDCCatalogService.class), RestfulServices.create(AAIService.class));
61 public DefaultServiceTemplateService(SDCCatalogService sdcCatalog, AAIService aaiService) {
62 this.sdcCatalog = sdcCatalog;
63 this.aaiService = aaiService;
67 public List<SDCServiceTemplate> listDistributedServiceTemplate() {
69 return this.sdcCatalog.listServices(CATEGORY_E2E_SERVICE, DISTRIBUTION_STATUS_DISTRIBUTED).execute().body();
70 } catch (IOException e) {
71 logger.error("Visit SDC Catalog occur exception");
72 logger.info("SDC Catalog Exception: ", e);
73 throw new SDCCatalogException("SDC Catalog is not available.", e);
78 public ServiceTemplateInputRsp fetchServiceTemplateInput(String uuid, String toscaModelPath) {
79 String rootPath = "http://localhost";// get from msb
80 String templateUrl = String.format("%s/%s", rootPath, toscaModelPath);
82 String toPath = String.format("temp/%s.csar", uuid);
84 downloadFile(templateUrl, toPath);
85 ServiceTemplateInput serviceTemplateInput = extractInputs(toPath);
86 List<VimInfo> vimInfos = aaiService.listVimInfo().execute().body();
87 return new ServiceTemplateInputRsp(serviceTemplateInput, vimInfos);
88 } catch (IOException e) {
89 throw new SDCCatalogException("download csar file failed!", e);
90 } catch (JToscaException e) {
91 throw new SDCCatalogException("parse csar file failed!", e);
95 private void downloadFile(String templateUrl, String toPath) throws IOException {
97 ResponseBody body = sdcCatalog.downloadCsar(templateUrl).execute().body();
98 Files.write(body.bytes(),new File(toPath));
99 } catch (IOException e) {
100 logger.error(String.format("Download %s failed!", templateUrl));
105 private ServiceTemplateInput extractInputs(String toPath) throws JToscaException {
106 ToscaTemplate tosca = new ToscaTemplate(toPath,null,true,null,true);
107 String name = tosca.getMetaData().getValue("name");
108 String type = tosca.getMetaData().getValue("type");
109 List<TemplateInput> templateInputs = new ArrayList<>();
110 for(Input input : tosca.getInputs()) {
111 templateInputs.add(new TemplateInput(
114 input.getDescription(),
115 String.valueOf(input.isRequired()),
116 String.valueOf(input.getDefault())
120 return new ServiceTemplateInput(name, type, templateInputs);