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