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.Collections;
40 import java.util.Enumeration;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.Properties;
45 import java.util.UUID;
46 import java.util.concurrent.TimeUnit;
47 import java.util.jar.JarEntry;
48 import java.util.jar.JarFile;
50 import javax.persistence.EntityManager;
51 import javax.persistence.EntityManagerFactory;
52 import javax.persistence.EntityTransaction;
53 import javax.persistence.Persistence;
54 import javax.persistence.Query;
56 import org.apache.commons.io.FileUtils;
57 import org.apache.commons.lang.StringEscapeUtils;
58 import org.apache.maven.model.Dependency;
59 import org.apache.maven.model.DeploymentRepository;
60 import org.apache.maven.model.DistributionManagement;
61 import org.apache.maven.model.Model;
62 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
63 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
64 import org.apache.maven.shared.invoker.DefaultInvoker;
65 import org.apache.maven.shared.invoker.InvocationRequest;
66 import org.apache.maven.shared.invoker.InvocationResult;
67 import org.apache.maven.shared.invoker.Invoker;
68 import org.codehaus.plexus.util.IOUtil;
69 import org.codehaus.plexus.util.WriterFactory;
70 import org.eclipse.persistence.config.PersistenceUnitProperties;
71 import org.openecomp.policy.api.PEDependency;
72 import org.openecomp.policy.api.PolicyException;
73 import org.openecomp.policy.brmsInterface.jpa.BRMSGroupInfo;
74 import org.openecomp.policy.brmsInterface.jpa.BRMSPolicyInfo;
75 import org.openecomp.policy.brmsInterface.jpa.DependencyInfo;
76 import org.openecomp.policy.common.im.AdministrativeStateException;
77 import org.openecomp.policy.common.im.IntegrityMonitor;
78 import org.openecomp.policy.common.logging.eelf.MessageCodes;
79 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
80 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
81 import org.openecomp.policy.common.logging.flexlogger.Logger;
82 import org.openecomp.policy.utils.BackUpHandler;
83 import org.openecomp.policy.utils.BackUpMonitor;
84 import org.openecomp.policy.utils.BusPublisher;
85 import org.openecomp.policy.utils.PolicyUtils;
86 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
87 import org.sonatype.nexus.client.NexusClient;
88 import org.sonatype.nexus.client.NexusClientException;
89 import org.sonatype.nexus.client.NexusConnectionException;
90 import org.sonatype.nexus.client.rest.NexusRestClient;
91 import org.sonatype.nexus.rest.model.NexusArtifact;
93 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
94 import com.att.nsa.cambria.client.CambriaClientBuilders;
95 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
96 import com.fasterxml.jackson.core.JsonProcessingException;
99 * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR).
100 * Mavenize and push policy to PR
105 @SuppressWarnings("deprecation")
106 public class BRMSPush {
107 private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName());
108 private static final String PROJECTSLOCATION = "RuleProjects";
109 private static final String GOALS[] = {"clean", "deploy"};
110 private static final String DEFAULT_VERSION = "1.1.0-SNAPSHOT";
111 private static final String DEPENDENCY_FILE = "dependency.json";
113 private static Map<String, String> modifiedGroups = new HashMap<>();
114 private static IntegrityMonitor im;
115 private static BackUpMonitor bm;
116 private static String resourceName = null;
117 private String defaultName = null;
118 private String repID = null;
119 private String repName = null;
120 private ArrayList<String> repURLs= null;
121 private String repUserName = null;
122 private String repPassword = null;
123 private String policyKeyID = null;
124 private boolean createFlag = false;
125 private String uebList = null;
126 private List<String> dmaapList = null;
127 private String pubTopic = null;
128 private PublisherBuilder pubBuilder = null;
129 protected BusPublisher publisher = null;
130 private Long uebDelay = Long.parseLong("20");
131 private Long dmaapDelay = Long.parseLong("5000");
132 private String notificationType = null;
133 private ArrayList<ControllerPOJO> controllers;
134 private HashMap<String, ArrayList<Object>> groupMap = new HashMap<>();
135 private Map<String, String> policyMap = new HashMap<>();
136 private String brmsdependencyversion;
137 private EntityManager em;
138 private boolean syncFlag = false;
140 public BRMSPush(String propertiesFile, BackUpHandler handler) throws PolicyException{
141 Properties config = new Properties();
142 Path file = Paths.get(propertiesFile);
143 if(Files.notExists(file)){
144 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Config File doesn't Exist in the specified Path " + file.toString());
145 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE+"Config File doesn't Exist in the specified Path " + file.toString());
147 if(file.toString().endsWith(".properties")){
148 // Grab the Properties.
149 setProperty(file, config, handler);
154 private void setProperty(Path file, Properties config, BackUpHandler handler) throws PolicyException{
157 in = new FileInputStream(file.toFile());
159 } catch (IOException e) {
160 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file.");
161 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file.");
163 LOGGER.info("Trying to set up IntegrityMonitor");
165 LOGGER.info("Trying to set up IntegrityMonitor");
166 resourceName = config.getProperty("RESOURCE_NAME");
167 if(resourceName==null){
168 LOGGER.warn("RESOURCE_NAME is missing setting default value. ");
169 resourceName = "brmsgw_pdp01";
171 resourceName = resourceName.trim();
172 im = IntegrityMonitor.getInstance(resourceName, config);
173 } catch (Exception e) {
174 LOGGER.error("Error starting Integerity Monitor: " + e);
176 LOGGER.info("Trying to set up BackUpMonitor");
178 bm = BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config, handler);
179 } catch (Exception e) {
180 LOGGER.error("Error starting BackUpMonitor: " + e);
182 config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml");
183 EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config);
184 em = emf.createEntityManager();
185 defaultName = config.getProperty("defaultName");
186 if(defaultName==null){
187 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file ");
188 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file");
190 defaultName = defaultName.trim();
191 repID = config.getProperty("repositoryID");
193 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
194 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
196 repID = repID.trim();
197 repName = config.getProperty("repositoryName");
199 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryName property is missing from the property file ");
200 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryName property is missing from the property file ");
202 repName = repName.trim();
203 String repURL = config.getProperty("repositoryURL");
205 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
206 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
208 if(repURL.contains(",")){
209 repURLs = new ArrayList<>(Arrays.asList(repURL.trim().split(",")));
211 repURLs = new ArrayList<>();
214 repUserName = config.getProperty("repositoryUsername");
215 repPassword = config.getProperty("repositoryPassword");
216 if(repUserName==null || repPassword==null){
217 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repostoryUserName and respositoryPassword properties are required.");
218 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repostoryUserName and respositoryPassword properties are required.");
220 repUserName = repUserName.trim();
221 repPassword = repPassword.trim();
222 policyKeyID = config.getProperty("policyKeyID");
223 if(policyKeyID==null){
224 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
225 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
227 policyKeyID = policyKeyID.trim();
228 String syncF = config.getProperty("sync", "false").trim();
229 syncFlag = Boolean.parseBoolean(syncF);
231 PolicyLogger.info("SYNC Flag is turned ON. DB will be given Priority.");
233 brmsdependencyversion = config.getProperty("brms.dependency.version");
234 if(brmsdependencyversion==null){
235 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "brmsdependencyversion property is missing from the property file, Using default Version.");
236 brmsdependencyversion = DEFAULT_VERSION;
238 brmsdependencyversion = brmsdependencyversion.trim();
242 notificationType = config.getProperty("NOTIFICATION_TYPE");
243 if("dmaap".equalsIgnoreCase(notificationType)){
245 LOGGER.info("Notification Type being used is DMaaP... creating instance of BusPublisher.");
246 // Setting up the Publisher for DMaaP MR
247 String dmaapServers = config.getProperty("NOTIFICATION_SERVERS");
248 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
249 String aafLogin = config.getProperty("CLIENT_ID").trim();
250 String aafPassword = config.getProperty("CLIENT_KEY").trim();
252 if(dmaapServers==null || pubTopic==null){
253 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
254 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
257 dmaapServers = dmaapServers.trim();
258 pubTopic = pubTopic.trim();
260 if(dmaapServers.contains(",")) {
261 dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
263 dmaapList = new ArrayList<>();
264 dmaapList.add(dmaapServers);
268 new BusPublisher.DmaapPublisherWrapper(this.dmaapList,
274 String dDelay = config.getProperty("NOTIFICATION_DELAY");
275 if(dDelay!=null && !dDelay.isEmpty()){
276 dDelay = dDelay.trim();
278 dmaapDelay = Long.parseLong(dDelay);
279 }catch (NumberFormatException e){
280 LOGGER.error("DMAAP_DELAY not a long format number" + e);
283 LOGGER.info("DMAAP BusPublisher is created.");
286 LOGGER.info("Notification Type being used is UEB... creating instance of PublisherBuilder.");
287 // Setting up the Publisher for UEB
288 uebList = config.getProperty("NOTIFICATION_SERVERS");
289 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
290 String apiKey = config.getProperty("UEB_API_KEY");
291 String apiSecret = config.getProperty("UEB_API_SECRET");
292 if(uebList==null || pubTopic==null){
293 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
294 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
296 uebList = uebList.trim();
297 pubTopic = pubTopic.trim();
298 pubBuilder = new CambriaClientBuilders.PublisherBuilder();
299 pubBuilder.usingHosts(uebList).onTopic(pubTopic);
300 if(apiKey!=null && !apiKey.isEmpty() &&
301 apiSecret!=null && !apiSecret.isEmpty()) {
302 apiKey= apiKey.trim();
303 apiSecret = apiSecret.trim();
304 pubBuilder.authenticatedBy(apiKey, apiSecret);
306 String uDelay = config.getProperty("NOTIFICATION_DELAY");
307 if(uDelay!=null && !uDelay.isEmpty()){
308 uDelay = uDelay.trim();
310 uebDelay = Long.parseLong(uDelay);
311 }catch (NumberFormatException e){
312 LOGGER.error("UEB_DELAY not a long format number" + e);
315 LOGGER.info("UEB PublisherBuilder is created.");
322 * Will Initialize the variables required for BRMSPush.
324 public void initiate(boolean flag) {
325 modifiedGroups = new HashMap<>();
326 controllers = new ArrayList<>();
328 bm.updateNotification();
329 } catch (Exception e) {
330 LOGGER.error("Error while updating Notification: " + e.getMessage());
332 if(flag) syncGroupInfo();
336 * Will Add rules to projects. Creates necessary folders if required.
338 public void addRule(String name, String rule, Map<String, String> responseAttributes) {
339 // 1 check the response Attributes and determine if this belongs to any projects.
340 // 2 if not create folder
343 // 5 store the groups that have been updated.
344 String kSessionName=null;
345 String selectedName = null;
346 if(!responseAttributes.isEmpty()){
347 // Pick selected Value
348 String userControllerName = null;
349 ArrayList<PEDependency> userDependencies = new ArrayList<>();
350 for(String key: responseAttributes.keySet()){
351 if(key.equals(policyKeyID)){
352 selectedName = responseAttributes.get(key);
354 //kmodule configurations
355 else if (key.equals("kSessionName")){
356 kSessionName=responseAttributes.get(key);
358 // Check User Specific values.
359 if (key.equals("$controller:")) {
361 PEDependency dependency = PolicyUtils.jsonStringToObject(responseAttributes.get(key), PEDependency.class);
362 userControllerName = key.replaceFirst("$controller:","");
363 addToGroup(userControllerName, dependency);
364 } catch (Exception e) {
365 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Error while resolving Controller: " + e);
369 else if(key.equals("$dependency$")){
370 String value = responseAttributes.get(key);
371 if(value.startsWith("[") && value.endsWith("]")){
372 value = value.substring(1, value.length()-1).trim();
373 List<String> dependencyStrings = Arrays.asList(value.split("},{"));
374 for(String dependencyString: dependencyStrings){
376 userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class));
377 } catch (Exception e) {
378 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Error while resolving Dependencies: " + e);
384 if(userControllerName!=null){
385 // Adding custom dependencies here.
386 ArrayList<Object> values = groupMap.get(userControllerName);
387 values.add(userDependencies);
388 groupMap.put(userControllerName, values);
389 selectedName = userControllerName;
392 // If no Match then pick Default.
393 if(selectedName==null){
394 selectedName = defaultName;
396 if (groupMap.containsKey(selectedName)) {
397 //If the key is not got as parameters set by the user, setting the default value for kSessionName as closedLoop
398 if(kSessionName==null){
399 if(selectedName==defaultName){
400 kSessionName="closedloop";
402 kSessionName= "closedloop-" + selectedName;
405 // create directories if missing.
406 manageProject(selectedName, kSessionName, name, rule);
407 addModifiedGroup(selectedName, "update"); // Will check for Create Later after generating the Pom.
411 private void syncGroupInfo() {
412 // Sync DB to JMemory.
413 EntityTransaction et = em.getTransaction();
415 Query query = em.createQuery("select b from BRMSGroupInfo AS b");
416 List<?> bList = query.getResultList();
417 if(bList.size()!=groupMap.size()){
418 for(Object value : bList){
419 BRMSGroupInfo brmsGroupInfo = (BRMSGroupInfo) value;
420 PEDependency dependency = new PEDependency();
421 dependency.setArtifactId(brmsGroupInfo.getArtifactId());
422 dependency.setGroupId(brmsGroupInfo.getGroupId());
423 dependency.setVersion(brmsGroupInfo.getVersion());
424 ArrayList<Object> values = new ArrayList<>();
425 values.add(dependency);
426 groupMap.put(brmsGroupInfo.getControllerName(), values);
429 query = em.createQuery("select g from BRMSPolicyInfo AS g");
430 bList = query.getResultList();
431 if(bList.size()!=policyMap.size()){
432 for(Object value: bList){
433 BRMSPolicyInfo brmsPolicyInfo = (BRMSPolicyInfo) value;
434 policyMap.put(brmsPolicyInfo.getPolicyName(), brmsPolicyInfo.getControllerName().getControllerName());
438 LOGGER.info("Updated Local Memory values with values from database.");
441 private void manageProject(String selectedName, String kSessionName, String name, String rule) {
442 // Check if the Project is in Sync. If not get the latest Version.
443 syncProject(selectedName);
444 createProject(PROJECTSLOCATION + File.separator
445 + getArtifactID(selectedName) + File.separator + "src"
446 + File.separator + "main" + File.separator + "resources",
449 PROJECTSLOCATION + File.separator
450 + getArtifactID(selectedName) + File.separator
451 + "src" + File.separator + "main" + File.separator
452 + "resources" + File.separator + "rules"
453 + File.separator + name + ".drl", rule);
454 addToPolicy(name,selectedName);
458 * Add Policy to JMemory and DataBase.
460 private void addToPolicy(String policyName, String controllerName) {
461 policyMap.put(policyName, controllerName);
462 EntityTransaction et = em.getTransaction();
464 Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn");
465 query.setParameter("pn", policyName);
466 List<?> pList = query.getResultList();
467 boolean createFlag = false;
468 BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo();
471 brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
472 if(!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)){
479 query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn");
480 query.setParameter("cn", controllerName);
481 List<?> bList = query.getResultList();
482 BRMSGroupInfo brmsGroupInfo = new BRMSGroupInfo();
484 brmsGroupInfo = (BRMSGroupInfo) bList.get(0);
486 brmsPolicyInfo.setPolicyName(policyName);
487 brmsPolicyInfo.setControllerName(brmsGroupInfo);
488 em.persist(brmsPolicyInfo);
494 private void syncProject(String selectedName) {
495 boolean projectExists = checkProject(selectedName);
497 String version = null;
498 version = getVersion(selectedName);
500 LOGGER.error("Error getting local version for the given Controller Name:"+ selectedName+" going with Default value");
503 String nextVersion = incrementVersion(version);
504 boolean outOfSync = checkRemoteSync(selectedName, nextVersion);
509 // We are out of Sync or Project is not Present.
510 downloadProject(selectedName);
513 private void downloadProject(String selectedName) {
514 NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
515 if(artifact==null) return;
516 String dirName = getDirectoryName(selectedName);
518 String fileName = "rule.jar";
520 website = new URL(artifact.getResourceURI());
521 ReadableByteChannel rbc = Channels.newChannel(website.openStream());
522 FileOutputStream fos = new FileOutputStream(fileName);
523 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
525 extractJar(fileName, dirName);
526 new File(fileName).delete();
527 } catch (IOException e) {
528 LOGGER.error("Error while downloading the project to File System. " + e.getMessage());
532 private void extractJar(String jarFileName, String artifactId) throws IOException{
533 JarFile jar = new JarFile(jarFileName);
534 Enumeration<?> enumEntries = jar.entries();
535 while (enumEntries.hasMoreElements()) {
536 JarEntry file = (JarEntry) enumEntries.nextElement();
538 String fileName = file.getName().substring(file.getName().lastIndexOf("/")+1);
539 if(file.getName().endsWith(".drl")){
540 String path = PROJECTSLOCATION+ File.separator + artifactId + File.separator + "src"+
541 File.separator+ "main" + File.separator + "resources" + File.separator+ "rules";
542 new File(path).mkdirs();
543 if(syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))){
544 f = new File(path + File.separator + fileName);
546 f = new File(path + File.separator + fileName);
548 }else if(file.getName().endsWith("pom.xml")){
549 String path = PROJECTSLOCATION+ File.separator + artifactId;
550 new File(path).mkdirs();
551 f = new File(path + File.separator + fileName);
552 }else if(file.getName().endsWith("kmodule.xml")){
553 String path = PROJECTSLOCATION+ File.separator + artifactId + File.separator + "src"+
554 File.separator+ "main" + File.separator + "resources" + File.separator+ "META-INF";
555 new File(path).mkdirs();
556 f = new File(path + File.separator + fileName);
559 InputStream is = jar.getInputStream(file);
560 FileOutputStream fos = new FileOutputStream(f);
561 while (is.available() > 0) {
562 fos.write(is.read());
567 LOGGER.info(fileName + " Created..");
573 private NexusArtifact getLatestArtifactFromNexus(String selectedName) {
574 List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, null);
577 NexusArtifact result = null;
578 for (NexusArtifact artifact : artifacts) {
579 int majorVal = Integer.parseInt(artifact.getVersion().substring(0, artifact.getVersion().indexOf(".")));
580 int minorVal = Integer.parseInt(artifact.getVersion().substring(artifact.getVersion().indexOf(".")+1,artifact.getVersion().lastIndexOf(".")));
585 if((bigNum==majorVal)&&(minorVal>smallNum)){
588 if(bigNum==majorVal && minorVal==smallNum){
595 private boolean checkRemoteSync(String selectedName, String version) {
596 List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, version);
597 return (artifacts.size()==0) ? false: true;
600 private List<NexusArtifact> getArtifactFromNexus(String selectedName, String version) {
601 final NexusClient client = new NexusRestClient();
603 boolean flag = false;
604 while(i<repURLs.size()){
606 String repURL = repURLs.get(0);
607 client.connect(repURL.substring(0, repURL.indexOf(repURL.split(":[0-9]+\\/nexus")[1])), repUserName, repPassword);
608 final NexusArtifact template = new NexusArtifact();
609 template.setGroupId(getGroupID(selectedName));
610 template.setArtifactId(getArtifactID(selectedName));
612 template.setVersion(version);
614 List<NexusArtifact> resultList = client.searchByGAV(template);
615 if(resultList!=null){
619 } catch (NexusClientException | NexusConnectionException | NullPointerException e) {
620 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. " +e.getMessage());
624 } catch (NexusClientException | NexusConnectionException e) {
625 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus." +e.getMessage());
628 Collections.rotate(repURLs, -1);
633 return new ArrayList<NexusArtifact>();
636 private void setVersion(String selectedName) {
637 String newVersion = "0.1.0";
639 NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
641 newVersion = incrementVersion(artifact.getVersion());
643 if(newVersion.equals("0.1.0")){
646 setVersion(newVersion, selectedName);
647 LOGGER.info("Controller: " + selectedName + "is on version: " + newVersion);
650 private String incrementVersion(String version) {
651 int majorVal = Integer.parseInt(version.substring(0, version.indexOf(".")));
652 int minorVal = Integer.parseInt(version.substring(version.indexOf(".")+1,version.lastIndexOf(".")));
659 return majorVal + "." + minorVal + version.substring(version.lastIndexOf("."));
662 private boolean checkProject(String selectedName) {
663 return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists();
666 private String getDirectoryName(String selectedName) {
667 return getArtifactID(selectedName);
671 * Will Push policies to the PolicyRepo.
673 * @param notificationType <String> type of notification Type.
674 * @throws PolicyException
676 public void pushRules() throws PolicyException{
677 // Check how many groups have been updated.
678 // Invoke their Maven process.
680 im.startTransaction();
681 } catch (AdministrativeStateException e) {
682 LOGGER.error("Error while starting Transaction " + e);
683 } catch (Exception e) {
684 LOGGER.error("Error while starting Transaction " + e);
686 if(!modifiedGroups.isEmpty()){
687 Boolean flag = false;
688 for(String group: modifiedGroups.keySet()){
689 InvocationResult result = null;
691 InvocationRequest request = new DefaultInvocationRequest();
694 request.setPomFile(new File(PROJECTSLOCATION+File.separator+getArtifactID(group)+File.separator+"pom.xml"));
695 request.setGoals(Arrays.asList(GOALS));
696 Invoker invoker = new DefaultInvoker();
697 result = invoker.execute(request);
698 if(result.getExecutionException()!=null){
699 LOGGER.error(result.getExecutionException());
700 }else if(result.getExitCode()!=0){
701 LOGGER.error("Maven Invocation failure..!");
704 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Maven Invocation issue for "+getArtifactID(group) + e.getMessage());
706 if(result!=null && result.getExitCode()==0){
707 LOGGER.info("Build Completed..!");
709 addNotification(group, "create");
711 addNotification(group, modifiedGroups.get(group));
715 throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!");
719 sendNotification(controllers);
728 * Removes a Rule from Rule Projects.
730 public void removeRule(String name){
731 String controllerName = getGroupName(name);
732 if(controllerName==null){
733 LOGGER.info("Error finding the controllerName for the given Policy: " + name);
736 syncProject(controllerName);
737 getNameAndSetRemove(controllerName, name);
740 private String getGroupName(String name) {
741 if(policyMap.containsKey(name)){
742 return policyMap.get(name);
745 return (policyMap.containsKey(name)) ? policyMap.get(name):null;
749 private void addModifiedGroup(String controllerName, String operation) {
750 if(controllerName!=null){
751 modifiedGroups.put(controllerName, operation);
755 private void addNotification(String controllerName, String operation) {
756 ControllerPOJO controllerPOJO = new ControllerPOJO();
757 controllerPOJO.setName(controllerName);
758 controllerPOJO.setOperation(operation);
759 HashMap<String, String> drools = new HashMap<>();
760 drools.put("groupId", getGroupID(controllerName));
761 drools.put("artifactId", getArtifactID(controllerName));
762 drools.put("version", getVersion(controllerName));
763 controllerPOJO.setDrools(drools);
764 controllers.add(controllerPOJO);
766 LOGGER.debug("Notification added: "
767 + PolicyUtils.objectToJsonString(controllerPOJO));
768 } catch (JsonProcessingException e) {
769 LOGGER.error(MessageCodes.ERROR_SCHEMA_INVALID
770 + "Json Processing Error " + e);
774 private void removedRuleModifiedGroup(String controllerName){
775 // This will be sending Notification to PDPD directly to Lock
776 ControllerPOJO controllerPOJO = new ControllerPOJO();
777 controllerPOJO.setName(controllerName);
778 controllerPOJO.setOperation("lock");
779 List<ControllerPOJO> controllers = new ArrayList<>();
780 controllers.add(controllerPOJO);
781 sendNotification(controllers);
784 private void sendNotification(List<ControllerPOJO> controllers){
785 NotificationPOJO notification = new NotificationPOJO();
786 String requestId = UUID.randomUUID().toString();
787 LOGGER.info("Generating notification RequestID : " + requestId);
788 notification.setRequestID(requestId);
789 notification.setEntity("controller");
790 notification.setControllers(controllers);
792 String notificationJson = PolicyUtils.objectToJsonString(notification);
793 LOGGER.info("Sending Notification :\n" + notificationJson);
794 sendMessage(notificationJson);
795 } catch (Exception e) {
796 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while sending notification to PDP-D " + e.getMessage());
800 private void sendMessage(String message) throws IOException, GeneralSecurityException, InterruptedException {
802 if("dmaap".equalsIgnoreCase(notificationType)) {
803 // Sending Message through DMaaP Message Router
804 LOGGER.debug("DMAAP Publishing Message");
806 publisher.send( "MyPartitionKey", message);
808 LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic);
810 Thread.sleep(dmaapDelay);
813 // Sending Message through UEB interface.
814 LOGGER.debug("UEB Publishing Message");
816 CambriaBatchingPublisher pub = pubBuilder.build();
817 pub.send( "MyPartitionKey", message);
819 final List<?> stuck = pub.close ( uebDelay, TimeUnit.SECONDS );
820 if ( stuck.size () > 0 ) {
821 LOGGER.error ( stuck.size() + " messages unsent" );
823 LOGGER.debug ( "Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic );
829 private void createPom(String name) {
830 Model model = new Model();
831 model.setModelVersion("4.0.0");
832 model.setGroupId(getGroupID(name));
833 model.setArtifactId(getArtifactID(name));
834 model.setVersion(getVersion(name));
836 DistributionManagement distributionManagement = new DistributionManagement();
837 DeploymentRepository repository = new DeploymentRepository();
838 repository.setId(repID);
839 repository.setName(repName);
840 repository.setUrl(repURLs.get(0));
841 distributionManagement.setRepository(repository);
842 model.setDistributionManagement(distributionManagement);
843 // Dependency Management goes here.
844 List<Dependency> dependencyList= new ArrayList<>();
845 if(groupMap.get(name).size()>1){
846 @SuppressWarnings("unchecked")
847 ArrayList<PEDependency> dependencies = (ArrayList<PEDependency>) groupMap.get(name).get(1);
848 for(PEDependency dependency: dependencies){
849 dependencyList.add(dependency.getDependency());
852 // Add Default dependencies.
853 dependencyList = getDependencies(name);
855 model.setDependencies(dependencyList);
856 Writer writer = null;
858 writer = WriterFactory.newXmlWriter(new File(PROJECTSLOCATION
859 + File.separator + getArtifactID(name) + File.separator
861 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
862 pomWriter.write(writer, model);
864 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
865 + "Error while creating POM for " + getArtifactID(name)
868 IOUtil.close(writer);
872 private List<Dependency> getDependencies(String controllerName) {
873 // Read the Dependency Information from property file.
874 Path file = Paths.get(DEPENDENCY_FILE);
875 if(!Files.notExists(file)){
877 String dependencyJSON = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
878 DependencyInfo dependencyInfo = PolicyUtils.jsonStringToObject(dependencyJSON, DependencyInfo.class);
879 String controller = "default";
880 if(dependencyInfo.getDependencies().containsKey(controllerName)){
881 controller = controllerName;
883 List<Dependency> dependencyList = new ArrayList<>();
884 for(PEDependency dependency: dependencyInfo.getDependencies().get(controller)){
885 dependencyList.add(dependency.getDependency());
887 return dependencyList;
888 } catch (IOException| NullPointerException e) {
889 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
890 + "Error while getting dependecy Information for controller: " + controllerName
894 return defaultDependencies(controllerName);
897 // Default Dependency Section. Can be changed as required.
898 public List<Dependency> defaultDependencies(String controllerName) {
900 List<Dependency> dependencyList = new ArrayList<>();
901 String version= StringEscapeUtils.escapeJava(brmsdependencyversion);
903 Dependency demoDependency = new Dependency();
904 demoDependency.setGroupId("org.openecomp.policy.drools-applications");
905 demoDependency.setArtifactId("demo");
906 demoDependency.setVersion(version);
907 dependencyList.add(demoDependency);
909 Dependency controlloopDependency = new Dependency();
910 controlloopDependency.setGroupId("org.openecomp.policy.drools-applications");
911 controlloopDependency.setArtifactId("controlloop");
912 controlloopDependency.setVersion(version);
913 dependencyList.add(controlloopDependency);
915 Dependency restDependency = new Dependency();
916 restDependency.setGroupId("org.openecomp.policy.drools-applications");
917 restDependency.setArtifactId("rest");
918 restDependency.setVersion(version);
919 dependencyList.add(restDependency);
921 Dependency appcDependency = new Dependency();
922 appcDependency.setGroupId("org.openecomp.policy.drools-applications");
923 appcDependency.setArtifactId("appc");
924 appcDependency.setVersion(version);
925 dependencyList.add(appcDependency);
927 Dependency aaiDependency = new Dependency();
928 aaiDependency.setGroupId("org.openecomp.policy.drools-applications");
929 aaiDependency.setArtifactId("aai");
930 aaiDependency.setVersion(version);
931 dependencyList.add(aaiDependency);
933 Dependency msoDependency = new Dependency();
934 msoDependency.setGroupId("org.openecomp.policy.drools-applications");
935 msoDependency.setArtifactId("mso");
936 msoDependency.setVersion(version);
937 dependencyList.add(msoDependency);
939 Dependency trafficgeneratorDependency = new Dependency();
940 trafficgeneratorDependency.setGroupId("org.openecomp.policy.drools-applications");
941 trafficgeneratorDependency.setArtifactId("trafficgenerator");
942 trafficgeneratorDependency.setVersion(version);
943 dependencyList.add(trafficgeneratorDependency);
944 return dependencyList;
947 private void createProject(String path,String ksessionName){
948 new File(path+File.separator+"rules").mkdirs();
949 new File(path+File.separator+"META-INF").mkdirs();
950 if(!Files.exists(Paths.get(path+File.separator+"META-INF"+File.separator+"kmodule.xml"))){
951 // Hard coding XML for PDP Drools to accept our Rules.
952 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"+
953 "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">" +"\n"+
954 "<kbase name=\"rules\" packages=\"rules\">" + "\n" +
955 "<ksession name=\""+ ksessionName +"\"/>"+ "\n" +
956 "</kbase></kmodule>";
957 copyDataToFile(path+File.separator+"META-INF"+File.separator+"kmodule.xml", xml);
961 private void copyDataToFile(String file, String rule) {
963 FileUtils.writeStringToFile(new File(file), rule);
964 } catch (Exception e) {
965 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Error while creating Rule for " + file + e.getMessage());
969 private void readGroups(Properties config) throws PolicyException{
970 String[] groupNames = null;
971 if(config.getProperty("groupNames").contains(",")){
972 groupNames = config.getProperty("groupNames").replaceAll(" ", "").split(",");
974 groupNames = new String[]{config.getProperty("groupNames").replaceAll(" ", "")};
976 if(groupNames==null || groupNames.length==0){
977 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "groupNames property is missing or empty from the property file ");
978 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "groupNames property is missing or empty from the property file ");
980 groupMap = new HashMap<>();
981 for(int counter=0; counter < groupNames.length ;counter++){
982 String name = groupNames[counter];
983 String groupID = config.getProperty(name+".groupID");
985 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".groupID property is missing from the property file ");
986 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".groupID property is missing from the property file ");
988 String artifactID = config.getProperty(name+".artifactID");
989 if(artifactID==null){
990 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".artifactID property is missing from the property file ");
991 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name+".artifactID property is missing from the property file ");
993 PEDependency dependency = new PEDependency();
994 dependency.setArtifactId(artifactID);
995 dependency.setGroupId(groupID);
996 // Add to list if we got all
997 addToGroup(name,dependency);
1001 private void addToGroup(String name, PEDependency dependency) {
1002 ArrayList<Object> values = new ArrayList<>();
1003 values.add(dependency);
1004 groupMap.put(name, values);
1005 EntityTransaction et = em.getTransaction();
1007 Query query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn");
1008 query.setParameter("cn", name);
1009 List<?> groupList = query.getResultList();
1010 BRMSGroupInfo brmsGroupInfo = null;
1011 if(groupList.size()>0){
1012 LOGGER.info("Controller name already Existing in DB. Will be updating the DB Values" + name);
1013 brmsGroupInfo = (BRMSGroupInfo) groupList.get(0);
1015 if(brmsGroupInfo==null){
1016 brmsGroupInfo = new BRMSGroupInfo();
1018 brmsGroupInfo.setControllerName(name);
1019 brmsGroupInfo.setGroupId(dependency.getGroupId());
1020 brmsGroupInfo.setArtifactId(dependency.getArtifactId());
1021 brmsGroupInfo.setVersion(dependency.getVersion());
1022 em.persist(brmsGroupInfo);
1027 private String getArtifactID(String name){
1028 return ((PEDependency) groupMap.get(name).get(0)).getArtifactId();
1031 private String getGroupID(String name){
1032 return ((PEDependency) groupMap.get(name).get(0)).getGroupId();
1035 private String getVersion(String name){
1036 return ((PEDependency) groupMap.get(name).get(0)).getVersion();
1039 private void getNameAndSetRemove(String controllerName, String policyName) {
1040 String artifactName = getArtifactID(controllerName);
1041 String ruleFolder= PROJECTSLOCATION + File.separator
1042 + artifactName + File.separator + "src"
1043 + File.separator + "main" + File.separator
1044 + "resources" + File.separator + "rules";
1045 File file = new File(ruleFolder+File.separator+ policyName +".drl");
1047 LOGGER.info("Deleted File.. " + file.getAbsolutePath());
1048 removePolicyFromGroup(policyName, controllerName);
1050 if(new File(ruleFolder).listFiles().length == 0) {
1051 removedRuleModifiedGroup(controllerName);
1053 // This is an update in terms of PDPD.
1054 addModifiedGroup(controllerName, "update");
1058 // Removes Policy from Memory and Database.
1059 private void removePolicyFromGroup(String policyName, String controllerName) {
1060 policyMap.remove(policyName);
1061 EntityTransaction et = em.getTransaction();
1063 Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn");
1064 query.setParameter("pn", policyName);
1065 List<?> pList = query.getResultList();
1066 BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo();
1069 brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
1070 if(brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)){
1071 em.remove(brmsPolicyInfo);
1078 private void setVersion(String newVersion, String controllerName) {
1079 PEDependency userController = (PEDependency) groupMap.get(controllerName).get(0);
1080 userController.setVersion(newVersion);
1081 groupMap.get(controllerName).set(0, userController);
1084 // Return BackUpMonitor
1085 public static BackUpMonitor getBackUpMonitor(){
1089 public void rotateURLs() {
1091 Collections.rotate(repURLs, -1);
1095 public int URLListSize() {
1097 return repURLs.size();