Support for delete of non normative interface types
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / InterfaceTypeServlet.java
1 /*
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2023 Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.v3.oas.annotations.Parameter;
26 import io.swagger.v3.oas.annotations.enums.ParameterIn;
27 import io.swagger.v3.oas.annotations.servers.Server;
28 import io.swagger.v3.oas.annotations.tags.Tag;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.core.Response;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.sdc.be.config.BeEcompErrorManager;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.impl.ComponentsUtils;
37 import org.openecomp.sdc.be.model.InterfaceDefinition;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
39 import org.openecomp.sdc.be.model.normatives.ElementTypeEnum;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
42 import org.openecomp.sdc.common.api.Constants;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
44 import org.springframework.stereotype.Controller;
45
46 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
47 @Path("/v1/catalog/interface-types")
48 @Tag(name = "SDCE-2 APIs")
49 @Server(url = "/sdc2/rest")
50 @Controller
51 public class InterfaceTypeServlet extends BeGenericServlet {
52
53     private static final Logger log = Logger.getLogger(InterfaceTypeServlet.class);
54     private final InterfaceLifecycleOperation interfaceLifecycleOperation;
55
56     public InterfaceTypeServlet(final ComponentsUtils componentsUtils,
57                                 InterfaceLifecycleOperation interfaceLifecycleOperation) {
58         super(componentsUtils);
59         this.interfaceLifecycleOperation = interfaceLifecycleOperation;
60     }
61
62     @DELETE
63     @Path("{interfaceTypeId}")
64     public Response deleteInterfaceType(@Parameter(in = ParameterIn.PATH, required = true, description = "The interface type id")
65                                         @PathParam("interfaceTypeId") final String interfaceTypeId) {
66         final Either<InterfaceDefinition, StorageOperationStatus> interfaceTypeEither = interfaceLifecycleOperation.getInterface(interfaceTypeId);
67         if (interfaceTypeEither.isRight()) {
68             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INTERFACE_LIFECYCLE_TYPES_NOT_FOUND,
69                 String.format("Failed to find interface type '%s'", interfaceTypeId)));
70         }
71         final InterfaceDefinition interfaceDefinition = interfaceTypeEither.left().value();
72         if (!interfaceDefinition.isUserCreated()) {
73             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.CANNOT_DELETE_SYSTEM_DEPLOYED_RESOURCES,
74                 ElementTypeEnum.INTERFACE_LIFECYCLE_TYPE.getToscaEntryName(),
75                 interfaceTypeId));
76         }
77         if (StringUtils.isEmpty(interfaceDefinition.getModel())) {
78             interfaceDefinition.setModel(Constants.DEFAULT_MODEL_NAME);
79         }
80         try {
81             interfaceLifecycleOperation.deleteInterfaceTypeById(interfaceTypeId);
82             interfaceLifecycleOperation.removeInterfaceTypeFromAdditionalType(interfaceDefinition);
83         } catch (Exception e) {
84             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Type");
85             log.debug("delete interface type failed with exception ", e);
86             throw e;
87         }
88         return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), null);
89     }
90 }