aa19edf0bdbc63849aba4504a051f3ca5f4bc78c
[policy/apex-pdp.git] /
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.policymodel.handling;
22
23 import javax.xml.bind.annotation.adapters.XmlAdapter;
24 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
25 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
26 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
27
28 /**
29  * This class makes the albums field optional in marshaled Policy Models.
30  * Empty albums are not marshaled to JSON/XML.
31  * When unmarshaled, if no albums value is present then a new empty albums entry is created.
32  *
33  * @author John Keeney (john.keeney@ericsson.com)
34  */
35 public class EmptyAlbumsAdapter extends XmlAdapter<AxContextAlbums, AxContextAlbums> {
36
37
38     /**
39      * Decide whether to marshall a context albums entry. Non-empty context albums are always marshalled.
40      * Empty albums are filtered.
41      *
42      * @param albums the albums entry
43      * @return the albums entry, or null if empty
44      * @throws Exception if there is a problem with the marshalling
45      * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object)
46      */
47     @Override
48     public AxContextAlbums marshal(AxContextAlbums albums) throws Exception {
49         if ((albums == null) || (albums.getAlbumsMap() == null) || (albums.getAlbumsMap().isEmpty())) {
50             return null;
51         } else {
52             return albums;
53         }
54     }
55
56     /**
57      * Decide whether to unmarshall a context albums entry - Always.
58      *
59      * @param val the albums entry
60      * @return the albums entry
61      * @throws Exception if there is a problem with the unmarshalling
62      * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
63      */
64     @Override
65     public AxContextAlbums unmarshal(AxContextAlbums val) throws Exception {
66         return val;
67     }
68
69     /**
70      * After unmarshalling has completed the model's context albums entry may be null/empty or default.
71      * If so the key for the albums entry should updated to a sensible value and additional keyinfo
72      * information should then be added for that key
73      *
74      * @param policyModel the policy model containing the possibly empty context albums entry
75      * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
76      */
77     public void doAfterUnmarshal(AxPolicyModel policyModel) {
78         AxArtifactKey nullkey = new AxArtifactKey();
79         AxArtifactKey blanknullalbumskey =
80             new AxArtifactKey(nullkey.getKey().getName() + "_Albums", nullkey.getKey().getVersion());
81         AxArtifactKey thisalbumskey = policyModel.getAlbums().getKey();
82         AxArtifactKey thismodelkey = policyModel.getKey();
83         AxContextAlbums thismodelalbums = policyModel.getAlbums();
84
85         if (nullkey.equals(thisalbumskey) || blanknullalbumskey.equals(thisalbumskey)) {
86             thismodelalbums.setKey(new AxArtifactKey(thismodelkey.getName() + "_Albums", thismodelkey.getVersion()));
87             policyModel.getKeyInformation().generateKeyInfo(thismodelalbums);
88         }
89     }
90
91 }