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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.legacy.mapping;
23 import java.util.HashMap;
26 import javax.ws.rs.core.Response;
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;
41 * This class maps a legacy operational policy to and from a TOSCA service template.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class LegacyOperationalPolicyMapper
46 implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
48 // Property name for the operational policy content
49 private static final String CONTENT_PROPERTY = "content";
51 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
53 private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
54 new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
57 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
58 String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
59 if (incomingVersion == null) {
60 incomingVersion = "1";
63 PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
65 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
67 toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
69 final Map<String, String> propertyMap = new HashMap<>();
70 toscaPolicy.setProperties(propertyMap);
71 toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent());
73 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
74 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
76 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
78 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
79 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
81 return serviceTemplate;
85 public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
86 ToscaUtils.assertPoliciesExist(serviceTemplate);
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);
95 final JpaToscaPolicy toscaPolicy =
96 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
98 final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
99 legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
100 legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
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);
108 final String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
109 if (content == null) {
110 String errorMessage = "property \"content\" not defined on TOSCA policy";
111 LOGGER.warn(errorMessage);
112 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
115 legacyOperationalPolicy.setContent(content);
117 return legacyOperationalPolicy;