2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.handling;
23 import java.util.Collection;
24 import java.util.LinkedList;
25 import java.util.List;
27 import java.util.TreeMap;
28 import javax.xml.bind.annotation.adapters.XmlAdapter;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
35 * This class implements a filter to prevent some keyinfo information being marshalled when a model is serialised.
37 public class KeyInfoMarshalFilter extends XmlAdapter<AxKeyInformation, AxKeyInformation> {
39 private List<AxKey> filterList = new LinkedList<>();
42 * Adds a key to the list to be filtered.
44 * @param key the key to add to the filter list
46 public void addFilterKey(AxKey key) {
51 * Remove a key from the list to be filtered.
53 * @param key the key to remove from the filter list
54 * @return true if the passed key was in the filter list and has been removed.
56 public boolean removeFilterKey(AxKey key) {
57 return filterList.remove(key);
61 * Adds some keys to the list to be filtered.
63 * @param keys the keys to add to the filter list
65 public void addFilterKeys(Collection<? extends AxKey> keys) {
66 filterList.addAll(keys);
70 * Decide whether to unmarshall some keyinfos - Always.
72 * @param val the keyinfo
74 * @throws Exception if there is some problem unmarshalling
75 * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
78 public AxKeyInformation unmarshal(AxKeyInformation val) throws Exception {
83 * Select which keyinfo entries will be marshalled - i.e. those not in the filter list.
85 * @param val the keyinfo
87 * @throws Exception if there is some problem with the marshalling
88 * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object)
91 public AxKeyInformation marshal(AxKeyInformation val) throws Exception {
92 if (val == null || val.getKeyInfoMap() == null || val.getKeyInfoMap().isEmpty() || filterList.isEmpty() ) {
95 //create a new keyinfo clone to avoid removing keyinfo entries from the original model
96 AxKeyInformation ret = new AxKeyInformation(val);
97 Map<AxArtifactKey, AxKeyInfo> retmap = new TreeMap<>(ret.getKeyInfoMap());
98 for (AxKey key : filterList) {
101 ret.setKeyInfoMap(retmap);