Sync Integ to Master
[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.io.Serializable;
7 import java.util.*;
8
9 /**
10  * Maps an object type (e.g. "monitoring", "workflows" etc) to a list of external references.
11  *
12  * "monitoring" -> { "ref1",  "ref2" },
13  * "workflows"  -> { "ref1",  "ref2" }
14  *
15  */
16 public class MapComponentInstanceExternalRefs extends ToscaDataDefinition implements Serializable {
17
18     private static final long serialVersionUID = 7788408255736272985L;
19
20     //Constructor
21     public MapComponentInstanceExternalRefs() {
22         this.setComponentInstanceExternalRefs(new HashMap<String, List<String>>());
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         List<String> externalRefsByObjectType = ((Map<String, List<String>>) getToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF)).get(objectType);
31         return externalRefsByObjectType;
32     }
33
34     public void setComponentInstanceExternalRefs(Map<String, List<String>> componentInstanceExternalRefs) {
35         setToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF, componentInstanceExternalRefs);
36     }
37
38     /**
39      * Adds a reference to the given object type. Will do nothing if already exist.
40      *
41      * @param objectType object type to associate reference to
42      * @param ref to add
43      */
44     public boolean addExternalRef(String objectType, String ref){
45
46         List<String> refList = this.getExternalRefsByObjectType(objectType);
47
48         if (refList == null) {
49             //Create list if does not exist and add it to map
50             refList = new ArrayList<String>();
51             this.getComponentInstanceExternalRefs().put(objectType, refList);
52         }
53
54         //Add reference to list if does not exist
55         if (!refList.contains(ref)){
56             return refList.add(ref);
57         }
58
59         return false;
60
61     }
62
63     public boolean deleteExternalRef(String objectType, String ref){
64         List<String> refList = this.getExternalRefsByObjectType(objectType);
65
66         if (refList != null) {
67             return refList.remove(ref);
68         } else {
69             return false;
70         }
71     }
72
73     public boolean replaceExternalRef(String objectType, String oldRef, String newRef) {
74         List<String> refList = this.getExternalRefsByObjectType(objectType);
75
76         if (refList != null &&  !refList.contains(newRef)) {
77             return Collections.replaceAll(refList, oldRef, newRef);
78         } else {
79             return false;
80         }
81     }
82
83 }