2260908140bbbfe8a05a1e2044de3f721a85a6e9
[vnfsdk/refrepo.git] /
1 /**
2  * Copyright 2017-18 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
51 import io.swagger.annotations.Api;
52 import io.swagger.annotations.ApiOperation;
53 import io.swagger.annotations.ApiParam;
54 import io.swagger.annotations.ApiResponse;
55 import io.swagger.annotations.ApiResponses;
56
57
58 @Path("/PackageResource")
59 @Api(tags = { "Package Resource" })
60 public class PackageResource {
61
62     static {
63
64     }
65     @Path("/updatestatus")
66     @POST
67     @ApiOperation(value = "update validate and lifecycle test status", response = UploadPackageResponse.class)
68     @Consumes(MediaType.APPLICATION_JSON)
69     @Produces(MediaType.APPLICATION_JSON)
70     @ApiResponses(value = {
71             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
72             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
73             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "update  error", response = String.class) })
74     public Response updateValidateStatus(@ApiParam(value = "http request body") @Context HttpServletRequest request,
75             @ApiParam(value = "http header") @Context HttpHeaders head) throws IOException {
76         InputStream input = request.getInputStream();
77         return PackageWrapper.getInstance().updateValidateStatus(input);
78
79     }
80
81     @Path("/csars")
82     @GET
83     @ApiOperation(value = "get csar package list by condition", response = PackageMeta.class, responseContainer = "List")
84     @Produces(MediaType.APPLICATION_JSON)
85     @ApiResponses(value = {
86             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
87             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
88             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
89     public Response queryPackageListByCond(@ApiParam(value = "csar name") @QueryParam("name") String name,
90             @ApiParam(value = "csar provider") @QueryParam("provider") String provider,
91             @ApiParam(value = "csar version") @QueryParam("version") String version,
92             @ApiParam(value = "delay to delete") @QueryParam("deletionPending") String deletionPending,
93             @ApiParam(value = "csar type") @QueryParam("type") String type) {
94         return PackageWrapper.getInstance().queryPackageListByCond(name, provider, version, deletionPending, type);
95     }
96
97     @Path("/csars/{csarId}")
98     @GET
99     @ApiOperation(value = "get csar package list", response = PackageMeta.class, responseContainer = "List")
100     @Produces(MediaType.APPLICATION_JSON)
101     @ApiResponses(value = {
102             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
103             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
104             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
105     public Response queryPackageById(@ApiParam(value = "csar id") @PathParam("csarId") String csarId) {
106         return PackageWrapper.getInstance().queryPackageById(csarId);
107     }
108
109     @Path("/csars")
110     @POST
111     @ApiOperation(value = "upload csar package", response = UploadPackageResponse.class)
112     @Consumes(MediaType.MULTIPART_FORM_DATA)
113     @Produces(MediaType.APPLICATION_JSON)
114     @ApiResponses(value = {
115             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
116             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
117             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
118     public Response uploadPackage(
119             @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream,
120             @FormDataParam("params") String details,
121             @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail,
122             @ApiParam(value = "http header") @Context HttpHeaders head)
123             throws IOException, MarketplaceResourceException {
124         return PackageWrapper.getInstance().uploadPackage(uploadedInputStream, fileDetail, details, head);
125     }
126
127     @Path("/csars/{csarId}")
128     @DELETE
129     @ApiOperation(value = "delete a package")
130     @ApiResponses(value = {
131             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
132             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
133             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
134     public Response delPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) {
135         return PackageWrapper.getInstance().delPackage(csarId);
136     }
137
138     @Path("/csars/{csarId}/files")
139     @GET
140     @ApiOperation(value = "get csar file uri by csarId", response = CsarFileUriResponse.class)
141     @Produces(MediaType.APPLICATION_JSON)
142     @ApiResponses(value = {
143             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
144             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
145             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
146     public Response getCsarFileUri(@ApiParam(value = "csar Id", required = true) @PathParam("csarId") String csarId) {
147         return PackageWrapper.getInstance().getCsarFileUri(csarId);
148     }
149
150     @Path("/csars/{csarId}/downloaded")
151     @GET
152     @ApiOperation(value = "update download count for a package", response = Response.class)
153     public Response updateDwonloadCount(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId) {
154         return PackageWrapper.getInstance().updateDwonloadCount(csarId);
155     }
156
157     @Path("/csars/{csarId}/reupload")
158     @POST
159     @ApiOperation(value = "re-upload csar package", response = UploadPackageResponse.class)
160     @Consumes(MediaType.MULTIPART_FORM_DATA)
161     @Produces(MediaType.APPLICATION_JSON)
162     @ApiResponses(value = {
163             @ApiResponse(code = HttpStatus.NOT_FOUND_404, message = "microservice not found", response = String.class),
164             @ApiResponse(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE_415, message = "Unprocessable MicroServiceInfo Entity ", response = String.class),
165             @ApiResponse(code = HttpStatus.INTERNAL_SERVER_ERROR_500, message = "resource grant error", response = String.class) })
166     public Response reUploadPackage(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId,
167             @ApiParam(value = "file inputstream", required = true) @FormDataParam("file") InputStream uploadedInputStream,
168             @FormDataParam("params") String details,
169             @ApiParam(value = "file detail", required = false) @FormDataParam("file") FormDataContentDisposition fileDetail,
170             @ApiParam(value = "http header") @Context HttpHeaders head)
171             throws IOException, MarketplaceResourceException {
172         return PackageWrapper.getInstance().reUploadPackage(csarId, uploadedInputStream, fileDetail, details, head);
173     }
174
175     @Path("/csars/{csarId}/onboardstatus")
176     @GET
177     @ApiOperation(value = "Get VNF OnBoarding Result", response = OnBoardingResult.class)
178     @Produces(MediaType.APPLICATION_JSON)
179     public Response getOnBoardingResult(@ApiParam("csar Id") @PathParam("csarId") String csarId,
180             @ApiParam("operation type") @QueryParam("operTypeId") String operTypeId,
181             @ApiParam("operation id") @QueryParam("operId") String operId) {
182         return PackageWrapper.getInstance().getOnBoardingResult(csarId, operTypeId, operId);
183     }
184
185     @Path("/csars/{csarId}/operresult")
186     @GET
187     @ApiOperation(value = "Get VNF OnBoarded Opeartion Result", response = Response.class)
188     @Produces(MediaType.APPLICATION_JSON)
189     public Response getOperStatus(@ApiParam(value = "csar Id") @PathParam("csarId") String csarId,
190             @ApiParam(value = "operation type") @QueryParam("operTypeId") String operTypeId) {
191         return PackageWrapper.getInstance().getOperResultByOperTypeId(csarId, operTypeId);
192     }
193
194     @Path("/csars/onboardsteps")
195     @GET
196     @ApiOperation(value = "Get VNF OnBoarded Steps", response = Response.class)
197     @Produces(MediaType.APPLICATION_JSON)
198     public Response getOnBoardingSteps() {
199         return PackageWrapper.getInstance().getOnBoardingSteps();
200     }
201
202     @Path("/healthcheck")
203     @GET
204     @ApiOperation(value = "Health for VNF Repository", response = Response.class)
205     @Produces(MediaType.APPLICATION_JSON)
206     public Response healthCheck() {
207
208         // Step 1: Check whether tomcat server is up
209         RestResponse resp = RestfulClient.get("127.0.0.1", CommonConstant.HTTP_PORT, CommonConstant.BASE_URL);
210         if (RestConstant.RESPONSE_CODE_200 != resp.getStatusCode()) {
211             return Response.serverError().build();
212         }
213
214         // Step 2: Check whether postgres database is up
215         try {
216             PackageManager.getInstance().queryPackageByCsarId("01");
217         } catch (Exception e) {
218             return Response.serverError().build();
219         }
220
221         return Response.ok().build();
222     }
223
224 }