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 javax.ws.rs.core.Response;
 
  28 import org.onap.policy.common.utils.coder.CoderException;
 
  29 import org.onap.policy.common.utils.coder.StandardCoder;
 
  30 import org.onap.policy.models.base.PfConceptKey;
 
  31 import org.onap.policy.models.base.PfModelRuntimeException;
 
  32 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
 
  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 operational policy to and from a TOSCA service template.
 
  45  * @author Liam Fallon (liam.fallon@est.tech)
 
  47 public class LegacyOperationalPolicyMapper
 
  48         implements JpaToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
 
  50     // Property name for the operational policy content
 
  51     private static final String CONTENT_PROPERTY = "content";
 
  53     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
 
  55     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
 
  56             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
 
  59     public JpaToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
 
  60         String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
 
  61         if (incomingVersion == null) {
 
  62             incomingVersion = "1";
 
  65         PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
 
  67         final JpaToscaPolicy toscaPolicy = new JpaToscaPolicy(policyKey);
 
  69         toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
 
  71         final Map<String, String> propertyMap = new HashMap<>();
 
  72         toscaPolicy.setProperties(propertyMap);
 
  74             toscaPolicy.getProperties().put(CONTENT_PROPERTY,
 
  75                     new StandardCoder().encode(legacyOperationalPolicy.getContent()));
 
  76         } catch (CoderException ce) {
 
  77             String errorMessage = "encoding of property \"content\" to JSON failed";
 
  78             LOGGER.warn(errorMessage, ce);
 
  79             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
 
  82         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
 
  83         serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
 
  85         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
 
  87         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
 
  88         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
 
  90         return serviceTemplate;
 
  94     public LegacyOperationalPolicy fromToscaServiceTemplate(final JpaToscaServiceTemplate serviceTemplate) {
 
  95         ToscaUtils.assertPoliciesExist(serviceTemplate);
 
  97         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
 
  98             String errorMessage = "more than one policy found in service template";
 
  99             LOGGER.warn(errorMessage);
 
 100             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
 
 104         final JpaToscaPolicy toscaPolicy =
 
 105                 serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
 
 107         final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
 
 108         legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
 
 109         legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
 
 111         if (toscaPolicy.getProperties() == null) {
 
 112             String errorMessage = "no properties defined on TOSCA policy";
 
 113             LOGGER.warn(errorMessage);
 
 114             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
 
 117         String content = null;
 
 119             content = new StandardCoder().decode(toscaPolicy.getProperties().get(CONTENT_PROPERTY), String.class);
 
 120         } catch (CoderException ce) {
 
 121             String errorMessage = "decoding of property \"content\" from JSON failed";
 
 122             LOGGER.warn(errorMessage, ce);
 
 123             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
 
 126         if (content == null) {
 
 127             String errorMessage = "property \"content\" not defined on TOSCA policy";
 
 128             LOGGER.warn(errorMessage);
 
 129             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
 
 132         legacyOperationalPolicy.setContent(content);
 
 134         return legacyOperationalPolicy;