Sonar Fixes
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brmsInterface / BRMSPush.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.brmsInterface;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.Writer;
29 import java.net.URL;
30 import java.nio.channels.Channels;
31 import java.nio.channels.ReadableByteChannel;
32 import java.nio.charset.StandardCharsets;
33 import java.nio.file.Files;
34 import java.nio.file.Path;
35 import java.nio.file.Paths;
36 import java.security.GeneralSecurityException;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.Enumeration;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Properties;
45 import java.util.UUID;
46 import java.util.concurrent.TimeUnit;
47 import java.util.jar.JarEntry;
48 import java.util.jar.JarFile;
49
50 import javax.persistence.EntityManager;
51 import javax.persistence.EntityManagerFactory;
52 import javax.persistence.EntityTransaction;
53 import javax.persistence.Persistence;
54 import javax.persistence.Query;
55
56 import org.apache.commons.io.FileUtils;
57 import org.apache.commons.lang.StringEscapeUtils;
58 import org.apache.maven.model.Dependency;
59 import org.apache.maven.model.DeploymentRepository;
60 import org.apache.maven.model.DistributionManagement;
61 import org.apache.maven.model.Model;
62 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
63 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
64 import org.apache.maven.shared.invoker.DefaultInvoker;
65 import org.apache.maven.shared.invoker.InvocationRequest;
66 import org.apache.maven.shared.invoker.InvocationResult;
67 import org.apache.maven.shared.invoker.Invoker;
68 import org.codehaus.plexus.util.IOUtil;
69 import org.codehaus.plexus.util.WriterFactory;
70 import org.eclipse.persistence.config.PersistenceUnitProperties;
71 import org.onap.policy.api.PEDependency;
72 import org.onap.policy.api.PolicyException;
73 import org.onap.policy.brmsInterface.jpa.BRMSGroupInfo;
74 import org.onap.policy.brmsInterface.jpa.BRMSPolicyInfo;
75 import org.onap.policy.brmsInterface.jpa.DependencyInfo;
76 import org.onap.policy.common.im.AdministrativeStateException;
77 import org.onap.policy.common.im.IntegrityMonitor;
78 import org.onap.policy.common.logging.eelf.MessageCodes;
79 import org.onap.policy.common.logging.eelf.PolicyLogger;
80 import org.onap.policy.common.logging.flexlogger.FlexLogger;
81 import org.onap.policy.common.logging.flexlogger.Logger;
82 import org.onap.policy.utils.BackUpHandler;
83 import org.onap.policy.utils.BackUpMonitor;
84 import org.onap.policy.utils.BusPublisher;
85 import org.onap.policy.utils.PolicyUtils;
86 import org.onap.policy.xacml.api.XACMLErrorConstants;
87 import org.sonatype.nexus.client.NexusClient;
88 import org.sonatype.nexus.client.NexusClientException;
89 import org.sonatype.nexus.client.NexusConnectionException;
90 import org.sonatype.nexus.client.rest.NexusRestClient;
91 import org.sonatype.nexus.rest.model.NexusArtifact;
92
93 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
94 import com.att.nsa.cambria.client.CambriaClientBuilders;
95 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
96 import com.fasterxml.jackson.core.JsonProcessingException;
97
98 /**
99  * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR). Mavenize and push policy
100  * to PR
101  * 
102  * @version 1.0
103  */
104
105 @SuppressWarnings("deprecation")
106 public class BRMSPush {
107     private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName());
108     private static final String PROJECTSLOCATION = "RuleProjects";
109     private static final String[] GOALS = { "clean", "deploy" };
110     private static final String DEFAULT_VERSION = "1.1.0-SNAPSHOT";
111     private static final String DEPENDENCY_FILE = "dependency.json";
112
113     private static Map<String, String> modifiedGroups = new HashMap<>();
114     private static IntegrityMonitor im;
115     private static BackUpMonitor bm;
116     private String defaultName = null;
117     private String repID = null;
118     private String repName = null;
119     private ArrayList<String> repURLs = null;
120     private String repUserName = null;
121     private String repPassword = null;
122     private String policyKeyID = null;
123     private boolean createFlag = false;
124     private String uebList = null;
125     private List<String> dmaapList = null;
126     private String pubTopic = null;
127     private PublisherBuilder pubBuilder = null;
128     protected BusPublisher publisher = null;
129     private Long uebDelay = Long.parseLong("20");
130     private Long dmaapDelay = Long.parseLong("5000");
131     private String notificationType = null;
132     private ArrayList<ControllerPOJO> controllers;
133     private HashMap<String, ArrayList<Object>> groupMap = new HashMap<>();
134     private Map<String, String> policyMap = new HashMap<>();
135     private String brmsdependencyversion;
136     private EntityManager em;
137     private boolean syncFlag = false;
138
139     public BRMSPush(String propertiesFile, BackUpHandler handler) throws PolicyException {
140         Properties config = new Properties();
141         Path file = Paths.get(propertiesFile);
142         if (Files.notExists(file)) {
143             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Config File doesn't Exist in the specified Path "
144                     + file.toString());
145             throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
146                     + "Config File doesn't Exist in the specified Path " + file.toString());
147         } else {
148             if (file.toString().endsWith(".properties")) {
149                 // Grab the Properties.
150                 setProperty(file, config, handler);
151             }
152         }
153     }
154
155     private void setProperty(Path file, Properties config, BackUpHandler handler) throws PolicyException {
156         InputStream in;
157         try {
158             in = new FileInputStream(file.toFile());
159             config.load(in);
160         } catch (IOException e) {
161             LOGGER.error(
162                     XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file.",
163                     e);
164             throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
165                     + "Data/File Read Error while reading from the property file.");
166         }
167         LOGGER.info("Trying to set up IntegrityMonitor");
168         String resourceName = null;
169         try {
170             LOGGER.info("Trying to set up IntegrityMonitor");
171             resourceName = config.getProperty("RESOURCE_NAME");
172             if (resourceName == null) {
173                 LOGGER.warn("RESOURCE_NAME is missing setting default value. ");
174                 resourceName = "brmsgw_pdp01";
175             }
176             resourceName = resourceName.trim();
177             setIntegrityMonitor(IntegrityMonitor.getInstance(resourceName, config));
178         } catch (Exception e) {
179             LOGGER.error("Error starting Integerity Monitor: " + e);
180         }
181         LOGGER.info("Trying to set up BackUpMonitor");
182         try {
183             setBackupMonitor(BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config,
184                     handler));
185         } catch (Exception e) {
186             LOGGER.error("Error starting BackUpMonitor: " + e);
187         }
188         config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml");
189         EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config);
190         em = emf.createEntityManager();
191         defaultName = config.getProperty("defaultName");
192         if (defaultName == null) {
193             LOGGER.error(
194                     XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file ");
195             throw new PolicyException(
196                     XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file");
197         }
198         defaultName = defaultName.trim();
199         repID = config.getProperty("repositoryID");
200         if (repID == null) {
201             LOGGER.error(
202                     XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
203             throw new PolicyException(
204                     XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
205         }
206         repID = repID.trim();
207         repName = config.getProperty("repositoryName");
208         if (repName == null) {
209             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
210                     + "repositoryName property is missing from the property file ");
211             throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
212                     + "repositoryName property is missing from the property file ");
213         }
214         repName = repName.trim();
215         String repURL = config.getProperty("repositoryURL");
216         if (repURL == null) {
217             LOGGER.error(
218                     XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
219             throw new PolicyException(
220                     XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
221         }
222         if (repURL.contains(",")) {
223             repURLs = new ArrayList<>(Arrays.asList(repURL.trim().split(",")));
224         } else {
225             repURLs = new ArrayList<>();
226             repURLs.add(repURL);
227         }
228         repUserName = config.getProperty("repositoryUsername");
229         repPassword = config.getProperty("repositoryPassword");
230         if (repUserName == null || repPassword == null) {
231             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
232                     + "repostoryUserName and respositoryPassword properties are required.");
233             throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
234                     + "repostoryUserName and respositoryPassword properties are required.");
235         }
236         repUserName = repUserName.trim();
237         repPassword = repPassword.trim();
238         policyKeyID = config.getProperty("policyKeyID");
239         if (policyKeyID == null) {
240             LOGGER.error(
241                     XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
242             throw new PolicyException(
243                     XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
244         }
245         policyKeyID = policyKeyID.trim();
246         String syncF = config.getProperty("sync", "false").trim();
247         syncFlag = Boolean.parseBoolean(syncF);
248         if (syncFlag) {
249             PolicyLogger.info("SYNC Flag is turned ON. DB will be given Priority.");
250         }
251         brmsdependencyversion = config.getProperty("brms.dependency.version");
252         if (brmsdependencyversion == null) {
253             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
254                     + "brmsdependencyversion property is missing from the property file, Using default Version.");
255             brmsdependencyversion = DEFAULT_VERSION;
256         }
257         brmsdependencyversion = brmsdependencyversion.trim();
258         readGroups(config);
259
260         // Setup Publisher
261         notificationType = config.getProperty("NOTIFICATION_TYPE");
262         if ("dmaap".equalsIgnoreCase(notificationType)) {
263
264             LOGGER.info("Notification Type being used is DMaaP... creating instance of BusPublisher.");
265             // Setting up the Publisher for DMaaP MR
266             String dmaapServers = config.getProperty("NOTIFICATION_SERVERS");
267             pubTopic = config.getProperty("NOTIFICATION_TOPIC");
268             String aafLogin = config.getProperty("CLIENT_ID").trim();
269             String aafPassword = config.getProperty("CLIENT_KEY").trim();
270
271             if (dmaapServers == null || pubTopic == null) {
272                 LOGGER.error(
273                         XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
274                 throw new PolicyException(
275                         XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
276             }
277
278             dmaapServers = dmaapServers.trim();
279             pubTopic = pubTopic.trim();
280
281             if (dmaapServers.contains(",")) {
282                 dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
283             } else {
284                 dmaapList = new ArrayList<>();
285                 dmaapList.add(dmaapServers);
286             }
287
288             this.publisher = new BusPublisher.DmaapPublisherWrapper(this.dmaapList, this.pubTopic, aafLogin,
289                     aafPassword);
290
291             String dDelay = config.getProperty("NOTIFICATION_DELAY");
292             if (dDelay != null && !dDelay.isEmpty()) {
293                 dDelay = dDelay.trim();
294                 try {
295                     dmaapDelay = Long.parseLong(dDelay);
296                 } catch (NumberFormatException e) {
297                     LOGGER.error("DMAAP_DELAY not a long format number" + e);
298                 }
299             }
300             LOGGER.info("DMAAP BusPublisher is created.");
301
302         } else {
303             LOGGER.info("Notification Type being used is UEB... creating instance of PublisherBuilder.");
304             // Setting up the Publisher for UEB
305             uebList = config.getProperty("NOTIFICATION_SERVERS");
306             pubTopic = config.getProperty("NOTIFICATION_TOPIC");
307             String apiKey = config.getProperty("UEB_API_KEY");
308             String apiSecret = config.getProperty("UEB_API_SECRET");
309             if (uebList == null || pubTopic == null) {
310                 LOGGER.error(
311                         XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
312                 throw new PolicyException(
313                         XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
314             }
315             uebList = uebList.trim();
316             pubTopic = pubTopic.trim();
317             pubBuilder = new CambriaClientBuilders.PublisherBuilder();
318             pubBuilder.usingHosts(uebList).onTopic(pubTopic);
319             if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) {
320                 apiKey = apiKey.trim();
321                 apiSecret = apiSecret.trim();
322                 pubBuilder.authenticatedBy(apiKey, apiSecret);
323             }
324             String uDelay = config.getProperty("NOTIFICATION_DELAY");
325             if (uDelay != null && !uDelay.isEmpty()) {
326                 uDelay = uDelay.trim();
327                 try {
328                     uebDelay = Long.parseLong(uDelay);
329                 } catch (NumberFormatException e) {
330                     LOGGER.error("UEB_DELAY not a long format number" + e);
331                 }
332             }
333             LOGGER.info("UEB PublisherBuilder is created.");
334
335         }
336
337     }
338
339     private static void setBackupMonitor(BackUpMonitor instance) {
340         bm = instance;
341     }
342
343     private static void setIntegrityMonitor(IntegrityMonitor instance) {
344         im = instance;
345     }
346
347     /**
348      * Will Initialize the variables required for BRMSPush.
349      */
350     public void initiate(boolean flag) {
351         resetModifiedGroups();
352         controllers = new ArrayList<>();
353         try {
354             bm.updateNotification();
355         } catch (Exception e) {
356             LOGGER.error("Error while updating Notification: " + e.getMessage(), e);
357         }
358         if (flag)
359             syncGroupInfo();
360     }
361
362     private static void resetModifiedGroups() {
363         modifiedGroups = new HashMap<>();
364     }
365
366     /**
367      * Will Add rules to projects. Creates necessary folders if required.
368      */
369     public void addRule(String name, String rule, Map<String, String> responseAttributes) {
370         // 1 check the response Attributes and determine if this belongs to any projects.
371         // 2 if not create folder
372         // 3 create pom.
373         // 4 copy the rule.
374         // 5 store the groups that have been updated.
375         String kSessionName = null;
376         String selectedName = null;
377         if (!responseAttributes.isEmpty()) {
378             // Pick selected Value
379             String userControllerName = null;
380             ArrayList<PEDependency> userDependencies = new ArrayList<>();
381             for (String key : responseAttributes.keySet()) {
382                 if (key.equals(policyKeyID)) {
383                     selectedName = responseAttributes.get(key);
384                 }
385                 // kmodule configurations
386                 else if (key.equals("kSessionName")) {
387                     kSessionName = responseAttributes.get(key);
388                 }
389                 // Check User Specific values.
390                 if (key.equals("$controller:")) {
391                     try {
392                         PEDependency dependency = PolicyUtils.jsonStringToObject(responseAttributes.get(key),
393                                 PEDependency.class);
394                         userControllerName = key.replaceFirst("$controller:", "");
395                         addToGroup(userControllerName, dependency);
396                     } catch (Exception e) {
397                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e);
398                     }
399
400                 } else if (key.equals("$dependency$")) {
401                     String value = responseAttributes.get(key);
402                     if (value.startsWith("[") && value.endsWith("]")) {
403                         value = value.substring(1, value.length() - 1).trim();
404                         List<String> dependencyStrings = Arrays.asList(value.split("},{"));
405                         for (String dependencyString : dependencyStrings) {
406                             try {
407                                 userDependencies
408                                         .add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class));
409                             } catch (Exception e) {
410                                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
411                                         + "Error while resolving Dependencies: " + e);
412                             }
413                         }
414                     }
415                 }
416             }
417             if (userControllerName != null) {
418                 // Adding custom dependencies here.
419                 ArrayList<Object> values = groupMap.get(userControllerName);
420                 values.add(userDependencies);
421                 groupMap.put(userControllerName, values);
422                 selectedName = userControllerName;
423             }
424         }
425         // If no Match then pick Default.
426         if (selectedName == null) {
427             selectedName = defaultName;
428         }
429         if (groupMap.containsKey(selectedName)) {
430             // If the key is not got as parameters set by the user, setting the default value for kSessionName as
431             // closedLoop
432             if (kSessionName == null) {
433                 if (selectedName == defaultName) {
434                     kSessionName = "closedloop";
435                 } else {
436                     kSessionName = "closedloop-" + selectedName;
437                 }
438             }
439             // create directories if missing.
440             manageProject(selectedName, kSessionName, name, rule);
441             addModifiedGroup(selectedName, "update"); // Will check for Create Later after generating the Pom.
442         }
443     }
444
445     private void syncGroupInfo() {
446         // Sync DB to JMemory.
447         EntityTransaction et = em.getTransaction();
448         et.begin();
449         Query query = em.createQuery("select b from BRMSGroupInfo AS b");
450         List<?> bList = query.getResultList();
451         if (bList.size() != groupMap.size()) {
452             for (Object value : bList) {
453                 BRMSGroupInfo brmsGroupInfo = (BRMSGroupInfo) value;
454                 PEDependency dependency = new PEDependency();
455                 dependency.setArtifactId(brmsGroupInfo.getArtifactId());
456                 dependency.setGroupId(brmsGroupInfo.getGroupId());
457                 dependency.setVersion(brmsGroupInfo.getVersion());
458                 ArrayList<Object> values = new ArrayList<>();
459                 values.add(dependency);
460                 groupMap.put(brmsGroupInfo.getControllerName(), values);
461             }
462         }
463         query = em.createQuery("select g from BRMSPolicyInfo AS g");
464         bList = query.getResultList();
465         if (bList.size() != policyMap.size()) {
466             for (Object value : bList) {
467                 BRMSPolicyInfo brmsPolicyInfo = (BRMSPolicyInfo) value;
468                 policyMap.put(brmsPolicyInfo.getPolicyName(), brmsPolicyInfo.getControllerName().getControllerName());
469             }
470         }
471         et.commit();
472         LOGGER.info("Updated Local Memory values with values from database.");
473     }
474
475     private void manageProject(String selectedName, String kSessionName, String name, String rule) {
476         // Check if the Project is in Sync. If not get the latest Version.
477         syncProject(selectedName);
478         createProject(PROJECTSLOCATION + File.separator + getArtifactID(selectedName) + File.separator + "src"
479                 + File.separator + "main" + File.separator + "resources", kSessionName);
480         copyDataToFile(PROJECTSLOCATION + File.separator + getArtifactID(selectedName) + File.separator + "src"
481                 + File.separator + "main" + File.separator + "resources" + File.separator + "rules" + File.separator
482                 + name + ".drl", rule);
483         addToPolicy(name, selectedName);
484     }
485
486     /*
487      * Add Policy to JMemory and DataBase.
488      */
489     private void addToPolicy(String policyName, String controllerName) {
490         policyMap.put(policyName, controllerName);
491         EntityTransaction et = em.getTransaction();
492         et.begin();
493         Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn");
494         query.setParameter("pn", policyName);
495         List<?> pList = query.getResultList();
496         boolean createFlag = false;
497         BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo();
498         if (pList.size() > 0) {
499             // Already exists.
500             brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
501             if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) {
502                 createFlag = true;
503             }
504         } else {
505             createFlag = true;
506         }
507         if (createFlag) {
508             query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn");
509             query.setParameter("cn", controllerName);
510             List<?> bList = query.getResultList();
511             BRMSGroupInfo brmsGroupInfo = new BRMSGroupInfo();
512             if (bList.size() > 0) {
513                 brmsGroupInfo = (BRMSGroupInfo) bList.get(0);
514             }
515             brmsPolicyInfo.setPolicyName(policyName);
516             brmsPolicyInfo.setControllerName(brmsGroupInfo);
517             em.persist(brmsPolicyInfo);
518             em.flush();
519         }
520         et.commit();
521     }
522
523     private void syncProject(String selectedName) {
524         boolean projectExists = checkProject(selectedName);
525         if (projectExists) {
526             String version = null;
527             version = getVersion(selectedName);
528             if (version == null) {
529                 LOGGER.error("Error getting local version for the given Controller Name:" + selectedName
530                         + " going with Default value");
531                 version = "0.1.0";
532             }
533             String nextVersion = incrementVersion(version);
534             boolean outOfSync = checkRemoteSync(selectedName, nextVersion);
535             if (!outOfSync) {
536                 return;
537             }
538         }
539         // We are out of Sync or Project is not Present.
540         downloadProject(selectedName);
541     }
542
543     private void downloadProject(String selectedName) {
544         NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
545         if (artifact == null)
546             return;
547         String dirName = getDirectoryName(selectedName);
548         URL website;
549         String fileName = "rule.jar";
550         try {
551             website = new URL(artifact.getResourceURI());
552             ReadableByteChannel rbc = Channels.newChannel(website.openStream());
553             FileOutputStream fos = new FileOutputStream(fileName);
554             fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
555             fos.close();
556             extractJar(fileName, dirName);
557             new File(fileName).delete();
558         } catch (IOException e) {
559             LOGGER.error("Error while downloading the project to File System. " + e.getMessage(), e);
560         }
561     }
562
563     private void extractJar(String jarFileName, String artifactId) throws IOException {
564         JarFile jar = new JarFile(jarFileName);
565         Enumeration<?> enumEntries = jar.entries();
566         while (enumEntries.hasMoreElements()) {
567             JarEntry file = (JarEntry) enumEntries.nextElement();
568             File f = null;
569             String fileName = file.getName().substring(file.getName().lastIndexOf("/") + 1);
570             if (file.getName().endsWith(".drl")) {
571                 String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
572                         + "main" + File.separator + "resources" + File.separator + "rules";
573                 new File(path).mkdirs();
574                 if (syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))) {
575                     f = new File(path + File.separator + fileName);
576                 } else {
577                     f = new File(path + File.separator + fileName);
578                 }
579             } else if (file.getName().endsWith("pom.xml")) {
580                 String path = PROJECTSLOCATION + File.separator + artifactId;
581                 new File(path).mkdirs();
582                 f = new File(path + File.separator + fileName);
583             } else if (file.getName().endsWith("kmodule.xml")) {
584                 String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
585                         + "main" + File.separator + "resources" + File.separator + "META-INF";
586                 new File(path).mkdirs();
587                 f = new File(path + File.separator + fileName);
588             }
589             if (f != null) {
590                 InputStream is = jar.getInputStream(file);
591                 FileOutputStream fos = new FileOutputStream(f);
592                 while (is.available() > 0) {
593                     fos.write(is.read());
594                 }
595                 fos.close();
596                 is.close();
597                 f = null;
598                 LOGGER.info(fileName + " Created..");
599             }
600         }
601         jar.close();
602     }
603
604     private NexusArtifact getLatestArtifactFromNexus(String selectedName) {
605         List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, null);
606         int bigNum = 0;
607         int smallNum = 0;
608         NexusArtifact result = null;
609         for (NexusArtifact artifact : artifacts) {
610             int majorVal = Integer.parseInt(artifact.getVersion().substring(0, artifact.getVersion().indexOf(".")));
611             int minorVal = Integer.parseInt(artifact.getVersion().substring(artifact.getVersion().indexOf(".") + 1,
612                     artifact.getVersion().lastIndexOf(".")));
613             if (majorVal > bigNum) {
614                 bigNum = majorVal;
615                 smallNum = minorVal;
616             }
617             if ((bigNum == majorVal) && (minorVal > smallNum)) {
618                 smallNum = minorVal;
619             }
620             if (bigNum == majorVal && minorVal == smallNum) {
621                 result = artifact;
622             }
623         }
624         return additionalNexusLatestCheck(selectedName, result);
625     }
626
627     // Additional Check due to Limitations from Nexus API to check if the artifact is the latest.
628     private NexusArtifact additionalNexusLatestCheck(String selectedName, NexusArtifact result) {
629         if(result==null){
630             return result;
631         }
632         String nextVersion = incrementVersion(result.getVersion());
633         List<NexusArtifact> artifact = getArtifactFromNexus(selectedName, nextVersion);
634         return artifact.isEmpty() ? result : additionalNexusLatestCheck(selectedName, artifact.get(0));
635     }
636
637     private boolean checkRemoteSync(String selectedName, String version) {
638         List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, version);
639         return (artifacts.size() == 0) ? false : true;
640     }
641
642     private List<NexusArtifact> getArtifactFromNexus(String selectedName, String version) {
643         final NexusClient client = new NexusRestClient();
644         int i = 0;
645         boolean flag = false;
646         while (i < repURLs.size()) {
647             try {
648                 String repURL = repURLs.get(0);
649                 client.connect(repURL.substring(0, repURL.indexOf(repURL.split(":[0-9]+\\/nexus")[1])), repUserName,
650                         repPassword);
651                 final NexusArtifact template = new NexusArtifact();
652                 template.setGroupId(getGroupID(selectedName));
653                 template.setArtifactId(getArtifactID(selectedName));
654                 if (version != null) {
655                     template.setVersion(version);
656                 }
657                 List<NexusArtifact> resultList = client.searchByGAV(template);
658                 if (resultList != null) {
659                     flag = true;
660                     return resultList;
661                 }
662             } catch (NexusClientException | NexusConnectionException | NullPointerException e) {
663                 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. "
664                         + e.getMessage(), e);
665             } finally {
666                 try {
667                     client.disconnect();
668                 } catch (NexusClientException | NexusConnectionException e) {
669                     LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus."
670                             + e.getMessage(), e);
671                 }
672                 if (!flag) {
673                     Collections.rotate(repURLs, -1);
674                     i++;
675                 }
676             }
677         }
678         return new ArrayList<>();
679     }
680
681     private void setVersion(String selectedName) {
682         String newVersion = "0.1.0";
683         createFlag = false;
684         NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
685         if (artifact != null) {
686             newVersion = incrementVersion(artifact.getVersion());
687         }
688         if (newVersion.equals("0.1.0")) {
689             createFlag = true;
690         }
691         setVersion(newVersion, selectedName);
692         LOGGER.info("Controller: " + selectedName + "is on version: " + newVersion);
693     }
694
695     private String incrementVersion(String version) {
696         int majorVal = Integer.parseInt(version.substring(0, version.indexOf(".")));
697         int minorVal = Integer.parseInt(version.substring(version.indexOf(".") + 1, version.lastIndexOf(".")));
698         if (minorVal >= 9) {
699             majorVal += 1;
700             minorVal = 0;
701         } else {
702             minorVal += 1;
703         }
704         return majorVal + "." + minorVal + version.substring(version.lastIndexOf("."));
705     }
706
707     private boolean checkProject(String selectedName) {
708         return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists();
709     }
710
711     private String getDirectoryName(String selectedName) {
712         return getArtifactID(selectedName);
713     }
714
715     /**
716      * Will Push policies to the PolicyRepo.
717      * 
718      * @param notificationType
719      *            <String> type of notification Type.
720      * @throws PolicyException
721      */
722     public void pushRules() throws PolicyException {
723         // Check how many groups have been updated.
724         // Invoke their Maven process.
725         try {
726             im.startTransaction();
727         } catch (AdministrativeStateException e) {
728             LOGGER.error("Error while starting Transaction " + e);
729         } catch (Exception e) {
730             LOGGER.error("Error while starting Transaction " + e);
731         }
732         if (!modifiedGroups.isEmpty()) {
733             Boolean flag = false;
734             for (String group : modifiedGroups.keySet()) {
735                 InvocationResult result = null;
736                 try {
737                     InvocationRequest request = new DefaultInvocationRequest();
738                     setVersion(group);
739                     createPom(group);
740                     request.setPomFile(new File(
741                             PROJECTSLOCATION + File.separator + getArtifactID(group) + File.separator + "pom.xml"));
742                     request.setGoals(Arrays.asList(GOALS));
743                     Invoker invoker = new DefaultInvoker();
744                     result = invoker.execute(request);
745                     if (result.getExecutionException() != null) {
746                         LOGGER.error(result.getExecutionException());
747                     } else if (result.getExitCode() != 0) {
748                         LOGGER.error("Maven Invocation failure..!");
749                     }
750                 } catch (Exception e) {
751                     LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation issue for "
752                             + getArtifactID(group) + e.getMessage(), e);
753                 }
754                 if (result != null && result.getExitCode() == 0) {
755                     LOGGER.info("Build Completed..!");
756                     if (createFlag) {
757                         addNotification(group, "create");
758                     } else {
759                         addNotification(group, modifiedGroups.get(group));
760                     }
761                     flag = true;
762                 } else {
763                     throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!");
764                 }
765             }
766             if (flag) {
767                 sendNotification(controllers);
768             }
769         }
770         if (im != null) {
771             im.endTransaction();
772         }
773     }
774
775     /**
776      * Removes a Rule from Rule Projects.
777      */
778     public void removeRule(String name) {
779         String controllerName = getGroupName(name);
780         if (controllerName == null) {
781             LOGGER.info("Error finding the controllerName for the given Policy: " + name);
782             return;
783         }
784         syncProject(controllerName);
785         getNameAndSetRemove(controllerName, name);
786     }
787
788     private String getGroupName(String name) {
789         if (policyMap.containsKey(name)) {
790             return policyMap.get(name);
791         } else {
792             syncGroupInfo();
793             return (policyMap.containsKey(name)) ? policyMap.get(name) : null;
794         }
795     }
796
797     private void addModifiedGroup(String controllerName, String operation) {
798         if (controllerName != null) {
799             modifiedGroups.put(controllerName, operation);
800         }
801     }
802
803     private void addNotification(String controllerName, String operation) {
804         ControllerPOJO controllerPOJO = new ControllerPOJO();
805         controllerPOJO.setName(controllerName);
806         controllerPOJO.setOperation(operation);
807         HashMap<String, String> drools = new HashMap<>();
808         drools.put("groupId", getGroupID(controllerName));
809         drools.put("artifactId", getArtifactID(controllerName));
810         drools.put("version", getVersion(controllerName));
811         controllerPOJO.setDrools(drools);
812         controllers.add(controllerPOJO);
813         try {
814             LOGGER.debug("Notification added: " + PolicyUtils.objectToJsonString(controllerPOJO));
815         } catch (JsonProcessingException e) {
816             LOGGER.error(MessageCodes.ERROR_SCHEMA_INVALID + "Json Processing Error " + e);
817         }
818     }
819
820     private void removedRuleModifiedGroup(String controllerName) {
821         // This will be sending Notification to PDPD directly to Lock
822         ControllerPOJO controllerPOJO = new ControllerPOJO();
823         controllerPOJO.setName(controllerName);
824         controllerPOJO.setOperation("lock");
825         List<ControllerPOJO> controllers = new ArrayList<>();
826         controllers.add(controllerPOJO);
827         sendNotification(controllers);
828     }
829
830     private void sendNotification(List<ControllerPOJO> controllers) {
831         NotificationPOJO notification = new NotificationPOJO();
832         String requestId = UUID.randomUUID().toString();
833         LOGGER.info("Generating notification RequestID : " + requestId);
834         notification.setRequestID(requestId);
835         notification.setEntity("controller");
836         notification.setControllers(controllers);
837         try {
838             String notificationJson = PolicyUtils.objectToJsonString(notification);
839             LOGGER.info("Sending Notification :\n" + notificationJson);
840             sendMessage(notificationJson);
841         } catch (Exception e) {
842             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while sending notification to PDP-D "
843                     + e.getMessage(), e);
844         }
845     }
846
847     private void sendMessage(String message) throws IOException, GeneralSecurityException, InterruptedException {
848
849         if ("dmaap".equalsIgnoreCase(notificationType)) {
850             // Sending Message through DMaaP Message Router
851             LOGGER.debug("DMAAP Publishing Message");
852
853             publisher.send("MyPartitionKey", message);
854
855             LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic);
856
857             Thread.sleep(dmaapDelay);
858             publisher.close();
859         } else {
860             // Sending Message through UEB interface.
861             LOGGER.debug("UEB Publishing Message");
862
863             CambriaBatchingPublisher pub = pubBuilder.build();
864             pub.send("MyPartitionKey", message);
865
866             final List<?> stuck = pub.close(uebDelay, TimeUnit.SECONDS);
867             if (stuck.size() > 0) {
868                 LOGGER.error(stuck.size() + " messages unsent");
869             } else {
870                 LOGGER.debug("Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic);
871             }
872         }
873
874     }
875
876     private void createPom(String name) {
877         Model model = new Model();
878         model.setModelVersion("4.0.0");
879         model.setGroupId(getGroupID(name));
880         model.setArtifactId(getArtifactID(name));
881         model.setVersion(getVersion(name));
882         model.setName(name);
883         DistributionManagement distributionManagement = new DistributionManagement();
884         DeploymentRepository repository = new DeploymentRepository();
885         repository.setId(repID);
886         repository.setName(repName);
887         repository.setUrl(repURLs.get(0));
888         distributionManagement.setRepository(repository);
889         model.setDistributionManagement(distributionManagement);
890         // Dependency Management goes here.
891         List<Dependency> dependencyList = new ArrayList<>();
892         if (groupMap.get(name).size() > 1) {
893             @SuppressWarnings("unchecked")
894             ArrayList<PEDependency> dependencies = (ArrayList<PEDependency>) groupMap.get(name).get(1);
895             for (PEDependency dependency : dependencies) {
896                 dependencyList.add(dependency.getDependency());
897             }
898         } else {
899             // Add Default dependencies.
900             dependencyList = getDependencies(name);
901         }
902         model.setDependencies(dependencyList);
903         Writer writer = null;
904         try {
905             writer = WriterFactory.newXmlWriter(
906                     new File(PROJECTSLOCATION + File.separator + getArtifactID(name) + File.separator + "pom.xml"));
907             MavenXpp3Writer pomWriter = new MavenXpp3Writer();
908             pomWriter.write(writer, model);
909         } catch (Exception e) {
910             LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating POM for " + getArtifactID(name)
911                     + e.getMessage(), e);
912         } finally {
913             IOUtil.close(writer);
914         }
915     }
916
917     private List<Dependency> getDependencies(String controllerName) {
918         // Read the Dependency Information from property file.
919         Path file = Paths.get(DEPENDENCY_FILE);
920         if (!Files.notExists(file)) {
921             try {
922                 String dependencyJSON = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
923                 DependencyInfo dependencyInfo = PolicyUtils.jsonStringToObject(dependencyJSON, DependencyInfo.class);
924                 String controller = "default";
925                 if (dependencyInfo.getDependencies().containsKey(controllerName)) {
926                     controller = controllerName;
927                 }
928                 List<Dependency> dependencyList = new ArrayList<>();
929                 for (PEDependency dependency : dependencyInfo.getDependencies().get(controller)) {
930                     dependencyList.add(dependency.getDependency());
931                 }
932                 return dependencyList;
933             } catch (IOException | NullPointerException e) {
934                 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
935                         + "Error while getting dependecy Information for controller: " + controllerName
936                         + e.getMessage(), e);
937             }
938         }
939         return defaultDependencies(controllerName);
940     }
941
942     // Default Dependency Section. Can be changed as required.
943     public List<Dependency> defaultDependencies(String controllerName) {
944
945         List<Dependency> dependencyList = new ArrayList<>();
946         String version = StringEscapeUtils.escapeJava(brmsdependencyversion);
947
948         Dependency demoDependency = new Dependency();
949         demoDependency.setGroupId("org.onap.policy.drools-applications");
950         demoDependency.setArtifactId("demo");
951         demoDependency.setVersion(version);
952         dependencyList.add(demoDependency);
953
954         Dependency controlloopDependency = new Dependency();
955         controlloopDependency.setGroupId("org.onap.policy.drools-applications");
956         controlloopDependency.setArtifactId("events");
957         controlloopDependency.setVersion(version);
958         dependencyList.add(controlloopDependency);
959
960         Dependency restDependency = new Dependency();
961         restDependency.setGroupId("org.onap.policy.drools-applications");
962         restDependency.setArtifactId("rest");
963         restDependency.setVersion(version);
964         dependencyList.add(restDependency);
965
966         Dependency appcDependency = new Dependency();
967         appcDependency.setGroupId("org.onap.policy.drools-applications");
968         appcDependency.setArtifactId("appc");
969         appcDependency.setVersion(version);
970         dependencyList.add(appcDependency);
971
972         Dependency aaiDependency = new Dependency();
973         aaiDependency.setGroupId("org.onap.policy.drools-applications");
974         aaiDependency.setArtifactId("aai");
975         aaiDependency.setVersion(version);
976         dependencyList.add(aaiDependency);
977
978         Dependency msoDependency = new Dependency();
979         msoDependency.setGroupId("org.onap.policy.drools-applications");
980         msoDependency.setArtifactId("mso");
981         msoDependency.setVersion(version);
982         dependencyList.add(msoDependency);
983
984         Dependency trafficgeneratorDependency = new Dependency();
985         trafficgeneratorDependency.setGroupId("org.onap.policy.drools-applications");
986         trafficgeneratorDependency.setArtifactId("trafficgenerator");
987         trafficgeneratorDependency.setVersion(version);
988         dependencyList.add(trafficgeneratorDependency);
989         return dependencyList;
990     }
991
992     private void createProject(String path, String ksessionName) {
993         new File(path + File.separator + "rules").mkdirs();
994         new File(path + File.separator + "META-INF").mkdirs();
995         if (!Files.exists(Paths.get(path + File.separator + "META-INF" + File.separator + "kmodule.xml"))) {
996             // Hard coding XML for PDP Drools to accept our Rules.
997             String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"
998                     + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">" + "\n"
999                     + "<kbase name=\"rules\" packages=\"rules\">" + "\n" + "<ksession name=\"" + ksessionName + "\"/>"
1000                     + "\n" + "</kbase></kmodule>";
1001             copyDataToFile(path + File.separator + "META-INF" + File.separator + "kmodule.xml", xml);
1002         }
1003     }
1004
1005     private void copyDataToFile(String file, String rule) {
1006         try {
1007             FileUtils.writeStringToFile(new File(file), rule);
1008         } catch (Exception e) {
1009             LOGGER.error(
1010                     XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating Rule for " + file + e.getMessage(),
1011                     e);
1012         }
1013     }
1014
1015     private void readGroups(Properties config) throws PolicyException {
1016         String[] groupNames = null;
1017         if (config.getProperty("groupNames").contains(",")) {
1018             groupNames = config.getProperty("groupNames").replaceAll(" ", "").split(",");
1019         } else {
1020             groupNames = new String[] { config.getProperty("groupNames").replaceAll(" ", "") };
1021         }
1022         if (groupNames == null || groupNames.length == 0) {
1023             LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
1024                     + "groupNames property is missing or empty from the property file ");
1025             throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
1026                     + "groupNames property is missing or empty from the property file ");
1027         }
1028         groupMap = new HashMap<>();
1029         for (int counter = 0; counter < groupNames.length; counter++) {
1030             String name = groupNames[counter];
1031             String groupID = config.getProperty(name + ".groupID");
1032             if (groupID == null) {
1033                 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1034                         + ".groupID property is missing from the property file ");
1035                 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1036                         + ".groupID property is missing from the property file ");
1037             }
1038             String artifactID = config.getProperty(name + ".artifactID");
1039             if (artifactID == null) {
1040                 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1041                         + ".artifactID property is missing from the property file ");
1042                 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1043                         + ".artifactID property is missing from the property file ");
1044             }
1045             PEDependency dependency = new PEDependency();
1046             dependency.setArtifactId(artifactID);
1047             dependency.setGroupId(groupID);
1048             // Add to list if we got all
1049             addToGroup(name, dependency);
1050         }
1051     }
1052
1053     private void addToGroup(String name, PEDependency dependency) {
1054         ArrayList<Object> values = new ArrayList<>();
1055         values.add(dependency);
1056         groupMap.put(name, values);
1057         EntityTransaction et = em.getTransaction();
1058         et.begin();
1059         Query query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn");
1060         query.setParameter("cn", name);
1061         List<?> groupList = query.getResultList();
1062         BRMSGroupInfo brmsGroupInfo = null;
1063         if (groupList.size() > 0) {
1064             LOGGER.info("Controller name already Existing in DB. Will be updating the DB Values" + name);
1065             brmsGroupInfo = (BRMSGroupInfo) groupList.get(0);
1066         }
1067         if (brmsGroupInfo == null) {
1068             brmsGroupInfo = new BRMSGroupInfo();
1069         }
1070         brmsGroupInfo.setControllerName(name);
1071         brmsGroupInfo.setGroupId(dependency.getGroupId());
1072         brmsGroupInfo.setArtifactId(dependency.getArtifactId());
1073         brmsGroupInfo.setVersion(dependency.getVersion());
1074         em.persist(brmsGroupInfo);
1075         em.flush();
1076         et.commit();
1077     }
1078
1079     private String getArtifactID(String name) {
1080         return ((PEDependency) groupMap.get(name).get(0)).getArtifactId();
1081     }
1082
1083     private String getGroupID(String name) {
1084         return ((PEDependency) groupMap.get(name).get(0)).getGroupId();
1085     }
1086
1087     private String getVersion(String name) {
1088         return ((PEDependency) groupMap.get(name).get(0)).getVersion();
1089     }
1090
1091     private void getNameAndSetRemove(String controllerName, String policyName) {
1092         String artifactName = getArtifactID(controllerName);
1093         String ruleFolder = PROJECTSLOCATION + File.separator + artifactName + File.separator + "src" + File.separator
1094                 + "main" + File.separator + "resources" + File.separator + "rules";
1095         File file = new File(ruleFolder + File.separator + policyName + ".drl");
1096         if (file.delete()) {
1097             LOGGER.info("Deleted File.. " + file.getAbsolutePath());
1098             removePolicyFromGroup(policyName, controllerName);
1099         }
1100         if (new File(ruleFolder).listFiles().length == 0) {
1101             removedRuleModifiedGroup(controllerName);
1102         } else {
1103             // This is an update in terms of PDPD.
1104             addModifiedGroup(controllerName, "update");
1105         }
1106     }
1107
1108     // Removes Policy from Memory and Database.
1109     private void removePolicyFromGroup(String policyName, String controllerName) {
1110         policyMap.remove(policyName);
1111         EntityTransaction et = em.getTransaction();
1112         et.begin();
1113         Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn");
1114         query.setParameter("pn", policyName);
1115         List<?> pList = query.getResultList();
1116         BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo();
1117         if (pList.size() > 0) {
1118             // Already exists.
1119             brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
1120             if (brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) {
1121                 em.remove(brmsPolicyInfo);
1122                 em.flush();
1123             }
1124         }
1125         et.commit();
1126     }
1127
1128     private void setVersion(String newVersion, String controllerName) {
1129         PEDependency userController = (PEDependency) groupMap.get(controllerName).get(0);
1130         userController.setVersion(newVersion);
1131         groupMap.get(controllerName).set(0, userController);
1132     }
1133
1134     // Return BackUpMonitor
1135     public static BackUpMonitor getBackUpMonitor() {
1136         return bm;
1137     }
1138
1139     public void rotateURLs() {
1140         if (repURLs != null) {
1141             Collections.rotate(repURLs, -1);
1142         }
1143     }
1144
1145     public int URLListSize() {
1146         if (repURLs != null) {
1147             return repURLs.size();
1148         } else
1149             return 0;
1150     }
1151 }