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