Add optional controllerName in Op. Legacy Policies
[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
27 import javax.ws.rs.core.Response;
28
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;
40
41 /**
42  * This class maps a legacy operational policy to and from a TOSCA service template.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class LegacyOperationalPolicyMapper
47         implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
48
49     // Property name for the operational policy content
50     private static final String CONTENT_PROPERTY = "content";
51     private static final String CONTROLLER_PROPERTY = "controllerName";
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
54
55     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
56             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
57
58     @Override
59     public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
60         String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
61         if (incomingVersion == null) {
62             incomingVersion = "1";
63         }
64
65         PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
66
67         final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
68
69         toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
70
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());
76         }
77
78         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
79         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");
80
81         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
82
83         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
84         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
85
86         return serviceTemplate;
87     }
88
89     @Override
90     public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
91         ToscaUtils.assertPoliciesExist(serviceTemplate);
92
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);
97         }
98
99         // Get the policy
100         final JpaToscaPolicy toscaPolicy =
101                 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
102
103         final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
104         legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
105         legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
106
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);
111         }
112
113         String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
114
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);
119         }
120
121         legacyOperationalPolicy.setContent(content);
122
123         String controllerName = toscaPolicy.getProperties().get(CONTROLLER_PROPERTY);
124         if (controllerName != null) {
125             legacyOperationalPolicy.setControllerName(controllerName);
126         }
127
128         return legacyOperationalPolicy;
129     }
130 }