aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java
blob: 6010f8e1b9da59a200e9348a01f29c69c40d8953 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy API
 * ================================================================================
 * Copyright (C) 2022 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.policy.api.main.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.BasicAuthDefinition;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.Info;
import io.swagger.annotations.ResponseHeader;
import io.swagger.annotations.SecurityDefinition;
import io.swagger.annotations.SwaggerDefinition;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
import org.onap.policy.api.main.service.ToscaServiceTemplateService;
import org.onap.policy.common.endpoints.event.comm.Topic;
import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.springframework.http.ResponseEntity;
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.RestController;

/**
 * Class to provide REST API services for Tosca Node templates.
 */
@RestController
@RequestMapping(path = "/policy/api/v1", produces = { "application/json", "application/yaml" })
@Api(value = "Tosca Node template Design API")
@SwaggerDefinition(
    info = @Info(
        description = "Tosca Node template Design API is publicly exposed for clients to Create/Read/Update/Delete"
            + " node templates which can be recognized"
            + " and executable by incorporated policy engines. It is an"
            + " independent component running rest service that takes all node templates design API calls"
            + " from clients and then assign them to different API working functions.",
        version = "1.0.0", title = "Tosca Node template Design",
        extensions = {@Extension(properties = {@ExtensionProperty(name = "planned-retirement-date", value = "tbd"),
            @ExtensionProperty(name = "component", value = "Policy Framework")})}),
    schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
    securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
@RequiredArgsConstructor
public class NodeTemplateController extends CommonRestController {

    private final ToscaServiceTemplateService toscaServiceTemplateService;

    /**
     * Creates one or more new tosca node templates in one call.
     *
     * @param body the body of the node templates in TOSCA definition
     *
     * @return the Response object containing the results of the API operation
     */
    @PostMapping("/nodetemplates")
    @ApiOperation(value = "Create one or more new node templates",
        notes = "Client should provide TOSCA body of the new node templates",
        authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
        response = ToscaServiceTemplate.class,
        responseHeaders = {
            @ResponseHeader(name = VERSION_MINOR_NAME,
                description = VERSION_MINOR_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_PATCH_NAME,
                description = VERSION_PATCH_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = REQUEST_ID_NAME,
                description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
        extensions = {
            @Extension(name = EXTENSION_NAME, properties = {
                @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
                @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
    @ApiResponses(value = {
        @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
    public ResponseEntity<ToscaServiceTemplate> createToscaNodeTemplates(
        @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
        @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
            ToscaServiceTemplate body) {

        if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
            NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
                toJson(body));
        }
        try {
            ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.createToscaNodeTemplates(body);
            return makeOkResponse(requestId, nodeTemplates);
        } catch (PfModelException | PfModelRuntimeException pfme) {
            final var msg = "POST /nodetemplates";
            throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
        }
    }


    /**
     * Updates one or more node templates in one call.
     *
     * @param body the body of the node templates in TOSCA definition
     *
     * @return the Response object containing the results of the API operation
     */
    @PutMapping("/nodetemplates")
    @ApiOperation(value = "Updates one or more new node templates",
        notes = "Client should provide TOSCA body of the updated node templates",
        authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
        response = ToscaServiceTemplate.class,
        responseHeaders = {
            @ResponseHeader(name = VERSION_MINOR_NAME,
                description = VERSION_MINOR_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_PATCH_NAME,
                description = VERSION_PATCH_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = REQUEST_ID_NAME,
                description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
        extensions = {
            @Extension(name = EXTENSION_NAME, properties = {
                @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
                @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
    @ApiResponses(value = {
        @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
    public ResponseEntity<ToscaServiceTemplate> updateToscaNodeTemplates(
        @RequestHeader(name = REQUEST_ID_NAME, required = false) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
        @RequestBody @ApiParam(value = "Entity body of tosca node templates", required = true)
            ToscaServiceTemplate body) {

        if (NetLoggerUtil.getNetworkLogger().isInfoEnabled()) {
            NetLoggerUtil.log(NetLoggerUtil.EventType.IN, Topic.CommInfrastructure.REST, "/nodetemplates",
                toJson(body));
        }
        try {
            ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.updateToscaNodeTemplates(body);
            return makeOkResponse(requestId, nodeTemplates);
        } catch (PfModelException | PfModelRuntimeException pfme) {
            final var msg = "PUT /nodetemplates";
            throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
        }
    }


    /**
     * Deletes a node template with specific name and version.
     *
     * @param name  the name of node template
     * @param version the version of node template
     * @return the Response object containing the results of the API operation
     */
    @DeleteMapping("/nodetemplates/{name}/versions/{version}")
    @ApiOperation(value = "Updates one or more new node templates",
        notes = "Client should provide TOSCA body of the updated node templates",
        authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplate", },
        response = ToscaServiceTemplate.class,
        responseHeaders = {
            @ResponseHeader(name = VERSION_MINOR_NAME,
                description = VERSION_MINOR_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_PATCH_NAME,
                description = VERSION_PATCH_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = REQUEST_ID_NAME,
                description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
        extensions = {
            @Extension(name = EXTENSION_NAME, properties = {
                @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
                @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")})})
    @ApiResponses(value = {
        @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = INVALID_BODY_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_ACCEPTABLE, message = INVALID_PAYLOAD_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)})
    public ResponseEntity<ToscaServiceTemplate> deleteToscaNodeTemplates(
        @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
        @PathVariable("version") @ApiParam(value = "Version of the node template",
            required = true) String version,
        @RequestHeader(name = REQUEST_ID_NAME, required = false)
        @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
        try {
            ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.deleteToscaNodeTemplate(name, version);
            return makeOkResponse(requestId, nodeTemplates);
        } catch (PfModelException | PfModelRuntimeException pfme) {
            final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version);
            throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
        }
    }


    /**
     * Retrieves the specified version of a node template.
     *
     * @param name the name of the node template
     * @param version the version of the node template
     *
     * @return the Response object containing the results of the API operation
     */
    @GetMapping("/nodetemplates/{name}/versions/{version}")
    @ApiOperation(value = "Retrieve one version of a tosca node template",
        notes = "Returns a particular version of a node template",
        response = ToscaNodeTemplate.class,
        responseContainer = "List",
        responseHeaders = {
            @ResponseHeader(name = VERSION_MINOR_NAME,
                description = VERSION_MINOR_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_PATCH_NAME,
                description = VERSION_PATCH_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = REQUEST_ID_NAME,
                description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
        authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
        extensions = {
            @Extension(name = EXTENSION_NAME, properties = {
                @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
                @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
            })
        }
    )
    @ApiResponses(value = {
        @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
    })
    public ResponseEntity<List<ToscaNodeTemplate>> getSpecificVersionOfNodeTemplate(
        @PathVariable("name") @ApiParam(value = "Name of the node template", required = true) String name,
        @PathVariable("version") @ApiParam(value = "Version of the node template",
            required = true) String version,
        @RequestHeader(name = REQUEST_ID_NAME, required = false)
        @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
        try {
            List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(name, version);
            return makeOkResponse(requestId, nodeTemplates);
        } catch (PfModelException | PfModelRuntimeException pfme) {
            var msg = String.format("GET /nodetemplates/%s/versions/%s", name, version);
            throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
        }
    }


    /**
     * Retrieves all the node templates from the tosca service template.
     *
     * @return the Response object containing the results of the API operation
     */
    @GetMapping("/nodetemplates")
    @ApiOperation(value = "Retrieve all the available tosca node templates",
        notes = "Returns all the node templates from the service template",
        response = ToscaNodeTemplate.class,
        responseContainer = "List",
        responseHeaders = {
            @ResponseHeader(name = VERSION_MINOR_NAME,
                description = VERSION_MINOR_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_PATCH_NAME,
                description = VERSION_PATCH_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
                response = String.class),
            @ResponseHeader(name = REQUEST_ID_NAME,
                description = REQUEST_ID_HDR_DESCRIPTION, response = UUID.class)},
        authorizations = @Authorization(value = AUTHORIZATION_TYPE), tags = {"nodeTemplates", },
        extensions = {
            @Extension(name = EXTENSION_NAME, properties = {
                @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
                @ExtensionProperty(name = LAST_MOD_NAME, value = "Jakarta")
            })
        }
    )
    @ApiResponses(value = {
        @ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = AUTHENTICATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = AUTHORIZATION_ERROR_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = NOT_FOUND_MESSAGE),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = SERVER_ERROR_MESSAGE)
    })
    public ResponseEntity<List<ToscaNodeTemplate>> getAllNodeTemplates(
        @RequestHeader(name = REQUEST_ID_NAME, required = false)
        @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
        try {
            List<ToscaNodeTemplate> nodeTemplates = toscaServiceTemplateService.fetchToscaNodeTemplates(null, null);
            return makeOkResponse(requestId, nodeTemplates);
        } catch (PfModelException | PfModelRuntimeException pfme) {
            var msg = "GET /nodetemplates";
            throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);
        }
    }

}