* ============LICENSE_START=======================================================
* ONAP Policy Model
* ================================================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2022 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.policy.models.tosca.authorative.concepts;
import com.google.gson.annotations.SerializedName;
-import io.swagger.annotations.ApiModelProperty;
import java.util.LinkedHashMap;
+import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import javax.ws.rs.core.Response;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
+import org.onap.policy.models.base.PfKey;
+import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.base.PfNameVersion;
/**
@Data
@NoArgsConstructor
public class ToscaEntity implements PfNameVersion {
- private String name;
+ private String name = PfKey.NULL_KEY_NAME;
+ private String version = PfKey.NULL_KEY_VERSION;
- private String version;
-
- @ApiModelProperty(name = "derived_from")
@SerializedName("derived_from")
private String derivedFrom;
- private Map<String, String> metadata;
-
+ private Map<String, Object> metadata;
private String description;
/**
- * Copy COnstructor.
+ * Copy Constructor.
*
* @param copyObject object to copy from
*/
if (copyObject.metadata != null) {
metadata = new LinkedHashMap<>();
- for (final Entry<String, String> metadataEntry : copyObject.metadata.entrySet()) {
+ for (final Entry<String, Object> metadataEntry : copyObject.metadata.entrySet()) {
metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
}
}
}
+ /**
+ * Get a key for this entity.
+ *
+ * @return a ToscaEntityKey for this entry
+ */
+ public ToscaEntityKey getKey() {
+ return new ToscaEntityKey(name, version);
+ }
+
+ @Override
+ public String getDefinedName() {
+ return (PfKey.NULL_KEY_NAME.equals(name) ? null : name);
+ }
+
+ @Override
+ public String getDefinedVersion() {
+ return (PfKey.NULL_KEY_VERSION.equals(version) ? null : version);
+ }
+
+ /**
+ * Convert a list of maps of TOSCA entities into a regular map.
+ *
+ * @param listOfMapsOfEntities the incoming list of maps of entities
+ * @return The entities on a regular map
+ * @throws PfModelRuntimeException on duplicate entity entries
+ */
+ public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityListMapAsMap(
+ List<Map<String, T>> listOfMapsOfEntities) {
+ // Declare the return map
+ Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
+
+ if (listOfMapsOfEntities == null) {
+ return entityMap;
+ }
+
+ for (Map<String, T> mapOfEntities : listOfMapsOfEntities) {
+ for (T entityEntry : mapOfEntities.values()) {
+ if (entityMap.containsKey(entityEntry.getKey())) {
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
+ "list of map of entities contains more than one entity with key " + entityEntry.getKey());
+ }
+ entityMap.put(entityEntry.getKey(), entityEntry);
+ }
+ }
+
+ return entityMap;
+ }
+
+ /**
+ * Convert a map of TOSCA entities into a regular map.
+ *
+ * @param mapOfEntities the incoming list of maps of entities
+ * @return The entities on a regular map
+ * @throws PfModelRuntimeException on duplicate entity entries
+ */
+ public static <T extends ToscaEntity> Map<ToscaEntityKey, T> getEntityMapAsMap(Map<String, T> mapOfEntities) {
+ // Declare the return map
+ Map<ToscaEntityKey, T> entityMap = new LinkedHashMap<>();
+
+ if (mapOfEntities == null) {
+ return entityMap;
+ }
+
+ for (T entityEntry : mapOfEntities.values()) {
+ if (entityMap.containsKey(entityEntry.getKey())) {
+ throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
+ "list of map of entities contains more than one entity with key " + entityEntry.getKey());
+ }
+
+ entityMap.put(entityEntry.getKey(), entityEntry);
+ }
+
+ return entityMap;
+ }
+
+ /**
+ * Method that should be specialised to return the type of the entity if the entity has a type.
+ *
+ * @return the type of the entity or null if it has no type
+ */
+ public String getType() {
+ return null;
+ }
+
+ /**
+ * Method that should be specialised to return the type version of the entity if the entity has a type.
+ *
+ * @return the type of the entity or null if it has no type
+ */
+ public String getTypeVersion() {
+ return null;
+ }
}