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;
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;
40 * This class maps a legacy operational policy to and from a TOSCA service template.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class LegacyOperationalPolicyMapper
45 implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
47 // Property name for the operational policy content
48 private static final String CONTENT_PROPERTY = "content";
49 private static final String CONTROLLER_PROPERTY = "controllerName";
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());
72 if (legacyOperationalPolicy.getControllerName() != null) {
73 toscaPolicy.getProperties().put(CONTROLLER_PROPERTY, legacyOperationalPolicy.getControllerName());
76 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
77 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");
79 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
81 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
82 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
84 return serviceTemplate;
88 public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
89 ToscaUtils.assertPoliciesExist(serviceTemplate);
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);
98 final JpaToscaPolicy toscaPolicy =
99 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
101 final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
102 legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
103 legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
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);
111 String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
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);
119 legacyOperationalPolicy.setContent(content);
121 String controllerName = toscaPolicy.getProperties().get(CONTROLLER_PROPERTY);
122 if (controllerName != null) {
123 legacyOperationalPolicy.setControllerName(controllerName);
126 return legacyOperationalPolicy;