Adding project endpoint and https support
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aai / simulator / controller / ProjectController.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.aai.simulator.controller;
21
22 import static org.onap.so.aai.simulator.utils.Constants.PROJECT_URL;
23 import static org.onap.so.aai.simulator.utils.Utils.getRequestErrorResponseEntity;
24 import static org.onap.so.aai.simulator.utils.Utils.getResourceVersion;
25 import java.util.Optional;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.aai.domain.yang.Project;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.so.aai.simulator.service.providers.ProjectCacheServiceProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.ResponseEntity;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PathVariable;
38 import org.springframework.web.bind.annotation.PutMapping;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestMapping;
41
42 /**
43  * @author waqas.ikram@ericsson.com
44  *
45  */
46 @Controller
47 @RequestMapping(path = PROJECT_URL)
48 public class ProjectController {
49     private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);
50
51     private final ProjectCacheServiceProvider cacheServiceProvider;
52
53     @Autowired
54     public ProjectController(final ProjectCacheServiceProvider cacheServiceProvider) {
55         this.cacheServiceProvider = cacheServiceProvider;
56     }
57
58     @PutMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
59             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
60     public ResponseEntity<?> putProject(@RequestBody final Project project,
61             @PathVariable("project-name") final String projectName, final HttpServletRequest request) {
62         LOGGER.info("Will put project for 'project-name': {} ...", project.getProjectName());
63
64         if (project.getResourceVersion() == null || project.getResourceVersion().isEmpty()) {
65             project.setResourceVersion(getResourceVersion());
66
67         }
68         cacheServiceProvider.putProject(projectName, project);
69         return ResponseEntity.accepted().build();
70
71     }
72
73     @GetMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
74             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
75     public ResponseEntity<?> getProject(@PathVariable("project-name") final String projectName,
76             final HttpServletRequest request) {
77         LOGGER.info("retrieving project for 'project-name': {} ...", projectName);
78
79         final Optional<Project> optional = cacheServiceProvider.getProject(projectName);
80         if (optional.isPresent()) {
81             final Project project = optional.get();
82             LOGGER.info("found project {} in cache", project);
83             return ResponseEntity.ok(project);
84         }
85
86         LOGGER.error("Couldn't find {} in cache", projectName);
87         return getRequestErrorResponseEntity(request);
88     }
89
90     @PutMapping(value = "/{project-name}/relationship-list/relationship",
91             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
92             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
93     public ResponseEntity<?> putProjectRelationShip(@RequestBody final Relationship relationship,
94             @PathVariable("project-name") final String projectName, final HttpServletRequest request) {
95
96         LOGGER.info("adding relationship for project-name: {} ...", projectName);
97         final boolean result = cacheServiceProvider.putProjectRelationShip(projectName, relationship);
98         if (result) {
99             LOGGER.info("added project relationship {} in cache", relationship);
100             return ResponseEntity.accepted().build();
101         }
102         LOGGER.error("Couldn't find {} in cache", projectName);
103         return getRequestErrorResponseEntity(request);
104
105     }
106
107 }