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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aai.simulator.controller;
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;
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;
48 * @author waqas.ikram@ericsson.com
52 @RequestMapping(path = PROJECT_URL)
53 public class ProjectController {
54 private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);
56 private final ProjectCacheServiceProvider cacheServiceProvider;
59 public ProjectController(final ProjectCacheServiceProvider cacheServiceProvider) {
60 this.cacheServiceProvider = cacheServiceProvider;
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());
69 if (project.getResourceVersion() == null || project.getResourceVersion().isEmpty()) {
70 project.setResourceVersion(getResourceVersion());
73 cacheServiceProvider.putProject(projectName, project);
74 return ResponseEntity.accepted().build();
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);
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);
92 final Format value = Format.forValue(format);
95 final Project project = optional.get();
96 LOGGER.info("found project {} in cache", project);
97 return ResponseEntity.ok(project);
99 final Map<String, Object> map = new HashMap<>();
100 map.put(projectName, 1);
101 return ResponseEntity.ok(new Result(map));
105 LOGGER.error("invalid format type :{}", format);
106 return getRequestErrorResponseEntity(request);
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) {
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();
122 LOGGER.error("Couldn't find {} in cache", projectName);
123 return getRequestErrorResponseEntity(request);