Merge "Update to Setup and Testcases due to PM Mapper dmaap plugin feature"
[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;
23 import static org.onap.so.aai.simulator.utils.Constants.PROJECT_URL;
24 import static org.onap.so.aai.simulator.utils.Utils.getRequestErrorResponseEntity;
25 import static org.onap.so.aai.simulator.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.aai.simulator.models.Format;
34 import org.onap.so.aai.simulator.models.Result;
35 import org.onap.so.aai.simulator.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}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
80             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
81     public ResponseEntity<?> getProject(@PathVariable("project-name") final String projectName,
82             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
83             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
84             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
85         LOGGER.info("retrieving project for 'project-name': {} ...", projectName);
86
87         final Optional<Project> optional = cacheServiceProvider.getProject(projectName);
88         if (!optional.isPresent()) {
89             LOGGER.error("Couldn't find {} in cache", projectName);
90             return getRequestErrorResponseEntity(request);
91         }
92
93         final Format value = Format.forValue(format);
94         switch (value) {
95             case RAW:
96                 final Project project = optional.get();
97                 LOGGER.info("found project {} in cache", project);
98                 return ResponseEntity.ok(project);
99             case COUNT:
100                 final Map<String, Object> map = new HashMap<>();
101                 map.put(PROJECT, 1);
102                 return ResponseEntity.ok(new Result(map));
103             default:
104                 break;
105         }
106         LOGGER.error("invalid format type :{}", format);
107         return getRequestErrorResponseEntity(request);
108     }
109
110     @PutMapping(value = "/{project-name}/relationship-list/relationship",
111             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
112             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
113     public ResponseEntity<?> putProjectRelationShip(@RequestBody final Relationship relationship,
114             @PathVariable("project-name") final String projectName, final HttpServletRequest request) {
115
116         LOGGER.info("adding relationship for project-name: {} ...", projectName);
117         if (cacheServiceProvider.putProjectRelationShip(projectName, relationship)) {
118             LOGGER.info("added project relationship {} in cache", relationship);
119             return ResponseEntity.accepted().build();
120         }
121         LOGGER.error("Couldn't find {} in cache", projectName);
122         return getRequestErrorResponseEntity(request);
123     }
124
125 }