Add JSON encoding to legacy policy properties
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / mapping / LegacyGuardPolicyMapper.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.LinkedHashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
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.LegacyGuardPolicyContent;
32 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
33 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
38 import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper;
39 import org.onap.policy.models.tosca.utils.ToscaUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * This class maps a legacy guard policy to and from a TOSCA service template.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class LegacyGuardPolicyMapper
49         implements JpaToscaServiceTemplateMapper<LegacyGuardPolicyInput, Map<String, LegacyGuardPolicyOutput>> {
50     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
51
52     // Tag for metadata fields
53     private static final String POLICY_ID = "policy-id";
54     private static final String POLICY_VERSION = "policy-version";
55
56     private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
57
58     static {
59         GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout",
60                 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
61         GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout",
62                 new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
63         GUARD_POLICY_TYPE_MAP.put("guard.blacklist",
64                 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
65     }
66
67     @Override
68     public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
69         PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
70         if (guardPolicyType == null) {
71             String errorMessage =
72                     "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
73             LOGGER.warn(errorMessage);
74             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
75         }
76
77         String version = legacyGuardPolicyInput.getPolicyVersion();
78         if (version != null) {
79             version = version + ".0.0";
80         } else {
81             version = guardPolicyType.getVersion();
82         }
83
84         PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
85
86         final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
87         toscaPolicy.setType(guardPolicyType);
88         toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
89
90         final Map<String, String> metadata = new LinkedHashMap<>();
91         metadata.put(POLICY_ID, toscaPolicy.getKey().getName());
92         metadata.put(POLICY_VERSION, Integer.toString(toscaPolicy.getKey().getMajorVersion()));
93         toscaPolicy.setMetadata(metadata);
94
95         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
96         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
97
98         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
99
100         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
101         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
102
103         return serviceTemplate;
104     }
105
106     @Override
107     public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
108             final JpaToscaServiceTemplate serviceTemplate) {
109         ToscaUtils.assertPoliciesExist(serviceTemplate);
110
111         final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
112
113         for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
114                 .values()) {
115
116             final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
117             legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
118             legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
119
120             if (toscaPolicy.getMetadata() == null) {
121                 String errorMessage = "no metadata defined on TOSCA policy";
122                 LOGGER.warn(errorMessage);
123                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
124             }
125
126             final Map<String, Object> metadata = new LinkedHashMap<>();
127             for (Entry<String, String> metadataEntry : toscaPolicy.getMetadata().entrySet()) {
128                 if (POLICY_VERSION.equals(metadataEntry.getKey())) {
129                     metadata.put(POLICY_VERSION, Integer.parseInt(metadataEntry.getValue()));
130                 } else {
131                     metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
132                 }
133             }
134             legacyGuardPolicyOutput.setMetadata(metadata);
135
136             if (toscaPolicy.getProperties() == null) {
137                 String errorMessage = "no properties defined on TOSCA policy";
138                 LOGGER.warn(errorMessage);
139                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
140             }
141
142             final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
143             content.setContent(toscaPolicy.getProperties());
144
145             final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
146             propertiesMap.put("content", content);
147             legacyGuardPolicyOutput.setProperties(propertiesMap);
148
149             legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
150         }
151
152         return legacyGuardPolicyOutputMap;
153     }
154 }