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;
26 import javax.ws.rs.core.Response;
28 import org.onap.policy.models.base.PfConceptKey;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent;
31 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
32 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
37 import org.onap.policy.models.tosca.simple.mapping.JpaToscaServiceTemplateMapper;
38 import org.onap.policy.models.tosca.utils.ToscaUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * This class maps a legacy guard policy to and from a TOSCA service template.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class LegacyGuardPolicyMapper
48 implements JpaToscaServiceTemplateMapper<LegacyGuardPolicyInput, Map<String, LegacyGuardPolicyOutput>> {
49 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
51 private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
54 GUARD_POLICY_TYPE_MAP.put("guard.frequency.scaleout",
55 new PfConceptKey("onap.policies.controlloop.guard.FrequencyLimiter:1.0.0"));
56 GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout",
57 new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
58 GUARD_POLICY_TYPE_MAP.put("guard.minmax.scaleout",
59 new PfConceptKey("onap.policies.controlloop.guard.MinMax:1.0.0"));
60 GUARD_POLICY_TYPE_MAP.put("guard.blacklist",
61 new PfConceptKey("onap.policies.controlloop.guard.Blacklist:1.0.0"));
65 public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
66 PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
67 if (guardPolicyType == null) {
69 "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
70 LOGGER.warn(errorMessage);
71 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
74 String version = legacyGuardPolicyInput.getPolicyVersion();
75 if (version != null) {
76 version = version + ".0.0";
78 version = guardPolicyType.getVersion();
81 PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
83 final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
84 toscaPolicy.setType(guardPolicyType);
85 toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
87 final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
88 serviceTemplate.setToscaDefinitionsVersion("tosca_simimport java.util.HashMap;\n" + "ple_yaml_1_0");
90 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
92 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
93 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
95 return serviceTemplate;
99 public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(
100 final JpaToscaServiceTemplate serviceTemplate) {
101 ToscaUtils.assertPoliciesExist(serviceTemplate);
103 final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
105 for (JpaToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap()
108 final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
109 legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
110 legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
112 final Map<String, Object> metadata = new LinkedHashMap<>();
113 metadata.put("policy-id", toscaPolicy.getKey().getName());
114 metadata.put("policy-version", toscaPolicy.getKey().getMajorVersion());
115 legacyGuardPolicyOutput.setMetadata(metadata);
117 if (toscaPolicy.getProperties() == null) {
118 String errorMessage = "no properties defined on TOSCA policy";
119 LOGGER.warn(errorMessage);
120 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
123 final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
125 content.setActor( toscaPolicy.getProperties().get("actor"));
126 content.setClname( toscaPolicy.getProperties().get("clname"));
127 content.setGuardActiveEnd( toscaPolicy.getProperties().get("guardActiveEnd"));
128 content.setGuardActiveStart(toscaPolicy.getProperties().get("guardActiveStart"));
129 content.setLimit( toscaPolicy.getProperties().get("limit"));
130 content.setMax( toscaPolicy.getProperties().get("max"));
131 content.setMin( toscaPolicy.getProperties().get("min"));
132 content.setRecipe( toscaPolicy.getProperties().get("recipe"));
133 content.setTargets( toscaPolicy.getProperties().get("targets"));
134 content.setTimeUnits( toscaPolicy.getProperties().get("timeUnits"));
135 content.setTimeWindow( toscaPolicy.getProperties().get("timeWindow"));
138 final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
139 propertiesMap.put("content", content);
140 legacyGuardPolicyOutput.setProperties(propertiesMap);
142 legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
145 return legacyGuardPolicyOutputMap;