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>> {
51 // Tag for metadata fields
52 private static final String POLICY_ID = "policy-id";
53 private static final String POLICY_VERSION = "policy-version";
55 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
57 private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
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"));
69 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
70 PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
71 if (guardPolicyType == null) {
73 "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
74 LOGGER.warn(errorMessage);
75 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
78 String version = legacyGuardPolicyInput.getPolicyVersion();
79 if (version != null) {
80 version = version + ".0.0";
82 version = guardPolicyType.getVersion();
85 PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
87 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
88 toscaPolicy.setType(guardPolicyType);
89 toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
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);
96 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
97 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
99 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
101 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
102 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
104 return serviceTemplate;
108 public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
109 final JpaToscaServiceTemplate serviceTemplate) {
110 ToscaUtils.assertPoliciesExist(serviceTemplate);
112 final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
114 for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
117 final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
118 legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
119 legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
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);
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()));
132 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
135 legacyGuardPolicyOutput.setMetadata(metadata);
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);
143 final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
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"));
158 final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
159 propertiesMap.put("content", content);
160 legacyGuardPolicyOutput.setProperties(propertiesMap);
162 legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
165 return legacyGuardPolicyOutputMap;