re base code
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / MapComponentInstanceExternalRefs.java
1 package org.openecomp.sdc.be.datatypes.elements;
2
3 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
4 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
5
6 import java.util.*;
7
8 /**
9  * Maps an object type (e.g. "monitoring", "workflows" etc) to a list of external references.
10  *
11  * "monitoring" -> { "ref1",  "ref2" },
12  * "workflows"  -> { "ref1",  "ref2" }
13  *
14  */
15 public class MapComponentInstanceExternalRefs extends ToscaDataDefinition {
16
17     public MapComponentInstanceExternalRefs() {
18         setComponentInstanceExternalRefs(new HashMap<>());
19     }
20
21     public MapComponentInstanceExternalRefs(Map<String, List<String>> instanceExternalReferences) {
22         setComponentInstanceExternalRefs(instanceExternalReferences);
23     }
24
25     public Map<String, List<String>> getComponentInstanceExternalRefs() {
26         return (Map<String, List<String>>) getToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF);
27     }
28
29     public List<String> getExternalRefsByObjectType(String objectType) {
30         return ((Map<String, List<String>>) getToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF)).get(objectType);
31     }
32
33     public void setComponentInstanceExternalRefs(Map<String, List<String>> componentInstanceExternalRefs) {
34         setToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF, componentInstanceExternalRefs);
35     }
36
37     /**
38      * Adds a reference to the given object type. Will do nothing if already exist.
39      *
40      * @param objectType object type to associate reference to
41      * @param ref to add
42      */
43     public boolean addExternalRef(String objectType, String ref){
44
45         List<String> refList = this.getExternalRefsByObjectType(objectType);
46
47         if (refList == null) {
48             //Create list if does not exist and add it to map
49             refList = new ArrayList<>();
50             this.getComponentInstanceExternalRefs().put(objectType, refList);
51         }
52
53         //Add reference to list if does not exist
54         if (!refList.contains(ref)){
55             return refList.add(ref);
56         }
57
58         return false;
59
60     }
61
62     public boolean deleteExternalRef(String objectType, String ref){
63         List<String> refList = this.getExternalRefsByObjectType(objectType);
64
65         if (refList != null) {
66             return refList.remove(ref);
67         } else {
68             return false;
69         }
70     }
71
72     public boolean replaceExternalRef(String objectType, String oldRef, String newRef) {
73         List<String> refList = this.getExternalRefsByObjectType(objectType);
74
75         if (refList != null &&  !refList.contains(newRef)) {
76             return Collections.replaceAll(refList, oldRef, newRef);
77         } else {
78             return false;
79         }
80     }
81
82 }