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