summaryrefslogtreecommitdiffstats
path: root/kubernetes/helm
ModeNameSize
d---------plugins68logstatsplain
d---------starters / onap-app35logstatsplain
f='#n15'>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
package org.openecomp.sdc.be.externalapi.servlet;

import fj.data.Either;
import org.openecomp.sdc.be.components.impl.ExternalRefsBusinessLogic;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.dto.ExternalRefDTO;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.servlets.BeGenericServlet;
import org.openecomp.sdc.common.datastructure.Wrapper;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.stereotype.Controller;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Map;

@Path("/v1/catalog")
@Controller
public class ExternalRefsServlet extends BeGenericServlet {

    private static final Logger log = Logger.getLogger(ExternalRefsServlet.class);
    private final ComponentsUtils componentsUtils;
    private final ExternalRefsBusinessLogic businessLogic;

    public ExternalRefsServlet(ExternalRefsBusinessLogic businessLogic, ComponentsUtils componentsUtils){
        this.businessLogic = businessLogic;
        this.componentsUtils = componentsUtils;
    }

    @GET
    @Path("/{assetType}/{uuid}/version/{version}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getComponentInstanceExternalRef(
            @PathParam("assetType") String assetType,
            @PathParam("uuid") String uuid,
            @PathParam("version") String version,
            @PathParam("componentInstanceName") String componentInstanceName,
            @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {

        log.debug("GET component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);

        Response r = validateRequest(xEcompInstanceId);
        if (r != null){
            return r;
        }

        Either<List<String>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, componentInstanceName, objectType);
        if (refsResult.isLeft()){
            return this.buildOkResponse(refsResult.left().value());
        } else {
            return this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, componentInstanceName, objectType, "");
        }
    }

    @GET
    @Path("/{assetType}/{uuid}/version/{version}/externalReferences/{objectType}")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, List<String>> getAssetExternalRefByObjectType(
            @PathParam("assetType") String assetType,
            @PathParam("uuid") String uuid,
            @PathParam("version") String version,
            @PathParam("objectType") String objectType, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {

        log.debug("GET asset external references {} {} {}", assetType, uuid, objectType);

        Response r = validateRequest(xEcompInstanceId);
        if (r != null){
            throw new WebApplicationException(r);
        }

        Either<Map<String, List<String>>, ActionStatus> refsResult = this.businessLogic.getExternalReferences(uuid, version, objectType);
        if (refsResult.isLeft()){
            return refsResult.left().value();
        } else {
            throw new WebApplicationException(this.buildExtRefErrorResponse(refsResult.right().value(), uuid, version, "", objectType, ""));
        }
    }

    @POST
    @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response addComponentInstanceExternalRef(
            @PathParam("assetType") String assetType,
            @PathParam("uuid") String uuid,
            @PathParam("componentInstanceName") String componentInstanceName,
            @PathParam("objectType") String objectType, ExternalRefDTO ref, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {

        log.debug("POST component instance external interfaces {} {} {} {} {}", assetType, uuid, componentInstanceName, objectType, ref);

        Response r = validateRequest(xEcompInstanceId);
        if (r != null){
            return r;
        }

        Either<String, ActionStatus> addResult = this.businessLogic.addExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, ref);
        if (addResult.isLeft()) {
            return Response.status(Response.Status.CREATED)
                    .entity(ref)
                    .build();
        } else {
            return this.buildExtRefErrorResponse(addResult.right().value(), uuid, "", componentInstanceName, objectType, ref.getReferenceUUID());
        }

    }

    @DELETE
    @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{reference}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteComponentInstanceReference(
            @PathParam("assetType") String assetType,
            @PathParam("uuid") String uuid,
            @PathParam("componentInstanceName") String componentInstanceName,
            @PathParam("objectType") String objectType,
            @PathParam("reference") String reference, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {

        log.debug("DELETE component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);

        Response r = validateRequest(xEcompInstanceId);
        if (r != null){
            return r;
        }

        Either<String, ActionStatus> deleteStatus = this.businessLogic.deleteExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, reference);
        if (deleteStatus.isLeft()){
            return this.buildOkResponse(new ExternalRefDTO(reference));
        } else {
            return this.buildExtRefErrorResponse(deleteStatus.right().value(), uuid, "", componentInstanceName, objectType, reference);
        }
    }

    @PUT
    @Path("/{assetType}/{uuid}/resourceInstances/{componentInstanceName}/externalReferences/{objectType}/{oldRefValue}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response updateComponentInstanceReference(
            @PathParam("assetType") String assetType,
            @PathParam("uuid") String uuid,
            @PathParam("componentInstanceName") String componentInstanceName,
            @PathParam("objectType") String objectType,
            @PathParam("oldRefValue") String oldRefValue,
            ExternalRefDTO newRefValueDTO, @HeaderParam("USER_ID") String userId, @HeaderParam("X-ECOMP-InstanceID") String xEcompInstanceId) {

        log.debug("PUT component instance external interfaces {} {} {} {}", assetType, uuid, componentInstanceName, objectType);

        Response r = validateRequest(xEcompInstanceId);
        if (r != null){
            return r;
        }

        String newRefValue = newRefValueDTO.getReferenceUUID();
        Either<String, ActionStatus> updateResult = this.businessLogic.updateExternalReference(ComponentTypeEnum.findByParamName(assetType), userId, uuid, componentInstanceName, objectType, oldRefValue, newRefValue);
        if (updateResult.isLeft()){
            return this.buildOkResponse(new ExternalRefDTO(newRefValue));
        } else {
            return this.buildExtRefErrorResponse(updateResult.right().value(), uuid, "", componentInstanceName, objectType, oldRefValue);
        }

    }

    private Response validateRequest(String xEcompInstanceIdHeader) {
        Wrapper<Response> responseWrapper = new Wrapper<>();

        //Validate X-ECOMP_INSTANCE_ID_HEADER
        if (xEcompInstanceIdHeader == null || xEcompInstanceIdHeader.isEmpty()){
            return this.buildExtRefErrorResponse(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID, "", "", "", "", "");
        }

        return responseWrapper.getInnerElement();
    }

    private Response buildExtRefErrorResponse(ActionStatus status, String uuid, String version, String componentInstanceName, String objectType, String ref){
        switch (status) {
            case RESOURCE_NOT_FOUND:
                return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid));
            case COMPONENT_VERSION_NOT_FOUND:
                return buildErrorResponse(componentsUtils.getResponseFormat(status, uuid, version));
            case COMPONENT_INSTANCE_NOT_FOUND:
                return buildErrorResponse(componentsUtils.getResponseFormat(status, componentInstanceName, uuid));
            case EXT_REF_ALREADY_EXIST:
                return Response.status(Response.Status.OK)
                        .entity(new ExternalRefDTO(ref))
                        .build();
            case EXT_REF_NOT_FOUND:
                return buildErrorResponse(componentsUtils.getResponseFormat(status, objectType + "/" + ref));
            case MISSING_X_ECOMP_INSTANCE_ID:
                return buildErrorResponse(componentsUtils.getResponseFormat(status));
            default:
                return this.buildGeneralErrorResponse();
        }
    }
}