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