47ecde228eb99d72bc487dfc96620b91b79ddd0a
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.datatypes.elements;
22
23 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
24 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
25
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33  * Maps an object type (e.g. "monitoring", "workflows" etc) to a list of external references.
34  *
35  * "monitoring" -> { "ref1",  "ref2" },
36  * "workflows"  -> { "ref1",  "ref2" }
37  */
38 public class MapComponentInstanceExternalRefs extends ToscaDataDefinition {
39
40     public MapComponentInstanceExternalRefs() {
41         setComponentInstanceExternalRefs(new HashMap<>());
42     }
43
44     public MapComponentInstanceExternalRefs(Map<String, List<String>> instanceExternalReferences) {
45         setComponentInstanceExternalRefs(instanceExternalReferences);
46     }
47
48     public Map<String, List<String>> getComponentInstanceExternalRefs() {
49         return (Map<String, List<String>>) getToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF);
50     }
51
52     public List<String> getExternalRefsByObjectType(String objectType) {
53         return ((Map<String, List<String>>) getToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF)).get(objectType);
54     }
55
56     public void setComponentInstanceExternalRefs(Map<String, List<String>> componentInstanceExternalRefs) {
57         setToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF, componentInstanceExternalRefs);
58     }
59
60     /**
61      * Adds a reference to the given object type. Will do nothing if already exist.
62      *
63      * @param objectType object type to associate reference to
64      * @param ref        to add
65      */
66     public boolean addExternalRef(String objectType, String ref) {
67
68         List<String> refList = this.getExternalRefsByObjectType(objectType);
69
70         if (refList == null) {
71             //Create list if does not exist and add it to map
72             refList = new ArrayList<>();
73             this.getComponentInstanceExternalRefs().put(objectType, refList);
74         }
75
76         //Add reference to list if does not exist
77         if (!refList.contains(ref)) {
78             return refList.add(ref);
79         }
80
81         return false;
82
83     }
84
85     public boolean deleteExternalRef(String objectType, String ref) {
86         List<String> refList = this.getExternalRefsByObjectType(objectType);
87
88         if (refList != null) {
89             return refList.remove(ref);
90         } else {
91             return false;
92         }
93     }
94
95     public boolean replaceExternalRef(String objectType, String oldRef, String newRef) {
96         List<String> refList = this.getExternalRefsByObjectType(objectType);
97
98         if (refList != null && !refList.contains(newRef)) {
99             return Collections.replaceAll(refList, oldRef, newRef);
100         } else {
101             return false;
102         }
103     }
104
105 }