d6f70d0c3f316f733a0713924f4fc0bc3affdee8
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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 java.io.InputStream;
19 import java.util.List;
20
21 import javax.ws.rs.client.Client;
22 import javax.ws.rs.client.ClientBuilder;
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.client.WebTarget;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27
28 import org.glassfish.jersey.client.ClientConfig;
29 import org.glassfish.jersey.media.multipart.BodyPart;
30 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
31 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
32 import org.glassfish.jersey.media.multipart.FormDataMultiPart;
33 import org.glassfish.jersey.media.multipart.MultiPartFeature;
34 import org.openo.commontosca.catalog.common.Config;
35 import org.openo.commontosca.catalog.model.externalservice.entity.containerEntity.ContainerSelfServiceOption;
36 import org.openo.commontosca.catalog.common.ToolUtil;
37 import org.openo.commontosca.catalog.model.externalservice.entity.containerEntity.ContainerSelfService;
38 import org.openo.commontosca.catalog.model.externalservice.entity.containerEntity.ContainerServiceNodeTemplateList;
39 import org.openo.commontosca.catalog.model.externalservice.entity.containerEntity.ContainerServiceTemplateList;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
44 import com.google.gson.JsonObject;
45
46 /**
47  * By rest requesting access container services
48  * @author 10189609
49  *
50  */
51 public class ContainerServiceConsumer {
52         private static final Logger LOG = LoggerFactory.getLogger(ContainerServiceConsumer.class);
53         /**
54          * get service template by template id from container service.
55          * @param templateid id
56          * @return template list entity
57          */
58     public static ContainerServiceTemplateList getServiceTemplates(
59             final String templateid) {
60         ClientConfig config = new ClientConfig(
61                 new ContainerServiceTemplateProvider());
62         IContainerTemplateRest containerservicetemplateproxy = ConsumerFactory
63                 .createConsumer(getBaseURL(), config,
64                         IContainerTemplateRest.class);
65         return containerservicetemplateproxy
66                 .getToscaServiceTemplate(templateid);
67     }
68
69     /**
70      * get operation input param xml from container service
71      * @param csarId
72      * @param operationId
73      * @return xml
74      */
75     public static String getOperationInputParamXml(final String csarId,
76             final String operationId) {
77         String inputparamsSoapXml = null;
78         ClientConfig config = new ClientConfig()
79                 .register(new ContainerSelfServiceProvider());
80         IContainerSelfServiceRest containerserviceoperationproxy = ConsumerFactory
81                 .createConsumer(getPackageURL(), config,
82                         IContainerSelfServiceRest.class);
83         String csarid = ToolUtil.formatCsar(csarId);
84         ContainerSelfService containerselfservice = containerserviceoperationproxy
85                 .getContainerSelfService(csarid);
86         if (containerselfservice != null) {
87             for (int i = 0; i < containerselfservice.getOptionList().size(); i++) {
88                 ContainerSelfServiceOption serviceOption = containerselfservice
89                         .getOptionList().get(i);
90                 if (serviceOption != null
91                         && operationId.equals(serviceOption.getId())) {
92                     inputparamsSoapXml = containerserviceoperationproxy
93                             .getContainerSelfServiceOptionInputMessage(
94                                         csarid,
95                                     serviceOption.getPlanInputMessageUrl());
96                     break;
97                 }
98             }
99         }
100         return inputparamsSoapXml;
101     }
102     
103     /**
104      * get operations by csar id.
105      * @param csarId
106      * @return xml
107      */
108     public static String getOperations(final String csarId) {
109         ClientConfig config = new ClientConfig()
110                         .register(new ContainerSelfServiceProvider());
111                 IContainerSelfServiceRest containerselfserviceproxy = ConsumerFactory
112                         .createConsumer(getPackageURL(), config,
113                                 IContainerSelfServiceRest.class);
114                 return containerselfserviceproxy.getContainerSelfServiceXML(ToolUtil.formatCsar(csarId));
115     }
116         
117         public static List<ContainerSelfServiceOption> getOperationList(final String csarId) {
118         ClientConfig config = new ClientConfig()
119                         .register(new ContainerSelfServiceProvider());
120                 IContainerSelfServiceRest containerserviceoperationproxy = ConsumerFactory
121                         .createConsumer(getPackageURL(), config,
122                 IContainerSelfServiceRest.class);
123         String csarid = ToolUtil.formatCsar(csarId);
124         ContainerSelfService containerselfservice = containerserviceoperationproxy
125                 .getContainerSelfService(csarid);      
126         return containerselfservice.getOptionList();
127     }
128
129     /**
130      * upload csar package to opentosca containerapi service.
131      * @param uploadedInputStream stream
132      * @param fileDetail
133      * @return response
134      */
135     public static Response uploadServicePackage(
136             InputStream uploadedInputStream,
137             FormDataContentDisposition fileDetail) {
138         final FormDataMultiPart formData = new FormDataMultiPart();
139         final BodyPart bodyPart = new FormDataBodyPart(fileDetail,
140                 uploadedInputStream, 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()
145                 .register(MultiPartFeature.class).build();
146         final WebTarget target = client.target(getPackageURL()).path(
147                 "/containerapi/CSARs");
148         final Response response = target.request().post(
149                 Entity.entity(formData, formData.getMediaType()));
150         return response;
151     }
152
153     /**
154      * upload csar package by file location.
155      * @param fileLocation
156      * @param fileDetail
157      * @return response
158      */
159     public static Response uploadServicePackageByLocation(String fileLocation) {
160         ClientConfig config = new ClientConfig();
161         IContainerExtPackageRest containerservicepackageproxy = ConsumerFactory
162                 .createConsumer(getBaseURL(), config,
163                         IContainerExtPackageRest.class);
164         String result = containerservicepackageproxy
165                 .uploadPackageByToscaService(fileLocation);
166         JsonObject json = new JsonObject();
167         json.addProperty("result", result);
168         return Response.ok(json.toString()).build();
169     }
170
171     /**
172      * delete a csar package by csar name.
173      * @param csarName
174      * @return
175      */
176     public static String delServicePackage(final String csarName) {
177         ClientConfig config = new ClientConfig();
178         IContainerExtPackageRest containerservicepackageproxy = ConsumerFactory
179                 .createConsumer(getBaseURL(), config,
180                     IContainerExtPackageRest.class);
181         LOG.info("url:" + getBaseURL() + " csarName:" + csarName);
182         return containerservicepackageproxy.deletePackageById(csarName);
183     }
184     
185     /**
186      * get node template list.
187      * @param templateId
188      * @return
189      */
190     public static ContainerServiceNodeTemplateList getNodeTemplates(
191                 final String templateId) {
192         ClientConfig config = new ClientConfig(new ContainerServiceNodeTemplateProvider());
193         IContainerTemplateRest containertemplateproxy = ConsumerFactory
194                         .createConsumer(getBaseURL(), config, IContainerTemplateRest.class);
195         return containertemplateproxy.getToscaServiceNodeTemplates(templateId);
196     }
197
198     /**
199      * get policy infomation by service template id from vnfd
200      * @param serviceTemplateID
201      * @return
202      */
203     public static String getPolicys(String serviceTemplateID) {
204         ClientConfig config = new ClientConfig(new StringProvider());
205         IContainerPortabilityRest containerPolicyproxy = ConsumerFactory
206                 .createConsumer(getBaseURL(), config,
207                         IContainerPortabilityRest.class);
208         return containerPolicyproxy.getToscaPolicys(serviceTemplateID);
209     }
210     
211     /**
212      * http://127.0.0.1:1337/containerapi/extension
213      * 
214      * @return
215      */
216     private static String getBaseURL() {
217         StringBuffer buffer = new StringBuffer();
218         buffer.append(Config.getConfigration().getOpentoscaServerAddr() + "/containerapi/extension");
219         return buffer.toString();
220     }
221
222     /**
223      * http://127.0.0.1:1337
224      * 
225      * @return
226      */
227     private static String getPackageURL() {
228         StringBuffer buffer = new StringBuffer();
229         buffer.append(Config.getConfigration().getOpentoscaServerAddr());
230         return buffer.toString();
231     }
232 }