Implementing download service CSAR endpoint
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdc-simulator / src / main / java / org / onap / so / sdcsimulator / controller / CatalogController.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdcsimulator.controller;
21
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;
37
38 /**
39  * @author Waqas Ikram (waqas.ikram@est.tech)
40  *
41  */
42 @Controller
43 @RequestMapping(path = CATALOG_URL)
44 public class CatalogController {
45     private static final Logger LOGGER = LoggerFactory.getLogger(CatalogController.class);
46
47     private final AssetProvider assetProvider;
48
49     @Autowired
50     public CatalogController(final AssetProvider assetProvider) {
51         this.assetProvider = assetProvider;
52     }
53
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));
58     }
59
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);
66         }
67         LOGGER.error("Unable to find csar: {}", csarId);
68
69         return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
70     }
71
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);
79         }
80         LOGGER.error("Unable to find metadata for csarId: {}", csarId);
81
82         return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
83     }
84
85
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));
90     }
91
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);
98         }
99         LOGGER.error("Unable to find csar: {}", csarId);
100
101         return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
102     }
103
104
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);
112         }
113         LOGGER.error("Unable to find metadata for csarId: {}", csarId);
114
115         return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
116     }
117
118
119 }