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