2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
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.onap.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;
49 import java.util.regex.Pattern;
51 import javax.persistence.EntityManager;
52 import javax.persistence.EntityManagerFactory;
53 import javax.persistence.EntityTransaction;
54 import javax.persistence.Persistence;
55 import javax.persistence.Query;
57 import org.apache.commons.io.FileUtils;
58 import org.apache.commons.lang.StringEscapeUtils;
59 import org.apache.maven.model.Dependency;
60 import org.apache.maven.model.DeploymentRepository;
61 import org.apache.maven.model.DistributionManagement;
62 import org.apache.maven.model.Model;
63 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
64 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
65 import org.apache.maven.shared.invoker.DefaultInvoker;
66 import org.apache.maven.shared.invoker.InvocationRequest;
67 import org.apache.maven.shared.invoker.InvocationResult;
68 import org.apache.maven.shared.invoker.Invoker;
69 import org.codehaus.plexus.util.IOUtil;
70 import org.codehaus.plexus.util.WriterFactory;
71 import org.eclipse.persistence.config.PersistenceUnitProperties;
72 import org.onap.policy.api.PEDependency;
73 import org.onap.policy.api.PolicyException;
74 import org.onap.policy.brmsInterface.jpa.BRMSGroupInfo;
75 import org.onap.policy.brmsInterface.jpa.BRMSPolicyInfo;
76 import org.onap.policy.brmsInterface.jpa.DependencyInfo;
77 import org.onap.policy.common.im.IntegrityMonitor;
78 import org.onap.policy.common.logging.eelf.MessageCodes;
79 import org.onap.policy.common.logging.eelf.PolicyLogger;
80 import org.onap.policy.common.logging.flexlogger.FlexLogger;
81 import org.onap.policy.common.logging.flexlogger.Logger;
82 import org.onap.policy.utils.BackUpHandler;
83 import org.onap.policy.utils.BackUpMonitor;
84 import org.onap.policy.utils.BusPublisher;
85 import org.onap.policy.utils.PolicyUtils;
86 import org.onap.policy.xacml.api.XACMLErrorConstants;
87 import org.sonatype.nexus.client.NexusClient;
88 import org.sonatype.nexus.client.NexusClientException;
89 import org.sonatype.nexus.client.NexusConnectionException;
90 import org.sonatype.nexus.client.rest.NexusRestClient;
91 import org.sonatype.nexus.rest.model.NexusArtifact;
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). Mavenize and push policy
105 @SuppressWarnings("deprecation")
106 public class BRMSPush {
107 private static final String GROUP_NAMES = "groupNames";
108 private static final String DROOLS_APPS_TEMPLATE_GROUP = "org.onap.policy.drools-applications.controlloop.templates";
109 private static final String DROOLS_APPS_MODEL_GROUP = "org.onap.policy.drools-applications.controlloop.common.model-impl";
110 private static final String META_INF = "META-INF";
111 private static final String KMODULE_XML_FILE = "kmodule.xml";
112 private static final String POM_XML_FILE = "pom.xml";
113 private static final String VERSION_0_1_0 = "0.1.0";
114 private static final String RULES = "rules";
115 private static final String RESOURCES = "resources";
116 private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName());
117 private static final String PROJECTSLOCATION = "RuleProjects";
118 private static final String[] GOALS = { "clean", "deploy" };
119 private static final String DEFAULT_VERSION = "1.2.0-SNAPSHOT";
120 private static final String DEPENDENCY_FILE = "dependency.json";
121 private static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml";
123 private static Map<String, String> modifiedGroups = new HashMap<>();
124 private static IntegrityMonitor im;
125 private static BackUpMonitor bm;
126 private String defaultName = null;
127 private String repID = null;
128 private String repName = null;
129 private ArrayList<String> repURLs = null;
130 private String repUserName = null;
131 private String repPassword = null;
132 private String policyKeyID = null;
133 private boolean createFlag = false;
134 private String uebList = null;
135 private List<String> dmaapList = null;
136 private String pubTopic = null;
137 private PublisherBuilder pubBuilder = null;
138 protected BusPublisher publisher = null;
139 private Long uebDelay = Long.parseLong("20");
140 private Long dmaapDelay = Long.parseLong("5000");
141 private String notificationType = null;
142 private ArrayList<ControllerPOJO> controllers;
143 private HashMap<String, ArrayList<Object>> groupMap = new HashMap<>();
144 private Map<String, String> policyMap = new HashMap<>();
145 private String brmsdependencyversion;
146 private EntityManager em;
147 private boolean syncFlag = false;
149 public BRMSPush(String propertiesFile, BackUpHandler handler) throws PolicyException {
150 if(propertiesFile==null || handler==null){
151 throw new PolicyException("Error no propertiesFile or handler");
153 Properties config = new Properties();
154 Path file = Paths.get(propertiesFile);
155 if (Files.notExists(file)) {
156 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Config File doesn't Exist in the specified Path "
158 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
159 + "Config File doesn't Exist in the specified Path " + file.toString());
161 if (file.toString().endsWith(".properties")) {
162 // Grab the Properties.
163 setProperty(file, config, handler);
168 private void setProperty(Path file, Properties config, BackUpHandler handler) throws PolicyException {
171 in = new FileInputStream(file.toFile());
173 } catch (IOException e) {
175 XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file.",
177 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
178 + "Data/File Read Error while reading from the property file.");
180 LOGGER.info("Trying to set up IntegrityMonitor");
181 String resourceName = null;
183 LOGGER.info("Trying to set up IntegrityMonitor");
184 resourceName = config.getProperty("RESOURCE_NAME");
185 if (resourceName == null) {
186 LOGGER.warn("RESOURCE_NAME is missing setting default value. ");
187 resourceName = "brmsgw_pdp01";
189 resourceName = resourceName.trim();
190 setIntegrityMonitor(IntegrityMonitor.getInstance(resourceName, config));
191 } catch (Exception e) {
192 LOGGER.error("Error starting Integerity Monitor: " + e);
194 LOGGER.info("Trying to set up BackUpMonitor");
196 setBackupMonitor(BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config,
198 } catch (Exception e) {
199 LOGGER.error("Error starting BackUpMonitor: " + e);
201 if(!config.containsKey(BRMSPERSISTENCE)){
202 config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml");
204 config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, config.getProperty(BRMSPERSISTENCE,"META-INF/persistenceBRMS.xml"));
206 EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config);
207 em = emf.createEntityManager();
208 defaultName = config.getProperty("defaultName");
209 if (defaultName == null) {
211 XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file ");
212 throw new PolicyException(
213 XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file");
215 defaultName = defaultName.trim();
216 repID = config.getProperty("repositoryID");
219 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
220 throw new PolicyException(
221 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
223 repID = repID.trim();
224 repName = config.getProperty("repositoryName");
225 if (repName == null) {
226 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
227 + "repositoryName property is missing from the property file ");
228 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
229 + "repositoryName property is missing from the property file ");
231 repName = repName.trim();
232 String repURL = config.getProperty("repositoryURL");
233 if (repURL == null) {
235 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
236 throw new PolicyException(
237 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
239 if (repURL.contains(",")) {
240 repURLs = new ArrayList<>(Arrays.asList(repURL.trim().split(",")));
242 repURLs = new ArrayList<>();
245 repUserName = config.getProperty("repositoryUsername");
246 repPassword = config.getProperty("repositoryPassword");
247 if (repUserName == null || repPassword == null) {
248 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
249 + "repostoryUserName and respositoryPassword properties are required.");
250 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
251 + "repostoryUserName and respositoryPassword properties are required.");
253 repUserName = repUserName.trim();
254 repPassword = repPassword.trim();
255 policyKeyID = config.getProperty("policyKeyID");
256 if (policyKeyID == null) {
258 XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
259 throw new PolicyException(
260 XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
262 policyKeyID = policyKeyID.trim();
263 String syncF = config.getProperty("sync", "false").trim();
264 syncFlag = Boolean.parseBoolean(syncF);
266 PolicyLogger.info("SYNC Flag is turned ON. DB will be given Priority.");
268 brmsdependencyversion = config.getProperty("brms.dependency.version");
269 if (brmsdependencyversion == null) {
270 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
271 + "brmsdependencyversion property is missing from the property file, Using default Version.");
272 brmsdependencyversion = DEFAULT_VERSION;
274 brmsdependencyversion = brmsdependencyversion.trim();
278 notificationType = config.getProperty("NOTIFICATION_TYPE");
279 if ("dmaap".equalsIgnoreCase(notificationType)) {
281 LOGGER.info("Notification Type being used is DMaaP... creating instance of BusPublisher.");
282 // Setting up the Publisher for DMaaP MR
283 String dmaapServers = config.getProperty("NOTIFICATION_SERVERS");
284 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
285 String aafLogin = config.getProperty("CLIENT_ID").trim();
286 String aafPassword = config.getProperty("CLIENT_KEY").trim();
288 if (dmaapServers == null || pubTopic == null) {
290 XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
291 throw new PolicyException(
292 XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
295 dmaapServers = dmaapServers.trim();
296 pubTopic = pubTopic.trim();
298 if (dmaapServers.contains(",")) {
299 dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
301 dmaapList = new ArrayList<>();
302 dmaapList.add(dmaapServers);
305 this.publisher = new BusPublisher.DmaapPublisherWrapper(this.dmaapList, this.pubTopic, aafLogin,
308 String dDelay = config.getProperty("NOTIFICATION_DELAY");
309 if (dDelay != null && !dDelay.isEmpty()) {
310 dDelay = dDelay.trim();
312 dmaapDelay = Long.parseLong(dDelay);
313 } catch (NumberFormatException e) {
314 LOGGER.error("DMAAP_DELAY not a long format number" + e);
317 LOGGER.info("DMAAP BusPublisher is created.");
320 LOGGER.info("Notification Type being used is UEB... creating instance of PublisherBuilder.");
321 // Setting up the Publisher for UEB
322 uebList = config.getProperty("NOTIFICATION_SERVERS");
323 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
324 String apiKey = config.getProperty("UEB_API_KEY");
325 String apiSecret = config.getProperty("UEB_API_SECRET");
326 if (uebList == null || pubTopic == null) {
328 XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
329 throw new PolicyException(
330 XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
332 uebList = uebList.trim();
333 pubTopic = pubTopic.trim();
334 pubBuilder = new CambriaClientBuilders.PublisherBuilder();
335 pubBuilder.usingHosts(uebList).onTopic(pubTopic);
336 if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) {
337 apiKey = apiKey.trim();
338 apiSecret = apiSecret.trim();
339 pubBuilder.authenticatedBy(apiKey, apiSecret);
341 String uDelay = config.getProperty("NOTIFICATION_DELAY");
342 if (uDelay != null && !uDelay.isEmpty()) {
343 uDelay = uDelay.trim();
345 uebDelay = Long.parseLong(uDelay);
346 } catch (NumberFormatException e) {
347 LOGGER.error("UEB_DELAY not a long format number" + e);
350 LOGGER.info("UEB PublisherBuilder is created.");
356 private static void setBackupMonitor(BackUpMonitor instance) {
360 private static void setIntegrityMonitor(IntegrityMonitor instance) {
365 * Will Initialize the variables required for BRMSPush.
367 public void initiate(boolean flag) {
368 resetModifiedGroups();
369 controllers = new ArrayList<>();
371 bm.updateNotification();
372 } catch (Exception e) {
373 LOGGER.error("Error while updating Notification: " + e.getMessage(), e);
379 public void resetDS(){
380 resetModifiedGroups();
381 controllers = new ArrayList<>();
384 private static void resetModifiedGroups() {
385 modifiedGroups = new HashMap<>();
389 * Will Add rules to projects. Creates necessary folders if required.
391 public void addRule(String name, String rule, Map<String, String> responseAttributes) {
392 // 1 check the response Attributes and determine if this belongs to any projects.
393 // 2 if not create folder
396 // 5 store the groups that have been updated.
397 String kSessionName = null;
398 String selectedName = null;
399 if (!responseAttributes.isEmpty()) {
400 // Pick selected Value
401 String userControllerName = null;
402 ArrayList<PEDependency> userDependencies = new ArrayList<>();
403 for (Map.Entry<String, String> entry: responseAttributes.entrySet()) {
404 String key = entry.getKey();
405 String value = entry.getValue();
406 if (key.equals(policyKeyID)) {
407 selectedName = value;
409 // kmodule configurations
410 else if ("kSessionName".equals(key)) {
411 kSessionName = value;
413 // Check User Specific values.
414 if ("$controller:".equals(key)) {
416 PEDependency dependency = PolicyUtils.jsonStringToObject(value,
418 userControllerName = key.replaceFirst("$controller:", "");
419 LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - " + dependency);
420 addToGroup(userControllerName, dependency);
421 } catch (Exception e) {
422 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e);
425 } else if ("$dependency$".equals(key) && value.startsWith("[") && value.endsWith("]")) {
426 value = value.substring(1, value.length() - 1).trim();
427 List<String> dependencyStrings = Arrays.asList(value.split(Pattern.quote("},{")));
428 for (String dependencyString : dependencyStrings) {
431 .add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class));
432 } catch (Exception e) {
433 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
434 + "Error while resolving Dependencies: " + e);
439 if (userControllerName != null) {
440 // Adding custom dependencies here.
441 ArrayList<Object> values = groupMap.get(userControllerName);
442 values.add(userDependencies);
443 groupMap.put(userControllerName, values);
444 selectedName = userControllerName;
447 // If no Match then pick Default.
448 if (selectedName == null) {
449 selectedName = defaultName;
451 if (groupMap.containsKey(selectedName)) {
452 // If the key is not got as parameters set by the user, setting the default value for kSessionName as
454 if (kSessionName == null) {
455 LOGGER.info("kSessionName is null, selectedName is : " + selectedName );
456 if (selectedName.equalsIgnoreCase(defaultName)) {
457 kSessionName = "closedloop";
459 kSessionName = "closedloop-" + selectedName;
462 // create directories if missing.
463 manageProject(selectedName, kSessionName, name, rule);
464 addModifiedGroup(selectedName, "update"); // Will check for Create Later after generating the Pom.
468 private void syncGroupInfo() {
469 // Sync DB to JMemory.
470 EntityTransaction et = em.getTransaction();
472 Query query = em.createQuery("select b from BRMSGroupInfo AS b");
473 List<?> bList = query.getResultList();
474 if (bList.size() != groupMap.size()) {
475 for (Object value : bList) {
476 BRMSGroupInfo brmsGroupInfo = (BRMSGroupInfo) value;
477 PEDependency dependency = new PEDependency();
478 dependency.setArtifactId(brmsGroupInfo.getArtifactId());
479 dependency.setGroupId(brmsGroupInfo.getGroupId());
480 dependency.setVersion(brmsGroupInfo.getVersion());
481 ArrayList<Object> values = new ArrayList<>();
482 values.add(dependency);
483 groupMap.put(brmsGroupInfo.getControllerName(), values);
486 query = em.createQuery("select g from BRMSPolicyInfo AS g");
487 bList = query.getResultList();
488 if (bList.size() != policyMap.size()) {
489 for (Object value : bList) {
490 BRMSPolicyInfo brmsPolicyInfo = (BRMSPolicyInfo) value;
491 policyMap.put(brmsPolicyInfo.getPolicyName(), brmsPolicyInfo.getControllerName().getControllerName());
495 LOGGER.info("Updated Local Memory values with values from database.");
498 private void manageProject(String selectedName, String kSessionName, String name, String rule) {
499 // Check if the Project is in Sync. If not get the latest Version.
500 syncProject(selectedName);
501 createProject(PROJECTSLOCATION + File.separator + getArtifactID(selectedName) + File.separator + "src"
502 + File.separator + "main" + File.separator + RESOURCES, kSessionName);
503 copyDataToFile(PROJECTSLOCATION + File.separator + getArtifactID(selectedName) + File.separator + "src"
504 + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES + File.separator
505 + name + ".drl", rule);
506 addToPolicy(name, selectedName);
510 * Add Policy to JMemory and DataBase.
512 private void addToPolicy(String policyName, String controllerName) {
513 policyMap.put(policyName, controllerName);
514 EntityTransaction et = em.getTransaction();
516 Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn");
517 query.setParameter("pn", policyName);
518 List<?> pList = query.getResultList();
519 boolean create = false;
520 BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo();
521 if (!pList.isEmpty()) {
523 brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
524 if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) {
531 query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn");
532 query.setParameter("cn", controllerName);
533 List<?> bList = query.getResultList();
534 BRMSGroupInfo brmsGroupInfo = new BRMSGroupInfo();
535 if (!bList.isEmpty()) {
536 brmsGroupInfo = (BRMSGroupInfo) bList.get(0);
538 brmsPolicyInfo.setPolicyName(policyName);
539 brmsPolicyInfo.setControllerName(brmsGroupInfo);
540 em.persist(brmsPolicyInfo);
546 private void syncProject(String selectedName) {
547 boolean projectExists = checkProject(selectedName);
550 version = getVersion(selectedName);
551 if (version == null) {
552 LOGGER.error("Error getting local version for the given Controller Name:" + selectedName
553 + " going with Default value");
554 version = VERSION_0_1_0;
556 String nextVersion = incrementVersion(version);
557 boolean outOfSync = checkRemoteSync(selectedName, nextVersion);
562 // We are out of Sync or Project is not Present.
563 downloadProject(selectedName);
566 private void downloadProject(String selectedName) {
567 NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
568 if (artifact == null)
570 String dirName = getDirectoryName(selectedName);
572 String fileName = "rule.jar";
574 website = new URL(artifact.getResourceURI());
575 try( ReadableByteChannel rbc = Channels.newChannel(website.openStream());
576 FileOutputStream fos = new FileOutputStream(fileName)){
577 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
578 extractJar(fileName, dirName);
579 new File(fileName).delete();
580 }catch (IOException e) {
581 LOGGER.error("Error while downloading the project to File System. " + e.getMessage(), e);
584 } catch (IOException e1) {
585 LOGGER.error("Error while retrieve the artifact. " + e1.getMessage(), e1);
589 private void extractJar(String jarFileName, String artifactId){
590 try (JarFile jar = new JarFile(jarFileName)) {
591 Enumeration<?> enumEntries = jar.entries();
592 while (enumEntries.hasMoreElements()) {
593 JarEntry file = (JarEntry) enumEntries.nextElement();
595 String fileName = file.getName().substring(file.getName().lastIndexOf("/") + 1);
596 if (file.getName().endsWith(".drl")) {
597 String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
598 + "main" + File.separator + RESOURCES + File.separator + RULES;
599 new File(path).mkdirs();
600 if (syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))) {
601 f = new File(path + File.separator + fileName);
603 f = new File(path + File.separator + fileName);
605 } else if (file.getName().endsWith(POM_XML_FILE)) {
606 String path = PROJECTSLOCATION + File.separator + artifactId;
607 new File(path).mkdirs();
608 f = new File(path + File.separator + fileName);
609 } else if (file.getName().endsWith(KMODULE_XML_FILE)) {
610 String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
611 + "main" + File.separator + RESOURCES + File.separator + META_INF;
612 new File(path).mkdirs();
613 f = new File(path + File.separator + fileName);
616 try (InputStream is = jar.getInputStream(file); FileOutputStream fos = new FileOutputStream(f)) {
617 while (is.available() > 0) {
618 fos.write(is.read());
620 LOGGER.info(fileName + " Created..");
621 } catch (IOException e) {
622 LOGGER.info("exception Occured" + e);
626 } catch (IOException e) {
627 LOGGER.info("exception Occured" + e);
631 private NexusArtifact getLatestArtifactFromNexus(String selectedName) {
632 List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, null);
635 NexusArtifact result = null;
636 for (NexusArtifact artifact : artifacts) {
637 int majorVal = Integer.parseInt(artifact.getVersion().substring(0, artifact.getVersion().indexOf(".")));
638 int minorVal = Integer.parseInt(artifact.getVersion().substring(artifact.getVersion().indexOf(".") + 1,
639 artifact.getVersion().lastIndexOf(".")));
640 if (majorVal > bigNum) {
644 if ((bigNum == majorVal) && (minorVal > smallNum)) {
647 if (bigNum == majorVal && minorVal == smallNum) {
651 return additionalNexusLatestCheck(selectedName, result);
654 // Additional Check due to Limitations from Nexus API to check if the artifact is the latest.
655 private NexusArtifact additionalNexusLatestCheck(String selectedName, NexusArtifact result) {
659 String nextVersion = incrementVersion(result.getVersion());
660 List<NexusArtifact> artifact = getArtifactFromNexus(selectedName, nextVersion);
661 return artifact.isEmpty() ? result : additionalNexusLatestCheck(selectedName, artifact.get(0));
664 private boolean checkRemoteSync(String selectedName, String version) {
665 List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, version);
666 return artifacts.isEmpty() ? false : true;
669 private List<NexusArtifact> getArtifactFromNexus(String selectedName, String version) {
670 final NexusClient client = new NexusRestClient();
672 boolean flag = false;
673 while (i < repURLs.size()) {
675 String repURL = repURLs.get(0);
676 client.connect(repURL.substring(0, repURL.indexOf(repURL.split(":[0-9]+\\/nexus")[1])), repUserName,
678 final NexusArtifact template = new NexusArtifact();
679 template.setGroupId(getGroupID(selectedName));
680 template.setArtifactId(getArtifactID(selectedName));
681 if (version != null) {
682 template.setVersion(version);
684 List<NexusArtifact> resultList = client.searchByGAV(template);
685 if (resultList != null) {
689 } catch (NexusClientException | NexusConnectionException | NullPointerException e) {
690 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. "
691 + e.getMessage(), e);
695 } catch (NexusClientException | NexusConnectionException e) {
696 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus."
697 + e.getMessage(), e);
700 Collections.rotate(repURLs, -1);
705 return new ArrayList<>();
708 private void setVersion(String selectedName) {
709 String newVersion = VERSION_0_1_0;
711 NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
712 if (artifact != null) {
713 newVersion = incrementVersion(artifact.getVersion());
715 if (VERSION_0_1_0.equals(newVersion)) {
718 setVersion(newVersion, selectedName);
719 LOGGER.info("Controller: " + selectedName + "is on version: " + newVersion);
722 private String incrementVersion(String version) {
723 int majorVal = Integer.parseInt(version.substring(0, version.indexOf(".")));
724 int minorVal = Integer.parseInt(version.substring(version.indexOf(".") + 1, version.lastIndexOf(".")));
731 return majorVal + "." + minorVal + version.substring(version.lastIndexOf("."));
734 private boolean checkProject(String selectedName) {
735 return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists();
738 private String getDirectoryName(String selectedName) {
739 return getArtifactID(selectedName);
743 * Will Push policies to the PolicyRepo.
745 * @param notificationType
746 * <String> type of notification Type.
747 * @throws PolicyException
749 public void pushRules() throws PolicyException {
750 // Check how many groups have been updated.
751 // Invoke their Maven process.
753 im.startTransaction();
754 } catch (Exception e) {
755 LOGGER.error("Error while starting Transaction " + e);
757 if (!modifiedGroups.isEmpty()) {
758 Boolean flag = false;
759 for (Map.Entry<String, String> entry : modifiedGroups.entrySet()) {
760 InvocationResult result = null;
761 String group = entry.getKey();
763 LOGGER.info("PushRules: ModifiedGroups, Key: " + group + ", Value: " + entry.getValue());
764 InvocationRequest request = new DefaultInvocationRequest();
767 request.setPomFile(new File(
768 PROJECTSLOCATION + File.separator + getArtifactID(group) + File.separator + POM_XML_FILE));
769 request.setGoals(Arrays.asList(GOALS));
770 Invoker invoker = new DefaultInvoker();
771 result = invoker.execute(request);
772 if (result.getExecutionException() != null) {
773 LOGGER.error(result.getExecutionException());
774 } else if (result.getExitCode() != 0) {
775 LOGGER.error("Maven Invocation failure..!");
777 } catch (Exception e) {
778 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation issue for "
779 + getArtifactID(group) + e.getMessage(), e);
781 if (result != null && result.getExitCode() == 0) {
782 LOGGER.info("Build Completed..!");
784 addNotification(group, "create");
786 addNotification(group, entry.getValue());
790 throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!");
794 sendNotification(controllers);
803 * Removes a Rule from Rule Projects.
805 public void removeRule(String name) {
806 String controllerName = getGroupName(name);
807 if (controllerName == null) {
808 LOGGER.info("Error finding the controllerName for the given Policy: " + name);
811 syncProject(controllerName);
812 getNameAndSetRemove(controllerName, name);
815 private String getGroupName(String name) {
816 if (policyMap.containsKey(name)) {
817 return policyMap.get(name);
820 return policyMap.containsKey(name) ? policyMap.get(name) : null;
824 private void addModifiedGroup(String controllerName, String operation) {
825 if (controllerName != null) {
826 modifiedGroups.put(controllerName, operation);
830 private void addNotification(String controllerName, String operation) {
831 ControllerPOJO controllerPOJO = new ControllerPOJO();
832 controllerPOJO.setName(controllerName);
833 controllerPOJO.setOperation(operation);
834 HashMap<String, String> drools = new HashMap<>();
835 drools.put("groupId", getGroupID(controllerName));
836 drools.put("artifactId", getArtifactID(controllerName));
837 drools.put("version", getVersion(controllerName));
838 controllerPOJO.setDrools(drools);
839 controllers.add(controllerPOJO);
841 LOGGER.debug("Notification added: " + PolicyUtils.objectToJsonString(controllerPOJO));
842 } catch (JsonProcessingException e) {
843 LOGGER.error(MessageCodes.ERROR_SCHEMA_INVALID + "Json Processing Error " + e);
847 private void removedRuleModifiedGroup(String controllerName) {
848 // This will be sending Notification to PDPD directly to Lock
849 ControllerPOJO controllerPOJO = new ControllerPOJO();
850 controllerPOJO.setName(controllerName);
851 controllerPOJO.setOperation("lock");
852 List<ControllerPOJO> controllerPojos = new ArrayList<>();
853 controllerPojos.add(controllerPOJO);
854 sendNotification(controllerPojos);
857 private void sendNotification(List<ControllerPOJO> controllers) {
858 NotificationPOJO notification = new NotificationPOJO();
859 String requestId = UUID.randomUUID().toString();
860 LOGGER.info("Generating notification RequestID : " + requestId);
861 notification.setRequestID(requestId);
862 notification.setEntity("controller");
863 notification.setControllers(controllers);
865 String notificationJson = PolicyUtils.objectToJsonString(notification);
866 LOGGER.info("Sending Notification :\n" + notificationJson);
867 sendMessage(notificationJson);
868 } catch (Exception e) {
869 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while sending notification to PDP-D "
870 + e.getMessage(), e);
874 private void sendMessage(String message) throws IOException, GeneralSecurityException, InterruptedException {
876 if ("dmaap".equalsIgnoreCase(notificationType)) {
877 // Sending Message through DMaaP Message Router
878 LOGGER.debug("DMAAP Publishing Message");
880 publisher.send("MyPartitionKey", message);
882 LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic);
884 Thread.sleep(dmaapDelay);
887 // Sending Message through UEB interface.
888 LOGGER.debug("UEB Publishing Message");
890 CambriaBatchingPublisher pub = pubBuilder.build();
891 pub.send("MyPartitionKey", message);
893 final List<?> stuck = pub.close(uebDelay, TimeUnit.SECONDS);
894 if (!stuck.isEmpty()) {
895 LOGGER.error(stuck.size() + " messages unsent");
897 LOGGER.debug("Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic);
903 private void createPom(String name) {
904 Model model = new Model();
905 model.setModelVersion("4.0.0");
906 model.setGroupId(getGroupID(name));
907 model.setArtifactId(getArtifactID(name));
908 model.setVersion(getVersion(name));
910 DistributionManagement distributionManagement = new DistributionManagement();
911 DeploymentRepository repository = new DeploymentRepository();
912 repository.setId(repID);
913 repository.setName(repName);
914 repository.setUrl(repURLs.get(0));
915 distributionManagement.setRepository(repository);
916 model.setDistributionManagement(distributionManagement);
917 // Dependency Management goes here.
918 List<Dependency> dependencyList = new ArrayList<>();
919 if (groupMap.get(name).size() > 1) {
920 @SuppressWarnings("unchecked")
921 ArrayList<PEDependency> dependencies = (ArrayList<PEDependency>) groupMap.get(name).get(1);
922 for (PEDependency dependency : dependencies) {
923 dependencyList.add(dependency.getDependency());
926 // Add Default dependencies.
927 dependencyList = getDependencies(name);
929 model.setDependencies(dependencyList);
930 Writer writer = null;
932 writer = WriterFactory.newXmlWriter(
933 new File(PROJECTSLOCATION + File.separator + getArtifactID(name) + File.separator + POM_XML_FILE));
934 MavenXpp3Writer pomWriter = new MavenXpp3Writer();
935 pomWriter.write(writer, model);
936 } catch (Exception e) {
937 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating POM for " + getArtifactID(name)
938 + e.getMessage(), e);
940 IOUtil.close(writer);
944 private List<Dependency> getDependencies(String controllerName) {
945 // Read the Dependency Information from property file.
946 Path file = Paths.get(DEPENDENCY_FILE);
947 if (!Files.notExists(file)) {
949 String dependencyJSON = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
950 DependencyInfo dependencyInfo = PolicyUtils.jsonStringToObject(dependencyJSON, DependencyInfo.class);
951 String controller = "default";
952 if (dependencyInfo.getDependencies().containsKey(controllerName)) {
953 controller = controllerName;
955 List<Dependency> dependencyList = new ArrayList<>();
956 for (PEDependency dependency : dependencyInfo.getDependencies().get(controller)) {
957 dependencyList.add(dependency.getDependency());
959 return dependencyList;
960 } catch (IOException | NullPointerException e) {
961 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
962 + "Error while getting dependecy Information for controller: " + controllerName
963 + e.getMessage(), e);
966 return defaultDependencies(controllerName);
969 // Default Dependency Section. Can be changed as required.
970 public List<Dependency> defaultDependencies(String controllerName) {
972 List<Dependency> dependencyList = new ArrayList<>();
973 String version = StringEscapeUtils.escapeJava(brmsdependencyversion);
975 Dependency demoDependency = new Dependency();
976 demoDependency.setGroupId(DROOLS_APPS_TEMPLATE_GROUP);
977 demoDependency.setArtifactId("template.demo");
978 demoDependency.setVersion(version);
979 dependencyList.add(demoDependency);
981 Dependency controlloopDependency = new Dependency();
982 controlloopDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
983 controlloopDependency.setArtifactId("events");
984 controlloopDependency.setVersion(version);
985 dependencyList.add(controlloopDependency);
987 Dependency restDependency = new Dependency();
988 restDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
989 restDependency.setArtifactId("controlloop.common.model-impl.rest");
990 restDependency.setVersion(version);
991 dependencyList.add(restDependency);
993 Dependency appcDependency = new Dependency();
994 appcDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
995 appcDependency.setArtifactId("controlloop.common.model-impl.appc");
996 appcDependency.setVersion(version);
997 dependencyList.add(appcDependency);
999 Dependency aaiDependency = new Dependency();
1000 aaiDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1001 aaiDependency.setArtifactId("controlloop.common.model-impl.aai");
1002 aaiDependency.setVersion(version);
1003 dependencyList.add(aaiDependency);
1005 Dependency msoDependency = new Dependency();
1006 msoDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1007 msoDependency.setArtifactId("controlloop.common.model-impl.so");
1008 msoDependency.setVersion(version);
1009 dependencyList.add(msoDependency);
1011 Dependency trafficgeneratorDependency = new Dependency();
1012 trafficgeneratorDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1013 trafficgeneratorDependency.setArtifactId("controlloop.common.model-impl.trafficgenerator");
1014 trafficgeneratorDependency.setVersion(version);
1015 dependencyList.add(trafficgeneratorDependency);
1016 return dependencyList;
1019 private void createProject(String path, String ksessionName) {
1020 new File(path + File.separator + RULES).mkdirs();
1021 new File(path + File.separator + META_INF).mkdirs();
1022 if (!Files.exists(Paths.get(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE))) {
1023 // Hard coding XML for PDP Drools to accept our Rules.
1024 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"
1025 + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">" + "\n"
1026 + "<kbase name=\"rules\" packages=\"rules\">" + "\n" + "<ksession name=\"" + ksessionName + "\"/>"
1027 + "\n" + "</kbase></kmodule>";
1028 copyDataToFile(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE, xml);
1032 private void copyDataToFile(String file, String rule) {
1034 FileUtils.writeStringToFile(new File(file), rule);
1035 } catch (Exception e) {
1037 XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating Rule for " + file + e.getMessage(),
1042 private void readGroups(Properties config) throws PolicyException {
1043 String[] groupNames;
1044 String groupNamesError = "groupNames property is missing or empty from the property file ";
1045 if (!config.containsKey(GROUP_NAMES) || config.getProperty(GROUP_NAMES)==null){
1046 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
1049 if (config.getProperty(GROUP_NAMES).contains(",")) {
1050 groupNames = config.getProperty(GROUP_NAMES).replaceAll(" ", "").split(",");
1052 groupNames = new String[] { config.getProperty(GROUP_NAMES).replaceAll(" ", "") };
1054 if (groupNames == null || groupNames.length == 0) {
1055 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
1057 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
1060 groupMap = new HashMap<>();
1061 for (int counter = 0; counter < groupNames.length; counter++) {
1062 String name = groupNames[counter];
1063 String groupID = config.getProperty(name + ".groupID");
1064 if (groupID == null) {
1065 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1066 + ".groupID property is missing from the property file ");
1067 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1068 + ".groupID property is missing from the property file ");
1070 String artifactID = config.getProperty(name + ".artifactID");
1071 if (artifactID == null) {
1072 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1073 + ".artifactID property is missing from the property file ");
1074 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1075 + ".artifactID property is missing from the property file ");
1077 PEDependency dependency = new PEDependency();
1078 dependency.setArtifactId(artifactID);
1079 dependency.setGroupId(groupID);
1080 // Add to list if we got all
1081 addToGroup(name, dependency);
1085 private void addToGroup(String name, PEDependency dependency) {
1086 ArrayList<Object> values = new ArrayList<>();
1087 values.add(dependency);
1088 groupMap.put(name, values);
1089 EntityTransaction et = em.getTransaction();
1091 Query query = em.createQuery("select b from BRMSGroupInfo as b where b.controllerName = :cn");
1092 query.setParameter("cn", name);
1093 List<?> groupList = query.getResultList();
1094 BRMSGroupInfo brmsGroupInfo = null;
1095 if (!groupList.isEmpty()) {
1096 LOGGER.info("Controller name already Existing in DB. Will be updating the DB Values" + name);
1097 brmsGroupInfo = (BRMSGroupInfo) groupList.get(0);
1099 if (brmsGroupInfo == null) {
1100 brmsGroupInfo = new BRMSGroupInfo();
1102 brmsGroupInfo.setControllerName(name);
1103 brmsGroupInfo.setGroupId(dependency.getGroupId());
1104 brmsGroupInfo.setArtifactId(dependency.getArtifactId());
1105 brmsGroupInfo.setVersion(dependency.getVersion());
1106 em.persist(brmsGroupInfo);
1111 private String getArtifactID(String name) {
1112 return ((PEDependency) groupMap.get(name).get(0)).getArtifactId();
1115 private String getGroupID(String name) {
1116 return ((PEDependency) groupMap.get(name).get(0)).getGroupId();
1119 private String getVersion(String name) {
1120 return ((PEDependency) groupMap.get(name).get(0)).getVersion();
1123 private void getNameAndSetRemove(String controllerName, String policyName) {
1124 String artifactName = getArtifactID(controllerName);
1125 String ruleFolder = PROJECTSLOCATION + File.separator + artifactName + File.separator + "src" + File.separator
1126 + "main" + File.separator + RESOURCES + File.separator + RULES;
1127 File file = new File(ruleFolder + File.separator + policyName + ".drl");
1128 if (file.delete()) {
1129 LOGGER.info("Deleted File.. " + file.getAbsolutePath());
1130 removePolicyFromGroup(policyName, controllerName);
1132 if (new File(ruleFolder).listFiles().length == 0) {
1133 removedRuleModifiedGroup(controllerName);
1135 // This is an update in terms of PDPD.
1136 addModifiedGroup(controllerName, "update");
1140 // Removes Policy from Memory and Database.
1141 private void removePolicyFromGroup(String policyName, String controllerName) {
1142 policyMap.remove(policyName);
1143 EntityTransaction et = em.getTransaction();
1145 Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn");
1146 query.setParameter("pn", policyName);
1147 List<?> pList = query.getResultList();
1148 BRMSPolicyInfo brmsPolicyInfo;
1149 if (!pList.isEmpty()) {
1151 brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
1152 if (brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) {
1153 em.remove(brmsPolicyInfo);
1160 private void setVersion(String newVersion, String controllerName) {
1161 PEDependency userController = (PEDependency) groupMap.get(controllerName).get(0);
1162 userController.setVersion(newVersion);
1163 groupMap.get(controllerName).set(0, userController);
1166 // Return BackUpMonitor
1167 public static BackUpMonitor getBackUpMonitor() {
1171 public void rotateURLs() {
1172 if (repURLs != null) {
1173 Collections.rotate(repURLs, -1);
1177 public int urlListSize() {
1178 if (repURLs != null) {
1179 return repURLs.size();