4923083dd6e345b1fd3d29f5e7ff050c86877a29
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / main / java / org / onap / so / aaisimulator / controller / OwningEntityController.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.aaisimulator.controller;
21
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;
27 import java.util.Map;
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;
47
48 /**
49  * @author waqas.ikram@ericsson.com
50  *
51  */
52 @Controller
53 @RequestMapping(path = OWNING_ENTITY_URL)
54 public class OwningEntityController {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(OwningEntityController.class);
57
58     private final OwnEntityCacheServiceProvider cacheServiceProvider;
59
60     @Autowired
61     public OwningEntityController(final OwnEntityCacheServiceProvider cacheServiceProvider) {
62         this.cacheServiceProvider = cacheServiceProvider;
63     }
64
65
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());
72
73         if (owningEntity.getResourceVersion() == null || owningEntity.getResourceVersion().isEmpty()) {
74             owningEntity.setResourceVersion(getResourceVersion());
75
76         }
77         cacheServiceProvider.putOwningEntity(owningEntityId, owningEntity);
78         return ResponseEntity.accepted().build();
79     }
80
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);
87
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);
92         }
93
94         final Format value = Format.forValue(format);
95         switch (value) {
96             case RAW:
97                 final OwningEntity owningEntity = optional.get();
98                 LOGGER.info("found OwningEntity {} in cache", owningEntity);
99                 return ResponseEntity.ok(owningEntity);
100             case COUNT:
101                 final Map<String, Object> map = new HashMap<>();
102                 map.put(OWNING_ENTITY, 1);
103                 return ResponseEntity.ok(new Results(map));
104             default:
105                 break;
106         }
107         LOGGER.error("invalid format type :{}", format);
108         return getRequestErrorResponseEntity(request);
109     }
110
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) {
116
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();
121         }
122         LOGGER.error("Couldn't add relationship for {} in cache", owningEntityId);
123         return getRequestErrorResponseEntity(request);
124     }
125
126 }