2 * Copyright 2016 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.openo.commontosca.catalog.model.externalservice.container;
18 import com.google.gson.JsonObject;
20 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
22 import org.glassfish.jersey.client.ClientConfig;
23 import org.glassfish.jersey.media.multipart.BodyPart;
24 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
25 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
26 import org.glassfish.jersey.media.multipart.FormDataMultiPart;
27 import org.glassfish.jersey.media.multipart.MultiPartFeature;
28 import org.openo.commontosca.catalog.common.Config;
29 import org.openo.commontosca.catalog.common.ToolUtil;
30 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerSelfService;
31 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerSelfServiceOption;
32 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerServiceNodeTemplateList;
33 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerServiceTemplateList;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import java.io.InputStream;
38 import java.util.List;
40 import javax.ws.rs.client.Client;
41 import javax.ws.rs.client.ClientBuilder;
42 import javax.ws.rs.client.Entity;
43 import javax.ws.rs.client.WebTarget;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
50 * By rest requesting access container services.
55 public class ContainerServiceConsumer {
56 private static final Logger LOG = LoggerFactory.getLogger(ContainerServiceConsumer.class);
59 * get service template by template id from container service.
61 * @param templateid id
62 * @return template list entity
64 public static ContainerServiceTemplateList getServiceTemplates(final String templateid) {
65 ClientConfig config = new ClientConfig(new ContainerServiceTemplateProvider());
66 IContainerTemplateRest containerservicetemplateproxy =
67 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerTemplateRest.class);
68 return containerservicetemplateproxy.getToscaServiceTemplate(templateid);
72 * get operation input param xml from container service.
74 * @param csarId package Id
75 * @param operationId operation id
78 public static String getOperationInputParamXml(final String csarId, final String operationId) {
79 String inputparamsSoapXml = null;
80 ClientConfig config = new ClientConfig().register(new ContainerSelfServiceProvider());
81 IContainerSelfServiceRest containerserviceoperationproxy =
82 ConsumerFactory.createConsumer(getPackageUrl(), config, IContainerSelfServiceRest.class);
83 String csarid = ToolUtil.formatCsar(csarId);
84 ContainerSelfService containerselfservice =
85 containerserviceoperationproxy.getContainerSelfService(csarid);
86 if (containerselfservice != null) {
87 for (int i = 0; i < containerselfservice.getOptionList().size(); i++) {
88 ContainerSelfServiceOption serviceOption = containerselfservice.getOptionList().get(i);
89 if (serviceOption != null && operationId.equals(serviceOption.getId())) {
91 containerserviceoperationproxy.getContainerSelfServiceOptionInputMessage(csarid,
92 serviceOption.getPlanInputMessageUrl());
97 return inputparamsSoapXml;
101 * get operations by csar id.
103 * @param csarId package id
106 public static String getOperations(final String csarId) {
107 ClientConfig config = new ClientConfig().register(new ContainerSelfServiceProvider());
108 IContainerSelfServiceRest containerselfserviceproxy =
109 ConsumerFactory.createConsumer(getPackageUrl(), config, IContainerSelfServiceRest.class);
110 return containerselfserviceproxy.getContainerSelfServiceXml(ToolUtil.formatCsar(csarId));
114 * get operation list.
115 * @param csarId package id
116 * @return container operation list
118 public static List<ContainerSelfServiceOption> getOperationList(final String csarId) {
119 ClientConfig config = new ClientConfig().register(new ContainerSelfServiceProvider());
120 IContainerSelfServiceRest containerserviceoperationproxy =
121 ConsumerFactory.createConsumer(getPackageUrl(), config, IContainerSelfServiceRest.class);
122 String csarid = ToolUtil.formatCsar(csarId);
123 ContainerSelfService containerselfservice =
124 containerserviceoperationproxy.getContainerSelfService(csarid);
125 return containerselfservice.getOptionList();
129 * upload csar package to opentosca containerapi service.
131 * @param uploadedInputStream stream
132 * @param fileDetail file detail
135 public static Response uploadServicePackage(InputStream uploadedInputStream,
136 FormDataContentDisposition fileDetail) {
137 final FormDataMultiPart formData = new FormDataMultiPart();
138 final BodyPart bodyPart =
139 new FormDataBodyPart(fileDetail, uploadedInputStream,
140 MediaType.APPLICATION_OCTET_STREAM_TYPE);
141 formData.bodyPart(bodyPart);
142 formData.setContentDisposition(fileDetail);
143 formData.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
144 final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
145 final WebTarget target = client.target(getPackageUrl()).path("/containerapi/CSARs");
146 final Response response =
147 target.request().post(Entity.entity(formData, formData.getMediaType()));
152 * upload csar package by file location.
154 * @param fileLocation file location
157 public static Response uploadServicePackageByLocation(String fileLocation) {
158 ClientConfig config = new ClientConfig();
159 IContainerExtPackageRest containerservicepackageproxy =
160 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerExtPackageRest.class);
161 String result = containerservicepackageproxy.uploadPackageByToscaService(fileLocation);
162 JsonObject json = new JsonObject();
163 json.addProperty("result", result);
164 return Response.ok(json.toString()).build();
168 * delete a csar package by csar name.
170 * @param csarName package name
171 * @return package id which deleted
173 public static String delServicePackage(final String csarName) {
174 ClientConfig config = new ClientConfig();
175 IContainerExtPackageRest containerservicepackageproxy =
176 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerExtPackageRest.class);
177 LOG.info("url:" + getBaseUrl() + " csarName:" + csarName);
178 return containerservicepackageproxy.deletePackageById(csarName);
182 * get node template list.
184 * @param templateId template id
185 * @return container service nodeTemplate list
187 public static ContainerServiceNodeTemplateList getNodeTemplates(final String templateId) {
188 ClientConfig config = new ClientConfig(new ContainerServiceNodeTemplateProvider());
189 IContainerTemplateRest containertemplateproxy =
190 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerTemplateRest.class);
191 return containertemplateproxy.getToscaServiceNodeTemplates(templateId);
195 * get policy infomation by service template id from vnfd.
197 * @param serviceTemplateId service template id
198 * @return tosca policys
200 public static String getPolicys(String serviceTemplateId) {
201 ClientConfig config = new ClientConfig(new StringProvider());
202 IContainerPortabilityRest containerPolicyproxy =
203 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerPortabilityRest.class);
204 return containerPolicyproxy.getToscaPolicys(serviceTemplateId);
208 * http://127.0.0.1:1337/containerapi/extension.
212 private static String getBaseUrl() {
213 StringBuffer buffer = new StringBuffer();
214 buffer.append(Config.getConfigration().getMsbServerAddr() + "/containerapi/extension");
215 return buffer.toString();
219 * http://127.0.0.1:1337
221 * @return package url
223 private static String getPackageUrl() {
224 StringBuffer buffer = new StringBuffer();
225 buffer.append(Config.getConfigration().getMsbServerAddr());
226 return buffer.toString();