2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.schemaservice.edges;
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;
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;
44 public class EdgeResource extends RESTAPI {
46 private final EdgeService edgeService;
47 private final SchemaVersions schemaVersions;
48 private final Gson gson;
51 public EdgeResource(EdgeService edgeService, SchemaVersions schemaVersions){
52 this.edgeService = edgeService;
53 this.schemaVersions = schemaVersions;
54 gson = new GsonBuilder().create();
59 @Produces({ "application/json"})
60 public Response retrieveSchema(@QueryParam("version") String version,
61 @Context HttpHeaders headers,
62 @Context UriInfo info)
64 Response response = null;
68 if(StringUtils.isEmpty(version)){
69 throw new AAIException("AAI_3050");
72 SchemaVersion schemaVersion = new SchemaVersion(version);
74 if(!schemaVersions.getVersions().contains(schemaVersion)){
75 throw new AAIException("AAI_3018", version);
78 Optional<EdgeRules> edgeRulesOptional = edgeService.findRules(version);
80 if(!edgeRulesOptional.isPresent()){
81 throw new AAIException("AAI_3001");
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"));