Update to latest common/models released versions
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brms / api / BrmsPush.java
index 8181c00..2aa2873 100644 (file)
@@ -2,15 +2,16 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Engine
  * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -25,7 +26,6 @@ import com.att.nsa.cambria.client.CambriaBatchingPublisher;
 import com.att.nsa.cambria.client.CambriaClientBuilders;
 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
 import com.fasterxml.jackson.core.JsonProcessingException;
-
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
@@ -53,14 +53,12 @@ import java.util.concurrent.TimeUnit;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.regex.Pattern;
-
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
 import javax.persistence.TypedQuery;
 import javax.ws.rs.ProcessingException;
-
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.maven.model.Dependency;
@@ -74,7 +72,6 @@ import org.apache.maven.shared.invoker.InvocationRequest;
 import org.apache.maven.shared.invoker.InvocationResult;
 import org.apache.maven.shared.invoker.Invoker;
 import org.codehaus.plexus.util.IOUtil;
-
 import org.codehaus.plexus.util.WriterFactory;
 import org.eclipse.persistence.config.PersistenceUnitProperties;
 import org.onap.policy.api.PEDependency;
@@ -94,13 +91,14 @@ import org.onap.policy.common.logging.flexlogger.Logger;
 import org.onap.policy.utils.BackUpHandler;
 import org.onap.policy.utils.BackUpMonitor;
 import org.onap.policy.utils.BusPublisher;
+import org.onap.policy.utils.PeCryptoUtils;
 import org.onap.policy.utils.PolicyUtils;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 
 /**
  * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR).
  * Mavenize and push policy to PR
- * 
+ *
  * @version 1.0
  */
 
@@ -109,7 +107,7 @@ public class BrmsPush {
     private static final String DROOLS_APPS_TEMPLATE_GROUP =
             "org.onap.policy.drools-applications.controlloop.templates";
     private static final String DROOLS_APPS_MODEL_GROUP =
-            "org.onap.policy.drools-applications.controlloop.common.model-impl";
+            "org.onap.policy.models.policy-models-interactions.model-impl";
     private static final String META_INF = "META-INF";
     private static final String KMODULE_XML_FILE = "kmodule.xml";
     private static final String POM_XML_FILE = "pom.xml";
@@ -119,9 +117,10 @@ public class BrmsPush {
     private static final Logger LOGGER = FlexLogger.getLogger(BrmsPush.class.getName());
     private static final String PROJECTSLOCATION = "RuleProjects";
     private static final String[] GOALS = {"clean", "deploy"};
-    private static final String DEFAULT_VERSION = "1.3.0-SNAPSHOT";
+    private static final String DEFAULT_VERSION = "1.5.1";
     private static final String DEPENDENCY_FILE = "dependency.json";
-    private static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml";
+    private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key";
+    public static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml";
 
     private static Map<String, String> modifiedGroups = new HashMap<>();
     private static IntegrityMonitor im;
@@ -151,7 +150,7 @@ public class BrmsPush {
 
     /**
      * Responsible to push policies to the BRMS PDP Policy Repository (PR).
-     * 
+     *
      * @param propertiesFile the properties file
      * @param handler the {@link BackUpHandler}
      * @throws PolicyException PolicyException related to the operation
@@ -188,6 +187,9 @@ public class BrmsPush {
             throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
                     + "Data/File Read Error while reading from the property file.");
         }
+        // init the aes key from prop or env
+        PeCryptoUtils.initAesKey(config.getProperty(PROP_AES_KEY));
+
         LOGGER.info("Trying to set up IntegrityMonitor");
         String resourceName = null;
         try {
@@ -255,7 +257,7 @@ public class BrmsPush {
             repUrlList.add(repUrl);
         }
         repUserName = config.getProperty("repositoryUsername");
-        repPassword = config.getProperty("repositoryPassword");
+        repPassword = PeCryptoUtils.decrypt(config.getProperty("repositoryPassword"));
         if (repUserName == null || repPassword == null) {
             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
                     + "repostoryUserName and respositoryPassword properties are required.");
@@ -415,7 +417,7 @@ public class BrmsPush {
             final ArrayList<PEDependency> userDependencies = new ArrayList<>();
             for (final Map.Entry<String, String> entry : responseAttributes.entrySet()) {
                 final String key = entry.getKey();
-                String value = entry.getValue();
+                final String value = entry.getValue();
                 if (key.equals(policyKeyId)) {
                     selectedName = value;
                 }
@@ -462,14 +464,13 @@ public class BrmsPush {
         }
     }
 
-    private String getUserControllerName(String key, String value) {
+    private String getUserControllerName(final String key, final String value) {
         String userControllerName = null;
         // Check User Specific values.
         try {
             final PEDependency dependency = PolicyUtils.jsonStringToObject(value, PEDependency.class);
             userControllerName = key.replaceFirst("$controller:", "");
-            LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - "
-                    + dependency);
+            LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - " + dependency);
             addToGroup(userControllerName, dependency);
         } catch (final Exception e) {
             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e);
@@ -477,16 +478,15 @@ public class BrmsPush {
         return userControllerName;
     }
 
-    private void updateUserDependencies(ArrayList<PEDependency> userDependencies, String value) {
-        //update the user dependencies supplied as parameter to this method
+    private void updateUserDependencies(final ArrayList<PEDependency> userDependencies, String value) {
+        // update the user dependencies supplied as parameter to this method
         value = value.substring(1, value.length() - 1).trim();
         final List<String> dependencyStrings = Arrays.asList(value.split(Pattern.quote("},{")));
         for (final String dependencyString : dependencyStrings) {
             try {
                 userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class));
             } catch (final Exception e) {
-                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Dependencies: "
-                        + e);
+                LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Dependencies: " + e);
             }
         }
     }
@@ -524,8 +524,10 @@ public class BrmsPush {
             LOGGER.info("Updated Local Memory values with values from database.");
         } catch (final Exception exception) {
             LOGGER.error("Unable to sync group info", exception);
-            et.rollback();
-            throw exception;
+            if (et.isActive()) {
+                et.rollback();
+            }
+
         }
     }
 
@@ -584,7 +586,6 @@ public class BrmsPush {
         } catch (final Exception exception) {
             LOGGER.error("Unable add policy to database", exception);
             et.rollback();
-            throw exception;
         }
     }
 
@@ -643,13 +644,13 @@ public class BrmsPush {
         }
     }
 
-    private void parseJarContents(String artifactId, JarFile jar, Enumeration<?> enumEntries) {
+    private void parseJarContents(final String artifactId, final JarFile jar, final Enumeration<?> enumEntries) {
         final JarEntry jarEntry = (JarEntry) enumEntries.nextElement();
         File file = null;
         final String fileName = jarEntry.getName().substring(jarEntry.getName().lastIndexOf("/") + 1);
         if (jarEntry.getName().endsWith(".drl")) {
-            final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src"
-                    + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES;
+            final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
+                    + "main" + File.separator + RESOURCES + File.separator + RULES;
             new File(path).mkdirs();
             if (syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))) {
                 file = new File(path + File.separator + fileName);
@@ -661,14 +662,13 @@ public class BrmsPush {
             new File(path).mkdirs();
             file = new File(path + File.separator + fileName);
         } else if (jarEntry.getName().endsWith(KMODULE_XML_FILE)) {
-            final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src"
-                    + File.separator + "main" + File.separator + RESOURCES + File.separator + META_INF;
+            final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
+                    + "main" + File.separator + RESOURCES + File.separator + META_INF;
             new File(path).mkdirs();
             file = new File(path + File.separator + fileName);
         }
         if (file != null) {
-            try (InputStream is = jar.getInputStream(jarEntry);
-                 FileOutputStream fos = new FileOutputStream(file)) {
+            try (InputStream is = jar.getInputStream(jarEntry); FileOutputStream fos = new FileOutputStream(file)) {
                 while (is.available() > 0) {
                     fos.write(is.read());
                 }
@@ -775,7 +775,7 @@ public class BrmsPush {
 
     /**
      * Will Push policies to the PolicyRepo.
-     * 
+     *
      * @throws PolicyException PolicyException related to the operation
      */
     public void pushRules() throws PolicyException {
@@ -1005,7 +1005,7 @@ public class BrmsPush {
 
     /**
      * Default Dependency Section. Can be changed as required.
-     * 
+     *
      * @param controllerName the controller name
      * @return changed dependency list
      */
@@ -1049,12 +1049,6 @@ public class BrmsPush {
         msoDependency.setArtifactId("controlloop.common.model-impl.so");
         msoDependency.setVersion(version);
         dependencyList.add(msoDependency);
-
-        final Dependency trafficgeneratorDependency = new Dependency();
-        trafficgeneratorDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
-        trafficgeneratorDependency.setArtifactId("controlloop.common.model-impl.trafficgenerator");
-        trafficgeneratorDependency.setVersion(version);
-        dependencyList.add(trafficgeneratorDependency);
         return dependencyList;
     }
 
@@ -1151,7 +1145,6 @@ public class BrmsPush {
         } catch (final Exception exception) {
             LOGGER.error("Unable add/update policy group to database for controller name: " + name, exception);
             et.rollback();
-            throw exception;
         }
     }
 
@@ -1207,7 +1200,6 @@ public class BrmsPush {
         } catch (final Exception exception) {
             LOGGER.error("Unable remove policy from group to database for policy name: " + policyName, exception);
             et.rollback();
-            throw exception;
         }
     }
 
@@ -1247,7 +1239,7 @@ public class BrmsPush {
 
     /**
      * Get URL List Size.
-     * 
+     *
      * @return URL list size
      */
     public int urlListSize() {