c5ade0cb6c837e7b629175c5ece85cf02b88f441
[integration/csit.git] / plans / usecases / pnf-sw-upgrade / so / simulator / 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.HttpServiceUtils.getHeaders;
25 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
26 import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Optional;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.core.MediaType;
32 import org.onap.aai.domain.yang.OwningEntity;
33 import org.onap.aai.domain.yang.Relationship;
34 import org.onap.so.aaisimulator.models.Format;
35 import org.onap.so.aaisimulator.models.Results;
36 import org.onap.so.aaisimulator.service.providers.OwnEntityCacheServiceProvider;
37 import org.onap.so.aaisimulator.utils.HttpServiceUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpHeaders;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestParam;
50
51 /**
52  * @author waqas.ikram@ericsson.com
53  *
54  */
55 @Controller
56 @RequestMapping(path = OWNING_ENTITY_URL)
57 public class OwningEntityController {
58
59     private static final Logger LOGGER = LoggerFactory.getLogger(OwningEntityController.class);
60
61     private final OwnEntityCacheServiceProvider cacheServiceProvider;
62
63     @Autowired
64     public OwningEntityController(final OwnEntityCacheServiceProvider cacheServiceProvider) {
65         this.cacheServiceProvider = cacheServiceProvider;
66     }
67
68     @PutMapping(value = "{owning-entity-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
69             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
70     public ResponseEntity<?> putOwningEntity(@RequestBody final OwningEntity owningEntity,
71             @PathVariable("owning-entity-id") final String owningEntityId, final HttpServletRequest request) {
72         LOGGER.info("Will add OwningEntity to cache with key 'owning-entity-id': {} ...",
73                 owningEntity.getOwningEntityId());
74
75         if (owningEntity.getResourceVersion() == null || owningEntity.getResourceVersion().isEmpty()) {
76             owningEntity.setResourceVersion(getResourceVersion());
77
78         }
79         cacheServiceProvider.putOwningEntity(owningEntityId, owningEntity);
80         return ResponseEntity.accepted().build();
81     }
82
83     @GetMapping(value = "{owning-entity-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
84     public ResponseEntity<?> getOwningEntity(@PathVariable("owning-entity-id") final String owningEntityId,
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 owning entity for 'owning-entity-id': {} ...", owningEntityId);
89
90         final Optional<OwningEntity> optional = cacheServiceProvider.getOwningEntity(owningEntityId);
91         if (!optional.isPresent()) {
92             LOGGER.error("Couldn't find {} in cache", owningEntityId);
93             return getRequestErrorResponseEntity(request);
94         }
95
96         final Format value = Format.forValue(format);
97         switch (value) {
98             case RAW:
99                 final OwningEntity owningEntity = optional.get();
100                 LOGGER.info("found OwningEntity {} in cache", owningEntity);
101                 return ResponseEntity.ok(owningEntity);
102             case COUNT:
103                 final Map<String, Object> map = new HashMap<>();
104                 map.put(OWNING_ENTITY, 1);
105                 return ResponseEntity.ok(new Results(map));
106             default:
107                 break;
108         }
109         LOGGER.error("invalid format type :{}", format);
110         return getRequestErrorResponseEntity(request);
111     }
112
113     @PutMapping(value = "/{owning-entity-id}/relationship-list/relationship",
114             consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
115             produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
116     public ResponseEntity<?> putOwningEntityRelationShip(@RequestBody final Relationship relationship,
117             @PathVariable("owning-entity-id") final String owningEntityId, final HttpServletRequest request) {
118
119         LOGGER.info("adding relationship for owning-entity-id: {} ...", owningEntityId);
120
121         if (relationship.getRelatedLink() != null) {
122             final String targetBaseUrl = HttpServiceUtils.getBaseUrl(request).toString();
123             final HttpHeaders incomingHeader = getHeaders(request);
124
125             final boolean result = cacheServiceProvider.addRelationShip(incomingHeader, targetBaseUrl,
126                     request.getRequestURI(), owningEntityId, relationship);
127             if (result) {
128                 LOGGER.info("added created bi directional relationship with {}", relationship.getRelatedLink());
129                 return ResponseEntity.accepted().build();
130             }
131         }
132
133         LOGGER.error("Unable to add relationship for related link: {}", relationship.getRelatedLink());
134         return getRequestErrorResponseEntity(request);
135     }
136
137 }