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.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"));
71 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
72 PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
73 if (guardPolicyType == null) {
75 "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
76 LOGGER.warn(errorMessage);
77 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
80 String version = legacyGuardPolicyInput.getPolicyVersion();
81 if (version != null) {
82 version = version + ".0.0";
84 version = guardPolicyType.getVersion();
87 PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
89 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
90 toscaPolicy.setType(guardPolicyType);
91 toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
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);
98 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
99 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
101 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
103 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
104 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
106 return serviceTemplate;
110 public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
111 final JpaToscaServiceTemplate serviceTemplate) {
112 ToscaUtils.assertPoliciesExist(serviceTemplate);
114 final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
116 for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
119 final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
120 legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
121 legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
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);
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()));
134 metadata.put(metadataEntry.getKey(), metadataEntry.getValue());
137 legacyGuardPolicyOutput.setMetadata(metadata);
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);
145 final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
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"));
160 final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
161 propertiesMap.put("content", content);
162 legacyGuardPolicyOutput.setProperties(propertiesMap);
164 legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
167 return legacyGuardPolicyOutputMap;