2 * Copyright 2016 [ZTE] and others.
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.
17 package org.openo.commontosca.catalog.model.externalservice.container;
19 import com.google.gson.JsonObject;
21 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
23 import org.glassfish.jersey.client.ClientConfig;
24 import org.glassfish.jersey.media.multipart.BodyPart;
25 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
26 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
27 import org.glassfish.jersey.media.multipart.FormDataMultiPart;
28 import org.glassfish.jersey.media.multipart.MultiPartFeature;
29 import org.openo.commontosca.catalog.common.Config;
30 import org.openo.commontosca.catalog.common.ToolUtil;
31 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerSelfService;
32 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerSelfServiceOption;
33 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerServiceNodeTemplateList;
34 import org.openo.commontosca.catalog.model.externalservice.entity.container.ContainerServiceTemplateList;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import java.io.InputStream;
39 import java.util.List;
41 import javax.ws.rs.client.Client;
42 import javax.ws.rs.client.ClientBuilder;
43 import javax.ws.rs.client.Entity;
44 import javax.ws.rs.client.WebTarget;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
51 * By rest requesting access container services.
56 public class ContainerServiceConsumer {
57 private static final Logger LOG = LoggerFactory.getLogger(ContainerServiceConsumer.class);
60 * get service template by template id from container service.
62 * @param templateid id
63 * @return template list entity
65 public static ContainerServiceTemplateList getServiceTemplates(final String templateid) {
66 ClientConfig config = new ClientConfig(new ContainerServiceTemplateProvider());
67 IContainerTemplateRest containerservicetemplateproxy =
68 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerTemplateRest.class);
69 return containerservicetemplateproxy.getToscaServiceTemplate(templateid);
73 * get operation input param xml from container service.
75 * @param csarId package Id
76 * @param operationId operation id
79 public static String getOperationInputParamXml(final String csarId, final String operationId) {
80 String inputparamsSoapXml = null;
81 ClientConfig config = new ClientConfig().register(new ContainerSelfServiceProvider());
82 IContainerSelfServiceRest containerserviceoperationproxy =
83 ConsumerFactory.createConsumer(getPackageUrl(), config, IContainerSelfServiceRest.class);
84 String csarid = ToolUtil.formatCsar(csarId);
85 ContainerSelfService containerselfservice =
86 containerserviceoperationproxy.getContainerSelfService(csarid);
87 if (containerselfservice != null) {
88 for (int i = 0; i < containerselfservice.getOptionList().size(); i++) {
89 ContainerSelfServiceOption serviceOption = containerselfservice.getOptionList().get(i);
90 if (serviceOption != null && operationId.equals(serviceOption.getId())) {
92 containerserviceoperationproxy.getContainerSelfServiceOptionInputMessage(csarid,
93 serviceOption.getPlanInputMessageUrl());
98 return inputparamsSoapXml;
102 * get operations by csar id.
104 * @param csarId package id
107 public static String getOperations(final String csarId) {
108 ClientConfig config = new ClientConfig().register(new ContainerSelfServiceProvider());
109 IContainerSelfServiceRest containerselfserviceproxy =
110 ConsumerFactory.createConsumer(getPackageUrl(), config, IContainerSelfServiceRest.class);
111 return containerselfserviceproxy.getContainerSelfServiceXml(ToolUtil.formatCsar(csarId));
115 * get operation list.
116 * @param csarId package id
117 * @return container operation list
119 public static List<ContainerSelfServiceOption> getOperationList(final String csarId) {
120 ClientConfig config = new ClientConfig().register(new ContainerSelfServiceProvider());
121 IContainerSelfServiceRest containerserviceoperationproxy =
122 ConsumerFactory.createConsumer(getPackageUrl(), config, IContainerSelfServiceRest.class);
123 String csarid = ToolUtil.formatCsar(csarId);
124 ContainerSelfService containerselfservice =
125 containerserviceoperationproxy.getContainerSelfService(csarid);
126 return containerselfservice.getOptionList();
130 * upload csar package to opentosca containerapi service.
132 * @param uploadedInputStream stream
133 * @param fileDetail file detail
136 public static Response uploadServicePackage(InputStream uploadedInputStream,
137 FormDataContentDisposition fileDetail) {
138 final FormDataMultiPart formData = new FormDataMultiPart();
139 final BodyPart bodyPart =
140 new FormDataBodyPart(fileDetail, uploadedInputStream,
141 MediaType.APPLICATION_OCTET_STREAM_TYPE);
142 formData.bodyPart(bodyPart);
143 formData.setContentDisposition(fileDetail);
144 formData.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
145 final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
146 final WebTarget target = client.target(getPackageUrl()).path("/containerapi/CSARs");
147 final Response response =
148 target.request().post(Entity.entity(formData, formData.getMediaType()));
153 * upload csar package by file location.
155 * @param fileLocation file location
158 public static Response uploadServicePackageByLocation(String fileLocation) {
159 ClientConfig config = new ClientConfig();
160 IContainerExtPackageRest containerservicepackageproxy =
161 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerExtPackageRest.class);
162 String result = containerservicepackageproxy.uploadPackageByToscaService(fileLocation);
163 JsonObject json = new JsonObject();
164 json.addProperty("result", result);
165 return Response.ok(json.toString()).build();
169 * delete a csar package by csar name.
171 * @param csarName package name
172 * @return package id which deleted
174 public static String delServicePackage(final String csarName) {
175 ClientConfig config = new ClientConfig();
176 IContainerExtPackageRest containerservicepackageproxy =
177 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerExtPackageRest.class);
178 LOG.info("url:" + getBaseUrl() + " csarName:" + csarName);
179 return containerservicepackageproxy.deletePackageById(csarName);
183 * get node template list.
185 * @param templateId template id
186 * @return container service nodeTemplate list
188 public static ContainerServiceNodeTemplateList getNodeTemplates(final String templateId) {
189 ClientConfig config = new ClientConfig(new ContainerServiceNodeTemplateProvider());
190 IContainerTemplateRest containertemplateproxy =
191 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerTemplateRest.class);
192 return containertemplateproxy.getToscaServiceNodeTemplates(templateId);
196 * get policy infomation by service template id from vnfd.
198 * @param serviceTemplateId service template id
199 * @return tosca policys
201 public static String getPolicys(String serviceTemplateId) {
202 ClientConfig config = new ClientConfig(new StringProvider());
203 IContainerPortabilityRest containerPolicyproxy =
204 ConsumerFactory.createConsumer(getBaseUrl(), config, IContainerPortabilityRest.class);
205 return containerPolicyproxy.getToscaPolicys(serviceTemplateId);
209 * http://127.0.0.1:1337/containerapi/extension.
213 private static String getBaseUrl() {
214 StringBuffer buffer = new StringBuffer();
215 buffer.append(Config.getConfigration().getMsbServerAddr() + "/containerapi/extension");
216 return buffer.toString();
220 * http://127.0.0.1:1337
222 * @return package url
224 private static String getPackageUrl() {
225 StringBuffer buffer = new StringBuffer();
226 buffer.append(Config.getConfigration().getMsbServerAddr());
227 return buffer.toString();