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