Merge "Change vFW payload from pg-streams to streams"
[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 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.legacy.mapping;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.ws.rs.core.Response;
27
28 import org.onap.policy.common.utils.coder.CoderException;
29 import org.onap.policy.common.utils.coder.StandardCoder;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
37 import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper;
38 import org.onap.policy.models.tosca.utils.ToscaUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * This class maps a legacy operational policy to and from a TOSCA service template.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class LegacyOperationalPolicyMapper
48         implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
49
50     // Property name for the operational policy content
51     private static final String CONTENT_PROPERTY = "content";
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         try {
74             toscaPolicy.getProperties().put(CONTENT_PROPERTY,
75                     new StandardCoder().encode(legacyOperationalPolicy.getContent()));
76         } catch (CoderException ce) {
77             String errorMessage = "encoding of property \"content\" to JSON failed";
78             LOGGER.warn(errorMessage, ce);
79             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
80         }
81
82         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
83         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
84
85         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
86
87         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
88         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
89
90         return serviceTemplate;
91     }
92
93     @Override
94     public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
95         ToscaUtils.assertPoliciesExist(serviceTemplate);
96
97         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
98             String errorMessage = "more than one policy found in service template";
99             LOGGER.warn(errorMessage);
100             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
101         }
102
103         // Get the policy
104         final JpaToscaPolicy toscaPolicy =
105                 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
106
107         final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
108         legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
109         legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
110
111         if (toscaPolicy.getProperties() == null) {
112             String errorMessage = "no properties defined on TOSCA policy";
113             LOGGER.warn(errorMessage);
114             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
115         }
116
117         String content = null;
118         try {
119             content = new StandardCoder().decode(toscaPolicy.getProperties().get(CONTENT_PROPERTY), String.class);
120         } catch (CoderException ce) {
121             String errorMessage = "decoding of property \"content\" from JSON failed";
122             LOGGER.warn(errorMessage, ce);
123             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
124         }
125
126         if (content == null) {
127             String errorMessage = "property \"content\" not defined on TOSCA policy";
128             LOGGER.warn(errorMessage);
129             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
130         }
131
132         legacyOperationalPolicy.setContent(content);
133
134         return legacyOperationalPolicy;
135     }
136 }