Add instructions to invoke the linter and code formatter plugins to the README and...
[aai/schema-service.git] / aai-schema-service / src / main / java / org / onap / aai / schemaservice / nodeschema / NodeSchemaResource.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.schemaservice.nodeschema;
22
23 import java.util.Optional;
24
25 import javax.ws.rs.GET;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.QueryParam;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.HttpHeaders;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.UriInfo;
33
34 import org.onap.aai.exceptions.AAIException;
35 import org.onap.aai.restcore.HttpMethod;
36 import org.onap.aai.restcore.RESTAPI;
37 import org.onap.aai.schemaservice.nodeschema.validation.AAISchemaValidationException;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.util.StringUtils;
40
41 @Path("/v1")
42 public class NodeSchemaResource extends RESTAPI {
43
44     private final NodeSchemaService nodeSchemaService;
45
46     private final SchemaVersions schemaVersions;
47
48     @Autowired
49     public NodeSchemaResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) {
50         this.nodeSchemaService = nodeSchemaService;
51         this.schemaVersions = schemaVersions;
52     }
53
54     @GET
55     @Path("/nodes")
56     @Produces({"application/xml"})
57     public Response retrieveSchema(@QueryParam("version") String version,
58         @Context HttpHeaders headers, @Context UriInfo info) {
59         Response response;
60         Optional<String> optionalSchema = nodeSchemaService.fetch(version);
61         try {
62
63             if (StringUtils.isEmpty(version)) {
64                 throw new AAIException("AAI_3050");
65             }
66
67             SchemaVersion schemaVersion = new SchemaVersion(version);
68
69             if (!schemaVersions.getVersions().contains(schemaVersion)) {
70                 throw new AAIException("AAI_3018", version);
71             }
72
73             if (!optionalSchema.isPresent()) {
74                 throw new AAIException("AAI_3001");
75             }
76
77             response = Response.ok(optionalSchema.get()).build();
78
79         } catch (AAIException ex) {
80             response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, ex);
81         } catch (AAISchemaValidationException ex) {
82             response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET,
83                 new AAIException("AAI_3051", version));
84         } catch (Exception ex) {
85             response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET,
86                 new AAIException("AAI_4000"));
87         }
88
89         return response;
90     }
91
92 }