aboutsummaryrefslogtreecommitdiffstats
path: root/model/policy-model/src/main/java/org/onap/policy/apex/model/policymodel/handling/EmptyAlbumsAdapter.java
blob: aa19edf0bdbc63849aba4504a051f3ca5f4bc78c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*-
 * ============LICENSE_START=======================================================
 * Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.policymodel.handling;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;

/**
 * This class makes the albums field optional in marshaled Policy Models.
 * Empty albums are not marshaled to JSON/XML.
 * When unmarshaled, if no albums value is present then a new empty albums entry is created.
 *
 * @author John Keeney (john.keeney@ericsson.com)
 */
public class EmptyAlbumsAdapter extends XmlAdapter<AxContextAlbums, AxContextAlbums> {


    /**
     * Decide whether to marshall a context albums entry. Non-empty context albums are always marshalled.
     * Empty albums are filtered.
     *
     * @param albums the albums entry
     * @return the albums entry, or null if empty
     * @throws Exception if there is a problem with the marshalling
     * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(Object)
     */
    @Override
    public AxContextAlbums marshal(AxContextAlbums albums) throws Exception {
        if ((albums == null) || (albums.getAlbumsMap() == null) || (albums.getAlbumsMap().isEmpty())) {
            return null;
        } else {
            return albums;
        }
    }

    /**
     * Decide whether to unmarshall a context albums entry - Always.
     *
     * @param val the albums entry
     * @return the albums entry
     * @throws Exception if there is a problem with the unmarshalling
     * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
     */
    @Override
    public AxContextAlbums unmarshal(AxContextAlbums val) throws Exception {
        return val;
    }

    /**
     * After unmarshalling has completed the model's context albums entry may be null/empty or default.
     * If so the key for the albums entry should updated to a sensible value and additional keyinfo
     * information should then be added for that key
     *
     * @param policyModel the policy model containing the possibly empty context albums entry
     * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(Object)
     */
    public void doAfterUnmarshal(AxPolicyModel policyModel) {
        AxArtifactKey nullkey = new AxArtifactKey();
        AxArtifactKey blanknullalbumskey =
            new AxArtifactKey(nullkey.getKey().getName() + "_Albums", nullkey.getKey().getVersion());
        AxArtifactKey thisalbumskey = policyModel.getAlbums().getKey();
        AxArtifactKey thismodelkey = policyModel.getKey();
        AxContextAlbums thismodelalbums = policyModel.getAlbums();

        if (nullkey.equals(thisalbumskey) || blanknullalbumskey.equals(thisalbumskey)) {
            thismodelalbums.setKey(new AxArtifactKey(thismodelkey.getName() + "_Albums", thismodelkey.getVersion()));
            policyModel.getKeyInformation().generateKeyInfo(thismodelalbums);
        }
    }

}