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.elements;
25 import com.fasterxml.jackson.annotation.JsonCreator;
26 import com.fasterxml.jackson.annotation.JsonValue;
27 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
29 import java.util.HashMap;
32 import java.util.stream.Collectors;
34 public class MapDataDefinition <T extends ToscaDataDefinition> extends ToscaDataDefinition {
36 protected Map<String, T > mapToscaDataDefinition;
38 public MapDataDefinition(MapDataDefinition<T> cdt) {
40 mapToscaDataDefinition = cdt.mapToscaDataDefinition;
44 public MapDataDefinition(Map<String, T > mapToscaDataDefinition) {
46 this.mapToscaDataDefinition = mapToscaDataDefinition;
49 public MapDataDefinition() {
53 public Map<String, T > getMapToscaDataDefinition() {
54 return mapToscaDataDefinition;
57 public void put(String key, T value){
58 if(mapToscaDataDefinition == null){
59 mapToscaDataDefinition = new HashMap<String, T>();
61 mapToscaDataDefinition.put(key, value);
64 public void delete(String key){
65 if(mapToscaDataDefinition != null && mapToscaDataDefinition.containsKey(key)){
66 mapToscaDataDefinition.remove(key);
70 public T findByKey(String key){
72 if(mapToscaDataDefinition != null && mapToscaDataDefinition.containsKey(key)){
73 value = mapToscaDataDefinition.get(key);
78 public void setOwnerIdIfEmpty(String ownerId) {
79 if ( mapToscaDataDefinition != null ){
80 mapToscaDataDefinition.entrySet().forEach(e -> e.getValue().setOwnerIdIfEmpty(ownerId));
85 public String findKeyByItemUidMatch(String uid){
86 if(null == mapToscaDataDefinition || uid == null)
88 Map.Entry<String, T> entry = mapToscaDataDefinition.entrySet().stream().filter(e ->
89 e.getValue().findUidMatch(uid))
90 .findAny().orElse(null);
93 return entry.getKey();
96 public <T extends ToscaDataDefinition> T removeByOwnerId(Set<String> ownerIdList) {
97 Map<String, T > collect = (Map<String, T >)mapToscaDataDefinition.entrySet().stream().filter(e -> ownerIdList.contains(e.getValue().getOwnerId())).collect(Collectors.toMap(Map.Entry::getKey, (Map.Entry::getValue)));
99 MapDataDefinition collectMap = new MapDataDefinition<>(collect);
101 mapToscaDataDefinition.entrySet().removeIf(e -> ownerIdList.contains(e.getValue().getOwnerId()));
103 return (T) collectMap;
107 public <T extends ToscaDataDefinition> T updateIfExist(T other, boolean allowDefaultValueOverride) {
109 Map<String, T > map = ((MapDataDefinition)other).getMapToscaDataDefinition();
111 map.entrySet().forEach(e ->{
112 String key = e.getKey();
113 if ( mapToscaDataDefinition.containsKey(key) ){
114 e.getValue().mergeFunction(mapToscaDataDefinition.get(key), allowDefaultValueOverride);
120 public boolean isEmpty(){
121 return mapToscaDataDefinition == null || mapToscaDataDefinition.isEmpty();