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.OWNING_ENTITY;
23 import static org.onap.so.aaisimulator.utils.Constants.OWNING_ENTITY_URL;
24 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.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.OwningEntity;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.onap.so.aaisimulator.models.Format;
34 import org.onap.so.aaisimulator.models.Results;
35 import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider;
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 = OWNING_ENTITY_URL)
54 public class OwningEntityController {
56 private static final Logger LOGGER = LoggerFactory.getLogger(OwningEntityController.class);
58 private final OwnEntityCacheServiceProvider cacheServiceProvider;
61 public OwningEntityController(final OwnEntityCacheServiceProvider cacheServiceProvider) {
62 this.cacheServiceProvider = cacheServiceProvider;
66 @PutMapping(value = "{owning-entity-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
67 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
68 public ResponseEntity<?> putOwningEntity(@RequestBody final OwningEntity owningEntity,
69 @PathVariable("owning-entity-id") final String owningEntityId, final HttpServletRequest request) {
70 LOGGER.info("Will add OwningEntity to cache with key 'owning-entity-id': {} ...",
71 owningEntity.getOwningEntityId());
73 if (owningEntity.getResourceVersion() == null || owningEntity.getResourceVersion().isEmpty()) {
74 owningEntity.setResourceVersion(getResourceVersion());
77 cacheServiceProvider.putOwningEntity(owningEntityId, owningEntity);
78 return ResponseEntity.accepted().build();
81 @GetMapping(value = "{owning-entity-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
82 public ResponseEntity<?> getOwningEntity(@PathVariable("owning-entity-id") final String owningEntityId,
83 @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
84 @RequestParam(name = "resultSize", required = false) final Integer resultSize,
85 @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
86 LOGGER.info("retrieving owning entity for 'owning-entity-id': {} ...", owningEntityId);
88 final Optional<OwningEntity> optional = cacheServiceProvider.getOwningEntity(owningEntityId);
89 if (!optional.isPresent()) {
90 LOGGER.error("Couldn't find {} in cache", owningEntityId);
91 return getRequestErrorResponseEntity(request);
94 final Format value = Format.forValue(format);
97 final OwningEntity owningEntity = optional.get();
98 LOGGER.info("found OwningEntity {} in cache", owningEntity);
99 return ResponseEntity.ok(owningEntity);
101 final Map<String, Object> map = new HashMap<>();
102 map.put(OWNING_ENTITY, 1);
103 return ResponseEntity.ok(new Results(map));
107 LOGGER.error("invalid format type :{}", format);
108 return getRequestErrorResponseEntity(request);
111 @PutMapping(value = "/{owning-entity-id}/relationship-list/relationship",
112 consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
113 produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
114 public ResponseEntity<?> putOwningEntityRelationShip(@RequestBody final Relationship relationship,
115 @PathVariable("owning-entity-id") final String owningEntityId, final HttpServletRequest request) {
117 LOGGER.info("adding relationship for owning-entity-id: {} ...", owningEntityId);
118 if (cacheServiceProvider.putOwningEntityRelationShip(owningEntityId, relationship)) {
119 LOGGER.info("added OwningEntity relationship {} in cache", relationship);
120 return ResponseEntity.accepted().build();
122 LOGGER.error("Couldn't add relationship for {} in cache", owningEntityId);
123 return getRequestErrorResponseEntity(request);