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