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.nodeschema;
23 import java.util.Optional;
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;
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;
42 public class NodeSchemaResource extends RESTAPI {
44 private final NodeSchemaService nodeSchemaService;
46 private final SchemaVersions schemaVersions;
49 public NodeSchemaResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) {
50 this.nodeSchemaService = nodeSchemaService;
51 this.schemaVersions = schemaVersions;
56 @Produces({"application/xml"})
57 public Response retrieveSchema(@QueryParam("version") String version,
58 @Context HttpHeaders headers, @Context UriInfo info) {
60 Optional<String> optionalSchema = nodeSchemaService.fetch(version);
63 if (StringUtils.isEmpty(version)) {
64 throw new AAIException("AAI_3050");
67 SchemaVersion schemaVersion = new SchemaVersion(version);
69 if (!schemaVersions.getVersions().contains(schemaVersion)) {
70 throw new AAIException("AAI_3018", version);
73 if (!optionalSchema.isPresent()) {
74 throw new AAIException("AAI_3001");
77 response = Response.ok(optionalSchema.get()).build();
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"));