aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/PropertyServlet.java
blob: 557091e3a17d18a52bd53bd35061de9d3b3cf6e2 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.servlets;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.inject.Singleton;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.impl.WebAppContextWrapper;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintSerialiser;
import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
import org.openecomp.sdc.be.resources.data.EntryData;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.exception.ResponseFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.jcabi.aspects.Loggable;

import fj.data.Either;
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;
@Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
@Path("/v1/catalog")
@Api(value = "Resource Property Servlet", description = "Resource Property Servlet")
@Singleton
public class PropertyServlet extends BeGenericServlet {

    private static final Logger log = LoggerFactory.getLogger(PropertyServlet.class);

    @POST
    @Path("resources/{resourceId}/properties")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create Resource Property", httpMethod = "POST", notes = "Returns created resource property", response = Response.class)
    @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource property created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
            @ApiResponse(code = 409, message = "Resource property already exist") })
    public Response createProperty(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId, @ApiParam(value = "Resource property to be created", required = true) String data,
            @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {

        ServletContext context = request.getSession().getServletContext();

        String url = request.getMethod() + " " + request.getRequestURI();
        log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data);

        try {
            // convert json to PropertyDefinition
            Either<Map<String, PropertyDefinition>, ActionStatus> either = getPropertyModel(resourceId, data);
            if (either.isRight()) {
                ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(either.right().value());
                return buildErrorResponse(responseFormat);
            }
            Map<String, PropertyDefinition> properties = either.left().value();
            if (properties == null || properties.size() != 1) {
                log.info("Property conetnt is invalid - {}", data);
                ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
                return buildErrorResponse(responseFormat);
            }
            Entry<String, PropertyDefinition> entry = properties.entrySet().iterator().next();
            String propertyName = entry.getKey();
            PropertyDefinition newPropertyDefinition = entry.getValue();

            // create the new property
            PropertyBusinessLogic businessLogic = getPropertyBL(context);
            Either<EntryData<String, PropertyDefinition>, ResponseFormat> status = businessLogic.createProperty(resourceId, propertyName, newPropertyDefinition, userId);
            if (status.isRight()) {
                log.info("Failed to create Property. Reason - ", status.right().value());
                return buildErrorResponse(status.right().value());
            }
            EntryData<String, PropertyDefinition> property = status.left().value();
            String name = property.getKey();
            PropertyDefinition propertyDefinition = property.getValue();

            log.debug("Property {} created successfully with id {}", name, propertyDefinition.getUniqueId());
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED);
            return buildOkResponse(responseFormat, propertyToJson(property));

        } catch (Exception e) {
            BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Create Property");
            log.debug("create property failed with exception", e);
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
            return buildErrorResponse(responseFormat);

        }
    }

    @GET
    @Path("resources/{resourceId}/properties/{propertyId}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create Resource Property", httpMethod = "GET", notes = "Returns property of resource", response = Response.class)
    @ApiResponses(value = { @ApiResponse(code = 200, message = "property"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
            @ApiResponse(code = 404, message = "Resource property not found") })
    public Response getProperty(@ApiParam(value = "resource id of property", required = true) @PathParam("resourceId") final String resourceId, @ApiParam(value = "proerty id to get", required = true) @PathParam("propertyId") final String propertyId,
            @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {

        ServletContext context = request.getSession().getServletContext();

        String url = request.getMethod() + " " + request.getRequestURI();
        log.debug("Start handle request of {}, modifier id is {}", url, userId);

        try {

            //
            PropertyBusinessLogic businessLogic = getPropertyBL(context);
            Either<Entry<String, PropertyDefinition>, ResponseFormat> status = businessLogic.getProperty(resourceId, propertyId, userId);

            if (status.isRight()) {
                log.info("Failed to get Property. Reason - ", status.right().value());
                return buildErrorResponse(status.right().value());
            }
            Entry<String, PropertyDefinition> property = status.left().value();
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
            return buildOkResponse(responseFormat, propertyToJson(property));
        } catch (Exception e) {
            BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Property");
            log.debug("get property failed with exception", e);
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
            return buildErrorResponse(responseFormat);

        }
    }

    @DELETE
    @Path("resources/{resourceId}/properties/{propertyId}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create Resource Property", httpMethod = "DELETE", notes = "Returns deleted property", response = Response.class)
    @ApiResponses(value = { @ApiResponse(code = 204, message = "deleted property"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
            @ApiResponse(code = 404, message = "Resource property not found") })
    public Response deleteProperty(@ApiParam(value = "resource id of property", required = true) @PathParam("resourceId") final String resourceId,
            @ApiParam(value = "Property id to delete", required = true) @PathParam("propertyId") final String propertyId, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {

        ServletContext context = request.getSession().getServletContext();

        String url = request.getMethod() + " " + request.getRequestURI();
        log.debug("Start handle request of {} modifier id is {}", url, userId);

        try {

            // delete the property
            PropertyBusinessLogic businessLogic = getPropertyBL(context);
            Either<Entry<String, PropertyDefinition>, ResponseFormat> status = businessLogic.deleteProperty(resourceId, propertyId, userId);
            if (status.isRight()) {
                log.debug("Failed to delete Property. Reason - ", status.right().value());
                return buildErrorResponse(status.right().value());
            }
            Entry<String, PropertyDefinition> property = status.left().value();
            String name = property.getKey();
            PropertyDefinition propertyDefinition = property.getValue();

            log.debug("Property {} deleted successfully with id {}", name, propertyDefinition.getUniqueId());
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT);
            return buildOkResponse(responseFormat, propertyToJson(property));

        } catch (Exception e) {
            BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Delete Property");
            log.debug("delete property failed with exception", e);
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
            return buildErrorResponse(responseFormat);

        }
    }

    @PUT
    @Path("resources/{resourceId}/properties/{propertyId}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Update Resource Property", httpMethod = "PUT", notes = "Returns updated property", response = Response.class)
    @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource property updated"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content") })
    public Response updateProperty(@ApiParam(value = "resource id to update with new property", required = true) @PathParam("resourceId") final String resourceId,
            @ApiParam(value = "proerty id to update", required = true) @PathParam("propertyId") final String propertyId, @ApiParam(value = "Resource property to update", required = true) String data, @Context final HttpServletRequest request,
            @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {

        ServletContext context = request.getSession().getServletContext();

        String url = request.getMethod() + " " + request.getRequestURI();
        log.debug("Start handle request of {}", url);

        // get modifier id
        User modifier = new User();
        modifier.setUserId(userId);
        log.debug("modifier id is {}", userId);

        try {
            // convert json to PropertyDefinition
            Either<Map<String, PropertyDefinition>, ActionStatus> either = getPropertyModel(resourceId, data);
            if (either.isRight()) {
                ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(either.right().value());
                return buildErrorResponse(responseFormat);
            }
            Map<String, PropertyDefinition> properties = either.left().value();
            if (properties == null || properties.size() != 1) {
                log.info("Property conetnt is invalid - {}", data);
                ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT);
                return buildErrorResponse(responseFormat);
            }
            Entry<String, PropertyDefinition> entry = properties.entrySet().iterator().next();
            PropertyDefinition newPropertyDefinition = entry.getValue();

            // update property
            PropertyBusinessLogic businessLogic = getPropertyBL(context);
            Either<EntryData<String, PropertyDefinition>, ResponseFormat> status = businessLogic.updateProperty(resourceId, propertyId, newPropertyDefinition, userId);
            if (status.isRight()) {
                log.info("Failed to update Property. Reason - ", status.right().value());
                return buildErrorResponse(status.right().value());
            }
            EntryData<String, PropertyDefinition> property = status.left().value();
            PropertyDefinition propertyDefinition = property.getValue();

            log.debug("Property id {} updated successfully ", propertyDefinition.getUniqueId());
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
            return buildOkResponse(responseFormat, propertyToJson(property));

        } catch (Exception e) {
            BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Property");
            log.debug("update property failed with exception", e);
            ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
            return buildErrorResponse(responseFormat);

        }
    }

    private Either<Map<String, PropertyDefinition>, ActionStatus> getPropertyModel(String resourceId, String data) {
        JSONParser parser = new JSONParser();
        JSONObject root;
        try {
            Map<String, PropertyDefinition> properties = new HashMap<String, PropertyDefinition>();
            root = (JSONObject) parser.parse(data);

            Set entrySet = root.entrySet();
            Iterator iterator = entrySet.iterator();
            while (iterator.hasNext()) {
                Entry next = (Entry) iterator.next();
                String propertyName = (String) next.getKey();
                JSONObject value = (JSONObject) next.getValue();
                String jsonString = value.toJSONString();
                Either<PropertyDefinition, ActionStatus> convertJsonToObject = convertJsonToObject(jsonString, PropertyDefinition.class);
                if (convertJsonToObject.isRight()) {
                    return Either.right(convertJsonToObject.right().value());
                }
                PropertyDefinition propertyDefinition = convertJsonToObject.left().value();
                String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(resourceId, (String) propertyName);
                propertyDefinition.setUniqueId(uniqueId);
                properties.put(propertyName, propertyDefinition);
            }

            return Either.left(properties);
        } catch (ParseException e) {
            log.info("Property conetnt is invalid - {}", data);
            return Either.right(ActionStatus.INVALID_CONTENT);
        }
    }

    private String propertyToJson(Map.Entry<String, PropertyDefinition> property) {
        JSONObject root = new JSONObject();
        String propertyName = property.getKey();
        PropertyDefinition propertyDefinition = property.getValue();
        JSONObject propertyDefinitionO = getPropertyDefinitionJSONObject(propertyDefinition);
        root.put(propertyName, propertyDefinitionO);
        propertyDefinition.getType();
        return root.toString();
    }

    private JSONObject getPropertyDefinitionJSONObject(PropertyDefinition propertyDefinition) {

        Either<String, ActionStatus> either = convertObjectToJson(propertyDefinition);
        if (either.isRight()) {
            return new JSONObject();
        }
        String value = either.left().value();
        try {
            JSONObject root = (JSONObject) new JSONParser().parse(value);
            return root;
        } catch (ParseException e) {
            log.info("failed to convert input to json");
            log.debug("failed to convert to json", e);
            return new JSONObject();
        }

    }

    private <T> Either<T, ActionStatus> convertJsonToObject(String data, Class<T> clazz) {
        T t = null;
        Type constraintType = new TypeToken<PropertyConstraint>() {
        }.getType();
        Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
        try {
            log.trace("convert json to object. json=\n {}", data);
            t = gson.fromJson(data, clazz);
            if (t == null) {
                log.info("object is null after converting from json");
                return Either.right(ActionStatus.INVALID_CONTENT);
            }
        } catch (Exception e) {
            // INVALID JSON
            log.info("failed to convert from json");
            log.debug("failed to convert from json", e);
            return Either.right(ActionStatus.INVALID_CONTENT);
        }
        return Either.left(t);
    }

    private <T> Either<String, ActionStatus> convertObjectToJson(PropertyDefinition propertyDefinition) {
        Type constraintType = new TypeToken<PropertyConstraint>() {
        }.getType();
        Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintSerialiser()).create();
        try {
            log.trace("convert object to json. propertyDefinition= {}", propertyDefinition);
            String json = gson.toJson(propertyDefinition);
            if (json == null) {
                log.info("object is null after converting to json");
                return Either.right(ActionStatus.INVALID_CONTENT);
            }
            return Either.left(json);
        } catch (Exception e) {
            // INVALID JSON
            log.info("failed to convert to json");
            log.debug("failed to convert fto json", e);
            return Either.right(ActionStatus.INVALID_CONTENT);
        }

    }

    private PropertyBusinessLogic getPropertyBL(ServletContext context) {
        WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
        WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
        PropertyBusinessLogic propertytBl = webApplicationContext.getBean(PropertyBusinessLogic.class);
        return propertytBl;
    }

}