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