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
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.", 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"));
67 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
68 PfConceptKey guardPolicyType = getGuardPolicyType(legacyGuardPolicyInput);
69 if (guardPolicyType == null) {
71 "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
72 LOGGER.warn(errorMessage);
73 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
76 String version = legacyGuardPolicyInput.getPolicyVersion();
77 if (version != null) {
78 version = version + ".0.0";
80 version = guardPolicyType.getVersion();
83 PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
85 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
86 toscaPolicy.setType(guardPolicyType);
87 toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
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);
94 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
95 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0_0");
97 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
99 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
100 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
102 return serviceTemplate;
106 public Map<String, LegacyGuardPolicyOutput>
107 fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
108 ToscaUtils.assertPoliciesExist(serviceTemplate);
110 final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
112 for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
115 final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
116 legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
117 legacyGuardPolicyOutput.setVersion(toscaPolicy.getKey().getVersion());
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);
125 final Map<String, Object> metadata = new LinkedHashMap<>(toscaPolicy.getMetadata());
127 // if version exists, convert it to int
128 metadata.computeIfPresent(POLICY_VERSION, (key, val) -> Integer.parseInt(val.toString()));
130 legacyGuardPolicyOutput.setMetadata(metadata);
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);
138 final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
139 content.setContent(toscaPolicy.getProperties());
141 final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
142 propertiesMap.put("content", content);
143 legacyGuardPolicyOutput.setProperties(propertiesMap);
145 legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
148 return legacyGuardPolicyOutputMap;
151 private PfConceptKey getGuardPolicyType(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
152 final String policyId = legacyGuardPolicyInput.getPolicyId();
153 if (policyId == null) {
157 for (Entry<String, PfConceptKey> guardPolicyTypeEntry : GUARD_POLICY_TYPE_MAP.entrySet()) {
158 if (policyId.startsWith(guardPolicyTypeEntry.getKey())) {
159 return guardPolicyTypeEntry.getValue();