Merge "Restructure for authorative models"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / mapping / LegacyOperationalPolicyMapper.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.models.tosca.legacy.mapping;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.ws.rs.core.Response;
27
28 import org.onap.policy.models.base.PfConceptKey;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
31 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
32 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
35 import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper;
36 import org.onap.policy.models.tosca.utils.ToscaUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * This class maps a legacy operational policy to and from a TOSCA service template.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class LegacyOperationalPolicyMapper
46         implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
47     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
48
49     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
50             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
51
52     @Override
53     public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
54         String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
55         if (incomingVersion == null) {
56             incomingVersion = "1";
57         }
58
59         PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
60
61         final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
62
63         toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
64
65         final Map<String, String> propertyMap = new HashMap<>();
66         toscaPolicy.setProperties(propertyMap);
67         toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent());
68
69         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
70         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
71
72         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
73
74         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
75         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
76
77         return serviceTemplate;
78     }
79
80     @Override
81     public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
82         ToscaUtils.assertPoliciesExist(serviceTemplate);
83
84         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
85             String errorMessage = "more than one policy found in service template";
86             LOGGER.warn(errorMessage);
87             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
88         }
89
90         // Get the policy
91         final JpaToscaPolicy toscaPolicy =
92                 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
93
94         final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
95         legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
96         legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
97
98         if (toscaPolicy.getProperties() == null) {
99             String errorMessage = "no properties defined on TOSCA policy";
100             LOGGER.warn(errorMessage);
101             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
102         }
103
104         final String content = toscaPolicy.getProperties().get("Content");
105         if (toscaPolicy.getProperties() == null) {
106             String errorMessage = "property \"Content\" not defined on TOSCA policy";
107             LOGGER.warn(errorMessage);
108             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
109         }
110
111         legacyOperationalPolicy.setContent(content);
112
113         return legacyOperationalPolicy;
114     }
115 }