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;
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;
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;
49 * @author waqas.ikram@ericsson.com
53 @RequestMapping(path = PROJECT_URL)
54 public class ProjectController {
55 private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);
57 private final ProjectCacheServiceProvider cacheServiceProvider;
60 public ProjectController(final ProjectCacheServiceProvider cacheServiceProvider) {
61 this.cacheServiceProvider = cacheServiceProvider;
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());
70 if (project.getResourceVersion() == null || project.getResourceVersion().isEmpty()) {
71 project.setResourceVersion(getResourceVersion());
74 cacheServiceProvider.putProject(projectName, project);
75 return ResponseEntity.accepted().build();
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);
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);
93 final Format value = Format.forValue(format);
96 final Project project = optional.get();
97 LOGGER.info("found project {} in cache", project);
98 return ResponseEntity.ok(project);
100 final Map<String, Object> map = new HashMap<>();
102 return ResponseEntity.ok(new Result(map));
106 LOGGER.error("invalid format type :{}", format);
107 return getRequestErrorResponseEntity(request);
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) {
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();
121 LOGGER.error("Couldn't find {} in cache", projectName);
122 return getRequestErrorResponseEntity(request);