41792aeb96806a9432b1591e5ae15bfbb5688ae8
[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-2020 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
48     // Property name for the operational policy content
49     private static final String CONTENT_PROPERTY = "content";
50
51     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
52
53     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
54             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
55
56     @Override
57     public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
58         String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
59         if (incomingVersion == null) {
60             incomingVersion = "1";
61         }
62
63         PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
64
65         final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
66
67         toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
68
69         final Map<String, String> propertyMap = new HashMap<>();
70         toscaPolicy.setProperties(propertyMap);
71         toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent());
72
73         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
74         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
75
76         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
77
78         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
79         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
80
81         return serviceTemplate;
82     }
83
84     @Override
85     public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
86         ToscaUtils.assertPoliciesExist(serviceTemplate);
87
88         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
89             String errorMessage = "more than one policy found in service template";
90             LOGGER.warn(errorMessage);
91             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
92         }
93
94         // Get the policy
95         final JpaToscaPolicy toscaPolicy =
96                 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
97
98         final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
99         legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
100         legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
101
102         if (toscaPolicy.getProperties() == null) {
103             String errorMessage = "no properties defined on TOSCA policy";
104             LOGGER.warn(errorMessage);
105             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
106         }
107
108         String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
109
110         if (content == null) {
111             String errorMessage = "property \"content\" not defined on TOSCA policy";
112             LOGGER.warn(errorMessage);
113             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
114         }
115
116         legacyOperationalPolicy.setContent(content);
117
118         return legacyOperationalPolicy;
119     }
120 }