Service operation UI merge
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ResourceInterfaceOperationServlet.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.servlets;
18
19 import com.jcabi.aspects.Loggable;
20 import fj.data.Either;
21 import io.swagger.annotations.Api;
22 import io.swagger.annotations.ApiOperation;
23 import io.swagger.annotations.ApiParam;
24 import io.swagger.annotations.ApiResponse;
25 import io.swagger.annotations.ApiResponses;
26 import java.util.Optional;
27 import java.util.UUID;
28 import javax.inject.Singleton;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43 import org.openecomp.sdc.be.components.impl.InterfaceOperationBusinessLogic;
44 import org.openecomp.sdc.be.config.BeEcompErrorManager;
45 import org.openecomp.sdc.be.dao.api.ActionStatus;
46 import org.openecomp.sdc.be.datamodel.utils.InterfaceUIDataConverter;
47 import org.openecomp.sdc.be.datatypes.elements.InterfaceOperationDataDefinition;
48 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
49 import org.openecomp.sdc.be.model.Operation;
50 import org.openecomp.sdc.be.model.User;
51 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
52 import org.openecomp.sdc.be.ui.model.UiResourceDataTransfer;
53 import org.openecomp.sdc.common.api.Constants;
54 import org.openecomp.sdc.exception.ResponseFormat;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
59 @Path("/v1/catalog/resources/{resourceId}/interfaceOperations")
60 @Consumes(MediaType.APPLICATION_JSON)
61 @Produces(MediaType.APPLICATION_JSON)
62 @Api(value = "Interface Operation", description = "Interface Operation Servlet")
63 @Singleton
64 public class ResourceInterfaceOperationServlet extends AbstractValidationsServlet {
65
66   private static final Logger log = LoggerFactory.getLogger(ResourceInterfaceOperationServlet.class);
67
68   @POST
69   @Consumes(MediaType.APPLICATION_JSON)
70   @Produces(MediaType.APPLICATION_JSON)
71   @Path("/")
72   @ApiOperation(value = "Create Interface Operation", httpMethod = "POST", notes = "Create Interface Operation", response = InterfaceOperationDataDefinition.class)
73   @ApiResponses(value = {@ApiResponse(code = 201, message = "Create Interface Operation"),
74       @ApiResponse(code = 403, message = "Restricted operation"),
75       @ApiResponse(code = 400, message = "Invalid content / Missing content"),
76       @ApiResponse(code = 409, message = "Interface Operation already exist")})
77   public Response createInterfaceOperation(
78       @ApiParam(value = "Interface Operation to create", required = true) String data,
79       @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
80       @Context final HttpServletRequest request,
81       @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
82     return createOrUpdate(data, resourceId, request, userId, false);
83   }
84
85   @PUT
86   @Consumes(MediaType.APPLICATION_JSON)
87   @Produces(MediaType.APPLICATION_JSON)
88   @Path("/")
89   @ApiOperation(value = "Update Interface Operation", httpMethod = "PUT", notes = "Update Interface Operation", response = InterfaceOperationDataDefinition.class)
90   @ApiResponses(value = {@ApiResponse(code = 201, message = "Update Interface Operation"),
91       @ApiResponse(code = 403, message = "Restricted operation"),
92       @ApiResponse(code = 400, message = "Invalid content / Missing content")})
93   public Response updateInterfaceOperation(
94       @ApiParam(value = "Interface Operation to update", required = true) String data,
95       @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
96       @Context final HttpServletRequest request,
97       @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
98     return createOrUpdate(data, resourceId, request, userId, true);
99   }
100
101   @DELETE
102   @Consumes(MediaType.APPLICATION_JSON)
103   @Produces(MediaType.APPLICATION_JSON)
104   @Path("/{interfaceOperationId}")
105   @ApiOperation(value = "Delete Interface Operation", httpMethod = "DELETE", notes = "Delete Interface Operation", response = InterfaceOperationDataDefinition.class)
106   @ApiResponses(value = {@ApiResponse(code = 201, message = "Delete Interface Operation"),
107       @ApiResponse(code = 403, message = "Restricted operation"),
108       @ApiResponse(code = 400, message = "Invalid content / Missing content")})
109   public Response deleteInterfaceOperation(
110       @ApiParam(value = "Interface Operation Id") @PathParam("interfaceOperationId") String interfaceOperationId,
111       @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
112       @Context final HttpServletRequest request,
113       @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
114     return delete(interfaceOperationId, resourceId, request, userId);
115   }
116
117   @GET
118   @Consumes(MediaType.APPLICATION_JSON)
119   @Produces(MediaType.APPLICATION_JSON)
120   @Path("/{interfaceOperationId}")
121   @ApiOperation(value = "Get Interface Operation", httpMethod = "GET", notes = "GET Interface Operation", response = InterfaceOperationDataDefinition.class)
122   @ApiResponses(value = {@ApiResponse(code = 201, message = "Get Interface Operation"),
123       @ApiResponse(code = 403, message = "Restricted operation"),
124       @ApiResponse(code = 400, message = "Invalid content / Missing content")})
125   public Response getInterfaceOperation(
126       @ApiParam(value = "Interface Operation Id") @PathParam("interfaceOperationId") String interfaceOperationId,
127       @ApiParam(value = "Resource Id") @PathParam("resourceId") String resourceId,
128       @Context final HttpServletRequest request,
129       @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
130
131     return get(interfaceOperationId, resourceId, request, userId);
132   }
133
134   private Response get (String interfaceOperationId, String resourceId, HttpServletRequest request, String userId){
135     ServletContext context = request.getSession().getServletContext();
136     String url = request.getMethod() + " " + request.getRequestURI();
137
138     User modifier = new User();
139     modifier.setUserId(userId);
140     log.debug("Start get request of {} with modifier id {}", url, userId);
141
142     try {
143       String resourceIdLower = resourceId.toLowerCase();
144       InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context);
145
146       Either<Operation, ResponseFormat> actionResponse = businessLogic.getInterfaceOperation(resourceIdLower, interfaceOperationId, modifier, true);
147       if (actionResponse.isRight()) {
148         log.error("failed to get interface operation");
149         return buildErrorResponse(actionResponse.right().value());
150       }
151
152       InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value());
153       Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition);
154       return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
155     }
156     catch (Exception e) {
157       BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Resource interface operations");
158       log.error("get resource interface operations failed with exception", e);
159       return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
160     }
161   }
162
163   private Response delete (String interfaceOperationId, String resourceId, HttpServletRequest
164       request, String userId){
165
166     ServletContext context = request.getSession().getServletContext();
167     String url = request.getMethod() + " " + request.getRequestURI();
168
169     User modifier = new User();
170     modifier.setUserId(userId);
171     log.debug("Start delete request of {} with modifier id {}", url, userId);
172
173     try {
174       String resourceIdLower = resourceId.toLowerCase();
175       InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context);
176
177       Either<Operation, ResponseFormat> actionResponse = businessLogic.deleteInterfaceOperation(resourceIdLower, interfaceOperationId, modifier, true);
178       if (actionResponse.isRight()) {
179         log.error("failed to delete interface operation");
180         return buildErrorResponse(actionResponse.right().value());
181       }
182
183       InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value());
184       Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition);
185       return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
186     }
187     catch (Exception e) {
188       BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Interface Operation");
189       log.error("Delete interface operation with an error", e);
190       return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
191     }
192   }
193
194   private Response createOrUpdate (String data, String resourceId, HttpServletRequest request, String userId, boolean isUpdate) {
195     ServletContext context = request.getSession().getServletContext();
196     String url = request.getMethod() + " " + request.getRequestURI();
197
198     User modifier = new User();
199     modifier.setUserId(userId);
200     log.debug("Start create or update request of {} with modifier id {}", url, userId);
201
202     try {
203       String resourceIdLower = resourceId.toLowerCase();
204       InterfaceOperationBusinessLogic businessLogic = getInterfaceOperationBL(context);
205
206       Operation operation = getMappedOperationData(data, isUpdate, modifier);
207       Either<Operation, ResponseFormat> actionResponse ;
208       if (isUpdate) {
209         actionResponse = businessLogic.updateInterfaceOperation(resourceIdLower, operation, modifier, true);
210       } else {
211         actionResponse = businessLogic.createInterfaceOperation(resourceIdLower, operation, modifier, true);
212       }
213
214       if (actionResponse.isRight()) {
215         log.error("failed to update or create interface operation");
216         return buildErrorResponse(actionResponse.right().value());
217       }
218
219       InterfaceOperationDataDefinition interfaceOperationDataDefinition = InterfaceUIDataConverter.convertOperationDataToInterfaceData(actionResponse.left().value());
220       Object result = RepresentationUtils.toFilteredRepresentation(interfaceOperationDataDefinition);
221       return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
222     }
223     catch (Exception e) {
224       BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Interface Operation Creation or update");
225       log.error("create or update interface Operation with an error", e);
226       return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
227     }
228   }
229
230   private Operation getMappedOperationData(String inputJson, boolean isUpdate, User user){
231     Either<UiResourceDataTransfer, ResponseFormat> uiResourceEither = getComponentsUtils().convertJsonToObjectUsingObjectMapper(inputJson, user, UiResourceDataTransfer.class, AuditingActionEnum.CREATE_RESOURCE, ComponentTypeEnum.RESOURCE);
232     Optional<InterfaceOperationDataDefinition> opDef = uiResourceEither.left().value().getInterfaceOperations().values().stream().findFirst();
233     InterfaceOperationDataDefinition interfaceOperationDataDefinition = new InterfaceOperationDataDefinition();
234     if(opDef.isPresent()) {
235       interfaceOperationDataDefinition = opDef.get();
236       if(!isUpdate)
237         interfaceOperationDataDefinition.setUniqueId(UUID.randomUUID().toString());
238     }
239     return InterfaceUIDataConverter.convertInterfaceDataToOperationData(interfaceOperationDataDefinition);
240   }
241
242 }
243