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=========================================================
21 package org.onap.aai.schemaservice.edges;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import java.util.Optional;
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;
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;
47 public class EdgeResource extends RESTAPI {
49 private final EdgeService edgeService;
50 private final SchemaVersions schemaVersions;
51 private final Gson gson;
54 public EdgeResource(EdgeService edgeService, SchemaVersions schemaVersions) {
55 this.edgeService = edgeService;
56 this.schemaVersions = schemaVersions;
57 gson = new GsonBuilder().create();
62 @Produces({"application/json"})
63 public Response retrieveSchema(@QueryParam("version") String version,
64 @Context HttpHeaders headers, @Context UriInfo info) {
65 Response response = null;
69 if (StringUtils.isEmpty(version)) {
70 throw new AAIException("AAI_3050");
73 SchemaVersion schemaVersion = new SchemaVersion(version);
75 if (!schemaVersions.getVersions().contains(schemaVersion)) {
76 throw new AAIException("AAI_3018", version);
79 Optional<EdgeRules> edgeRulesOptional = edgeService.findRules(version);
81 if (!edgeRulesOptional.isPresent()) {
82 throw new AAIException("AAI_3001");
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"));