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