Merge "Add @NonNull to PolicyIdent"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / mapping / LegacyGuardPolicyMapper.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.legacy.mapping;
22
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26 import javax.ws.rs.core.Response;
27
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.ToscaPolicies;
34 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
35 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
37 import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper;
38 import org.onap.policy.models.tosca.utils.ToscaUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * This class maps a legacy guard policy to and from a TOSCA service template.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class LegacyGuardPolicyMapper
48         implements ToscaServiceTemplateMapper<LegacyGuardPolicyInput, Map<String, LegacyGuardPolicyOutput>> {
49     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
50
51     private static final Map<String, PfConceptKey> GUARD_POLICY_TYPE_MAP = new LinkedHashMap<>();
52
53     static {
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"));
62     }
63
64     @Override
65     public ToscaServiceTemplate toToscaServiceTemplate(final LegacyGuardPolicyInput legacyGuardPolicyInput) {
66         PfConceptKey guardPolicyType = GUARD_POLICY_TYPE_MAP.get(legacyGuardPolicyInput.getPolicyId());
67         if (guardPolicyType == null) {
68             String errorMessage =
69                     "policy type for guard policy \"" + legacyGuardPolicyInput.getPolicyId() + "\" unknown";
70             LOGGER.warn(errorMessage);
71             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
72         }
73
74         String version = legacyGuardPolicyInput.getPolicyVersion();
75         if (version != null) {
76             version = version + ".0.0";
77         } else {
78             version = guardPolicyType.getVersion();
79         }
80
81         PfConceptKey policyKey = new PfConceptKey(legacyGuardPolicyInput.getPolicyId(), version);
82
83         final ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey);
84         toscaPolicy.setType(guardPolicyType);
85         toscaPolicy.setProperties(legacyGuardPolicyInput.getContent().getAsPropertyMap());
86
87         final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
88         serviceTemplate.setToscaDefinitionsVersion("tosca_simimport java.util.HashMap;\n" + "ple_yaml_1_0");
89
90         serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
91
92         serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
93         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
94
95         return serviceTemplate;
96     }
97
98     @Override
99     public Map<String, LegacyGuardPolicyOutput> fromToscaServiceTemplate(final ToscaServiceTemplate serviceTemplate) {
100         ToscaUtils.assertPoliciesExist(serviceTemplate);
101
102         final Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyOutputMap = new LinkedHashMap<>();
103
104         for (ToscaPolicy toscaPolicy : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().values()) {
105
106             final LegacyGuardPolicyOutput legacyGuardPolicyOutput = new LegacyGuardPolicyOutput();
107             legacyGuardPolicyOutput.setType(toscaPolicy.getType().getName());
108             legacyGuardPolicyOutput.setVersion(toscaPolicy.getType().getVersion());
109
110             final Map<String, Object> metadata = new LinkedHashMap<>();
111             metadata.put("policy-id", toscaPolicy.getKey().getName());
112             metadata.put("policy-version", toscaPolicy.getKey().getMajorVersion());
113             legacyGuardPolicyOutput.setMetadata(metadata);
114
115             if (toscaPolicy.getProperties() == null) {
116                 String errorMessage = "no properties defined on TOSCA policy";
117                 LOGGER.warn(errorMessage);
118                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
119             }
120
121             final LegacyGuardPolicyContent content = new LegacyGuardPolicyContent();
122             // @formatter:off
123             content.setActor(           toscaPolicy.getProperties().get("actor"));
124             content.setClname(          toscaPolicy.getProperties().get("clname"));
125             content.setGuardActiveEnd(  toscaPolicy.getProperties().get("guardActiveEnd"));
126             content.setGuardActiveStart(toscaPolicy.getProperties().get("guardActiveStart"));
127             content.setLimit(           toscaPolicy.getProperties().get("limit"));
128             content.setMax(             toscaPolicy.getProperties().get("max"));
129             content.setMin(             toscaPolicy.getProperties().get("min"));
130             content.setRecipe(          toscaPolicy.getProperties().get("recipe"));
131             content.setTargets(         toscaPolicy.getProperties().get("targets"));
132             content.setTimeUnits(       toscaPolicy.getProperties().get("timeUnits"));
133             content.setTimeWindow(      toscaPolicy.getProperties().get("timeWindow"));
134             // @formatter:on
135
136             final Map<String, LegacyGuardPolicyContent> propertiesMap = new LinkedHashMap<>();
137             propertiesMap.put("content", content);
138             legacyGuardPolicyOutput.setProperties(propertiesMap);
139
140             if (toscaPolicy.getProperties() == null) {
141                 String errorMessage = "property \"Content\" not defined on TOSCA policy";
142                 LOGGER.warn(errorMessage);
143                 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
144             }
145
146             legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput);
147         }
148
149         return legacyGuardPolicyOutputMap;
150     }
151 }