41cf4df89f503643554e8e3a573588a4fdb27a6a
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / handling / KeyInfoMarshalFilter.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.basicmodel.handling;
22
23 import java.util.Collection;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
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;
33
34 /**
35  * This class implements a filter to prevent some keyinfo information being marshalled when a model is serialised.
36  */
37 public class KeyInfoMarshalFilter extends XmlAdapter<AxKeyInformation, AxKeyInformation> {
38
39     private List<AxKey> filterList = new LinkedList<>();
40
41     /**
42      * Adds a key to the list to be filtered.
43      *
44      * @param key the key to add to the filter list
45      */
46     public void addFilterKey(AxKey key) {
47         filterList.add(key);
48     }
49
50     /**
51      * Remove a key from the list to be filtered.
52      *
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.
55      */
56     public boolean removeFilterKey(AxKey key) {
57         return filterList.remove(key);
58     }
59
60     /**
61      * Adds some keys to the list to be filtered.
62      *
63      * @param keys the keys to add to the filter list
64      */
65     public void addFilterKeys(Collection<? extends AxKey> keys) {
66         filterList.addAll(keys);
67     }
68
69     /**
70      * Decide whether to unmarshall some keyinfos - Always.
71      *
72      * @param val the keyinfo
73      * @return the keyinfo
74      * @throws Exception if there is some problem unmarshalling
75      * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
76      */
77     @Override
78     public AxKeyInformation unmarshal(AxKeyInformation val) throws Exception {
79         return val;
80     }
81
82     /**
83      * Select which keyinfo entries will be marshalled - i.e. those not in the filter list.
84      *
85      * @param val the keyinfo
86      * @return the keyinfo
87      * @throws Exception if there is some problem with the marshalling
88      * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object)
89      */
90     @Override
91     public AxKeyInformation marshal(AxKeyInformation val) throws Exception {
92         if (val == null || val.getKeyInfoMap() == null || val.getKeyInfoMap().isEmpty() || filterList.isEmpty() ) {
93             return val;
94         }
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) {
99             retmap.remove(key);
100         }
101         ret.setKeyInfoMap(retmap);
102         return ret;
103     }
104 }