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> {
47 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
49 private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
50 new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
53 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
54 String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
55 if (incomingVersion == null) {
56 incomingVersion = "1";
59 PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
61 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
63 toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
65 final Map<String, String> propertyMap = new HashMap<>();
66 toscaPolicy.setProperties(propertyMap);
67 toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent());
69 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
70 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
72 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
74 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
75 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
77 return serviceTemplate;
81 public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
82 ToscaUtils.assertPoliciesExist(serviceTemplate);
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);
91 final JpaToscaPolicy toscaPolicy =
92 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
94 final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
95 legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
96 legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
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);
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);
111 legacyOperationalPolicy.setContent(content);
113 return legacyOperationalPolicy;