2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.brmsInterface;
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;
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;
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;
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;
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;
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;
97 * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR).
98 * Mavenize and push policy to PR
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";
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;
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());
143 if(file.toString().endsWith(".properties")){
144 // Grab the Properties.
145 setProperty(file, config, handler);
150 private void setProperty(Path file, Properties config, BackUpHandler handler) throws PolicyException{
153 in = new FileInputStream(file.toFile());
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.");
159 LOGGER.info("Trying to set up IntegrityMonitor");
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";
167 resourceName = resourceName.trim();
168 im = IntegrityMonitor.getInstance(resourceName, config);
169 } catch (Exception e) {
170 LOGGER.error("Error starting Integerity Monitor: " + e);
172 LOGGER.info("Trying to set up BackUpMonitor");
174 bm = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config, handler);
175 } catch (Exception e) {
176 LOGGER.error("Error starting BackUpMonitor: " + e);
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");
186 defaultName = defaultName.trim();
187 repID = config.getProperty("repositoryID");
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 ");
192 repID = repID.trim();
193 repName = config.getProperty("repositoryName");
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 ");
198 repName = repName.trim();
199 repURL = config.getProperty("repositoryURL");
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 ");
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.");
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 ");
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;
224 brmsdependencyversion = brmsdependencyversion.trim();
228 notificationType = config.getProperty("NOTIFICATION_TYPE");
229 if("dmaap".equalsIgnoreCase(notificationType)){
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();
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 ");
243 dmaapServers = dmaapServers.trim();
244 pubTopic = pubTopic.trim();
246 if(dmaapServers.contains(",")) {
247 dmaapList = new ArrayList<String>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
249 dmaapList = new ArrayList<String>();
250 dmaapList.add(dmaapServers);
254 new BusPublisher.DmaapPublisherWrapper(this.dmaapList,
260 String dDelay = config.getProperty("NOTIFICATION_DELAY");
261 if(dDelay!=null && !dDelay.isEmpty()){
262 dDelay = dDelay.trim();
264 dmaapDelay = Long.parseLong(dDelay);
265 }catch (NumberFormatException e){
266 LOGGER.error("DMAAP_DELAY not a long format number" + e);
269 LOGGER.info("DMAAP BusPublisher is created.");
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 ");
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);
292 String uDelay = config.getProperty("NOTIFICATION_DELAY");
293 if(uDelay!=null && !uDelay.isEmpty()){
294 uDelay = uDelay.trim();
296 uebDelay = Long.parseLong(uDelay);
297 }catch (NumberFormatException e){
298 LOGGER.error("UEB_DELAY not a long format number" + e);
301 LOGGER.info("UEB PublisherBuilder is created.");
308 * Will Initialize the variables required for BRMSPush.
310 public void initiate(boolean flag) {
311 modifiedGroups = new HashMap<String, String>();
312 controllers = new ArrayList<ControllerPOJO>();
314 bm.updateNotification();
315 } catch (Exception e) {
316 LOGGER.error("Error while updating Notification: " + e.getMessage());
318 if(flag) syncGroupInfo();
322 * Will Add rules to projects. Creates necessary folders if required.
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
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);
340 //kmodule configurations
341 else if (key.equals("kSessionName")){
342 kSessionName=responseAttributes.get(key);
344 // Check User Specific values.
345 if (key.equals("$controller:")) {
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);
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){
362 userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class));
363 } catch (Exception e) {
364 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Error while resolving Dependencies: " + e);
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;
378 // If no Match then pick Default.
379 if(selectedName==null){
380 selectedName = defaultName;
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";
388 kSessionName= "closedloop-" + selectedName;
391 // create directories if missing.
392 manageProject(selectedName, kSessionName, name, rule);
393 addModifiedGroup(selectedName, "update"); // Will check for Create Later after generating the Pom.
397 private void syncGroupInfo() {
398 // Sync DB to JMemory.
399 EntityTransaction et = em.getTransaction();
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);
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());
424 LOGGER.info("Updated Local Memory values with values from database.");
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",
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);
443 private void addToPolicy(String policyName, String controllerName) {
444 policyMap.put(policyName, controllerName);
445 EntityTransaction et = em.getTransaction();
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();
454 brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
455 if(!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)){
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();
467 brmsGroupInfo = (BRMSGroupInfo) bList.get(0);
469 brmsPolicyInfo.setPolicyName(policyName);
470 brmsPolicyInfo.setControllerName(brmsGroupInfo);
471 em.persist(brmsPolicyInfo);
477 private void syncProject(String selectedName) {
478 boolean projectExists = checkProject(selectedName);
480 String version = null;
481 version = getVersion(selectedName);
483 LOGGER.error("Error getting local version for the given Controller Name:"+ selectedName+" going with Default value");
486 String nextVersion = incrementVersion(version);
487 boolean outOfSync = checkRemoteSync(selectedName, nextVersion);
492 // We are out of Sync or Project is not Present.
493 downloadProject(selectedName);
496 private void downloadProject(String selectedName) {
497 NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
498 if(artifact==null) return;
499 String dirName = getDirectoryName(selectedName);
501 String fileName = "rule.jar";
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);
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());
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();
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);
538 InputStream is = jar.getInputStream(file);
539 FileOutputStream fos = new FileOutputStream(f);
540 while (is.available() > 0) {
541 fos.write(is.read());
546 LOGGER.info(fileName + " Created..");
552 private NexusArtifact getLatestArtifactFromNexus(String selectedName) {
553 List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, null);
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(".")));
564 if((bigNum==majorVal)&&(minorVal>smallNum)){
567 if(bigNum==majorVal && minorVal==smallNum){
574 private boolean checkRemoteSync(String selectedName, String version) {
575 List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, version);
576 return (artifacts.size()==0) ? false: true;
579 private List<NexusArtifact> getArtifactFromNexus(String selectedName, String version) {
580 final NexusClient client = new NexusRestClient();
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));
587 template.setVersion(version);
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());
595 } catch (NexusClientException | NexusConnectionException e) {
596 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus." +e.getMessage());
599 return new ArrayList<NexusArtifact>();
602 private void setVersion(String selectedName) {
603 String newVersion = "0.1.0";
605 NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
607 newVersion = incrementVersion(artifact.getVersion());
609 if(newVersion.equals("0.1.0")){
612 setVersion(newVersion, selectedName);
613 LOGGER.info("Controller: " + selectedName + "is on version: " + newVersion);
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(".")));
625 return majorVal + "." + minorVal + version.substring(version.lastIndexOf("."));
628 private boolean checkProject(String selectedName) {
629 return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists();
632 private String getDirectoryName(String selectedName) {
633 return getArtifactID(selectedName);
637 * Will Push policies to the PolicyRepo.
639 * @param notificationType <String> type of notification Type.
641 public void pushRules(){
642 // Check how many groups have been updated.
643 // Invoke their Maven process.
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);
651 if(!modifiedGroups.isEmpty()){
652 Boolean flag = false;
653 for(String group: modifiedGroups.keySet()){
655 InvocationRequest request = new DefaultInvocationRequest();
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..!");
667 if(result.getExitCode()==0){
668 LOGGER.info("Build Completed..!");
670 addNotification(group, "create");
672 addNotification(group, modifiedGroups.get(group));
677 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Maven Invocation issue for "+getArtifactID(group) + e.getMessage());
681 sendNotification(controllers);
690 * Removes a Rule from Rule Projects.
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);
698 syncProject(controllerName);
699 getNameAndSetRemove(controllerName, name);
702 private String getGroupName(String name) {
703 if(policyMap.containsKey(name)){
704 return policyMap.get(name);
707 return (policyMap.containsKey(name)) ? policyMap.get(name):null;
711 private void addModifiedGroup(String controllerName, String operation) {
712 if(controllerName!=null){
713 modifiedGroups.put(controllerName, operation);
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);
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);
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);
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);
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());
762 private void sendMessage(String message) throws IOException, GeneralSecurityException, InterruptedException {
764 if("dmaap".equalsIgnoreCase(notificationType)) {
765 // Sending Message through DMaaP Message Router
766 LOGGER.debug("DMAAP Publishing Message");
768 publisher.send( "MyPartitionKey", message);
770 LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic);
772 Thread.sleep(dmaapDelay);
775 // Sending Message through UEB interface.
776 LOGGER.debug("UEB Publishing Message");
778 CambriaBatchingPublisher pub = pubBuilder.build();
779 pub.send( "MyPartitionKey", message);
781 final List<?> stuck = pub.close ( uebDelay, TimeUnit.SECONDS );
782 if ( stuck.size () > 0 ) {
783 LOGGER.error ( stuck.size() + " messages unsent" );
785 LOGGER.debug ( "Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic );
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));
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());
814 // Add Default dependencies.
815 dependencyList = getDependencies(name);
817 model.setDependencies(dependencyList);
818 Writer writer = null;
820 writer = WriterFactory.newXmlWriter(new File(PROJECTSLOCATION
821 + File.separator + getArtifactID(name) + File.separator
823 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
824 pomWriter.write(writer, model);
826 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
827 + "Error while creating POM for " + getArtifactID(name)
830 IOUtil.close(writer);
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)){
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;
845 List<Dependency> dependencyList = new ArrayList<Dependency>();
846 for(PEDependency dependency: dependencyInfo.getDependencies().get(controller)){
847 dependencyList.add(dependency.getDependency());
849 return dependencyList;
850 } catch (IOException| NullPointerException e) {
851 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
852 + "Error while getting dependecy Information for controller: " + controllerName
856 return defaultDependencies(controllerName);
859 // Default Dependency Section. Can be changed as required.
860 public List<Dependency> defaultDependencies(String controllerName) {
862 List<Dependency> dependencyList = new ArrayList<Dependency>();
863 String version= StringEscapeUtils.escapeJava(brmsdependencyversion);
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);
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);
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);
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);
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);
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);
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;
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);
923 private void copyDataToFile(String file, String rule) {
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());
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(",");
936 groupNames = new String[]{config.getProperty("groupNames").replaceAll(" ", "")};
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 ");
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");
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 ");
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 ");
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);
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();
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);
977 if(brmsGroupInfo==null){
978 brmsGroupInfo = new BRMSGroupInfo();
980 brmsGroupInfo.setControllerName(name);
981 brmsGroupInfo.setGroupId(dependency.getGroupId());
982 brmsGroupInfo.setArtifactId(dependency.getArtifactId());
983 brmsGroupInfo.setVersion(dependency.getVersion());
984 em.persist(brmsGroupInfo);
989 private String getArtifactID(String name){
990 return ((PEDependency) groupMap.get(name).get(0)).getArtifactId();
993 private String getGroupID(String name){
994 return ((PEDependency) groupMap.get(name).get(0)).getGroupId();
997 private String getVersion(String name){
998 return ((PEDependency) groupMap.get(name).get(0)).getVersion();
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");
1009 LOGGER.info("Deleted File.. " + file.getAbsolutePath());
1011 if(new File(ruleFolder).listFiles().length == 0) {
1012 removedRuleModifiedGroup(controllerName);
1014 // This is an update in terms of PDPD.
1015 addModifiedGroup(controllerName, "update");
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);
1025 // Return BackUpMonitor
1026 public static BackUpMonitor getBackUpMonitor(){