Adding aai nodes endpoint
[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.HashMap;
26 import java.util.Map;
27 import java.util.Optional;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.ws.rs.core.MediaType;
30 import org.onap.aai.domain.yang.Project;
31 import org.onap.aai.domain.yang.Relationship;
32 import org.onap.so.aai.simulator.models.Format;
33 import org.onap.so.aai.simulator.models.Result;
34 import org.onap.so.aai.simulator.service.providers.ProjectCacheServiceProvider;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.ResponseEntity;
39 import org.springframework.stereotype.Controller;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.PutMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestParam;
46
47 /**
48  * @author waqas.ikram@ericsson.com
49  *
50  */
51 @Controller
52 @RequestMapping(path = PROJECT_URL)
53 public class ProjectController {
54     private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);
55
56     private final ProjectCacheServiceProvider cacheServiceProvider;
57
58     @Autowired
59     public ProjectController(final ProjectCacheServiceProvider cacheServiceProvider) {
60         this.cacheServiceProvider = cacheServiceProvider;
61     }
62
63     @PutMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
64             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
65     public ResponseEntity<?> putProject(@RequestBody final Project project,
66             @PathVariable("project-name") final String projectName, final HttpServletRequest request) {
67         LOGGER.info("Will put project for 'project-name': {} ...", project.getProjectName());
68
69         if (project.getResourceVersion() == null || project.getResourceVersion().isEmpty()) {
70             project.setResourceVersion(getResourceVersion());
71
72         }
73         cacheServiceProvider.putProject(projectName, project);
74         return ResponseEntity.accepted().build();
75
76     }
77
78     @GetMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
79             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(projectName, 1);
101                 return ResponseEntity.ok(new Result(map));
102             default:
103                 break;
104         }
105         LOGGER.error("invalid format type :{}", format);
106         return getRequestErrorResponseEntity(request);
107
108
109     }
110
111     @PutMapping(value = "/{project-name}/relationship-list/relationship",
112             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
113             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
114     public ResponseEntity<?> putProjectRelationShip(@RequestBody final Relationship relationship,
115             @PathVariable("project-name") final String projectName, final HttpServletRequest request) {
116
117         LOGGER.info("adding relationship for project-name: {} ...", projectName);
118         if (cacheServiceProvider.putProjectRelationShip(projectName, relationship)) {
119             LOGGER.info("added project relationship {} in cache", relationship);
120             return ResponseEntity.accepted().build();
121         }
122         LOGGER.error("Couldn't find {} in cache", projectName);
123         return getRequestErrorResponseEntity(request);
124     }
125
126 }