All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.mt.subscription.ServiceInstance Maven / Gradle / Ivy

There is a newer version: 3.3.1
Show newest version
/*******************************************************************************
 *   © 2019-2024 SAP SE or an SAP affiliate company. All rights reserved.
 ******************************************************************************/
package com.sap.cloud.mt.subscription;

import com.sap.cloud.mt.subscription.ServiceOperation.Status;
import com.sap.cloud.mt.subscription.ServiceOperation.Type;
import org.apache.commons.lang3.StringUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Service instance parameters
 */
public class ServiceInstance extends HashMap {
    public static final String ID = "id";
    public static final String NAME = "name";
    private List bindings = new ArrayList<>();
    private ServiceBinding newestBinding = null;
    private final ServiceOperation lastOperation;

    public ServiceInstance(Map m) {
        super(m);
        lastOperation = new ServiceOperation(m != null ? m : new HashMap());

    }

    public String getId() {
        return containsKey(ID) ? (String) get(ID) : "";
    }

    public boolean isReady() {
        return containsKey("ready") && (boolean) get("ready");
    }

    public String getName() {
        return containsKey(NAME) ? (String) get(NAME) : "";
    }

    public String getServicePlanId() {
        return (String) get("service_plan_id");
    }

    public List getTenants() {
        var labels = (Map) get("labels");
        if (labels == null) {
            return new ArrayList<>();
        }
        var tenants = (List) labels.get("tenant_id");
        return tenants != null ? tenants.stream().filter(t -> !StringUtils.isBlank(t)).toList() : new ArrayList<>();
    }

    public void insertTenant(String tenantId) {
        var labels = (Map) get("labels");
        if (labels == null) {
            labels = new HashMap();
            put("labels", labels);
        }
        var tenants = (List) labels.get("tenant_id");
        if (tenants == null) {
            tenants = new ArrayList();
            labels.put("tenant_id", tenants);
        }
        tenants.add(tenantId);
    }

    public boolean hasTenant() {
        return !getTenants().isEmpty();
    }

    public Instant getCreatedAt() {
        return Instant.parse((String) get("created_at"));
    }

    public Instant getUpdatedAt() {
        return Instant.parse((String) get("updated_at"));
    }

    public String getUsable() {
        return (String) get("usable");
    }

    public ServiceOperation getLastOperation() {
        return lastOperation;
    }

    public List getBindings() {
        return bindings;
    }

    public void setBindings(List bindings) {
        this.bindings = bindings != null ? bindings : new ArrayList<>();
        final AtomicReference newestBindingRef = new AtomicReference<>(null);
        this.bindings.stream()
                .filter(b -> b.getServiceInstanceId().equals(getId()))
                .filter(ServiceBinding::isUsable).forEach(b -> {
                    if (newestBindingRef.get() == null ||
                            b.getCreatedAt().isAfter(newestBindingRef.get().getCreatedAt())) {
                        newestBindingRef.set(b);
                    }
                });
        if (newestBindingRef.get() == null) {
            this.newestBinding = null;
        } else {
            this.newestBinding = newestBindingRef.get();
        }
    }

    public Optional getBinding() {
        return newestBinding != null ? Optional.of(newestBinding) : Optional.empty();
    }

    public boolean isUsable() {
        var status = lastOperation.getStatus();
        return isReady() &&
                !(lastOperation.getType() == Type.CREATE && (status == Status.FAILED || status == Status.IN_PROGRESS));
    }

    private boolean bindingTenantIsContainedInInstanceTenant(List instanceTenants, List bindingTenants) {
        return instanceTenants.stream().anyMatch(bindingTenants::contains);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy