9c3d70097e6c5021b88ecc424fb952c6c9ac1844
[vnfsdk/refrepo.git] /
1 /**
2  * Copyright 2017 Huawei Technologies Co., Ltd.
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.onap.vnfsdk.marketplace.resource;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.DELETE;
25 import javax.ws.rs.GET;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.PathParam;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.QueryParam;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.HttpHeaders;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35
36 import org.eclipse.jetty.http.HttpStatus;
37 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
38 import org.glassfish.jersey.media.multipart.FormDataParam;
39 import org.onap.vnfsdk.marketplace.common.CommonConstant;
40 import org.onap.vnfsdk.marketplace.db.exception.MarketplaceResourceException;
41 import org.onap.vnfsdk.marketplace.db.resource.PackageManager;
42 import org.onap.vnfsdk.marketplace.entity.response.CsarFileUriResponse;
43 import org.onap.vnfsdk.marketplace.entity.response.PackageMeta;
44 import org.onap.vnfsdk.marketplace.entity.response.UploadPackageResponse;
45 import org.onap.vnfsdk.marketplace.onboarding.entity.OnBoardingResult;
46 import org.onap.vnfsdk.marketplace.rest.RestConstant;
47 import org.onap.vnfsdk.marketplace.rest.RestResponse;
48 import org.onap.vnfsdk.marketplace.rest.RestfulClient;
49 import org.onap.vnfsdk.marketplace.wrapper.PackageWrapper;
50 import org.open.infc.grpc.Result;
51 import org.open.infc.grpc.client.OpenRemoteCli;
52
53 import io.swagger.annotations.Api;
54 import io.swagger.annotations.ApiOperation;
55 import io.swagger.annotations.ApiParam;
56 import io.swagger.annotations.ApiResponse;
57 import io.swagger.annotations.ApiResponses;
58
59
60 @Path("/PackageResource")
61 @Api(tags = { "Package Resource" })
62 public class PackageResource {
63
64     static {
65
66     }
67     @Path("/updatestatus")
68     @POST
69     @ApiOperation(value = "update validate and lifecycle test status", response = UploadPackageResponse.class)
70     @Consumes(MediaType.APPLICATION_JSON)
71     @Produces(MediaType.APPLICATION_JSON)
72     @ApiResponses(value = {
73             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
74             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
75             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "update  error", response = String.class) })
76     public Response updateValidateStatus(@ApiParam(value = "http request body") @Context HttpServletRequest request,
77             @ApiParam(value = "http header") @Context HttpHeaders head) throws IOException {
78         InputStream input = request.getInputStream();
79         return PackageWrapper.getInstance().updateValidateStatus(input);
80
81     }
82
83     @Path("/csars")
84     @GET
85     @ApiOperation(value = "get csar package list by condition", response = PackageMeta.class, responseContainer = "List")
86     @Produces(MediaType.APPLICATION_JSON)
87     @ApiResponses(value = {
88             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
89             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
90             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
91     public Response queryPackageListByCond(@ApiParam(value = "csar name") @QueryParam("name") String name,
92             @ApiParam(value = "csar provider") @QueryParam("provider") String provider,
93             @ApiParam(value = "csar version") @QueryParam("version") String version,
94             @ApiParam(value = "delay to delete") @QueryParam("deletionPending") String deletionPending,
95             @ApiParam(value = "csar type") @QueryParam("type") String type) {
96         return PackageWrapper.getInstance().queryPackageListByCond(name, provider, version, deletionPending, type);
97     }
98
99     @Path("/csars/{csarId}")
100     @GET
101     @ApiOperation(value = "get csar package list", response = PackageMeta.class, responseContainer = "List")
102     @Produces(MediaType.APPLICATION_JSON)
103     @ApiResponses(value = {
104             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
105             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
106             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
107     public Response queryPackageById(@ApiParam(value = "csar id") @PathParam("csarId") String csarId) {
108         return PackageWrapper.getInstance().queryPackageById(csarId);
109     }
110
111     @Path("/csars")
112     @POST
113     @ApiOperation(value = "upload csar package", response = UploadPackageResponse.class)
114     @Consumes(MediaType.MULTIPART_FORM_DATA)
115     @Produces(MediaType.APPLICATION_JSON)
116     @ApiResponses(value = {
117             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
118             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
119             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
120     public Response uploadPackage(
121             @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream,
122             @FormDataParam("params") String details,
123             @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail,
124             @ApiParam(value = "http header") @Context HttpHeaders head)
125             throws IOException, MarketplaceResourceException {
126         return PackageWrapper.getInstance().uploadPackage(uploadedInputStream, fileDetail, details, head);
127     }
128
129     @Path("/vtp/tests")
130     @GET
131     @ApiOperation(value = "VTP Test cases", response = UploadPackageResponse.class)
132     @Produces(MediaType.APPLICATION_JSON)
133     @ApiResponses(value = {
134             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unsupported type", response = String.class),
135             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "Failed to retrieve the tests", response = String.class) })
136     public Response listTests() throws IOException, MarketplaceResourceException {
137         Result result = null;
138         try {
139             result = OpenRemoteCli.run(new String[] { "-P", "open-cli", "schema-list", "--product", "onap-vtp", "--format", "json" });
140         } catch (Exception e) {
141             return Response.serverError().build();
142         }
143         return Response.ok(result.getOutput(), MediaType.APPLICATION_JSON).build();
144     }
145
146     @Path("/csars/{csarId}")
147     @DELETE
148     @ApiOperation(value = "delete a package")
149     @ApiResponses(value = {
150             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
151             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
152             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
153     public Response delPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) {
154         return PackageWrapper.getInstance().delPackage(csarId);
155     }
156
157     @Path("/csars/{csarId}/files")
158     @GET
159     @ApiOperation(value = "get csar file uri by csarId", response = CsarFileUriResponse.class)
160     @Produces(MediaType.APPLICATION_JSON)
161     @ApiResponses(value = {
162             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
163             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
164             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
165     public Response getCsarFileUri(@ApiParam(value = "csar Id", required = true) @PathParam("csarId") String csarId) {
166         return PackageWrapper.getInstance().getCsarFileUri(csarId);
167     }
168
169     @Path("/csars/{csarId}/downloaded")
170     @GET
171     @ApiOperation(value = "update download count for a package", response = Response.class)
172     public Response updateDwonloadCount(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) {
173         return PackageWrapper.getInstance().updateDwonloadCount(csarId);
174     }
175
176     @Path("/csars/{csarId}/reupload")
177     @POST
178     @ApiOperation(value = "re-upload csar package", response = UploadPackageResponse.class)
179     @Consumes(MediaType.MULTIPART_FORM_DATA)
180     @Produces(MediaType.APPLICATION_JSON)
181     @ApiResponses(value = {
182             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
183             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
184             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
185     public Response reUploadPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId,
186             @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream,
187             @FormDataParam("params") String details,
188             @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail,
189             @ApiParam(value = "http header") @Context HttpHeaders head)
190             throws IOException, MarketplaceResourceException {
191         return PackageWrapper.getInstance().reUploadPackage(csarId, uploadedInputStream, fileDetail, details, head);
192     }
193
194     @Path("/csars/{csarId}/onboardstatus")
195     @GET
196     @ApiOperation(value = "Get VNF OnBoarding Result", response = OnBoardingResult.class)
197     @Produces(MediaType.APPLICATION_JSON)
198     public Response getOnBoardingResult(@ApiParam("csar Id") @PathParam("csarId") String csarId,
199             @ApiParam("operation type") @QueryParam("operTypeId") String operTypeId,
200             @ApiParam("operation id") @QueryParam("operId") String operId) {
201         return PackageWrapper.getInstance().getOnBoardingResult(csarId, operTypeId, operId);
202     }
203
204     @Path("/csars/{csarId}/operresult")
205     @GET
206     @ApiOperation(value = "Get VNF OnBoarded Opeartion Result", response = Response.class)
207     @Produces(MediaType.APPLICATION_JSON)
208     public Response getOperStatus(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId,
209             @ApiParam(value = "operation type") @QueryParam("operTypeId") String operTypeId) {
210         return PackageWrapper.getInstance().getOperResultByOperTypeId(csarId, operTypeId);
211     }
212
213     @Path("/csars/onboardsteps")
214     @GET
215     @ApiOperation(value = "Get VNF OnBoarded Steps", response = Response.class)
216     @Produces(MediaType.APPLICATION_JSON)
217     public Response getOnBoardingSteps() {
218         return PackageWrapper.getInstance().getOnBoardingSteps();
219     }
220
221     @Path("/healthcheck")
222     @GET
223     @ApiOperation(value = "Health for VNF Repository", response = Response.class)
224     @Produces(MediaType.APPLICATION_JSON)
225     public Response healthCheck() {
226
227         // Step 1: Check whether tomcat server is up
228         RestResponse resp = RestfulClient.get("127.0.0.1", CommonConstant.HTTP_PORT, CommonConstant.BASE_URL);
229         if (RestConstant.RESPONSE_CODE_200 != resp.getStatusCode()) {
230             return Response.serverError().build();
231         }
232
233         // Step 2: Check whether postgres database is up
234         try {
235             PackageManager.getInstance().queryPackageByCsarId("01");
236         } catch (Exception e) {
237             return Response.serverError().build();
238         }
239
240         return Response.ok().build();
241     }
242
243 }