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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.legacy.mapping;
23 import java.util.LinkedHashMap;
25 import java.util.Map.Entry;
27 import javax.ws.rs.core.Response;
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;
44 * This class maps a legacy guard policy to and from a TOSCA service template.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class LegacyGuardPolicyMapper
49 implements JpaToscaServiceTemplateMapper<LegacyGuardPolicyInput, Map<String, LegacyGuardPolicyOutput>> {
50 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
52 // Tag for metadata fields
53 private static final String POLICY_ID = "policy-id";
54 private static final String POLICY_VERSION = "policy-version";
56 private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
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.",
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"));
68 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
69 PfConceptKey guardPolicyType = getGuardPolicyType(legacyGuardPolicyInput);
70 if (guardPolicyType == null) {
72 "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
73 LOGGER.warn(errorMessage);
74 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
77 String version = legacyGuardPolicyInput.getPolicyVersion();
78 if (version != null) {
79 version = version + ".0.0";
81 version = guardPolicyType.getVersion();
84 PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
86 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
87 toscaPolicy.setType(guardPolicyType);
88 toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
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);
95 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
96 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
98 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
100 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
101 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
103 return serviceTemplate;
107 public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
108 final JpaToscaServiceTemplate serviceTemplate) {
109 ToscaUtils.assertPoliciesExist(serviceTemplate);
111 final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
113 for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
116 final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
117 legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
118 legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
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);
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()));
131 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
134 legacyGuardPolicyOutput.setMetadata(metadata);
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);
142 final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
143 content.setContent(toscaPolicy.getProperties());
145 final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
146 propertiesMap.put("content", content);
147 legacyGuardPolicyOutput.setProperties(propertiesMap);
149 legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
152 return legacyGuardPolicyOutputMap;
155 private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
156 final String policyId = legacyGuardPolicyInput.getPolicyId();
157 if (policyId == null) {
161 for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
162 if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
163 return guardPolicyTypeEntry.getValue();