Add optional controllerName in Op. Legacy Policies
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / mapping / LegacyOperationalPolicyMapper.java
index 2dddda2..881a5fa 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
+ *  Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,8 +26,6 @@ import java.util.Map;
 
 import javax.ws.rs.core.Response;
 
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
@@ -49,6 +48,7 @@ public class LegacyOperationalPolicyMapper
 
     // Property name for the operational policy content
     private static final String CONTENT_PROPERTY = "content";
+    private static final String CONTROLLER_PROPERTY = "controllerName";
 
     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
 
@@ -70,17 +70,13 @@ public class LegacyOperationalPolicyMapper
 
         final Map<String, String> propertyMap = new HashMap<>();
         toscaPolicy.setProperties(propertyMap);
-        try {
-            toscaPolicy.getProperties().put(CONTENT_PROPERTY,
-                    new StandardCoder().encode(legacyOperationalPolicy.getContent()));
-        } catch (CoderException ce) {
-            String errorMessage = "encoding of property \"content\" to JSON failed";
-            LOGGER.warn(errorMessage, ce);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
+        toscaPolicy.getProperties().put(CONTENT_PROPERTY, legacyOperationalPolicy.getContent());
+        if (legacyOperationalPolicy.getControllerName() != null) {
+            toscaPolicy.getProperties().put(CONTROLLER_PROPERTY, legacyOperationalPolicy.getControllerName());
         }
 
         final JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
-        serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
+        serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0");
 
         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
 
@@ -114,14 +110,7 @@ public class LegacyOperationalPolicyMapper
             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
         }
 
-        String content = null;
-        try {
-            content = new StandardCoder().decode(toscaPolicy.getProperties().get(CONTENT_PROPERTY), String.class);
-        } catch (CoderException ce) {
-            String errorMessage = "decoding of property \"content\" from JSON failed";
-            LOGGER.warn(errorMessage, ce);
-            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, ce);
-        }
+        String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY);
 
         if (content == null) {
             String errorMessage = "property \"content\" not defined on TOSCA policy";
@@ -131,6 +120,11 @@ public class LegacyOperationalPolicyMapper
 
         legacyOperationalPolicy.setContent(content);
 
+        String controllerName = toscaPolicy.getProperties().get(CONTROLLER_PROPERTY);
+        if (controllerName != null) {
+            legacyOperationalPolicy.setControllerName(controllerName);
+        }
+
         return legacyOperationalPolicy;
     }
 }