Changes for Checkstyle 8.32
[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  *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.legacy.mapping;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import javax.ws.rs.core.Response;
27 import org.onap.policy.models.base.PfConceptKey;
28 import org.onap.policy.models.base.PfModelRuntimeException;
29 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
30 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
31 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
32 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
34 import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper;
35 import org.onap.policy.models.tosca.utils.ToscaUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * This class maps a legacy operational policy to and from a TOSCA service template.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  */
44 public class LegacyOperationalPolicyMapper
45         implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
46
47     // Property name for the operational policy content
48     private static final String CONTENT_PROPERTY = "content";
49     private static final String CONTROLLER_PROPERTY = "controllerName";
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         if (legacyOperationalPolicy.getControllerName() != null) {
73             toscaPolicy.getProperties().put(CONTROLLER_PROPERTY, legacyOperationalPolicy.getControllerName());
74         }
75
76         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
77         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");
78
79         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
80
81         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
82         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
83
84         return serviceTemplate;
85     }
86
87     @Override
88     public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
89         ToscaUtils.assertPoliciesExist(serviceTemplate);
90
91         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
92             String errorMessage = "more than one policy found in service template";
93             LOGGER.warn(errorMessage);
94             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
95         }
96
97         // Get the policy
98         final JpaToscaPolicy toscaPolicy =
99                 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
100
101         final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
102         legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
103         legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
104
105         if (toscaPolicy.getProperties() == null) {
106             String errorMessage = "no properties defined on TOSCA policy";
107             LOGGER.warn(errorMessage);
108             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
109         }
110
111         String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
112
113         if (content == null) {
114             String errorMessage = "property \"content\" not defined on TOSCA policy";
115             LOGGER.warn(errorMessage);
116             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
117         }
118
119         legacyOperationalPolicy.setContent(content);
120
121         String controllerName = toscaPolicy.getProperties().get(CONTROLLER_PROPERTY);
122         if (controllerName != null) {
123             legacyOperationalPolicy.setControllerName(controllerName);
124         }
125
126         return legacyOperationalPolicy;
127     }
128 }