2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7 * Modifications Copyright (C) 2019 Nordix Foundation.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.brms.api;
25 import com.att.nsa.cambria.client.CambriaBatchingPublisher;
26 import com.att.nsa.cambria.client.CambriaClientBuilders;
27 import com.att.nsa.cambria.client.CambriaClientBuilders.PublisherBuilder;
28 import com.fasterxml.jackson.core.JsonProcessingException;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.Writer;
36 import java.nio.channels.Channels;
37 import java.nio.channels.ReadableByteChannel;
38 import java.nio.charset.StandardCharsets;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.security.GeneralSecurityException;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.List;
50 import java.util.Properties;
51 import java.util.UUID;
52 import java.util.concurrent.TimeUnit;
53 import java.util.jar.JarEntry;
54 import java.util.jar.JarFile;
55 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.TypedQuery;
61 import javax.ws.rs.ProcessingException;
62 import org.apache.commons.io.FileUtils;
63 import org.apache.commons.lang.StringEscapeUtils;
64 import org.apache.maven.model.Dependency;
65 import org.apache.maven.model.DeploymentRepository;
66 import org.apache.maven.model.DistributionManagement;
67 import org.apache.maven.model.Model;
68 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
69 import org.apache.maven.shared.invoker.DefaultInvocationRequest;
70 import org.apache.maven.shared.invoker.DefaultInvoker;
71 import org.apache.maven.shared.invoker.InvocationRequest;
72 import org.apache.maven.shared.invoker.InvocationResult;
73 import org.apache.maven.shared.invoker.Invoker;
74 import org.codehaus.plexus.util.IOUtil;
75 import org.codehaus.plexus.util.WriterFactory;
76 import org.eclipse.persistence.config.PersistenceUnitProperties;
77 import org.onap.policy.api.PEDependency;
78 import org.onap.policy.api.PolicyException;
79 import org.onap.policy.brms.api.nexus.NexusRestSearchParameters;
80 import org.onap.policy.brms.api.nexus.NexusRestWrapper;
81 import org.onap.policy.brms.api.nexus.NexusRestWrapperException;
82 import org.onap.policy.brms.api.nexus.pojo.NexusArtifact;
83 import org.onap.policy.brms.entity.BrmsGroupInfo;
84 import org.onap.policy.brms.entity.BrmsPolicyInfo;
85 import org.onap.policy.brms.entity.DependencyInfo;
86 import org.onap.policy.common.im.IntegrityMonitor;
87 import org.onap.policy.common.logging.eelf.MessageCodes;
88 import org.onap.policy.common.logging.eelf.PolicyLogger;
89 import org.onap.policy.common.logging.flexlogger.FlexLogger;
90 import org.onap.policy.common.logging.flexlogger.Logger;
91 import org.onap.policy.utils.BackUpHandler;
92 import org.onap.policy.utils.BackUpMonitor;
93 import org.onap.policy.utils.BusPublisher;
94 import org.onap.policy.utils.PeCryptoUtils;
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.models.policy-models-interactions.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.5.1-SNAPSHOT";
121 private static final String DEPENDENCY_FILE = "dependency.json";
122 private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key";
123 public static final String BRMSPERSISTENCE = "brmsEclipselink.persistencexml";
125 private static Map<String, String> modifiedGroups = new HashMap<>();
126 private static IntegrityMonitor im;
127 private static BackUpMonitor bm;
128 private String defaultName = null;
129 private String repId = null;
130 private String repName = null;
131 private List<String> repUrlList = null;
132 private String repUserName = null;
133 private String repPassword = null;
134 private String policyKeyId = null;
135 private boolean createFlag = false;
136 private String uebList = null;
137 private List<String> dmaapList = null;
138 private String pubTopic = null;
139 private PublisherBuilder pubBuilder = null;
140 protected BusPublisher publisher = null;
141 private Long uebDelay = Long.parseLong("20");
142 private Long dmaapDelay = Long.parseLong("5000");
143 private String notificationType = null;
144 private List<ControllerPojo> controllers;
145 private Map<String, ArrayList<Object>> groupMap = new HashMap<>();
146 private final Map<String, String> policyMap = new HashMap<>();
147 private String brmsdependencyversion;
148 private EntityManager em;
149 private boolean syncFlag = false;
152 * Responsible to push policies to the BRMS PDP Policy Repository (PR).
154 * @param propertiesFile the properties file
155 * @param handler the {@link BackUpHandler}
156 * @throws PolicyException PolicyException related to the operation
158 public BrmsPush(final String propertiesFile, final BackUpHandler handler) throws PolicyException {
159 if (propertiesFile == null || handler == null) {
160 throw new PolicyException("Error no propertiesFile or handler");
162 final Properties config = new Properties();
163 final Path file = Paths.get(propertiesFile);
164 if (Files.notExists(file)) {
165 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Config File doesn't Exist in the specified Path "
167 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
168 + "Config File doesn't Exist in the specified Path " + file.toString());
170 if (file.toString().endsWith(".properties")) {
171 // Grab the Properties.
172 setProperty(file, config, handler);
177 private void setProperty(final Path file, final Properties config, final BackUpHandler handler)
178 throws PolicyException {
181 in = new FileInputStream(file.toFile());
183 } catch (final IOException e) {
185 XACMLErrorConstants.ERROR_DATA_ISSUE + "Data/File Read Error while reading from the property file.",
187 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
188 + "Data/File Read Error while reading from the property file.");
190 // init the aes key from prop or env
191 PeCryptoUtils.initAesKey(config.getProperty(PROP_AES_KEY));
193 LOGGER.info("Trying to set up IntegrityMonitor");
194 String resourceName = null;
196 LOGGER.info("Trying to set up IntegrityMonitor");
197 resourceName = config.getProperty("RESOURCE_NAME");
198 if (resourceName == null) {
199 LOGGER.warn("RESOURCE_NAME is missing setting default value. ");
200 resourceName = "brmsgw_pdp01";
202 resourceName = resourceName.trim();
203 setIntegrityMonitor(IntegrityMonitor.getInstance(resourceName, config));
204 } catch (final Exception e) {
205 LOGGER.error("Error starting Integerity Monitor: " + e);
207 LOGGER.info("Trying to set up BackUpMonitor");
209 setBackupMonitor(BackUpMonitor.getInstance(BackUpMonitor.ResourceNode.BRMS.toString(), resourceName, config,
211 } catch (final Exception e) {
212 LOGGER.error("Error starting BackUpMonitor: " + e);
214 if (!config.containsKey(BRMSPERSISTENCE)) {
215 config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML, "META-INF/persistenceBRMS.xml");
217 config.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
218 config.getProperty(BRMSPERSISTENCE, "META-INF/persistenceBRMS.xml"));
220 final EntityManagerFactory emf = Persistence.createEntityManagerFactory("BRMSGW", config);
221 em = emf.createEntityManager();
222 defaultName = config.getProperty("defaultName");
223 if (defaultName == null) {
225 XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file ");
226 throw new PolicyException(
227 XACMLErrorConstants.ERROR_DATA_ISSUE + "defaultName property is missing from the property file");
229 defaultName = defaultName.trim();
230 repId = config.getProperty("repositoryID");
233 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
234 throw new PolicyException(
235 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryID property is missing from the property file ");
237 repId = repId.trim();
238 repName = config.getProperty("repositoryName");
239 if (repName == null) {
240 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
241 + "repositoryName property is missing from the property file ");
242 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
243 + "repositoryName property is missing from the property file ");
245 repName = repName.trim();
246 final String repUrl = config.getProperty("repositoryURL");
247 if (repUrl == null) {
249 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
250 throw new PolicyException(
251 XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
253 if (repUrl.contains(",")) {
254 repUrlList = new ArrayList<>(Arrays.asList(repUrl.trim().split(",")));
256 repUrlList = new ArrayList<>();
257 repUrlList.add(repUrl);
259 repUserName = config.getProperty("repositoryUsername");
260 repPassword = PeCryptoUtils.decrypt(config.getProperty("repositoryPassword"));
261 if (repUserName == null || repPassword == null) {
262 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
263 + "repostoryUserName and respositoryPassword properties are required.");
264 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE
265 + "repostoryUserName and respositoryPassword properties are required.");
267 repUserName = repUserName.trim();
268 repPassword = repPassword.trim();
269 policyKeyId = config.getProperty("policyKeyID");
270 if (policyKeyId == null) {
272 XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
273 throw new PolicyException(
274 XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
276 policyKeyId = policyKeyId.trim();
277 final String syncF = config.getProperty("sync", "false").trim();
278 syncFlag = Boolean.parseBoolean(syncF);
280 PolicyLogger.info("SYNC Flag is turned ON. DB will be given Priority.");
282 brmsdependencyversion = config.getProperty("brms.dependency.version");
283 if (brmsdependencyversion == null) {
284 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE
285 + "brmsdependencyversion property is missing from the property file, Using default Version.");
286 brmsdependencyversion = DEFAULT_VERSION;
288 brmsdependencyversion = brmsdependencyversion.trim();
292 notificationType = config.getProperty("NOTIFICATION_TYPE");
293 if ("dmaap".equalsIgnoreCase(notificationType)) {
295 LOGGER.info("Notification Type being used is DMaaP... creating instance of BusPublisher.");
296 // Setting up the Publisher for DMaaP MR
297 String dmaapServers = config.getProperty("NOTIFICATION_SERVERS");
298 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
299 final String aafLogin = config.getProperty("CLIENT_ID").trim();
300 final String aafPassword = config.getProperty("CLIENT_KEY").trim();
302 if (dmaapServers == null || pubTopic == null) {
304 XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
305 throw new PolicyException(
306 XACMLErrorConstants.ERROR_DATA_ISSUE + "DMaaP properties are missing from the property file ");
309 dmaapServers = dmaapServers.trim();
310 pubTopic = pubTopic.trim();
312 if (dmaapServers.contains(",")) {
313 dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
315 dmaapList = new ArrayList<>();
316 dmaapList.add(dmaapServers);
320 new BusPublisher.DmaapPublisherWrapper(this.dmaapList, this.pubTopic, aafLogin, aafPassword);
322 String notificationDelay = config.getProperty("NOTIFICATION_DELAY");
323 if (notificationDelay != null && !notificationDelay.isEmpty()) {
324 notificationDelay = notificationDelay.trim();
326 dmaapDelay = Long.parseLong(notificationDelay);
327 } catch (final NumberFormatException e) {
328 LOGGER.error("DMAAP_DELAY not a long format number" + e);
331 LOGGER.info("DMAAP BusPublisher is created.");
334 LOGGER.info("Notification Type being used is UEB... creating instance of PublisherBuilder.");
335 // Setting up the Publisher for UEB
336 uebList = config.getProperty("NOTIFICATION_SERVERS");
337 pubTopic = config.getProperty("NOTIFICATION_TOPIC");
338 if (uebList == null || pubTopic == null) {
340 XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
341 throw new PolicyException(
342 XACMLErrorConstants.ERROR_DATA_ISSUE + "UEB properties are missing from the property file ");
344 uebList = uebList.trim();
345 pubTopic = pubTopic.trim();
346 pubBuilder = new CambriaClientBuilders.PublisherBuilder();
347 pubBuilder.usingHosts(uebList).onTopic(pubTopic).usingHttps(true);
348 String apiKey = config.getProperty("UEB_API_KEY");
349 String apiSecret = config.getProperty("UEB_API_SECRET");
350 if (apiKey != null && !apiKey.isEmpty() && apiSecret != null && !apiSecret.isEmpty()) {
351 apiKey = apiKey.trim();
352 apiSecret = apiSecret.trim();
353 pubBuilder.authenticatedBy(apiKey, apiSecret);
355 String notificationDelay = config.getProperty("NOTIFICATION_DELAY");
356 if (notificationDelay != null && !notificationDelay.isEmpty()) {
357 notificationDelay = notificationDelay.trim();
359 uebDelay = Long.parseLong(notificationDelay);
360 } catch (final NumberFormatException e) {
361 LOGGER.error("UEB_DELAY not a long format number" + e);
364 LOGGER.info("UEB PublisherBuilder is created.");
370 private static void setBackupMonitor(final BackUpMonitor instance) {
374 private static void setIntegrityMonitor(final IntegrityMonitor instance) {
379 * Will Initialize the variables required for BRMSPush.
381 public void initiate(final boolean flag) {
382 resetModifiedGroups();
383 controllers = new ArrayList<>();
385 bm.updateNotification();
386 } catch (final Exception e) {
387 LOGGER.error("Error while updating Notification: " + e.getMessage(), e);
394 public void resetDs() {
395 resetModifiedGroups();
396 controllers = new ArrayList<>();
399 private static void resetModifiedGroups() {
400 modifiedGroups = new HashMap<>();
404 * Will Add rules to projects. Creates necessary folders if required.
406 public void addRule(final String name, final String rule, final Map<String, String> responseAttributes) {
407 // 1 check the response Attributes and determine if this belongs to any projects.
408 // 2 if not create folder
411 // 5 store the groups that have been updated.
412 String ksessionName = null;
413 String selectedName = null;
414 if (!responseAttributes.isEmpty()) {
415 // Pick selected Value
416 String userControllerName = null;
417 final ArrayList<PEDependency> userDependencies = new ArrayList<>();
418 for (final Map.Entry<String, String> entry : responseAttributes.entrySet()) {
419 final String key = entry.getKey();
420 final String value = entry.getValue();
421 if (key.equals(policyKeyId)) {
422 selectedName = value;
424 // kmodule configurations
425 else if ("kSessionName".equals(key)) {
426 ksessionName = value;
428 // Check User Specific values.
429 if ("$controller:".equals(key)) {
430 userControllerName = getUserControllerName(key, value);
431 } else if ("$dependency$".equals(key) && value.startsWith("[") && value.endsWith("]")) {
432 updateUserDependencies(userDependencies, value);
435 if (userControllerName != null) {
436 // Adding custom dependencies here.
437 final ArrayList<Object> values = groupMap.get(userControllerName);
438 values.add(userDependencies);
439 groupMap.put(userControllerName, values);
440 selectedName = userControllerName;
443 // If no Match then pick Default.
444 if (selectedName == null) {
445 selectedName = defaultName;
447 if (groupMap.containsKey(selectedName)) {
448 // If the key is not got as parameters set by the user, setting the default value for
451 if (ksessionName == null) {
452 LOGGER.info("kSessionName is null, selectedName is : " + selectedName);
453 if (selectedName.equalsIgnoreCase(defaultName)) {
454 ksessionName = "closedloop";
456 ksessionName = "closedloop-" + selectedName;
459 // create directories if missing.
460 manageProject(selectedName, ksessionName, name, rule);
462 // Will check for Create Later after generating the Pom.
463 addModifiedGroup(selectedName, "update");
467 private String getUserControllerName(final String key, final String value) {
468 String userControllerName = null;
469 // Check User Specific values.
471 final PEDependency dependency = PolicyUtils.jsonStringToObject(value, PEDependency.class);
472 userControllerName = key.replaceFirst("$controller:", "");
473 LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - " + dependency);
474 addToGroup(userControllerName, dependency);
475 } catch (final Exception e) {
476 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e);
478 return userControllerName;
481 private void updateUserDependencies(final ArrayList<PEDependency> userDependencies, String value) {
482 // update the user dependencies supplied as parameter to this method
483 value = value.substring(1, value.length() - 1).trim();
484 final List<String> dependencyStrings = Arrays.asList(value.split(Pattern.quote("},{")));
485 for (final String dependencyString : dependencyStrings) {
487 userDependencies.add(PolicyUtils.jsonStringToObject(dependencyString, PEDependency.class));
488 } catch (final Exception e) {
489 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Dependencies: " + e);
494 private void syncGroupInfo() {
495 // Sync DB to JMemory.
496 final EntityTransaction et = em.getTransaction();
499 final TypedQuery<BrmsGroupInfo> groupInfoQuery =
500 em.createQuery("select b from BrmsGroupInfo AS b", BrmsGroupInfo.class);
501 final List<BrmsGroupInfo> groupInfoResult = groupInfoQuery.getResultList();
502 if (groupInfoResult.size() != groupMap.size()) {
503 for (final BrmsGroupInfo brmsGroupInfo : groupInfoResult) {
504 final PEDependency dependency = new PEDependency();
505 dependency.setArtifactId(brmsGroupInfo.getArtifactId());
506 dependency.setGroupId(brmsGroupInfo.getGroupId());
507 dependency.setVersion(brmsGroupInfo.getVersion());
508 final ArrayList<Object> values = new ArrayList<>();
509 values.add(dependency);
510 groupMap.put(brmsGroupInfo.getControllerName(), values);
514 final TypedQuery<BrmsPolicyInfo> policyInfoQuery =
515 em.createQuery("select g from BrmsPolicyInfo AS g", BrmsPolicyInfo.class);
516 final List<BrmsPolicyInfo> policyInfoResult = policyInfoQuery.getResultList();
517 if (policyInfoResult.size() != policyMap.size()) {
518 for (final BrmsPolicyInfo brmsPolicyInfo : policyInfoResult) {
519 policyMap.put(brmsPolicyInfo.getPolicyName(),
520 brmsPolicyInfo.getControllerName().getControllerName());
524 LOGGER.info("Updated Local Memory values with values from database.");
525 } catch (final Exception exception) {
526 LOGGER.error("Unable to sync group info", exception);
534 private void manageProject(final String selectedName, final String ksessionName, final String name,
536 // Check if the Project is in Sync. If not get the latest Version.
537 syncProject(selectedName);
538 createProject(PROJECTSLOCATION + File.separator + getArtifactId(selectedName) + File.separator + "src"
539 + File.separator + "main" + File.separator + RESOURCES, ksessionName);
540 copyDataToFile(PROJECTSLOCATION + File.separator + getArtifactId(selectedName) + File.separator + "src"
541 + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES + File.separator + name
543 addToPolicy(name, selectedName);
547 * Add Policy to JMemory and DataBase.
549 private void addToPolicy(final String policyName, final String controllerName) {
551 final EntityTransaction et = em.getTransaction();
554 boolean create = false;
555 final TypedQuery<BrmsPolicyInfo> policyInfoQuery =
556 em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn", BrmsPolicyInfo.class);
557 policyInfoQuery.setParameter("pn", policyName);
558 final List<BrmsPolicyInfo> policyInfoResultList = policyInfoQuery.getResultList();
559 BrmsPolicyInfo brmsPolicyInfo = new BrmsPolicyInfo();
560 if (!policyInfoResultList.isEmpty()) {
562 brmsPolicyInfo = policyInfoResultList.get(0);
563 if (!brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) {
570 final TypedQuery<BrmsGroupInfo> groupInfoQuery = em.createQuery(
571 "select b from BrmsGroupInfo as b where b.controllerName = :cn", BrmsGroupInfo.class);
572 groupInfoQuery.setParameter("cn", controllerName);
573 final List<BrmsGroupInfo> groupInfoResultList = groupInfoQuery.getResultList();
574 BrmsGroupInfo brmsGroupInfo = new BrmsGroupInfo();
575 if (!groupInfoResultList.isEmpty()) {
576 brmsGroupInfo = groupInfoResultList.get(0);
578 brmsPolicyInfo.setPolicyName(policyName);
579 brmsPolicyInfo.setControllerName(brmsGroupInfo);
580 em.persist(brmsPolicyInfo);
585 policyMap.put(policyName, controllerName);
586 } catch (final Exception exception) {
587 LOGGER.error("Unable add policy to database", exception);
592 private void syncProject(final String selectedName) {
593 final boolean projectExists = checkProject(selectedName);
596 version = getVersion(selectedName);
597 if (version == null) {
598 LOGGER.error("Error getting local version for the given Controller Name:" + selectedName
599 + " going with Default value");
600 version = VERSION_0_1_0;
602 final String nextVersion = incrementVersion(version);
603 final boolean outOfSync = checkRemoteSync(selectedName, nextVersion);
608 // We are out of Sync or Project is not Present.
609 downloadProject(selectedName);
612 private void downloadProject(final String selectedName) {
613 final NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
614 if (artifact == null) {
617 final String dirName = getDirectoryName(selectedName);
619 final String fileName = "rule.jar";
621 website = new URL(artifact.getUrlPath() + ".jar");
622 try (ReadableByteChannel rbc = Channels.newChannel(website.openStream());
623 FileOutputStream fos = new FileOutputStream(fileName)) {
624 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
625 extractJar(fileName, dirName);
626 new File(fileName).delete();
627 } catch (final IOException e) {
628 LOGGER.error("Error while downloading the project to File System. " + e.getMessage(), e);
631 } catch (final IOException e1) {
632 LOGGER.error("Error while retrieve the artifact. " + e1.getMessage(), e1);
636 private void extractJar(final String jarFileName, final String artifactId) {
637 try (JarFile jar = new JarFile(jarFileName)) {
638 final Enumeration<?> enumEntries = jar.entries();
639 while (enumEntries.hasMoreElements()) {
640 parseJarContents(artifactId, jar, enumEntries);
642 } catch (final IOException e) {
643 LOGGER.info("exception Occured" + e);
647 private void parseJarContents(final String artifactId, final JarFile jar, final Enumeration<?> enumEntries) {
648 final JarEntry jarEntry = (JarEntry) enumEntries.nextElement();
650 final String fileName = jarEntry.getName().substring(jarEntry.getName().lastIndexOf("/") + 1);
651 if (jarEntry.getName().endsWith(".drl")) {
652 final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
653 + "main" + File.separator + RESOURCES + File.separator + RULES;
654 new File(path).mkdirs();
655 if (syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))) {
656 file = new File(path + File.separator + fileName);
658 file = new File(path + File.separator + fileName);
660 } else if (jarEntry.getName().endsWith(POM_XML_FILE)) {
661 final String path = PROJECTSLOCATION + File.separator + artifactId;
662 new File(path).mkdirs();
663 file = new File(path + File.separator + fileName);
664 } else if (jarEntry.getName().endsWith(KMODULE_XML_FILE)) {
665 final String path = PROJECTSLOCATION + File.separator + artifactId + File.separator + "src" + File.separator
666 + "main" + File.separator + RESOURCES + File.separator + META_INF;
667 new File(path).mkdirs();
668 file = new File(path + File.separator + fileName);
671 try (InputStream is = jar.getInputStream(jarEntry); FileOutputStream fos = new FileOutputStream(file)) {
672 while (is.available() > 0) {
673 fos.write(is.read());
675 LOGGER.info(fileName + " Created..");
676 } catch (final IOException e) {
677 LOGGER.info("exception Occured" + e);
682 private NexusArtifact getLatestArtifactFromNexus(final String selectedName) {
683 final List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, null);
686 NexusArtifact result = null;
687 for (final NexusArtifact artifact : artifacts) {
689 Integer.parseInt(artifact.getVersion().substring(0, artifact.getVersion().indexOf(".")));
690 final int minorVal = Integer.parseInt(artifact.getVersion()
691 .substring(artifact.getVersion().indexOf(".") + 1, artifact.getVersion().lastIndexOf(".")));
692 if (majorVal > bigNum) {
696 if ((bigNum == majorVal) && (minorVal > smallNum)) {
699 if (bigNum == majorVal && minorVal == smallNum) {
703 return additionalNexusLatestCheck(selectedName, result);
706 // Additional Check due to Limitations from Nexus API to check if the artifact is the latest.
707 private NexusArtifact additionalNexusLatestCheck(final String selectedName, final NexusArtifact result) {
708 if (result == null) {
711 final String nextVersion = incrementVersion(result.getVersion());
712 final List<NexusArtifact> artifact = getArtifactFromNexus(selectedName, nextVersion);
713 return artifact.isEmpty() ? result : additionalNexusLatestCheck(selectedName, artifact.get(0));
716 private boolean checkRemoteSync(final String selectedName, final String version) {
717 final List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, version);
718 return artifacts.isEmpty() ? false : true;
721 private List<NexusArtifact> getArtifactFromNexus(final String selectedName, final String version) {
722 NexusRestWrapper restWrapper = null;
724 boolean flag = false;
725 while (index < repUrlList.size()) {
727 final String repUrl = repUrlList.get(0);
729 new NexusRestWrapper(repUrl.substring(0, repUrl.indexOf(repUrl.split(":[0-9]+\\/nexus")[1])),
730 repUserName, repPassword);
731 final NexusRestSearchParameters searchParameters = new NexusRestSearchParameters();
732 searchParameters.useFilterSearch(getGroupId(selectedName), getArtifactId(selectedName), version, null,
735 final List<NexusArtifact> resultList = restWrapper.findArtifact(searchParameters).getArtifactList();
736 if (resultList != null) {
740 } catch (NexusRestWrapperException | ProcessingException e) {
741 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. "
742 + e.getMessage(), e);
744 if (null != restWrapper) {
748 Collections.rotate(repUrlList, -1);
753 return new ArrayList<>();
756 private String incrementVersion(final String version) {
757 int majorVal = Integer.parseInt(version.substring(0, version.indexOf(".")));
758 int minorVal = Integer.parseInt(version.substring(version.indexOf(".") + 1, version.lastIndexOf(".")));
765 return majorVal + "." + minorVal + version.substring(version.lastIndexOf("."));
768 private boolean checkProject(final String selectedName) {
769 return new File(PROJECTSLOCATION + File.separator + getDirectoryName(selectedName)).exists();
772 private String getDirectoryName(final String selectedName) {
773 return getArtifactId(selectedName);
777 * Will Push policies to the PolicyRepo.
779 * @throws PolicyException PolicyException related to the operation
781 public void pushRules() throws PolicyException {
782 // Check how many groups have been updated.
783 // Invoke their Maven process.
785 im.startTransaction();
786 } catch (final Exception e) {
787 LOGGER.error("Error while starting Transaction " + e);
789 if (!modifiedGroups.isEmpty()) {
790 if (buildAndGenerateJarFile()) {
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 Boolean buildAndGenerateJarFile() throws PolicyException {
813 Boolean flag = false;
814 for (final Map.Entry<String, String> entry : modifiedGroups.entrySet()) {
815 InvocationResult result = null;
816 final String group = entry.getKey();
818 LOGGER.info("PushRules: ModifiedGroups, Key: " + group + ", Value: " + entry.getValue());
819 final InvocationRequest request = new DefaultInvocationRequest();
822 request.setPomFile(new File(
823 PROJECTSLOCATION + File.separator + getArtifactId(group) + File.separator + POM_XML_FILE));
824 request.setGoals(Arrays.asList(GOALS));
825 final Invoker invoker = new DefaultInvoker();
826 result = invoker.execute(request);
827 if (result.getExecutionException() != null) {
828 LOGGER.error(result.getExecutionException());
829 } else if (result.getExitCode() != 0) {
830 LOGGER.error("Maven Invocation failure..!");
832 } catch (final Exception e) {
833 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation issue for "
834 + getArtifactId(group) + e.getMessage(), e);
836 if (result != null && result.getExitCode() == 0) {
837 LOGGER.info("Build Completed..!");
839 addNotification(group, "create");
841 addNotification(group, entry.getValue());
845 throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!");
851 private String getGroupName(final String name) {
852 if (policyMap.containsKey(name)) {
853 return policyMap.get(name);
856 return policyMap.containsKey(name) ? policyMap.get(name) : null;
860 private void addModifiedGroup(final String controllerName, final String operation) {
861 if (controllerName != null) {
862 modifiedGroups.put(controllerName, operation);
866 private void addNotification(final String controllerName, final String operation) {
867 final ControllerPojo controllerPojo = new ControllerPojo();
868 controllerPojo.setName(controllerName);
869 controllerPojo.setOperation(operation);
870 final HashMap<String, String> drools = new HashMap<>();
871 drools.put("groupId", getGroupId(controllerName));
872 drools.put("artifactId", getArtifactId(controllerName));
873 drools.put("version", getVersion(controllerName));
874 controllerPojo.setDrools(drools);
875 controllers.add(controllerPojo);
877 LOGGER.debug("Notification added: " + PolicyUtils.objectToJsonString(controllerPojo));
878 } catch (final JsonProcessingException e) {
879 LOGGER.error(MessageCodes.ERROR_SCHEMA_INVALID + "Json Processing Error " + e);
883 private void removedRuleModifiedGroup(final String controllerName) {
884 // This will be sending Notification to PDPD directly to Lock
885 final ControllerPojo controllerPojo = new ControllerPojo();
886 controllerPojo.setName(controllerName);
887 controllerPojo.setOperation("lock");
888 final List<ControllerPojo> controllerPojos = new ArrayList<>();
889 controllerPojos.add(controllerPojo);
890 sendNotification(controllerPojos);
893 private void sendNotification(final List<ControllerPojo> controllers) {
894 final NotificationPojo notification = new NotificationPojo();
895 final String requestId = UUID.randomUUID().toString();
896 LOGGER.info("Generating notification RequestID : " + requestId);
897 notification.setRequestId(requestId);
898 notification.setEntity("controller");
899 notification.setControllers(controllers);
901 final String notificationJson = PolicyUtils.objectToJsonString(notification);
902 LOGGER.info("Sending Notification :\n" + notificationJson);
903 sendMessage(notificationJson);
904 } catch (final Exception e) {
905 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while sending notification to PDP-D "
906 + e.getMessage(), e);
910 private void sendMessage(final String message) throws IOException, GeneralSecurityException, InterruptedException {
912 if ("dmaap".equalsIgnoreCase(notificationType)) {
913 // Sending Message through DMaaP Message Router
914 LOGGER.debug("DMAAP Publishing Message");
916 publisher.send("MyPartitionKey", message);
918 LOGGER.debug("Message Published on DMaaP :" + dmaapList.get(0) + "for Topic: " + pubTopic);
920 Thread.sleep(dmaapDelay);
923 // Sending Message through UEB interface.
924 LOGGER.debug("UEB Publishing Message");
926 final CambriaBatchingPublisher pub = pubBuilder.build();
927 pub.send("MyPartitionKey", message);
929 final List<?> stuck = pub.close(uebDelay, TimeUnit.SECONDS);
930 if (!stuck.isEmpty()) {
931 LOGGER.error(stuck.size() + " messages unsent");
933 LOGGER.debug("Clean exit; Message Published on UEB : " + uebList + "for Topic: " + pubTopic);
939 private void createPom(final String name) {
940 final Model model = new Model();
941 model.setModelVersion("4.0.0");
942 model.setGroupId(getGroupId(name));
943 model.setArtifactId(getArtifactId(name));
944 model.setVersion(getVersion(name));
946 final DistributionManagement distributionManagement = new DistributionManagement();
947 final DeploymentRepository repository = new DeploymentRepository();
948 repository.setId(repId);
949 repository.setName(repName);
950 repository.setUrl(repUrlList.get(0));
951 distributionManagement.setRepository(repository);
952 model.setDistributionManagement(distributionManagement);
953 // Dependency Management goes here.
954 List<Dependency> dependencyList = new ArrayList<>();
955 if (groupMap.get(name).size() > 1) {
956 @SuppressWarnings("unchecked")
957 final ArrayList<PEDependency> dependencies = (ArrayList<PEDependency>) groupMap.get(name).get(1);
958 for (final PEDependency dependency : dependencies) {
959 dependencyList.add(dependency.getDependency());
962 // Add Default dependencies.
963 dependencyList = getDependencies(name);
965 model.setDependencies(dependencyList);
966 Writer writer = null;
968 writer = WriterFactory.newXmlWriter(
969 new File(PROJECTSLOCATION + File.separator + getArtifactId(name) + File.separator + POM_XML_FILE));
970 final MavenXpp3Writer pomWriter = new MavenXpp3Writer();
971 pomWriter.write(writer, model);
972 } catch (final Exception e) {
973 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating POM for " + getArtifactId(name)
974 + e.getMessage(), e);
976 IOUtil.close(writer);
980 private List<Dependency> getDependencies(final String controllerName) {
981 // Read the Dependency Information from property file.
982 final Path file = Paths.get(DEPENDENCY_FILE);
983 if (!Files.notExists(file)) {
985 final String dependencyJson = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
986 final DependencyInfo dependencyInfo =
987 PolicyUtils.jsonStringToObject(dependencyJson, DependencyInfo.class);
988 String controller = "default";
989 if (dependencyInfo.getDependencies().containsKey(controllerName)) {
990 controller = controllerName;
992 final List<Dependency> dependencyList = new ArrayList<>();
993 for (final PEDependency dependency : dependencyInfo.getDependencies().get(controller)) {
994 dependencyList.add(dependency.getDependency());
996 return dependencyList;
997 } catch (IOException | NullPointerException e) {
998 LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW
999 + "Error while getting dependecy Information for controller: " + controllerName
1000 + e.getMessage(), e);
1003 return defaultDependencies(controllerName);
1007 * Default Dependency Section. Can be changed as required.
1009 * @param controllerName the controller name
1010 * @return changed dependency list
1012 public List<Dependency> defaultDependencies(final String controllerName) {
1014 final List<Dependency> dependencyList = new ArrayList<>();
1015 final String version = StringEscapeUtils.escapeJava(brmsdependencyversion);
1017 final Dependency demoDependency = new Dependency();
1018 demoDependency.setGroupId(DROOLS_APPS_TEMPLATE_GROUP);
1019 demoDependency.setArtifactId("template.demo");
1020 demoDependency.setVersion(version);
1021 dependencyList.add(demoDependency);
1023 final Dependency controlloopDependency = new Dependency();
1024 controlloopDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1025 controlloopDependency.setArtifactId("events");
1026 controlloopDependency.setVersion(version);
1027 dependencyList.add(controlloopDependency);
1029 final Dependency restDependency = new Dependency();
1030 restDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1031 restDependency.setArtifactId("controlloop.common.model-impl.rest");
1032 restDependency.setVersion(version);
1033 dependencyList.add(restDependency);
1035 final Dependency appcDependency = new Dependency();
1036 appcDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1037 appcDependency.setArtifactId("controlloop.common.model-impl.appc");
1038 appcDependency.setVersion(version);
1039 dependencyList.add(appcDependency);
1041 final Dependency aaiDependency = new Dependency();
1042 aaiDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1043 aaiDependency.setArtifactId("controlloop.common.model-impl.aai");
1044 aaiDependency.setVersion(version);
1045 dependencyList.add(aaiDependency);
1047 final Dependency msoDependency = new Dependency();
1048 msoDependency.setGroupId(DROOLS_APPS_MODEL_GROUP);
1049 msoDependency.setArtifactId("controlloop.common.model-impl.so");
1050 msoDependency.setVersion(version);
1051 dependencyList.add(msoDependency);
1052 return dependencyList;
1055 private void createProject(final String path, final String ksessionName) {
1056 new File(path + File.separator + RULES).mkdirs();
1057 new File(path + File.separator + META_INF).mkdirs();
1058 if (!Files.exists(Paths.get(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE))) {
1059 // Hard coding XML for PDP Drools to accept our Rules.
1060 final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n"
1061 + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">" + "\n"
1062 + "<kbase name=\"rules\" packages=\"rules\">" + "\n" + "<ksession name=\"" + ksessionName + "\"/>"
1063 + "\n" + "</kbase></kmodule>";
1064 copyDataToFile(path + File.separator + META_INF + File.separator + KMODULE_XML_FILE, xml);
1068 private void copyDataToFile(final String file, final String rule) {
1070 FileUtils.writeStringToFile(new File(file), rule);
1071 } catch (final Exception e) {
1073 XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while creating Rule for " + file + e.getMessage(),
1078 private void readGroups(final Properties config) throws PolicyException {
1079 String[] groupNames;
1080 final String groupNamesError = "groupNames property is missing or empty from the property file ";
1081 if (!config.containsKey(GROUP_NAMES) || config.getProperty(GROUP_NAMES) == null) {
1082 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + groupNamesError);
1084 if (config.getProperty(GROUP_NAMES).contains(",")) {
1085 groupNames = config.getProperty(GROUP_NAMES).replaceAll(" ", "").split(",");
1087 groupNames = new String[] {config.getProperty(GROUP_NAMES).replaceAll(" ", "")};
1089 if (groupNames == null || groupNames.length == 0) {
1090 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + groupNamesError);
1091 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + groupNamesError);
1093 groupMap = new HashMap<>();
1094 for (int counter = 0; counter < groupNames.length; counter++) {
1095 final String name = groupNames[counter];
1096 final String groupId = config.getProperty(name + ".groupID");
1097 if (groupId == null) {
1098 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1099 + ".groupID property is missing from the property file ");
1100 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1101 + ".groupID property is missing from the property file ");
1103 final String artifactId = config.getProperty(name + ".artifactID");
1104 if (artifactId == null) {
1105 LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1106 + ".artifactID property is missing from the property file ");
1107 throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + name
1108 + ".artifactID property is missing from the property file ");
1110 final PEDependency dependency = new PEDependency();
1111 dependency.setArtifactId(artifactId);
1112 dependency.setGroupId(groupId);
1113 // Add to list if we got all
1114 addToGroup(name, dependency);
1118 private void addToGroup(final String name, final PEDependency dependency) {
1119 final EntityTransaction et = em.getTransaction();
1122 final TypedQuery<BrmsGroupInfo> query = em
1123 .createQuery("select b from BrmsGroupInfo as b where b.controllerName = :cn", BrmsGroupInfo.class);
1124 query.setParameter("cn", name);
1125 final List<BrmsGroupInfo> groupList = query.getResultList();
1126 BrmsGroupInfo brmsGroupInfo = null;
1127 if (!groupList.isEmpty()) {
1128 LOGGER.info("Controller name already Existing in DB. Will be updating the DB Values" + name);
1129 brmsGroupInfo = groupList.get(0);
1131 if (brmsGroupInfo == null) {
1132 brmsGroupInfo = new BrmsGroupInfo();
1134 brmsGroupInfo.setControllerName(name);
1135 brmsGroupInfo.setGroupId(dependency.getGroupId());
1136 brmsGroupInfo.setArtifactId(dependency.getArtifactId());
1137 brmsGroupInfo.setVersion(dependency.getVersion());
1138 em.persist(brmsGroupInfo);
1142 final ArrayList<Object> values = new ArrayList<>();
1143 values.add(dependency);
1144 groupMap.put(name, values);
1145 } catch (final Exception exception) {
1146 LOGGER.error("Unable add/update policy group to database for controller name: " + name, exception);
1151 private String getArtifactId(final String name) {
1152 return ((PEDependency) groupMap.get(name).get(0)).getArtifactId();
1155 private String getGroupId(final String name) {
1156 return ((PEDependency) groupMap.get(name).get(0)).getGroupId();
1159 private String getVersion(final String name) {
1160 return ((PEDependency) groupMap.get(name).get(0)).getVersion();
1163 private void getNameAndSetRemove(final String controllerName, final String policyName) {
1164 final String artifactName = getArtifactId(controllerName);
1165 final String ruleFolder = PROJECTSLOCATION + File.separator + artifactName + File.separator + "src"
1166 + File.separator + "main" + File.separator + RESOURCES + File.separator + RULES;
1167 final File file = new File(ruleFolder + File.separator + policyName + ".drl");
1168 if (file.delete()) {
1169 LOGGER.info("Deleted File.. " + file.getAbsolutePath());
1170 removePolicyFromGroup(policyName, controllerName);
1172 if (new File(ruleFolder).listFiles().length == 0) {
1173 removedRuleModifiedGroup(controllerName);
1175 // This is an update in terms of PDPD.
1176 addModifiedGroup(controllerName, "update");
1180 // Removes Policy from Memory and Database.
1181 private void removePolicyFromGroup(final String policyName, final String controllerName) {
1182 final EntityTransaction et = em.getTransaction();
1185 final TypedQuery<BrmsPolicyInfo> query =
1186 em.createQuery("select b from BrmsPolicyInfo as b where b.policyName = :pn", BrmsPolicyInfo.class);
1187 query.setParameter("pn", policyName);
1188 final List<BrmsPolicyInfo> pList = query.getResultList();
1189 BrmsPolicyInfo brmsPolicyInfo;
1190 if (!pList.isEmpty()) {
1192 brmsPolicyInfo = pList.get(0);
1193 if (brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)) {
1194 em.remove(brmsPolicyInfo);
1199 policyMap.remove(policyName);
1200 } catch (final Exception exception) {
1201 LOGGER.error("Unable remove policy from group to database for policy name: " + policyName, exception);
1206 private void setVersion(final String selectedName) {
1207 String newVersion = VERSION_0_1_0;
1209 final NexusArtifact artifact = getLatestArtifactFromNexus(selectedName);
1210 if (artifact != null) {
1211 newVersion = incrementVersion(artifact.getVersion());
1213 if (VERSION_0_1_0.equals(newVersion)) {
1216 setVersion(newVersion, selectedName);
1217 LOGGER.info("Controller: " + selectedName + "is on version: " + newVersion);
1220 private void setVersion(final String newVersion, final String controllerName) {
1221 final PEDependency userController = (PEDependency) groupMap.get(controllerName).get(0);
1222 userController.setVersion(newVersion);
1223 groupMap.get(controllerName).set(0, userController);
1226 // Return BackUpMonitor
1227 public static BackUpMonitor getBackUpMonitor() {
1234 public void rotateUrls() {
1235 if (repUrlList != null) {
1236 Collections.rotate(repUrlList, -1);
1241 * Get URL List Size.
1243 * @return URL list size
1245 public int urlListSize() {
1246 return repUrlList != null ? repUrlList.size() : 0;