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