org.opendaylight.vtn.javaapi.validation.physical.DomainLogicalPortResourceValidator.java Source code

Java tutorial

Introduction

Here is the source code for org.opendaylight.vtn.javaapi.validation.physical.DomainLogicalPortResourceValidator.java

Source

/*
 * Copyright (c) 2012-2014 NEC Corporation
 * All rights reserved.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.vtn.javaapi.validation.physical;

import com.google.gson.JsonObject;
import org.opendaylight.vtn.core.util.Logger;
import org.opendaylight.vtn.javaapi.constants.VtnServiceConsts;
import org.opendaylight.vtn.javaapi.constants.VtnServiceJsonConsts;
import org.opendaylight.vtn.javaapi.exception.VtnServiceException;
import org.opendaylight.vtn.javaapi.ipc.enums.UncJavaAPIErrorCode;
import org.opendaylight.vtn.javaapi.resources.AbstractResource;
import org.opendaylight.vtn.javaapi.resources.physical.DomainLogicalPortsResource;
import org.opendaylight.vtn.javaapi.validation.CommonValidator;
import org.opendaylight.vtn.javaapi.validation.VtnServiceValidator;

/**
 * The Class DomainLogicalPortResourceValidator validates get method of Logical
 * Port API.
 */
public class DomainLogicalPortResourceValidator extends VtnServiceValidator {

    private static final Logger LOG = Logger.getLogger(DomainLogicalPortResourceValidator.class.getName());

    private final AbstractResource resource;
    private final CommonValidator validator = new CommonValidator();

    /**
     * Instantiates a new DomainLogicalPort resource validator.
     * 
     * @param resource
     *            the instance of AbstractResource
     */
    public DomainLogicalPortResourceValidator(final AbstractResource resource) {
        this.resource = resource;
    }

    /**
     * Validate uri parameters for DomainLogicalPort API
     * 
     * @return true, if successful
     */
    @Override
    public final boolean validateUri() {
        LOG.trace("Start DomainLogicalPortResourceValidator#validateUri()");
        boolean isValid = false;
        setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.CONTROLLERID);
        if (resource instanceof DomainLogicalPortsResource) {
            if (((DomainLogicalPortsResource) resource).getcontrollerId() != null
                    && !((DomainLogicalPortsResource) resource).getcontrollerId().isEmpty()) {
                isValid = validator.isValidMaxLengthAlphaNum(
                        ((DomainLogicalPortsResource) resource).getcontrollerId(), VtnServiceJsonConsts.LEN_31);
            }
            if (isValid) {
                setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.DOMAINID);
                if (((DomainLogicalPortsResource) resource).getdomainId() != null
                        && !((DomainLogicalPortsResource) resource).getdomainId().isEmpty()) {
                    isValid = validator.isValidDomainId(((DomainLogicalPortsResource) resource).getdomainId(),
                            VtnServiceJsonConsts.LEN_31);
                } else {
                    isValid = false;
                }
            }
            setListOpFlag(true);
        }
        LOG.trace("Complete DomainLogicalPortResourceValidator#validateUri()");
        return isValid;
    }

    @Override
    public final void validate(final String method, final JsonObject requestBody) throws VtnServiceException {
        LOG.trace("Start DomainLogicalPortResourceValidator#validate()");
        LOG.info("Validating request for " + method + " of DomainLogicalPortResourceValidator");
        boolean isValid = false;
        try {
            isValid = validateUri();
            if (isValid && requestBody != null && VtnServiceConsts.GET.equals(method)) {
                isValid = validateGet(requestBody, isListOpFlag());
                updateOpParameterForList(requestBody);
            } else if (isValid) {
                setInvalidParameter(VtnServiceConsts.INCORRECT_METHOD_INVOCATION);
                isValid = false;
            }
        } catch (final NumberFormatException e) {
            LOG.error(e, "Inside catch:NumberFormatException");
            isValid = false;
        }
        // Throws exception if validation fails
        if (!isValid) {
            LOG.error("Validation failed");
            throw new VtnServiceException(Thread.currentThread().getStackTrace()[1].getMethodName(),
                    UncJavaAPIErrorCode.VALIDATION_ERROR.getErrorCode(),
                    UncJavaAPIErrorCode.VALIDATION_ERROR.getErrorMessage());
        }
        LOG.info("Validation successful");
        LOG.trace("Complete DomainLogicalPortResourceValidator#validate()");
    }

    /**
     * Validate get request json for Logical Port API
     * 
     * @param requestBody
     *            the request Json object
     * @return true, if successful
     */
    private boolean validateGet(final JsonObject requestBody, final boolean opFlag) {
        LOG.trace("Start DomainLogicalPortResourceValidator#validateGet()");
        boolean isValid = true;
        // validation for key: targetdb
        setInvalidParameter(VtnServiceJsonConsts.TARGETDB);
        if (requestBody.has(VtnServiceJsonConsts.TARGETDB)
                && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB).getAsString() != null
                && !requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB).getAsString().isEmpty()) {
            isValid = requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.TARGETDB).getAsString()
                    .equalsIgnoreCase(VtnServiceJsonConsts.STATE);
        } else {
            requestBody.remove(VtnServiceJsonConsts.TARGETDB);
            requestBody.addProperty(VtnServiceJsonConsts.TARGETDB, VtnServiceJsonConsts.STATE);
        }
        if (!opFlag) {
            if (requestBody.has(VtnServiceJsonConsts.OP)) {
                requestBody.remove(VtnServiceJsonConsts.OP);
            } else {
                LOG.debug("No need to remove -op ");
            }
            if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
                requestBody.remove(VtnServiceJsonConsts.INDEX);
            } else {
                LOG.debug("No need to remove - index");
            }
            if (requestBody.has(VtnServiceJsonConsts.MAX)) {
                requestBody.remove(VtnServiceJsonConsts.MAX);
            } else {
                LOG.debug("No need to remove-max count");
            }
        } else {
            // validation for key: op
            if (isValid) {
                setInvalidParameter(VtnServiceJsonConsts.OP);
                isValid = validator.isValidOperation(requestBody);
            }
            // validation for key: index
            if (isValid) {
                setInvalidParameter(VtnServiceJsonConsts.INDEX);
                if (requestBody.has(VtnServiceJsonConsts.INDEX)
                        && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString() != null
                        && !requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString().isEmpty()) {
                    isValid = validator.isValidMaxLength(
                            requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString(),
                            VtnServiceJsonConsts.LEN_319);
                }
            }
            // validation for key: max_repitition
            if (isValid) {
                setInvalidParameter(VtnServiceJsonConsts.MAX);
                isValid = validator.isValidMaxRepetition(requestBody);
                /*
                 * if (requestBody.has(VtnServiceJsonConsts.MAX) &&
                 * requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.MAX)
                 * .getAsString() != null) { isValid =
                 * validator.isValidMaxLengthNumber(requestBody
                 * .getAsJsonPrimitive(VtnServiceJsonConsts.MAX)
                 * .getAsString()); }
                 */
            }
        }
        if (isValid) {
            setInvalidParameter(VtnServiceJsonConsts.LOGICAL_PORT_ID);
            if (requestBody.has(VtnServiceJsonConsts.LOGICAL_PORT_ID)
                    && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.LOGICAL_PORT_ID).getAsString() != null
                    && !requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.LOGICAL_PORT_ID).getAsString()
                            .isEmpty()) {
                isValid = validator.isValidMaxLength(
                        requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.LOGICAL_PORT_ID).getAsString(),
                        VtnServiceJsonConsts.LEN_319);
            }
        }
        LOG.trace("Complete DomainLogicalPortResourceValidator#isValidGet");
        return isValid;
    }
}