da809fb7f22b957b861589b599c066eb46b0ac29
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 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.openo.commontosca.catalog.model.externalservice.container;
17
18 import com.google.gson.JsonObject;
19
20 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
21
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;
36
37 import java.io.InputStream;
38 import java.util.List;
39
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;
46
47
48
49 /**
50  * By rest requesting access container services.
51  * 
52  * @author 10189609
53  * 
54  */
55 public class ContainerServiceConsumer {
56   private static final Logger LOG = LoggerFactory.getLogger(ContainerServiceConsumer.class);
57
58   /**
59    * get service template by template id from container service.
60    * 
61    * @param templateid id
62    * @return template list entity
63    */
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);
69   }
70
71   /**
72    * get operation input param xml from container service.
73    * 
74    * @param csarId package Id
75    * @param operationId operation id
76    * @return xml
77    */
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())) {
90           inputparamsSoapXml =
91               containerserviceoperationproxy.getContainerSelfServiceOptionInputMessage(csarid,
92                   serviceOption.getPlanInputMessageUrl());
93           break;
94         }
95       }
96     }
97     return inputparamsSoapXml;
98   }
99
100   /**
101    * get operations by csar id.
102    * 
103    * @param csarId package id
104    * @return xml
105    */
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));
111   }
112
113   /**
114    * get operation list.
115    * @param csarId package id
116    * @return container operation list
117    */
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();
126   }
127
128   /**
129    * upload csar package to opentosca containerapi service.
130    * 
131    * @param uploadedInputStream stream
132    * @param fileDetail file detail
133    * @return response
134    */
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()));
148     return response;
149   }
150
151   /**
152    * upload csar package by file location.
153    * 
154    * @param fileLocation file location
155    * @return response
156    */
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();
165   }
166
167   /**
168    * delete a csar package by csar name.
169    * 
170    * @param csarName package name
171    * @return package id which deleted
172    */
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);
179   }
180
181   /**
182    * get node template list.
183    * 
184    * @param templateId template id
185    * @return container service nodeTemplate list
186    */
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);
192   }
193
194   /**
195    * get policy infomation by service template id from vnfd.
196    * 
197    * @param serviceTemplateId service template id
198    * @return tosca policys
199    */
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);
205   }
206
207   /**
208    * http://127.0.0.1:1337/containerapi/extension.
209    * 
210    * @return base url
211    */
212   private static String getBaseUrl() {
213     StringBuffer buffer = new StringBuffer();
214     buffer.append(Config.getConfigration().getMsbServerAddr() + "/containerapi/extension");
215     return buffer.toString();
216   }
217
218   /**
219    * http://127.0.0.1:1337
220    * 
221    * @return package url
222    */
223   private static String getPackageUrl() {
224     StringBuffer buffer = new StringBuffer();
225     buffer.append(Config.getConfigration().getMsbServerAddr());
226     return buffer.toString();
227   }
228 }