Merge "Add models-pdp to models repo"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / mapping / LegacyOperationalPolicyMapper.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.HashMap;
24 import java.util.Map;
25
26 import org.onap.policy.models.base.PfConceptKey;
27 import org.onap.policy.models.base.PfReferenceKey;
28 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
29 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
30 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
31 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
32 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
33 import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper;
34
35 /**
36  * This class maps a legacy operational policy to and from a TOSCA service template.
37  *
38  * @author Liam Fallon (liam.fallon@est.tech)
39  */
40 public class LegacyOperationalPolicyMapper
41         implements ToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
42
43     // TODO: Do this correctly with an atomic integer
44     private static int nextVersion = 1;
45
46     @Override
47     public ToscaServiceTemplate toToscaServiceTemplate(LegacyOperationalPolicy legacyOperationalPolicy) {
48         PfConceptKey policyKey =
49                 new PfConceptKey(legacyOperationalPolicy.getPolicyId(), getNextVersion());
50
51         ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey);
52
53         // TODO: Find out how to parse the PolicyType from the content
54         // TODO: Check if this is the correct way to set the policy type version
55         toscaPolicy.setType(new PfConceptKey("SomeDerivedPolicyType", "1.0.1"));
56
57         Map<String, String> propertyMap = new HashMap<>();
58         toscaPolicy.setProperties(propertyMap);
59         toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent());
60
61         PfConceptKey serviceTemplateKey = new PfConceptKey("ServiceTemplate", "1.0.2");
62         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey);
63         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
64
65         PfReferenceKey topologyTemplateKey = new PfReferenceKey(serviceTemplateKey, "TopolocyTemplate");
66         serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate(topologyTemplateKey));
67
68         PfConceptKey policiesKey = new PfConceptKey("Policies", "1.0.3");
69         serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies(policiesKey));
70         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
71
72         return serviceTemplate;
73     }
74
75     @Override
76     public LegacyOperationalPolicy fromToscaServiceTemplate(ToscaServiceTemplate serviceTemplate) {
77         // TODO Auto-generated method stub
78         return null;
79     }
80
81     /**
82      * Get the next policy version.
83      *
84      * @return the next version
85      */
86     private static String getNextVersion() {
87         return "1.0." + nextVersion++;
88     }
89 }