2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.datatypes.elements;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
27 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
28 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31 * Maps an object type (e.g. "monitoring", "workflows" etc) to a list of external references.
33 * "monitoring" -> { "ref1", "ref2" }, "workflows" -> { "ref1", "ref2" }
35 public class MapComponentInstanceExternalRefs extends ToscaDataDefinition {
37 public MapComponentInstanceExternalRefs() {
38 setComponentInstanceExternalRefs(new HashMap<>());
41 public MapComponentInstanceExternalRefs(Map<String, List<String>> instanceExternalReferences) {
42 setComponentInstanceExternalRefs(instanceExternalReferences);
45 public Map<String, List<String>> getComponentInstanceExternalRefs() {
46 return (Map<String, List<String>>) getToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF);
49 public void setComponentInstanceExternalRefs(Map<String, List<String>> componentInstanceExternalRefs) {
50 setToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF, componentInstanceExternalRefs);
53 public List<String> getExternalRefsByObjectType(String objectType) {
54 return ((Map<String, List<String>>) getToscaPresentationValue(JsonPresentationFields.EXTERNAL_REF)).get(objectType);
58 * Adds a reference to the given object type. Will do nothing if already exist.
60 * @param objectType object type to associate reference to
63 public boolean addExternalRef(String objectType, String ref) {
64 List<String> refList = this.getExternalRefsByObjectType(objectType);
65 if (refList == null) {
66 //Create list if does not exist and add it to map
67 refList = new ArrayList<>();
68 this.getComponentInstanceExternalRefs().put(objectType, refList);
70 //Add reference to list if does not exist
71 if (!refList.contains(ref)) {
72 return refList.add(ref);
77 public boolean deleteExternalRef(String objectType, String ref) {
78 List<String> refList = this.getExternalRefsByObjectType(objectType);
79 if (refList != null) {
80 return refList.remove(ref);
86 public boolean replaceExternalRef(String objectType, String oldRef, String newRef) {
87 List<String> refList = this.getExternalRefsByObjectType(objectType);
88 if (refList != null && !refList.contains(newRef)) {
89 return Collections.replaceAll(refList, oldRef, newRef);