cd7c4001335c05686f0624fe61a29c1ff3641980
[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.Utils.getRequestErrorResponseEntity;
25 import static org.onap.so.aaisimulator.utils.Utils.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.Result;
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}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
82             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
83     public ResponseEntity<?> getOwningEntity(@PathVariable("owning-entity-id") final String owningEntityId,
84             @RequestParam(name = "resultIndex", required = false) final Integer resultIndex,
85             @RequestParam(name = "resultSize", required = false) final Integer resultSize,
86             @RequestParam(name = "format", required = false) final String format, final HttpServletRequest request) {
87         LOGGER.info("retrieving owning entity for 'owning-entity-id': {} ...", owningEntityId);
88
89         final Optional<OwningEntity> optional = cacheServiceProvider.getOwningEntity(owningEntityId);
90         if (!optional.isPresent()) {
91             LOGGER.error("Couldn't find {} in cache", owningEntityId);
92             return getRequestErrorResponseEntity(request);
93         }
94
95         final Format value = Format.forValue(format);
96         switch (value) {
97             case RAW:
98                 final OwningEntity owningEntity = optional.get();
99                 LOGGER.info("found OwningEntity {} in cache", owningEntity);
100                 return ResponseEntity.ok(owningEntity);
101             case COUNT:
102                 final Map<String, Object> map = new HashMap<>();
103                 map.put(OWNING_ENTITY, 1);
104                 return ResponseEntity.ok(new Result(map));
105             default:
106                 break;
107         }
108         LOGGER.error("invalid format type :{}", format);
109         return getRequestErrorResponseEntity(request);
110     }
111
112     @PutMapping(value = "/{owning-entity-id}/relationship-list/relationship",
113             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
114             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
115     public ResponseEntity<?> putOwningEntityRelationShip(@RequestBody final Relationship relationship,
116             @PathVariable("owning-entity-id") final String owningEntityId, final HttpServletRequest request) {
117
118         LOGGER.info("adding relationship for owning-entity-id: {} ...", owningEntityId);
119         if (cacheServiceProvider.putOwningEntityRelationShip(owningEntityId, relationship)) {
120             LOGGER.info("added OwningEntity relationship {} in cache", relationship);
121             return ResponseEntity.accepted().build();
122         }
123         LOGGER.error("Couldn't add relationship for {} in cache", owningEntityId);
124         return getRequestErrorResponseEntity(request);
125     }
126
127 }