aboutsummaryrefslogtreecommitdiffstats
path: root/plans/so/integration-etsi-testing/so-simulators/sdnc-simulator/src/main/java/org/onap/so/sdncsimulator/controller/OperationsController.java
blob: 58f903828a3ee2e0d5da5046e7ab24b920e51a4b (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 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.sdncsimulator.controller;

import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration.DELETESERVICEINSTANCE;
import static org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration.DELETEVNFINSTANCE;
import static org.onap.sdnc.northbound.client.model.GenericResourceApiSvcActionEnumeration.DELETE;
import static org.onap.so.sdncsimulator.utils.Constants.OPERATIONS_URL;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestinformationRequestInformation;
import org.onap.sdnc.northbound.client.model.GenericResourceApiSdncrequestheaderSdncRequestHeader;
import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
import org.onap.sdnc.northbound.client.model.GenericResourceApiSvcActionEnumeration;
import org.onap.sdnc.northbound.client.model.GenericResourceApiVnfOperationInformation;
import org.onap.sdnc.northbound.client.model.GenericResourceApiVfModuleOperationInformation;
import org.onap.so.sdncsimulator.models.InputRequest;
import org.onap.so.sdncsimulator.models.Output;
import org.onap.so.sdncsimulator.models.OutputRequest;
import org.onap.so.sdncsimulator.providers.ServiceOperationsCacheServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
@Controller
@RequestMapping(path = OPERATIONS_URL)
public class OperationsController {
    private static final String HTTP_STATUS_OK = HttpStatus.OK.value() + "";

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

    private final ServiceOperationsCacheServiceProvider cacheServiceProvider;

    @Autowired
    public OperationsController(final ServiceOperationsCacheServiceProvider cacheServiceProvider) {
        this.cacheServiceProvider = cacheServiceProvider;
    }

    @PostMapping(value = "/GENERIC-RESOURCE-API:service-topology-operation/",
            consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
            produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> postServiceOperationInformation(
            @RequestBody final InputRequest<GenericResourceApiServiceOperationInformation> inputRequest,
            final HttpServletRequest request) {
        LOGGER.info("Request Received: {}  ...", inputRequest);

        final GenericResourceApiServiceOperationInformation apiServiceOperationInformation = inputRequest.getInput();

        if (apiServiceOperationInformation == null) {
            LOGGER.error("Invalid input request: {}", inputRequest);
            return ResponseEntity.badRequest().build();
        }

        final Output output = getOutput(apiServiceOperationInformation);
        final OutputRequest outputRequest = new OutputRequest(output);

        if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
            LOGGER.info("Sucessfully executed service request sending response: {}", outputRequest);
            return ResponseEntity.ok(outputRequest);
        }
        LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
        return ResponseEntity.badRequest().body(outputRequest);

    }

    @PostMapping(value = "/GENERIC-RESOURCE-API:vnf-topology-operation/",
            consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
            produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> postVnfOperationInformation(
            @RequestBody final InputRequest<GenericResourceApiVnfOperationInformation> inputRequest,
            final HttpServletRequest request) {
        LOGGER.info("Request Received: {}  ...", inputRequest);

        final GenericResourceApiVnfOperationInformation apiVnfOperationInformation = inputRequest.getInput();
        if (apiVnfOperationInformation == null) {
            LOGGER.error("Invalid input request: {}", inputRequest);
            return ResponseEntity.badRequest().build();
        }

        final Output output = getOutput(apiVnfOperationInformation);
        final OutputRequest outputRequest = new OutputRequest(output);

        if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
            LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
            return ResponseEntity.ok(outputRequest);
        }

        LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
        return ResponseEntity.badRequest().body(outputRequest);

    }

    private Output getOutput(final GenericResourceApiServiceOperationInformation serviceOperationInformation) {
        final GenericResourceApiRequestinformationRequestInformation requestInformation =
                serviceOperationInformation.getRequestInformation();
        final GenericResourceApiSdncrequestheaderSdncRequestHeader sdncRequestHeader =
                serviceOperationInformation.getSdncRequestHeader();
        if (requestInformation != null && sdncRequestHeader != null) {
            final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
            final GenericResourceApiSvcActionEnumeration svcAction = sdncRequestHeader.getSvcAction();
            if (DELETESERVICEINSTANCE.equals(requestAction) && DELETE.equals(svcAction)) {
                LOGGER.info("RequestAction: {} and SvcAction: {} will delete service instance from cache ...",
                        requestAction, svcAction);
                return cacheServiceProvider.deleteServiceOperationInformation(serviceOperationInformation);
            }
        }
        return cacheServiceProvider.putServiceOperationInformation(serviceOperationInformation);
    }

    private Output getOutput(final GenericResourceApiVnfOperationInformation apiVnfOperationInformation) {
        final GenericResourceApiRequestinformationRequestInformation requestInformation =
                apiVnfOperationInformation.getRequestInformation();
        if (requestInformation != null) {
            final GenericResourceApiRequestActionEnumeration requestAction = requestInformation.getRequestAction();
            if (DELETEVNFINSTANCE.equals(requestAction)) {
                LOGGER.info("RequestAction: {} will delete vnf instance from cache ...", requestAction);
                return cacheServiceProvider.deleteVnfOperationInformation(apiVnfOperationInformation);
            }
        }
        return cacheServiceProvider.putVnfOperationInformation(apiVnfOperationInformation);
    }

    @PostMapping(value = "/GENERIC-RESOURCE-API:vf-module-topology-operation/",
            consumes = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML},
            produces = {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public ResponseEntity<?> postVfModuleOperationInformation(
            @RequestBody final InputRequest<GenericResourceApiVfModuleOperationInformation> inputRequest,
            final HttpServletRequest request) {
        LOGGER.info("Request Received for VfModule : {}  ...", inputRequest);

        final GenericResourceApiVfModuleOperationInformation apiVfModuleOperationInformation = inputRequest.getInput();
        if (apiVfModuleOperationInformation == null) {
            LOGGER.error("Invalid input request: {}", inputRequest);
            return ResponseEntity.badRequest().build();
        }

        final Output output = getOutput(apiVfModuleOperationInformation);
        final OutputRequest outputRequest = new OutputRequest(output);

        if (output.getResponseCode().equals(HTTP_STATUS_OK)) {
            LOGGER.info("Sucessfully executed request vnf sending response: {}", outputRequest);
            return ResponseEntity.ok(outputRequest);
        }

        LOGGER.error("Unable to execute input request: {}, will send OutputRequest: {}", inputRequest, outputRequest);
        return ResponseEntity.badRequest().body(outputRequest);

    }

    private Output getOutput(final GenericResourceApiVfModuleOperationInformation apiVfModuleOperationInformation) {

        return cacheServiceProvider.putVfModuleOperationInformation(apiVfModuleOperationInformation);
    }

}