com.sap.cloud.mt.subscription.ServiceInstance Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multi-tenant-subscription Show documentation
Show all versions of multi-tenant-subscription Show documentation
Spring Boot Enablement Parent
/*******************************************************************************
* © 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 com.sap.cloud.mt.tools.api.Cloner;
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.Objects;
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";
public static final String LABELS = "labels";
public static final String TENANT_ID = "tenant_id";
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 ServiceInstance createCopy() {
var serviceInstanceCopy = new ServiceInstance(Cloner.clone(this, Map.class));
serviceInstanceCopy.setBindings(bindings);
return serviceInstanceCopy;
}
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 void clearTenants() {
var labels = (Map) get(LABELS);
if (labels == null) {
return;
}
var tenants = (List) labels.get(TENANT_ID);
if (tenants != null) {
tenants.clear();
}
}
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));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
ServiceInstance that = (ServiceInstance) o;
return Objects.equals(bindings, that.bindings) && Objects.equals(newestBinding, that.newestBinding) && Objects.equals(lastOperation, that.lastOperation);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), bindings, newestBinding, lastOperation);
}
}