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.HashMap;
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;
36 * This class maps a legacy operational policy to and from a TOSCA service template.
38 * @author Liam Fallon (liam.fallon@est.tech)
40 public class LegacyOperationalPolicyMapper
41 implements ToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
43 // TODO: Do this correctly with an atomic integer
44 private static int nextVersion = 1;
47 public ToscaServiceTemplate toToscaServiceTemplate(LegacyOperationalPolicy legacyOperationalPolicy) {
48 PfConceptKey policyKey =
49 new PfConceptKey(legacyOperationalPolicy.getPolicyId(), getNextVersion());
51 ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey);
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"));
57 Map<String, String> propertyMap = new HashMap<>();
58 toscaPolicy.setProperties(propertyMap);
59 toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent());
61 PfConceptKey serviceTemplateKey = new PfConceptKey("ServiceTemplate", "1.0.2");
62 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey);
63 serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
65 PfReferenceKey topologyTemplateKey = new PfReferenceKey(serviceTemplateKey, "TopolocyTemplate");
66 serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate(topologyTemplateKey));
68 PfConceptKey policiesKey = new PfConceptKey("Policies", "1.0.3");
69 serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies(policiesKey));
70 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
72 return serviceTemplate;
76 public LegacyOperationalPolicy fromToscaServiceTemplate(ToscaServiceTemplate serviceTemplate) {
77 // TODO Auto-generated method stub
82 * Get the next policy version.
84 * @return the next version
86 private static String getNextVersion() {
87 return "1.0." + nextVersion++;