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.aaisimulator.controller;
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.Constants.RELATIONSHIP_LIST_RELATIONSHIP_URL;
25 import static org.onap.so.aaisimulator.utils.HttpServiceUtils.getHeaders;
26 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
27 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
28 import java.util.HashMap;
30 import java.util.Optional;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.core.MediaType;
33 import org.onap.aai.domain.yang.Project;
34 import org.onap.aai.domain.yang.Relationship;
35 import org.onap.so.aaisimulator.models.Format;
36 import org.onap.so.aaisimulator.models.Results;
37 import org.onap.so.aaisimulator.service.providers.ProjectCacheServiceProvider;
38 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PathVariable;
47 import org.springframework.web.bind.annotation.PutMapping;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestParam;
53 * @author waqas.ikram@ericsson.com
57 @RequestMapping(path = PROJECT_URL)
58 public class ProjectController {
59 private static final Logger LOGGER = LoggerFactory.getLogger(ProjectController.class);
61 private final ProjectCacheServiceProvider cacheServiceProvider;
64 public ProjectController(final ProjectCacheServiceProvider cacheServiceProvider) {
65 this.cacheServiceProvider = cacheServiceProvider;
68 @PutMapping(value = "/{project-name}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
69 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
70 public ResponseEntity<?> putProject(@RequestBody final Project project,
71 @PathVariable("project-name") final String projectName, final HttpServletRequest request) {
72 LOGGER.info("Will put project for 'project-name': {} ...", project.getProjectName());
74 if (project.getResourceVersion() == null || project.getResourceVersion().isEmpty()) {
75 project.setResourceVersion(getResourceVersion());
78 cacheServiceProvider.putProject(projectName, project);
79 return ResponseEntity.accepted().build();
83 @GetMapping(value = "/{project-name}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
84 public ResponseEntity<?> getProject(@PathVariable("project-name") final String projectName,
85 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
86 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
87 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
88 LOGGER.info("retrieving project for 'project-name': {} ...", projectName);
90 final Optional<Project> optional = cacheServiceProvider.getProject(projectName);
91 if (!optional.isPresent()) {
92 LOGGER.error("Couldn't find {} in cache", projectName);
93 return getRequestErrorResponseEntity(request);
96 final Format value = Format.forValue(format);
99 final Project project = optional.get();
100 LOGGER.info("found project {} in cache", project);
101 return ResponseEntity.ok(project);
103 final Map<String, Object> map = new HashMap<>();
105 return ResponseEntity.ok(new Results(map));
109 LOGGER.error("invalid format type :{}", format);
110 return getRequestErrorResponseEntity(request);
113 @PutMapping(value = "/{project-name}" + RELATIONSHIP_LIST_RELATIONSHIP_URL,
114 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
115 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
116 public ResponseEntity<?> putProjectRelationShip(@RequestBody final Relationship relationship,
117 @PathVariable("project-name") final String projectName, final HttpServletRequest request) {
119 LOGGER.info("adding relationship for project-name: {} ...", projectName);
121 if (relationship.getRelatedLink() != null) {
122 final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
123 final HttpHeaders incomingHeader = getHeaders(request);
125 final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
126 request.getRequestURI(), projectName, relationship);
128 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
129 return ResponseEntity.accepted().build();
133 LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
134 return getRequestErrorResponseEntity(request);