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.brms.api;
23 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
24 import com.att.nsa.cambria.client.CambriaClientBuilders;
25 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
26 import com.fasterxml.jackson.core.JsonProcessingException;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.Writer;
35 import java.nio.channels.Channels;
36 import java.nio.channels.ReadableByteChannel;
37 import java.nio.charset.StandardCharsets;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.security.GeneralSecurityException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collections;
45 import java.util.Enumeration;
46 import java.util.HashMap;
47 import java.util.List;
49 import java.util.Properties;
50 import java.util.UUID;
51 import java.util.concurrent.TimeUnit;
52 import java.util.jar.JarEntry;
53 import java.util.jar.JarFile;
54 import java.util.regex.Pattern;
56 import javax.persistence.EntityManager;
57 import javax.persistence.EntityManagerFactory;
58 import javax.persistence.EntityTransaction;
59 import javax.persistence.Persistence;
60 import javax.persistence.Query;
61 import javax.ws.rs.ProcessingException;
63 import org.apache.commons.io.FileUtils;
64 import org.apache.commons.lang.StringEscapeUtils;
65 import org.apache.maven.model.Dependency;
66 import org.apache.maven.model.DeploymentRepository;
67 import org.apache.maven.model.DistributionManagement;
68 import org.apache.maven.model.Model;
69 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
70 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
71 import org.apache.maven.shared.invoker.DefaultInvoker;
72 import org.apache.maven.shared.invoker.InvocationRequest;
73 import org.apache.maven.shared.invoker.InvocationResult;
74 import org.apache.maven.shared.invoker.Invoker;
75 import org.codehaus.plexus.util.IOUtil;
76 import org.codehaus.plexus.util.WriterFactory;
77 import org.eclipse.persistence.config.PersistenceUnitProperties;
78 import org.onap.policy.api.PEDependency;
79 import org.onap.policy.api.PolicyException;
80 import org.onap.policy.brms.api.nexus.NexusRestSearchParameters;
81 import org.onap.policy.brms.api.nexus.NexusRestWrapper;
82 import org.onap.policy.brms.api.nexus.NexusRestWrapperException;
83 import org.onap.policy.brms.api.nexus.pojo.NexusArtifact;
84 import org.onap.policy.brms.entity.BrmsGroupInfo;
85 import org.onap.policy.brms.entity.BrmsPolicyInfo;
86 import org.onap.policy.brms.entity.DependencyInfo;
87 import org.onap.policy.common.im.IntegrityMonitor;
88 import org.onap.policy.common.logging.eelf.MessageCodes;
89 import org.onap.policy.common.logging.eelf.PolicyLogger;
90 import org.onap.policy.common.logging.flexlogger.FlexLogger;
91 import org.onap.policy.common.logging.flexlogger.Logger;
92 import org.onap.policy.utils.BackUpHandler;
93 import org.onap.policy.utils.BackUpMonitor;
94 import org.onap.policy.utils.BusPublisher;
95 import org.onap.policy.utils.PolicyUtils;
96 import org.onap.policy.xacml.api.XACMLErrorConstants;
99 * BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR).
100 * Mavenize and push policy to PR
105 public class BrmsPush {
106 private static final String GROUP_NAMES = "groupNames";
107 private static final String DROOLS_APPS_TEMPLATE_GROUP =
108 "org.onap.policy.drools-applications.controlloop.templates";
109 private static final String DROOLS_APPS_MODEL_GROUP =
110 "org.onap.policy.drools-applications.controlloop.common.model-impl";
111 private static final String META_INF = "META-INF";
112 private static final String KMODULE_XML_FILE = "kmodule.xml";
113 private static final String POM_XML_FILE = "pom.xml";
114 private static final String VERSION_0_1_0 = "0.1.0";
115 private static final String RULES = "rules";
116 private static final String RESOURCES = "resources";
117 private static final Logger LOGGER = FlexLogger.getLogger(BrmsPush.class.getName());
118 private static final String PROJECTSLOCATION = "RuleProjects";
119 private static final String[] GOALS = {"clean", "deploy"};
120 private static final String DEFAULT_VERSION = "1.2.1-SNAPSHOT";
121 private static final String DEPENDENCY_FILE = "dependency.json";
122 private static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml";
124 private static Map<String, String> modifiedGroups = new HashMap<>();
125 private static IntegrityMonitor im;
126 private static BackUpMonitor bm;
127 private String defaultName = null;
128 private String repId = null;
129 private String repName = null;
130 private List<String> repUrlList = null;
131 private String repUserName = null;
132 private String repPassword = null;
133 private String policyKeyId = null;
134 private boolean createFlag = false;
135 private String uebList = null;
136 private List<String> dmaapList = null;
137 private String pubTopic = null;
138 private PublisherBuilder pubBuilder = null;
139 protected BusPublisher publisher = null;
140 private Long uebDelay = Long.parseLong("20");
141 private Long dmaapDelay = Long.parseLong("5000");
142 private String notificationType = null;
143 private List<ControllerPojo> controllers;
144 private Map<String, ArrayList<Object>> groupMap = new HashMap<>();
145 private final Map<String, String> policyMap = new HashMap<>();
146 private String brmsdependencyversion;
147 private EntityManager em;
148 private boolean syncFlag = false;
151 * Responsible to push policies to the BRMS PDP Policy Repository (PR).
153 * @param propertiesFile the properties file
154 * @param handler the {@link BackUpHandler}
155 * @throws PolicyException PolicyException related to the operation
157 public BrmsPush(final String propertiesFile, final BackUpHandler handler) throws PolicyException {
158 if (propertiesFile == null || handler == null) {
159 throw new PolicyException("Error no propertiesFile or handler");
161 final Properties config = new Properties();
162 final Path file = Paths.get(propertiesFile);
163 if (Files.notExists(file)) {
164 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Config File doesn't Exist in the specified Path "
166 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
167 + "Config File doesn't Exist in the specified Path " + file.toString());
169 if (file.toString().endsWith(".properties")) {
170 // Grab the Properties.
171 setProperty(file, config, handler);
176 private void setProperty(final Path file, final Properties config, final BackUpHandler handler)
177 throws PolicyException {
180 in = new FileInputStream(file.toFile());
182 } catch (final IOException e) {
184 XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file.",
186 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
187 + "Data/File Read Error while reading from the property file.");
189 LOGGER.info("Trying to set up IntegrityMonitor");
190 String resourceName = null;
192 LOGGER.info("Trying to set up IntegrityMonitor");
193 resourceName = config.getProperty("RESOURCE_NAME");
194 if (resourceName == null) {
195 LOGGER.warn("RESOURCE_NAME is missing setting default value. ");
196 resourceName = "brmsgw_pdp01";
198 resourceName = resourceName.trim();
199 setIntegrityMonitor(IntegrityMonitor.getInstance(resourceName, config));
200 } catch (final Exception e) {
201 LOGGER.error("Error starting Integerity Monitor: " + e);
203 LOGGER.info("Trying to set up BackUpMonitor");
205 setBackupMonitor(BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config,
207 } catch (final Exception e) {
208 LOGGER.error("Error starting BackUpMonitor: " + e);
210 if (!config.containsKey(BRMSPERSISTENCE)) {
211 config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml");
213 config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
214 config.getProperty(BRMSPERSISTENCE, "META-INF/persistenceBRMS.xml"));
216 final EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config);
217 em = emf.createEntityManager();
218 defaultName = config.getProperty("defaultName");
219 if (defaultName == null) {
221 XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file ");
222 throw new PolicyException(
223 XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file");
225 defaultName = defaultName.trim();
226 repId = config.getProperty("repositoryID");
229 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
230 throw new PolicyException(
231 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
233 repId = repId.trim();
234 repName = config.getProperty("repositoryName");
235 if (repName == null) {
236 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
237 + "repositoryName property is missing from the property file ");
238 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
239 + "repositoryName property is missing from the property file ");
241 repName = repName.trim();
242 final String repUrl = config.getProperty("repositoryURL");
243 if (repUrl == null) {
245 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
246 throw new PolicyException(
247 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
249 if (repUrl.contains(",")) {
250 repUrlList = new ArrayList<>(Arrays.asList(repUrl.trim().split(",")));
252 repUrlList = new ArrayList<>();
253 repUrlList.add(repUrl);
255 repUserName = config.getProperty("repositoryUsername");
256 repPassword = config.getProperty("repositoryPassword");
257 if (repUserName == null || repPassword == null) {
258 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
259 + "repostoryUserName and respositoryPassword properties are required.");
260 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
261 + "repostoryUserName and respositoryPassword properties are required.");
263 repUserName = repUserName.trim();
264 repPassword = repPassword.trim();
265 policyKeyId = config.getProperty("policyKeyID");
266 if (policyKeyId == null) {
268 XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
269 throw new PolicyException(
270 XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
272 policyKeyId = policyKeyId.trim();
273 final String syncF = config.getProperty("sync", "false").trim();
274 syncFlag = Boolean.parseBoolean(syncF);
276 PolicyLogger.info("SYNC Flag is turned ON. DB will be given Priority.");
278 brmsdependencyversion = config.getProperty("brms.dependency.version");
279 if (brmsdependencyversion == null) {
280 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
281 + "brmsdependencyversion property is missing from the property file, Using default Version.");
282 brmsdependencyversion = DEFAULT_VERSION;
284 brmsdependencyversion = brmsdependencyversion.trim();
288 notificationType = config.getProperty("NOTIFICATION_TYPE");
289 if ("dmaap".equalsIgnoreCase(notificationType)) {
291 LOGGER.info("Notification Type being used is DMaaP... creating instance of BusPublisher.");
292 // Setting up the Publisher for DMaaP MR
293 String dmaapServers = config.getProperty("NOTIFICATION_SERVERS");
294 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
295 final String aafLogin = config.getProperty("CLIENT_ID").trim();
296 final String aafPassword = config.getProperty("CLIENT_KEY").trim();
298 if (dmaapServers == null || pubTopic == null) {
300 XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
301 throw new PolicyException(
302 XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
305 dmaapServers = dmaapServers.trim();
306 pubTopic = pubTopic.trim();
308 if (dmaapServers.contains(",")) {
309 dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
311 dmaapList = new ArrayList<>();
312 dmaapList.add(dmaapServers);
316 new BusPublisher.DmaapPublisherWrapper(this.dmaapList, this.pubTopic, aafLogin, aafPassword);
318 String notificationDelay = config.getProperty("NOTIFICATION_DELAY");
319 if (notificationDelay != null && !notificationDelay.isEmpty()) {
320 notificationDelay = notificationDelay.trim();
322 dmaapDelay = Long.parseLong(notificationDelay);
323 } catch (final NumberFormatException e) {
324 LOGGER.error("DMAAP_DELAY not a long format number" + e);
327 LOGGER.info("DMAAP BusPublisher is created.");
330 LOGGER.info("Notification Type being used is UEB... creating instance of PublisherBuilder.");
331 // Setting up the Publisher for UEB
332 uebList = config.getProperty("NOTIFICATION_SERVERS");
333 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
334 if (uebList == null || pubTopic == null) {
336 XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
337 throw new PolicyException(
338 XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
340 uebList = uebList.trim();
341 pubTopic = pubTopic.trim();
342 pubBuilder = new CambriaClientBuilders.PublisherBuilder();
343 pubBuilder.usingHosts(uebList).onTopic(pubTopic);
344 String apiKey = config.getProperty("UEB_API_KEY");
345 String apiSecret = config.getProperty("UEB_API_SECRET");
346 if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) {
347 apiKey = apiKey.trim();
348 apiSecret = apiSecret.trim();
349 pubBuilder.authenticatedBy(apiKey, apiSecret);
351 String notificationDelay = config.getProperty("NOTIFICATION_DELAY");
352 if (notificationDelay != null && !notificationDelay.isEmpty()) {
353 notificationDelay = notificationDelay.trim();
355 uebDelay = Long.parseLong(notificationDelay);
356 } catch (final NumberFormatException e) {
357 LOGGER.error("UEB_DELAY not a long format number" + e);
360 LOGGER.info("UEB PublisherBuilder is created.");
366 private static void setBackupMonitor(final BackUpMonitor instance) {
370 private static void setIntegrityMonitor(final IntegrityMonitor instance) {
375 * Will Initialize the variables required for BRMSPush.
377 public void initiate(final boolean flag) {
378 resetModifiedGroups();
379 controllers = new ArrayList<>();
381 bm.updateNotification();
382 } catch (final Exception e) {
383 LOGGER.error("Error while updating Notification: " + e.getMessage(), e);
390 public void resetDs() {
391 resetModifiedGroups();
392 controllers = new ArrayList<>();
395 private static void resetModifiedGroups() {
396 modifiedGroups = new HashMap<>();
400 * Will Add rules to projects. Creates necessary folders if required.
402 public void addRule(final String name, final String rule, final Map<String, String> responseAttributes) {
403 // 1 check the response Attributes and determine if this belongs to any projects.
404 // 2 if not create folder
407 // 5 store the groups that have been updated.
408 String ksessionName = null;
409 String selectedName = null;
410 if (!responseAttributes.isEmpty()) {
411 // Pick selected Value
412 String userControllerName = null;
413 final ArrayList<PEDependency> userDependencies = new ArrayList<>();
414 for (final Map.Entry<String, String> entry : responseAttributes.entrySet()) {
415 final String key = entry.getKey();
416 String value = entry.getValue();
417 if (key.equals(policyKeyId)) {
418 selectedName = value;
420 // kmodule configurations
421 else if ("kSessionName".equals(key)) {
422 ksessionName = value;
424 // Check User Specific values.
425 if ("$controller:".equals(key)) {
427 final PEDependency dependency = PolicyUtils.jsonStringToObject(value, PEDependency.class);
428 userControllerName = key.replaceFirst("$controller:", "");
429 LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - "
431 addToGroup(userControllerName, dependency);
432 } catch (final Exception e) {
433 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e);
436 } else if ("$dependency$".equals(key) && value.startsWith("[") && value.endsWith("]")) {
437 value = value.substring(1, value.length() - 1).trim();
438 final List<String> dependencyStrings = Arrays.asList(value.split(Pattern.quote("},{")));
439 for (final String dependencyString : dependencyStrings) {
441 userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class));
442 } catch (final Exception e) {
443 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Dependencies: "
449 if (userControllerName != null) {
450 // Adding custom dependencies here.
451 final ArrayList<Object> values = groupMap.get(userControllerName);
452 values.add(userDependencies);
453 groupMap.put(userControllerName, values);
454 selectedName = userControllerName;
457 // If no Match then pick Default.
458 if (selectedName == null) {
459 selectedName = defaultName;
461 if (groupMap.containsKey(selectedName)) {
462 // If the key is not got as parameters set by the user, setting the default value for
465 if (ksessionName == null) {
466 LOGGER.info("kSessionName is null, selectedName is : " + selectedName);
467 if (selectedName.equalsIgnoreCase(defaultName)) {
468 ksessionName = "closedloop";
470 ksessionName = "closedloop-" + selectedName;
473 // create directories if missing.
474 manageProject(selectedName, ksessionName, name, rule);
476 // Will check for Create Later after generating the Pom.
477 addModifiedGroup(selectedName, "update");
481 private void syncGroupInfo() {
482 // Sync DB to JMemory.
483 final EntityTransaction et = em.getTransaction();
485 Query query = em.createQuery("select b from BrmsGroupInfo AS b");
486 List<?> result = query.getResultList();
487 if (result.size() != groupMap.size()) {
488 for (final Object value : result) {
489 final BrmsGroupInfo brmsGroupInfo = (BrmsGroupInfo) value;
490 final PEDependency dependency = new PEDependency();
491 dependency.setArtifactId(brmsGroupInfo.getArtifactId());
492 dependency.setGroupId(brmsGroupInfo.getGroupId());
493 dependency.setVersion(brmsGroupInfo.getVersion());
494 final ArrayList<Object> values = new ArrayList<>();
495 values.add(dependency);
496 groupMap.put(brmsGroupInfo.getControllerName(), values);
499 query = em.createQuery("select g from BrmsPolicyInfo AS g");
500 result = query.getResultList();
501 if (result.size() != policyMap.size()) {
502 for (final Object value : result) {
503 final BrmsPolicyInfo brmsPolicyInfo = (BrmsPolicyInfo) value;
504 policyMap.put(brmsPolicyInfo.getPolicyName(), brmsPolicyInfo.getControllerName().getControllerName());
508 LOGGER.info("Updated Local Memory values with values from database.");
511 private void manageProject(final String selectedName, final String ksessionName, final String name,
513 // Check if the Project is in Sync. If not get the latest Version.
514 syncProject(selectedName);
515 createProject(PROJECTSLOCATION + File.separator + getArtifactId(selectedName) + File.separator + "src"
516 + File.separator + "main" + File.separator + RESOURCES, ksessionName);
517 copyDataToFile(PROJECTSLOCATION + File.separator + getArtifactId(selectedName) + File.separator + "src"
518 + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES + File.separator + name
520 addToPolicy(name, selectedName);
524 * Add Policy to JMemory and DataBase.
526 private void addToPolicy(final String policyName, final String controllerName) {
527 policyMap.put(policyName, controllerName);
528 final EntityTransaction et = em.getTransaction();
530 Query query = em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn");
531 query.setParameter("pn", policyName);
532 final List<?> pList = query.getResultList();
533 boolean create = false;
534 BrmsPolicyInfo brmsPolicyInfo = new BrmsPolicyInfo();
535 if (!pList.isEmpty()) {
537 brmsPolicyInfo = (BrmsPolicyInfo) pList.get(0);
538 if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) {
545 query = em.createQuery("select b from BrmsGroupInfo as b where b.controllerName = :cn");
546 query.setParameter("cn", controllerName);
547 final List<?> bList = query.getResultList();
548 BrmsGroupInfo brmsGroupInfo = new BrmsGroupInfo();
549 if (!bList.isEmpty()) {
550 brmsGroupInfo = (BrmsGroupInfo) bList.get(0);
552 brmsPolicyInfo.setPolicyName(policyName);
553 brmsPolicyInfo.setControllerName(brmsGroupInfo);
554 em.persist(brmsPolicyInfo);
560 private void syncProject(final String selectedName) {
561 final boolean projectExists = checkProject(selectedName);
564 version = getVersion(selectedName);
565 if (version == null) {
566 LOGGER.error("Error getting local version for the given Controller Name:" + selectedName
567 + " going with Default value");
568 version = VERSION_0_1_0;
570 final String nextVersion = incrementVersion(version);
571 final boolean outOfSync = checkRemoteSync(selectedName, nextVersion);
576 // We are out of Sync or Project is not Present.
577 downloadProject(selectedName);
580 private void downloadProject(final String selectedName) {
581 final NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
582 if (artifact == null) {
585 final String dirName = getDirectoryName(selectedName);
587 final String fileName = "rule.jar";
589 website = new URL(artifact.getUrlPath() + ".jar");
590 try (ReadableByteChannel rbc = Channels.newChannel(website.openStream());
591 FileOutputStream fos = new FileOutputStream(fileName)) {
592 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
593 extractJar(fileName, dirName);
594 new File(fileName).delete();
595 } catch (final IOException e) {
596 LOGGER.error("Error while downloading the project to File System. " + e.getMessage(), e);
599 } catch (final IOException e1) {
600 LOGGER.error("Error while retrieve the artifact. " + e1.getMessage(), e1);
604 private void extractJar(final String jarFileName, final String artifactId) {
605 try (JarFile jar = new JarFile(jarFileName)) {
606 final Enumeration<?> enumEntries = jar.entries();
607 while (enumEntries.hasMoreElements()) {
608 final JarEntry jarEntry = (JarEntry) enumEntries.nextElement();
610 final String fileName = jarEntry.getName().substring(jarEntry.getName().lastIndexOf("/") + 1);
611 if (jarEntry.getName().endsWith(".drl")) {
612 final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src"
613 + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES;
614 new File(path).mkdirs();
615 if (syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))) {
616 file = new File(path + File.separator + fileName);
618 file = new File(path + File.separator + fileName);
620 } else if (jarEntry.getName().endsWith(POM_XML_FILE)) {
621 final String path = PROJECTSLOCATION + File.separator + artifactId;
622 new File(path).mkdirs();
623 file = new File(path + File.separator + fileName);
624 } else if (jarEntry.getName().endsWith(KMODULE_XML_FILE)) {
625 final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src"
626 + File.separator + "main" + File.separator + RESOURCES + File.separator + META_INF;
627 new File(path).mkdirs();
628 file = new File(path + File.separator + fileName);
631 try (InputStream is = jar.getInputStream(jarEntry);
632 FileOutputStream fos = new FileOutputStream(file)) {
633 while (is.available() > 0) {
634 fos.write(is.read());
636 LOGGER.info(fileName + " Created..");
637 } catch (final IOException e) {
638 LOGGER.info("exception Occured" + e);
642 } catch (final IOException e) {
643 LOGGER.info("exception Occured" + e);
647 private NexusArtifact getLatestArtifactFromNexus(final String selectedName) {
648 final List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, null);
651 NexusArtifact result = null;
652 for (final NexusArtifact artifact : artifacts) {
654 Integer.parseInt(artifact.getVersion().substring(0, artifact.getVersion().indexOf(".")));
655 final int minorVal = Integer.parseInt(artifact.getVersion()
656 .substring(artifact.getVersion().indexOf(".") + 1, artifact.getVersion().lastIndexOf(".")));
657 if (majorVal > bigNum) {
661 if ((bigNum == majorVal) && (minorVal > smallNum)) {
664 if (bigNum == majorVal && minorVal == smallNum) {
668 return additionalNexusLatestCheck(selectedName, result);
671 // Additional Check due to Limitations from Nexus API to check if the artifact is the latest.
672 private NexusArtifact additionalNexusLatestCheck(final String selectedName, final NexusArtifact result) {
673 if (result == null) {
676 final String nextVersion = incrementVersion(result.getVersion());
677 final List<NexusArtifact> artifact = getArtifactFromNexus(selectedName, nextVersion);
678 return artifact.isEmpty() ? result : additionalNexusLatestCheck(selectedName, artifact.get(0));
681 private boolean checkRemoteSync(final String selectedName, final String version) {
682 final List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, version);
683 return artifacts.isEmpty() ? false : true;
686 private List<NexusArtifact> getArtifactFromNexus(final String selectedName, final String version) {
687 NexusRestWrapper restWrapper = null;
689 boolean flag = false;
690 while (index < repUrlList.size()) {
692 final String repUrl = repUrlList.get(0);
694 new NexusRestWrapper(repUrl.substring(0, repUrl.indexOf(repUrl.split(":[0-9]+\\/nexus")[1])),
695 repUserName, repPassword);
696 final NexusRestSearchParameters searchParameters = new NexusRestSearchParameters();
697 searchParameters.useFilterSearch(
698 getGroupId(selectedName), getArtifactId(selectedName), version, null, null);
700 final List<NexusArtifact> resultList = restWrapper.findArtifact(searchParameters).getArtifactList();
701 if (resultList != null) {
705 } catch (NexusRestWrapperException | ProcessingException e) {
706 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. "
707 + e.getMessage(), e);
709 if (null != restWrapper) {
713 Collections.rotate(repUrlList, -1);
718 return new ArrayList<>();
721 private String incrementVersion(final String version) {
722 int majorVal = Integer.parseInt(version.substring(0, version.indexOf(".")));
723 int minorVal = Integer.parseInt(version.substring(version.indexOf(".") + 1, version.lastIndexOf(".")));
730 return majorVal + "." + minorVal + version.substring(version.lastIndexOf("."));
733 private boolean checkProject(final String selectedName) {
734 return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists();
737 private String getDirectoryName(final String selectedName) {
738 return getArtifactId(selectedName);
742 * Will Push policies to the PolicyRepo.
744 * @throws PolicyException PolicyException related to the operation
746 public void pushRules() throws PolicyException {
747 // Check how many groups have been updated.
748 // Invoke their Maven process.
750 im.startTransaction();
751 } catch (final Exception e) {
752 LOGGER.error("Error while starting Transaction " + e);
754 if (!modifiedGroups.isEmpty()) {
755 Boolean flag = false;
756 for (final Map.Entry<String, String> entry : modifiedGroups.entrySet()) {
757 InvocationResult result = null;
758 final String group = entry.getKey();
760 LOGGER.info("PushRules: ModifiedGroups, Key: " + group + ", Value: " + entry.getValue());
761 final InvocationRequest request = new DefaultInvocationRequest();
764 request.setPomFile(new File(
765 PROJECTSLOCATION + File.separator + getArtifactId(group) + File.separator + POM_XML_FILE));
766 request.setGoals(Arrays.asList(GOALS));
767 final Invoker invoker = new DefaultInvoker();
768 result = invoker.execute(request);
769 if (result.getExecutionException() != null) {
770 LOGGER.error(result.getExecutionException());
771 } else if (result.getExitCode() != 0) {
772 LOGGER.error("Maven Invocation failure..!");
774 } catch (final Exception e) {
775 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation issue for "
776 + getArtifactId(group) + e.getMessage(), e);
778 if (result != null && result.getExitCode() == 0) {
779 LOGGER.info("Build Completed..!");
781 addNotification(group, "create");
783 addNotification(group, entry.getValue());
787 throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!");
791 sendNotification(controllers);
800 * Removes a Rule from Rule Projects.
802 public void removeRule(final String name) {
803 final String controllerName = getGroupName(name);
804 if (controllerName == null) {
805 LOGGER.info("Error finding the controllerName for the given Policy: " + name);
808 syncProject(controllerName);
809 getNameAndSetRemove(controllerName, name);
812 private String getGroupName(final String name) {
813 if (policyMap.containsKey(name)) {
814 return policyMap.get(name);
817 return policyMap.containsKey(name) ? policyMap.get(name) : null;
821 private void addModifiedGroup(final String controllerName, final String operation) {
822 if (controllerName != null) {
823 modifiedGroups.put(controllerName, operation);
827 private void addNotification(final String controllerName, final String operation) {
828 final ControllerPojo controllerPojo = new ControllerPojo();
829 controllerPojo.setName(controllerName);
830 controllerPojo.setOperation(operation);
831 final HashMap<String, String> drools = new HashMap<>();
832 drools.put("groupId", getGroupId(controllerName));
833 drools.put("artifactId", getArtifactId(controllerName));
834 drools.put("version", getVersion(controllerName));
835 controllerPojo.setDrools(drools);
836 controllers.add(controllerPojo);
838 LOGGER.debug("Notification added: " + PolicyUtils.objectToJsonString(controllerPojo));
839 } catch (final JsonProcessingException e) {
840 LOGGER.error(MessageCodes.ERROR_SCHEMA_INVALID + "Json Processing Error " + e);
844 private void removedRuleModifiedGroup(final String controllerName) {
845 // This will be sending Notification to PDPD directly to Lock
846 final ControllerPojo controllerPojo = new ControllerPojo();
847 controllerPojo.setName(controllerName);
848 controllerPojo.setOperation("lock");
849 final List<ControllerPojo> controllerPojos = new ArrayList<>();
850 controllerPojos.add(controllerPojo);
851 sendNotification(controllerPojos);
854 private void sendNotification(final List<ControllerPojo> controllers) {
855 final NotificationPojo notification = new NotificationPojo();
856 final String requestId = UUID.randomUUID().toString();
857 LOGGER.info("Generating notification RequestID : " + requestId);
858 notification.setRequestId(requestId);
859 notification.setEntity("controller");
860 notification.setControllers(controllers);
862 final String notificationJson = PolicyUtils.objectToJsonString(notification);
863 LOGGER.info("Sending Notification :\n" + notificationJson);
864 sendMessage(notificationJson);
865 } catch (final Exception e) {
866 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while sending notification to PDP-D "
867 + e.getMessage(), e);
871 private void sendMessage(final String message) throws IOException, GeneralSecurityException, InterruptedException {
873 if ("dmaap".equalsIgnoreCase(notificationType)) {
874 // Sending Message through DMaaP Message Router
875 LOGGER.debug("DMAAP Publishing Message");
877 publisher.send("MyPartitionKey", message);
879 LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic);
881 Thread.sleep(dmaapDelay);
884 // Sending Message through UEB interface.
885 LOGGER.debug("UEB Publishing Message");
887 final CambriaBatchingPublisher pub = pubBuilder.build();
888 pub.send("MyPartitionKey", message);
890 final List<?> stuck = pub.close(uebDelay, TimeUnit.SECONDS);
891 if (!stuck.isEmpty()) {
892 LOGGER.error(stuck.size() + " messages unsent");
894 LOGGER.debug("Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic);
900 private void createPom(final String name) {
901 final Model model = new Model();
902 model.setModelVersion("4.0.0");
903 model.setGroupId(getGroupId(name));
904 model.setArtifactId(getArtifactId(name));
905 model.setVersion(getVersion(name));
907 final DistributionManagement distributionManagement = new DistributionManagement();
908 final DeploymentRepository repository = new DeploymentRepository();
909 repository.setId(repId);
910 repository.setName(repName);
911 repository.setUrl(repUrlList.get(0));
912 distributionManagement.setRepository(repository);
913 model.setDistributionManagement(distributionManagement);
914 // Dependency Management goes here.
915 List<Dependency> dependencyList = new ArrayList<>();
916 if (groupMap.get(name).size() > 1) {
917 @SuppressWarnings("unchecked")
918 final ArrayList<PEDependency> dependencies = (ArrayList<PEDependency>) groupMap.get(name).get(1);
919 for (final PEDependency dependency : dependencies) {
920 dependencyList.add(dependency.getDependency());
923 // Add Default dependencies.
924 dependencyList = getDependencies(name);
926 model.setDependencies(dependencyList);
927 Writer writer = null;
929 writer = WriterFactory.newXmlWriter(
930 new File(PROJECTSLOCATION + File.separator + getArtifactId(name) + File.separator + POM_XML_FILE));
931 final MavenXpp3Writer pomWriter = new MavenXpp3Writer();
932 pomWriter.write(writer, model);
933 } catch (final Exception e) {
934 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating POM for " + getArtifactId(name)
935 + e.getMessage(), e);
937 IOUtil.close(writer);
941 private List<Dependency> getDependencies(final String controllerName) {
942 // Read the Dependency Information from property file.
943 final Path file = Paths.get(DEPENDENCY_FILE);
944 if (!Files.notExists(file)) {
946 final String dependencyJson = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
947 final DependencyInfo dependencyInfo =
948 PolicyUtils.jsonStringToObject(dependencyJson, DependencyInfo.class);
949 String controller = "default";
950 if (dependencyInfo.getDependencies().containsKey(controllerName)) {
951 controller = controllerName;
953 final List<Dependency> dependencyList = new ArrayList<>();
954 for (final PEDependency dependency : dependencyInfo.getDependencies().get(controller)) {
955 dependencyList.add(dependency.getDependency());
957 return dependencyList;
958 } catch (IOException | NullPointerException e) {
959 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
960 + "Error while getting dependecy Information for controller: " + controllerName
961 + e.getMessage(), e);
964 return defaultDependencies(controllerName);
968 * Default Dependency Section. Can be changed as required.
970 * @param controllerName the controller name
971 * @return changed dependency list
973 public List<Dependency> defaultDependencies(final String controllerName) {
975 final List<Dependency> dependencyList = new ArrayList<>();
976 final String version = StringEscapeUtils.escapeJava(brmsdependencyversion);
978 final Dependency demoDependency = new Dependency();
979 demoDependency.setGroupId(DROOLS_APPS_TEMPLATE_GROUP);
980 demoDependency.setArtifactId("template.demo");
981 demoDependency.setVersion(version);
982 dependencyList.add(demoDependency);
984 final Dependency controlloopDependency = new Dependency();
985 controlloopDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
986 controlloopDependency.setArtifactId("events");
987 controlloopDependency.setVersion(version);
988 dependencyList.add(controlloopDependency);
990 final Dependency restDependency = new Dependency();
991 restDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
992 restDependency.setArtifactId("controlloop.common.model-impl.rest");
993 restDependency.setVersion(version);
994 dependencyList.add(restDependency);
996 final Dependency appcDependency = new Dependency();
997 appcDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
998 appcDependency.setArtifactId("controlloop.common.model-impl.appc");
999 appcDependency.setVersion(version);
1000 dependencyList.add(appcDependency);
1002 final Dependency aaiDependency = new Dependency();
1003 aaiDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1004 aaiDependency.setArtifactId("controlloop.common.model-impl.aai");
1005 aaiDependency.setVersion(version);
1006 dependencyList.add(aaiDependency);
1008 final Dependency msoDependency = new Dependency();
1009 msoDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1010 msoDependency.setArtifactId("controlloop.common.model-impl.so");
1011 msoDependency.setVersion(version);
1012 dependencyList.add(msoDependency);
1014 final Dependency trafficgeneratorDependency = new Dependency();
1015 trafficgeneratorDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1016 trafficgeneratorDependency.setArtifactId("controlloop.common.model-impl.trafficgenerator");
1017 trafficgeneratorDependency.setVersion(version);
1018 dependencyList.add(trafficgeneratorDependency);
1019 return dependencyList;
1022 private void createProject(final String path, final String ksessionName) {
1023 new File(path + File.separator + RULES).mkdirs();
1024 new File(path + File.separator + META_INF).mkdirs();
1025 if (!Files.exists(Paths.get(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE))) {
1026 // Hard coding XML for PDP Drools to accept our Rules.
1027 final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"
1028 + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">" + "\n"
1029 + "<kbase name=\"rules\" packages=\"rules\">" + "\n" + "<ksession name=\"" + ksessionName + "\"/>"
1030 + "\n" + "</kbase></kmodule>";
1031 copyDataToFile(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE, xml);
1035 private void copyDataToFile(final String file, final String rule) {
1037 FileUtils.writeStringToFile(new File(file), rule);
1038 } catch (final Exception e) {
1040 XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating Rule for " + file + e.getMessage(),
1045 private void readGroups(final Properties config) throws PolicyException {
1046 String[] groupNames;
1047 final String groupNamesError = "groupNames property is missing or empty from the property file ";
1048 if (!config.containsKey(GROUP_NAMES) || config.getProperty(GROUP_NAMES) == null) {
1049 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + groupNamesError);
1051 if (config.getProperty(GROUP_NAMES).contains(",")) {
1052 groupNames = config.getProperty(GROUP_NAMES).replaceAll(" ", "").split(",");
1054 groupNames = new String[] {config.getProperty(GROUP_NAMES).replaceAll(" ", "")};
1056 if (groupNames == null || groupNames.length == 0) {
1057 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + groupNamesError);
1058 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + groupNamesError);
1060 groupMap = new HashMap<>();
1061 for (int counter = 0; counter < groupNames.length; counter++) {
1062 final String name = groupNames[counter];
1063 final 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 final 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 final 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(final String name, final PEDependency dependency) {
1086 final ArrayList<Object> values = new ArrayList<>();
1087 values.add(dependency);
1088 groupMap.put(name, values);
1089 final EntityTransaction et = em.getTransaction();
1091 final Query query = em.createQuery("select b from BrmsGroupInfo as b where b.controllerName = :cn");
1092 query.setParameter("cn", name);
1093 final 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(final String name) {
1112 return ((PEDependency) groupMap.get(name).get(0)).getArtifactId();
1115 private String getGroupId(final String name) {
1116 return ((PEDependency) groupMap.get(name).get(0)).getGroupId();
1119 private String getVersion(final String name) {
1120 return ((PEDependency) groupMap.get(name).get(0)).getVersion();
1123 private void getNameAndSetRemove(final String controllerName, final String policyName) {
1124 final String artifactName = getArtifactId(controllerName);
1125 final String ruleFolder = PROJECTSLOCATION + File.separator + artifactName + File.separator + "src"
1126 + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES;
1127 final 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(final String policyName, final String controllerName) {
1142 policyMap.remove(policyName);
1143 final EntityTransaction et = em.getTransaction();
1145 final Query query = em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn");
1146 query.setParameter("pn", policyName);
1147 final 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(final String selectedName) {
1161 String newVersion = VERSION_0_1_0;
1163 final NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
1164 if (artifact != null) {
1165 newVersion = incrementVersion(artifact.getVersion());
1167 if (VERSION_0_1_0.equals(newVersion)) {
1170 setVersion(newVersion, selectedName);
1171 LOGGER.info("Controller: " + selectedName + "is on version: " + newVersion);
1174 private void setVersion(final String newVersion, final String controllerName) {
1175 final PEDependency userController = (PEDependency) groupMap.get(controllerName).get(0);
1176 userController.setVersion(newVersion);
1177 groupMap.get(controllerName).set(0, userController);
1180 // Return BackUpMonitor
1181 public static BackUpMonitor getBackUpMonitor() {
1188 public void rotateUrls() {
1189 if (repUrlList != null) {
1190 Collections.rotate(repUrlList, -1);
1195 * Get URL List Size.
1197 * @return URL list size
1199 public int urlListSize() {
1200 return repUrlList != null ? repUrlList.size() : 0;