Serializaiton of properties to DB as JSON
[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
51     // Tag for metadata fields
52     private static final String POLICY_ID = "policy-id";
53     private static final String POLICY_VERSION = "policy-version";
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
56
57     private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
58
59     static {
60         GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout",
61                 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
62         GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout",
63                 new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
64         GUARD_POLICY_TYPE_MAP.put("guard.blacklist",
65                 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
66     }
67
68     @Override
69     public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
70         PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
71         if (guardPolicyType == null) {
72             String errorMessage =
73                     "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
74             LOGGER.warn(errorMessage);
75             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
76         }
77
78         String version = legacyGuardPolicyInput.getPolicyVersion();
79         if (version != null) {
80             version = version + ".0.0";
81         } else {
82             version = guardPolicyType.getVersion();
83         }
84
85         PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
86
87         final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
88         toscaPolicy.setType(guardPolicyType);
89         toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
90
91         final Map<String, String> metadata = new LinkedHashMap<>();
92         metadata.put(POLICY_ID, toscaPolicy.getKey().getName());
93         metadata.put(POLICY_VERSION, Integer.toString(toscaPolicy.getKey().getMajorVersion()));
94         toscaPolicy.setMetadata(metadata);
95
96         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
97         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
98
99         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
100
101         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
102         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
103
104         return serviceTemplate;
105     }
106
107     @Override
108     public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
109             final JpaToscaServiceTemplate serviceTemplate) {
110         ToscaUtils.assertPoliciesExist(serviceTemplate);
111
112         final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
113
114         for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
115                 .values()) {
116
117             final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
118             legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
119             legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
120
121             if (toscaPolicy.getMetadata() == null) {
122                 String errorMessage = "no metadata defined on TOSCA policy";
123                 LOGGER.warn(errorMessage);
124                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
125             }
126
127             final Map<String, Object> metadata = new LinkedHashMap<>();
128             for (Entry<String, String> metadataEntry : toscaPolicy.getMetadata().entrySet()) {
129                 if (POLICY_VERSION.equals(metadataEntry.getKey())) {
130                     metadata.put(POLICY_VERSION, Integer.parseInt(metadataEntry.getValue()));
131                 } else {
132                     metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
133                 }
134             }
135             legacyGuardPolicyOutput.setMetadata(metadata);
136
137             if (toscaPolicy.getProperties() == null) {
138                 String errorMessage = "no properties defined on TOSCA policy";
139                 LOGGER.warn(errorMessage);
140                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
141             }
142
143             final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
144             // @formatter:off
145             content.setActor(           toscaPolicy.getProperties().get("actor"));
146             content.setClname(          toscaPolicy.getProperties().get("clname"));
147             content.setGuardActiveEnd(  toscaPolicy.getProperties().get("guardActiveEnd"));
148             content.setGuardActiveStart(toscaPolicy.getProperties().get("guardActiveStart"));
149             content.setLimit(           toscaPolicy.getProperties().get("limit"));
150             content.setMax(             toscaPolicy.getProperties().get("max"));
151             content.setMin(             toscaPolicy.getProperties().get("min"));
152             content.setRecipe(          toscaPolicy.getProperties().get("recipe"));
153             content.setTargets(         toscaPolicy.getProperties().get("targets"));
154             content.setTimeUnits(       toscaPolicy.getProperties().get("timeUnits"));
155             content.setTimeWindow(      toscaPolicy.getProperties().get("timeWindow"));
156             // @formatter:on
157
158             final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
159             propertiesMap.put("content", content);
160             legacyGuardPolicyOutput.setProperties(propertiesMap);
161
162             legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
163         }
164
165         return legacyGuardPolicyOutputMap;
166     }
167 }