b7ebdcec33cac48b54d0bd924d1b9ab010788ea8
[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-2020 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.",
60                 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
61         GUARD_POLICY_TYPE_MAP.put("guard.minmax.", new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
62         GUARD_POLICY_TYPE_MAP.put("guard.blacklist.",
63                 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
64     }
65
66     @Override
67     public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
68         PfConceptKey guardPolicyType = getGuardPolicyType(legacyGuardPolicyInput);
69         if (guardPolicyType == null) {
70             String errorMessage =
71                     "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
72             LOGGER.warn(errorMessage);
73             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
74         }
75
76         String version = legacyGuardPolicyInput.getPolicyVersion();
77         if (version != null) {
78             version = version + ".0.0";
79         } else {
80             version = guardPolicyType.getVersion();
81         }
82
83         PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
84
85         final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
86         toscaPolicy.setType(guardPolicyType);
87         toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
88
89         final Map<String, String> metadata = new LinkedHashMap<>();
90         metadata.put(POLICY_ID, toscaPolicy.getKey().getName());
91         metadata.put(POLICY_VERSION, Integer.toString(toscaPolicy.getKey().getMajorVersion()));
92         toscaPolicy.setMetadata(metadata);
93
94         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
95         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
96
97         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
98
99         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
100         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
101
102         return serviceTemplate;
103     }
104
105     @Override
106     public Map<String, LegacyGuardPolicyOutput>
107             fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
108         ToscaUtils.assertPoliciesExist(serviceTemplate);
109
110         final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
111
112         for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
113                 .values()) {
114
115             final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
116             legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
117             legacyGuardPolicyOutput.setVersion(toscaPolicy.getKey().getVersion());
118
119             if (toscaPolicy.getMetadata() == null) {
120                 String errorMessage = "no metadata defined on TOSCA policy";
121                 LOGGER.warn(errorMessage);
122                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
123             }
124
125             final Map<String, Object> metadata = new LinkedHashMap<>(toscaPolicy.getMetadata());
126
127             // if version exists, convert it to int
128             metadata.computeIfPresent(POLICY_VERSION, (key, val) -> Integer.parseInt(val.toString()));
129
130             legacyGuardPolicyOutput.setMetadata(metadata);
131
132             if (toscaPolicy.getProperties() == null) {
133                 String errorMessage = "no properties defined on TOSCA policy";
134                 LOGGER.warn(errorMessage);
135                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
136             }
137
138             final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
139             content.setContent(toscaPolicy.getProperties());
140
141             final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
142             propertiesMap.put("content", content);
143             legacyGuardPolicyOutput.setProperties(propertiesMap);
144
145             legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
146         }
147
148         return legacyGuardPolicyOutputMap;
149     }
150
151     private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
152         final String policyId = legacyGuardPolicyInput.getPolicyId();
153         if (policyId == null) {
154             return null;
155         }
156
157         for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
158             if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
159                 return guardPolicyTypeEntry.getValue();
160             }
161         }
162
163         return null;
164     }
165 }