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