aboutsummaryrefslogtreecommitdiffstats
path: root/plans/usecases/pnf-sw-upgrade/so/simulator/aai-simulator/src/main/java/org/onap/so/aaisimulator/controller/PnfsController.java
blob: ff0e3dcf0d75166b0d3218bef674644f18864856 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.aaisimulator.controller;


import org.onap.aai.domain.yang.v15.Pnf;
import org.onap.aai.domain.yang.v15.Pnfs;
import org.onap.so.aaisimulator.service.providers.PnfCacheServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Optional;

import static org.onap.so.aaisimulator.utils.Constants.APPLICATION_MERGE_PATCH_JSON;
import static org.onap.so.aaisimulator.utils.Constants.PNF;
import static org.onap.so.aaisimulator.utils.Constants.PNFS_URL;
import static org.onap.so.aaisimulator.utils.Constants.X_HTTP_METHOD_OVERRIDE;
import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getRequestErrorResponseEntity;
import static org.onap.so.aaisimulator.utils.RequestErrorResponseUtils.getResourceVersion;

/**
 * @author Raj Gumma (raj.gumma@est.tech)
 */
@Controller
@RequestMapping(path = PNFS_URL)
public class PnfsController {

    private static final Logger LOGGER = LoggerFactory.getLogger(PnfsController.class);

    private final PnfCacheServiceProvider cacheServiceProvider;


    @Autowired
    public PnfsController(final PnfCacheServiceProvider cacheServiceProvider) {
        this.cacheServiceProvider = cacheServiceProvider;
    }

    @PutMapping(value = "/pnf/{pnf-id}", consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
            produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> putPnf(@RequestBody final Pnf pnf,
                                    @PathVariable("pnf-id") final String pnfId, final HttpServletRequest request) {
        LOGGER.info("Will add Pnf to cache with 'pnf-id': {} ...", pnfId);

        if (pnf.getResourceVersion() == null || pnf.getResourceVersion().isEmpty()) {
            pnf.setResourceVersion(getResourceVersion());
        }
        cacheServiceProvider.putPnf(pnfId, pnf);
        return ResponseEntity.accepted().build();
    }

    @GetMapping(value = "/pnf/{pnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> getPnf(@PathVariable("pnf-id") final String pnfId, final HttpServletRequest request) {
        LOGGER.info("Will get Pnf for 'pnf-id': {} ", pnfId);

        final Optional<Pnf> optional = cacheServiceProvider.getPnf(pnfId);

        if (optional.isPresent()) {
            final Pnf pnf = optional.get();
            LOGGER.info("found Pnf {} in cache", pnf);
            return ResponseEntity.ok(pnf);
        }

        LOGGER.error("Unable to find Pnf in cache for 'pnf-id': {}", pnfId);
        return getRequestErrorResponseEntity(request, "pnf");

    }

    @PostMapping(value = "/pnf/{pnf-id}",
            consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, APPLICATION_MERGE_PATCH_JSON},
            produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> patchPnf(@RequestBody final Pnf pnf,
                                      @PathVariable("pnf-id") final String pnfId,
                                      @RequestHeader(value = X_HTTP_METHOD_OVERRIDE, required = false) final String xHttpHeaderOverride,
                                      final HttpServletRequest request) {

        LOGGER.info("Will post Pnf to cache with 'pnf-id': {} and '{}': {} ...", pnfId, X_HTTP_METHOD_OVERRIDE,
                xHttpHeaderOverride);

        if (HttpMethod.PATCH.toString().equalsIgnoreCase(xHttpHeaderOverride)) {
            if (cacheServiceProvider.patchPnf(pnfId, pnf)) {
                return ResponseEntity.accepted().build();
            }
            LOGGER.error("Unable to apply patch to Pnf using 'pnf-id': {} ... ", pnfId);
            return getRequestErrorResponseEntity(request, PNF);
        }
        LOGGER.error("{} not supported ... ", xHttpHeaderOverride);

        return getRequestErrorResponseEntity(request, PNF);
    }

    @GetMapping(produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> getPnfs(@RequestParam(name = "selflink") final String selflink,
                                     final HttpServletRequest request) {
        LOGGER.info("will retrieve Pnfs using selflink: {}", selflink);

        final List<Pnf> pnfList = cacheServiceProvider.getPnfs(selflink);

        if (pnfList.isEmpty()) {
            LOGGER.error("No matching pnfs found using selflink: {}", selflink);
            return getRequestErrorResponseEntity(request, PNF);
        }

        LOGGER.info("found {} Pnfs in cache", pnfList.size());
        final Pnfs pnfs = new Pnfs();
        pnfs.getPnf().addAll(pnfList);
        return ResponseEntity.ok(pnfs);
    }

    @DeleteMapping(value = "/pnf/{pnf-id}", produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> deletePnf(@PathVariable("pnf-id") final String pnfId,
                                       @RequestParam(name = "resource-version") final String resourceVersion, final HttpServletRequest request) {
        LOGGER.info("Will delete Pnf for 'pnf-id': {} and 'resource-version': {}", pnfId, resourceVersion);

        if (cacheServiceProvider.deletePnf(pnfId, resourceVersion)) {
            LOGGER.info("Successfully delete Pnf from cache for 'pnf-id': {} and 'resource-version': {}", pnfId,
                    resourceVersion);
            return ResponseEntity.noContent().build();
        }

        LOGGER.error("Unable to delete Pnf for 'pnf-id': {} and 'resource-version': {} ...", pnfId,
                resourceVersion);
        return getRequestErrorResponseEntity(request, PNF);

    }

}