2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.sdcsimulator.controller;
22 import static org.onap.so.sdcsimulator.utils.Constants.CATALOG_URL;
23 import java.util.Optional;
24 import javax.ws.rs.core.MediaType;
25 import org.onap.so.sdcsimulator.models.AssetType;
26 import org.onap.so.sdcsimulator.models.Metadata;
27 import org.onap.so.sdcsimulator.providers.AssetProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.annotation.GetMapping;
35 import org.springframework.web.bind.annotation.PathVariable;
36 import org.springframework.web.bind.annotation.RequestMapping;
39 * @author Waqas Ikram (waqas.ikram@est.tech)
43 @RequestMapping(path = CATALOG_URL)
44 public class CatalogController {
45 private static final Logger LOGGER = LoggerFactory.getLogger(CatalogController.class);
47 private final AssetProvider assetProvider;
50 public CatalogController(final AssetProvider assetProvider) {
51 this.assetProvider = assetProvider;
54 @GetMapping(value = "/resources", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
55 public ResponseEntity<?> getResources() {
56 LOGGER.info("Running getResources ...");
57 return ResponseEntity.ok().body(assetProvider.getAssetInfo(AssetType.RESOURCES));
60 @GetMapping(value = "/resources/{csarId}/toscaModel", produces = MediaType.APPLICATION_OCTET_STREAM)
61 public ResponseEntity<byte[]> getResourceCsar(@PathVariable("csarId") final String csarId) {
62 LOGGER.info("Running getCsar for {} ...", csarId);
63 final Optional<byte[]> resource = assetProvider.getAsset(csarId, AssetType.RESOURCES);
64 if (resource.isPresent()) {
65 return new ResponseEntity<>(resource.get(), HttpStatus.OK);
67 LOGGER.error("Unable to find csar: {}", csarId);
69 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
72 @GetMapping(value = "/resources/{csarId}/metadata",
73 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
74 public ResponseEntity<Metadata> getResourceMetadata(@PathVariable("csarId") final String csarId) {
75 LOGGER.info("Running getResourceMetadata for {} ...", csarId);
76 final Optional<Metadata> resource = assetProvider.getMetadata(csarId, AssetType.RESOURCES);
77 if (resource.isPresent()) {
78 return new ResponseEntity<>(resource.get(), HttpStatus.OK);
80 LOGGER.error("Unable to find metadata for csarId: {}", csarId);
82 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
86 @GetMapping(value = "/services", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
87 public ResponseEntity<?> getServices() {
88 LOGGER.info("Running getServices ...");
89 return ResponseEntity.ok().body(assetProvider.getAssetInfo(AssetType.SERVICES));
92 @GetMapping(value = "/services/{csarId}/toscaModel", produces = MediaType.APPLICATION_OCTET_STREAM)
93 public ResponseEntity<byte[]> getServiceCsar(@PathVariable("csarId") final String csarId) {
94 LOGGER.info("Running getServiceCsar for {} ...", csarId);
95 final Optional<byte[]> resource = assetProvider.getAsset(csarId, AssetType.SERVICES);
96 if (resource.isPresent()) {
97 return new ResponseEntity<>(resource.get(), HttpStatus.OK);
99 LOGGER.error("Unable to find csar: {}", csarId);
101 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
105 @GetMapping(value = "/services/{csarId}/metadata",
106 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
107 public ResponseEntity<Metadata> getServiceMetadata(@PathVariable("csarId") final String csarId) {
108 LOGGER.info("Running getServiceMetadata for {} ...", csarId);
109 final Optional<Metadata> resource = assetProvider.getMetadata(csarId, AssetType.SERVICES);
110 if (resource.isPresent()) {
111 return new ResponseEntity<>(resource.get(), HttpStatus.OK);
113 LOGGER.error("Unable to find metadata for csarId: {}", csarId);
115 return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);