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 / edges / EdgeResource.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.edges;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.util.Optional;
27
28 import javax.ws.rs.GET;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.UriInfo;
36
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.restcore.HttpMethod;
39 import org.onap.aai.restcore.RESTAPI;
40 import org.onap.aai.schemaservice.nodeschema.SchemaVersion;
41 import org.onap.aai.schemaservice.nodeschema.SchemaVersions;
42 import org.onap.aai.schemaservice.nodeschema.validation.AAISchemaValidationException;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.util.StringUtils;
45
46 @Path("/v1")
47 public class EdgeResource extends RESTAPI {
48
49     private final EdgeService edgeService;
50     private final SchemaVersions schemaVersions;
51     private final Gson gson;
52
53     @Autowired
54     public EdgeResource(EdgeService edgeService, SchemaVersions schemaVersions) {
55         this.edgeService = edgeService;
56         this.schemaVersions = schemaVersions;
57         gson = new GsonBuilder().create();
58     }
59
60     @GET
61     @Path("/edgerules")
62     @Produces({"application/json"})
63     public Response retrieveSchema(@QueryParam("version") String version,
64         @Context HttpHeaders headers, @Context UriInfo info) {
65         Response response = null;
66
67         try {
68
69             if (StringUtils.isEmpty(version)) {
70                 throw new AAIException("AAI_3050");
71             }
72
73             SchemaVersion schemaVersion = new SchemaVersion(version);
74
75             if (!schemaVersions.getVersions().contains(schemaVersion)) {
76                 throw new AAIException("AAI_3018", version);
77             }
78
79             Optional<EdgeRules> edgeRulesOptional = edgeService.findRules(version);
80
81             if (!edgeRulesOptional.isPresent()) {
82                 throw new AAIException("AAI_3001");
83             }
84
85             response = Response.ok(gson.toJson(edgeRulesOptional.get())).build();
86         } catch (AAIException ex) {
87             response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, ex);
88         } catch (AAISchemaValidationException ex) {
89             response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET,
90                 new AAIException("AAI_3051", version));
91         } catch (Exception ex) {
92             response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET,
93                 new AAIException("AAI_4000"));
94         }
95
96         return response;
97     }
98 }