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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.legacy.mapping;
24 import java.util.HashMap;
27 import javax.ws.rs.core.Response;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfModelRuntimeException;
31 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
32 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
36 import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper;
37 import org.onap.policy.models.tosca.utils.ToscaUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * This class maps a legacy operational policy to and from a TOSCA service template.
44 * @author Liam Fallon (liam.fallon@est.tech)
46 public class LegacyOperationalPolicyMapper
47 implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
49 // Property name for the operational policy content
50 private static final String CONTENT_PROPERTY = "content";
51 private static final String CONTROLLER_PROPERTY = "controllerName";
53 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
55 private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
56 new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
59 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
60 String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
61 if (incomingVersion == null) {
62 incomingVersion = "1";
65 PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
67 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
69 toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
71 final Map<String, String> propertyMap = new HashMap<>();
72 toscaPolicy.setProperties(propertyMap);
73 toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent());
74 if (legacyOperationalPolicy.getControllerName() != null) {
75 toscaPolicy.getProperties().put(CONTROLLER_PROPERTY, legacyOperationalPolicy.getControllerName());
78 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
79 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");
81 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
83 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
84 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
86 return serviceTemplate;
90 public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
91 ToscaUtils.assertPoliciesExist(serviceTemplate);
93 if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
94 String errorMessage = "more than one policy found in service template";
95 LOGGER.warn(errorMessage);
96 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
100 final JpaToscaPolicy toscaPolicy =
101 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
103 final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
104 legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
105 legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
107 if (toscaPolicy.getProperties() == null) {
108 String errorMessage = "no properties defined on TOSCA policy";
109 LOGGER.warn(errorMessage);
110 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
113 String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
115 if (content == null) {
116 String errorMessage = "property \"content\" not defined on TOSCA policy";
117 LOGGER.warn(errorMessage);
118 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
121 legacyOperationalPolicy.setContent(content);
123 String controllerName = toscaPolicy.getProperties().get(CONTROLLER_PROPERTY);
124 if (controllerName != null) {
125 legacyOperationalPolicy.setControllerName(controllerName);
128 return legacyOperationalPolicy;