2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.datatypes.tosca;
23 import com.fasterxml.jackson.annotation.JsonCreator;
24 import com.fasterxml.jackson.annotation.JsonValue;
25 import fj.data.Either;
26 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import java.util.stream.Collectors;
36 public abstract class ToscaDataDefinition {
38 protected Map<String, Object> toscaPresentation;
40 public ToscaDataDefinition() {
41 toscaPresentation = new HashMap<>();
45 public ToscaDataDefinition(Map<String, Object> art) {
46 toscaPresentation = art;
50 public Object getToscaPresentationValue(JsonPresentationFields name) {
51 if (toscaPresentation != null && toscaPresentation.containsKey(name.getPresentation())) {
52 return toscaPresentation.get(name.getPresentation());
57 public void setToscaPresentationValue(JsonPresentationFields name, Object value) {
59 if (toscaPresentation == null) {
60 toscaPresentation = new HashMap<>();
62 toscaPresentation.put(name.getPresentation(), value);
67 public void setOwnerIdIfEmpty(String ownerId) {
68 if (getOwnerId() == null) {
73 public String getType() {
74 return (String) getToscaPresentationValue(JsonPresentationFields.TYPE);
77 public String getVersion() {
78 return (String) getToscaPresentationValue(JsonPresentationFields.VERSION);
81 public void setType(String type) {
82 setToscaPresentationValue(JsonPresentationFields.TYPE, type);
85 public void setOwnerId(String ownerId) {
86 setToscaPresentationValue(JsonPresentationFields.OWNER_ID, ownerId);
89 public String getOwnerId() {
90 return (String) getToscaPresentationValue(JsonPresentationFields.OWNER_ID);
93 // default merge function for merging data maps - implement where needed and use mergeDataMaps method where applicable instead of map1.putAll(map2)
94 public <T extends ToscaDataDefinition> T mergeFunction(T other, boolean allowDefaultValueOverride) {
95 other.setOwnerId(getOwnerId());
99 public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2) {
100 return mergeDataMaps(map1, map2, false);
103 // return Either.right(item key) if an illegal merge was attempted (overriding data type is forbidden)
104 public static <T extends ToscaDataDefinition> Either<Map<String, T>, String> mergeDataMaps(Map<String, T> map1, Map<String, T> map2, boolean allowDefaultValueOverride) {
105 for (Entry<String, T> entry : map2.entrySet()) {
106 map1.merge(entry.getKey(), entry.getValue(), (item1, item2) -> item1.mergeFunction(item2, allowDefaultValueOverride));
107 // validate merge success
108 if (!map1.containsKey(entry.getKey())) {
109 return Either.right(entry.getKey());
112 return Either.left(map1);
115 public static <T extends ToscaDataDefinition> Map<String, T> listToMapByName(List<T> dataList) {
116 return null == dataList ? new HashMap<>() : dataList.stream()
117 .collect(Collectors.toMap(p -> (String) p.getToscaPresentationValue(JsonPresentationFields.NAME), p -> p));
120 public boolean findUidMatch(String uid) {
121 return uid.equals(getToscaPresentationValue(JsonPresentationFields.UNIQUE_ID));
125 public <T extends ToscaDataDefinition> T removeByOwnerId(Set<String> ownerIdList) {
129 public static <T extends ToscaDataDefinition> T removeAndCollectByOwnerId(T complexStructure, Set<String> ownerIdList) {
130 return complexStructure.removeByOwnerId(ownerIdList);
133 public <T extends ToscaDataDefinition> T updateIfExist(T other, boolean allowDefaultValueOverride) {
137 public boolean isEmpty() {
141 public void setSchema(final SchemaDefinition schemaDef){};