/*
 * Copyright (c) 2016 Open Baton (http://openbaton.org)
 *
 * 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.
 */

package org.openbaton.nfvo.api.interceptors;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.kjetland.jackson.jsonSchema.JsonSchemaGenerator;
import com.networknt.schema.ValidationMessage;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openbaton.catalogue.mano.descriptor.NetworkServiceDescriptor;
import org.openbaton.catalogue.mano.descriptor.VNFDependency;
import org.openbaton.catalogue.mano.descriptor.VirtualLinkDescriptor;
import org.openbaton.exceptions.BadRequestException;
import org.openbaton.nfvo.api.configuration.CustomHttpServletRequestWrapper;
import org.openbaton.nfvo.common.utils.schema.SchemaValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Service
public class SchemaValidatorInterceptor extends HandlerInterceptorAdapter {

  private Logger log = LoggerFactory.getLogger(this.getClass());
  @Autowired private Gson gson;

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {

    String requestURL = request.getRequestURL().toString();
    if (request.getRequestURI().equalsIgnoreCase("/error")) {
      return true;
    }
    CustomHttpServletRequestWrapper wrapper = new CustomHttpServletRequestWrapper(request);
    String requestBody = wrapper.getBody();
    String classSchema = null;
    Class<?> parameterClass = null;
    if (handler instanceof org.springframework.web.method.HandlerMethod) {
      org.springframework.web.method.HandlerMethod handlerMethod =
          (org.springframework.web.method.HandlerMethod) handler;

      Annotation[][] annotations = handlerMethod.getMethod().getParameterAnnotations();
      for (int i = 0; i < annotations.length; i++) {
        Annotation[] methodParameters = annotations[i];
        for (Annotation a : methodParameters) {
          if (a instanceof RequestBody) {
            parameterClass = handlerMethod.getMethod().getParameterTypes()[i];
            if (!parameterClass.getName().equals("com.google.gson.JsonObject")) {
              classSchema = getJsonSchemaFromClass(parameterClass);
            }
          }
        }
      }
    }

    if (classSchema != null) {
      log.trace("Request Body is : " + requestBody);
      log.trace("Request url is : " + requestURL);
      Set<ValidationMessage> validationMessages;
      try {
        if (parameterClass
            .getCanonicalName()
            .equals(NetworkServiceDescriptor.class.getCanonicalName())) {
          NetworkServiceDescriptor networkServiceDescriptor =
              gson.fromJson(requestBody, NetworkServiceDescriptor.class);
          if (networkServiceDescriptor
                  .getVnfd()
                  .stream()
                  .filter(vnfd -> vnfd.getId() != null)
                  .count()
              > 0) {
            Set<ValidationMessage> errors = new HashSet<>();
            //Validating VLDs
            networkServiceDescriptor
                .getVld()
                .forEach(
                    vld -> {
                      try {
                        errors.addAll(
                            SchemaValidator.validateSchema(
                                getJsonSchemaFromClass(VirtualLinkDescriptor.class),
                                gson.toJson(vld)));
                      } catch (BadRequestException | IOException e) {
                        e.printStackTrace();
                      }
                    });
            //Validating VNFDeps
            networkServiceDescriptor
                .getVnf_dependency()
                .forEach(
                    vnfDependency -> {
                      try {
                        errors.addAll(
                            SchemaValidator.validateSchema(
                                getJsonSchemaFromClass(VNFDependency.class),
                                gson.toJson(vnfDependency)));
                      } catch (BadRequestException | IOException e) {
                        e.printStackTrace();
                      }
                    });
            if (!errors.isEmpty()) {
              handleErrorMessages(request, response, errors);
              return false;
            }
            return true;
          }
        }
        validationMessages = SchemaValidator.validateSchema(classSchema, requestBody);
      } catch (BadRequestException e) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        return false;
      }
      if (validationMessages.size() > 0) {
        handleErrorMessages(request, response, validationMessages);
        return false;
      } else {
        log.trace("Response body is : no error");
        return super.preHandle(wrapper, response, handler);
      }
    } else {
      if (!requestURL.contains("/api/v1")
          && !request.getMethod().equalsIgnoreCase("get")
          && !request.getMethod().equalsIgnoreCase("delete")) {
        log.warn("Not able to generate schema for url ...");
        log.warn("URL: " + requestURL);
      }
    }
    return super.preHandle(wrapper, response, handler);
  }

  private void handleErrorMessages(
      HttpServletRequest request,
      HttpServletResponse response,
      Set<ValidationMessage> validationMessages)
      throws IOException {
    StringBuilder validationResult = new StringBuilder();
    for (ValidationMessage s : validationMessages) {
      String message = s.getMessage();
      validationResult.append(message).append(", ");
    }

    log.trace("Response body is : " + validationResult);
    log.trace(request.getHeader("Accept"));
    if (request.getHeader("Accept").equalsIgnoreCase("application/json")
        || request.getHeader("Accept").equalsIgnoreCase("application/octet-stream")) {
      response.setContentLength(validationResult.length());
      response.setContentType(MediaType.APPLICATION_JSON_VALUE);
      response.sendError(
          HttpServletResponse.SC_BAD_REQUEST, validationResult.toString().replace("$.", " "));
    }
  }

  @SuppressWarnings("unchecked")
  private String getJsonSchemaFromClass(Class javaClass) throws IOException {

    ObjectMapper mapper = new ObjectMapper();
    JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);
    JsonNode jsonSchema = schemaGen.generateJsonSchema(javaClass);
    String jsonSchemaAsString = mapper.writeValueAsString(jsonSchema);
    log.trace(jsonSchemaAsString);
    return jsonSchemaAsString;
  }
}

@@ -786,24 +786,59 @@
 .gson.Gson;%0A
+import com.google.gson.JsonObject;%0A
 import com.k
@@ -936,216 +936,8 @@
 ge;%0A
-import java.io.IOException;%0Aimport java.lang.annotation.Annotation;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;
 %0Aimp
@@ -1676,16 +1676,227 @@
 apter;%0A%0A
+import java.io.IOException;%0Aimport java.lang.annotation.Annotation;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A
 @Service
@@ -3170,16 +3170,25 @@
 lass.get
+Canonical
 Name().e
@@ -3197,36 +3197,92 @@
 als(
-%22com.google.gson.JsonObject%22
+JsonObject.class.getCanonicalName())%0A                && !parameterClass.isPrimitive(
 )) %7B

fix schema validation for primitive