aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
blob: 41792aeb96806a9432b1591e5ae15bfbb5688ae8 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019-2020 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.models.tosca.legacy.mapping;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Response;

import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper;
import org.onap.policy.models.tosca.utils.ToscaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class maps a legacy operational policy to and from a TOSCA service template.
 *
 * @author Liam Fallon (liam.fallon@est.tech)
 */
public class LegacyOperationalPolicyMapper
        implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {

    // Property name for the operational policy content
    private static final String CONTENT_PROPERTY = "content";

    private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);

    private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
            new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");

    @Override
    public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
        String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
        if (incomingVersion == null) {
            incomingVersion = "1";
        }

        PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");

        final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);

        toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);

        final Map<String, String> propertyMap = new HashMap<>();
        toscaPolicy.setProperties(propertyMap);
        toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent());

        final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
        serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");

        serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());

        serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
        serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);

        return serviceTemplate;
    }

    @Override
    public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
        ToscaUtils.assertPoliciesExist(serviceTemplate);

        if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
            String errorMessage = "more than one policy found in service template";
            LOGGER.warn(errorMessage);
            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
        }

        // Get the policy
        final JpaToscaPolicy toscaPolicy =
                serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();

        final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
        legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
        legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));

        if (toscaPolicy.getProperties() == null) {
            String errorMessage = "no properties defined on TOSCA policy";
            LOGGER.warn(errorMessage);
            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
        }

        String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);

        if (content == null) {
            String errorMessage = "property \"content\" not defined on TOSCA policy";
            LOGGER.warn(errorMessage);
            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
        }

        legacyOperationalPolicy.setContent(content);

        return legacyOperationalPolicy;
    }
}