import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import org.openecomp.policy.common.im.AdministrativeStateException;
import org.openecomp.policy.common.im.IntegrityMonitor;
import org.openecomp.policy.common.logging.eelf.MessageCodes;
+import org.openecomp.policy.common.logging.eelf.PolicyLogger;
import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
import org.openecomp.policy.common.logging.flexlogger.Logger;
import org.openecomp.policy.utils.BackUpHandler;
* BRMSPush: Application responsible to push policies to the BRMS PDP Policy Repository (PR).
* Mavenize and push policy to PR
*
- * @version 0.9
+ * @version 1.0
*/
+@SuppressWarnings("deprecation")
public class BRMSPush {
private static final Logger LOGGER = FlexLogger.getLogger(BRMSPush.class.getName());
private static final String PROJECTSLOCATION = "RuleProjects";
private static final String DEFAULT_VERSION = "1.1.0-SNAPSHOT";
private static final String DEPENDENCY_FILE = "dependency.json";
- private static Map<String, String> modifiedGroups = new HashMap<String, String>();
+ private static Map<String, String> modifiedGroups = new HashMap<>();
private static IntegrityMonitor im;
private static BackUpMonitor bm;
private static String resourceName = null;
private String defaultName = null;
private String repID = null;
private String repName = null;
- private String repURL= null;
+ private ArrayList<String> repURLs= null;
private String repUserName = null;
private String repPassword = null;
private String policyKeyID = null;
private Long dmaapDelay = Long.parseLong("5000");
private String notificationType = null;
private ArrayList<ControllerPOJO> controllers;
- private HashMap<String, ArrayList<Object>> groupMap = new HashMap<String, ArrayList<Object>>();
- private Map<String, String> policyMap = new HashMap<String,String>();
+ private HashMap<String, ArrayList<Object>> groupMap = new HashMap<>();
+ private Map<String, String> policyMap = new HashMap<>();
private String brmsdependencyversion;
private EntityManager em;
+ private boolean syncFlag = false;
public BRMSPush(String propertiesFile, BackUpHandler handler) throws PolicyException{
Properties config = new Properties();
throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryName property is missing from the property file ");
}
repName = repName.trim();
- repURL = config.getProperty("repositoryURL");
+ String repURL = config.getProperty("repositoryURL");
if(repURL==null){
LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "repositoryURL property is missing from the property file ");
}
- repURL = repURL.trim();
+ if(repURL.contains(",")){
+ repURLs = new ArrayList<>(Arrays.asList(repURL.trim().split(",")));
+ }else{
+ repURLs = new ArrayList<>();
+ repURLs.add(repURL);
+ }
repUserName = config.getProperty("repositoryUsername");
repPassword = config.getProperty("repositoryPassword");
if(repUserName==null || repPassword==null){
throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "policyKeyID property is missing from the property file ");
}
policyKeyID = policyKeyID.trim();
+ String syncF = config.getProperty("sync", "false").trim();
+ syncFlag = Boolean.parseBoolean(syncF);
+ if(syncFlag){
+ PolicyLogger.info("SYNC Flag is turned ON. DB will be given Priority.");
+ }
brmsdependencyversion = config.getProperty("brms.dependency.version");
if(brmsdependencyversion==null){
LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "brmsdependencyversion property is missing from the property file, Using default Version.");
pubTopic = pubTopic.trim();
if(dmaapServers.contains(",")) {
- dmaapList = new ArrayList<String>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
+ dmaapList = new ArrayList<>(Arrays.asList(dmaapServers.split("\\s*,\\s*")));
} else {
- dmaapList = new ArrayList<String>();
+ dmaapList = new ArrayList<>();
dmaapList.add(dmaapServers);
}
* Will Initialize the variables required for BRMSPush.
*/
public void initiate(boolean flag) {
- modifiedGroups = new HashMap<String, String>();
- controllers = new ArrayList<ControllerPOJO>();
+ modifiedGroups = new HashMap<>();
+ controllers = new ArrayList<>();
try {
bm.updateNotification();
} catch (Exception e) {
if(!responseAttributes.isEmpty()){
// Pick selected Value
String userControllerName = null;
- ArrayList<PEDependency> userDependencies = new ArrayList<PEDependency>();
+ ArrayList<PEDependency> userDependencies = new ArrayList<>();
for(String key: responseAttributes.keySet()){
if(key.equals(policyKeyID)){
selectedName = responseAttributes.get(key);
dependency.setArtifactId(brmsGroupInfo.getArtifactId());
dependency.setGroupId(brmsGroupInfo.getGroupId());
dependency.setVersion(brmsGroupInfo.getVersion());
- ArrayList<Object> values = new ArrayList<Object>();
+ ArrayList<Object> values = new ArrayList<>();
values.add(dependency);
groupMap.put(brmsGroupInfo.getControllerName(), values);
}
addToPolicy(name,selectedName);
}
+ /*
+ * Add Policy to JMemory and DataBase.
+ */
private void addToPolicy(String policyName, String controllerName) {
policyMap.put(policyName, controllerName);
EntityTransaction et = em.getTransaction();
String path = PROJECTSLOCATION+ File.separator + artifactId + File.separator + "src"+
File.separator+ "main" + File.separator + "resources" + File.separator+ "rules";
new File(path).mkdirs();
- f = new File(path + File.separator + fileName);
+ if(syncFlag && policyMap.containsKey(fileName.replace(".drl", ""))){
+ f = new File(path + File.separator + fileName);
+ }else{
+ f = new File(path + File.separator + fileName);
+ }
}else if(file.getName().endsWith("pom.xml")){
String path = PROJECTSLOCATION+ File.separator + artifactId;
new File(path).mkdirs();
result = artifact;
}
}
- return result;
+ return additionalNexusLatestCheck(selectedName, result);
+ }
+
+ // Additional Check due to Limitations from Nexus API to check if the artifact is the latest.
+ private NexusArtifact additionalNexusLatestCheck(String selectedName, NexusArtifact result) {
+ String nextVersion = incrementVersion(result.getVersion());
+ List<NexusArtifact> artifact = getArtifactFromNexus(selectedName, nextVersion);
+ return artifact.isEmpty()? result: additionalNexusLatestCheck(selectedName, artifact.get(0));
}
private boolean checkRemoteSync(String selectedName, String version) {
private List<NexusArtifact> getArtifactFromNexus(String selectedName, String version) {
final NexusClient client = new NexusRestClient();
- try {
- client.connect(repURL.substring(0, repURL.indexOf(repURL.split(":[0-9]+\\/nexus")[1])), repUserName, repPassword);
- final NexusArtifact template = new NexusArtifact();
- template.setGroupId(getGroupID(selectedName));
- template.setArtifactId(getArtifactID(selectedName));
- if(version!=null){
- template.setVersion(version);
- }
- return client.searchByGAV(template);
- } catch (NexusClientException | NexusConnectionException | NullPointerException e) {
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. " +e.getMessage());
- } finally {
+ int i = 0 ;
+ boolean flag = false;
+ while(i<repURLs.size()){
try {
- client.disconnect();
- } catch (NexusClientException | NexusConnectionException e) {
- LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus." +e.getMessage());
+ String repURL = repURLs.get(0);
+ client.connect(repURL.substring(0, repURL.indexOf(repURL.split(":[0-9]+\\/nexus")[1])), repUserName, repPassword);
+ final NexusArtifact template = new NexusArtifact();
+ template.setGroupId(getGroupID(selectedName));
+ template.setArtifactId(getArtifactID(selectedName));
+ if(version!=null){
+ template.setVersion(version);
+ }
+ List<NexusArtifact> resultList = client.searchByGAV(template);
+ if(resultList!=null){
+ flag = true;
+ return resultList;
+ }
+ } catch (NexusClientException | NexusConnectionException | NullPointerException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Connection to remote Nexus has failed. " +e.getMessage());
+ } finally {
+ try {
+ client.disconnect();
+ } catch (NexusClientException | NexusConnectionException e) {
+ LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "failed to disconnect Connection from Nexus." +e.getMessage());
+ }
+ if(!flag){
+ Collections.rotate(repURLs, -1);
+ i++;
+ }
}
}
- return new ArrayList<NexusArtifact>();
+ return new ArrayList<>();
}
private void setVersion(String selectedName) {
* Will Push policies to the PolicyRepo.
*
* @param notificationType <String> type of notification Type.
+ * @throws PolicyException
*/
- public void pushRules(){
+ public void pushRules() throws PolicyException{
// Check how many groups have been updated.
// Invoke their Maven process.
try {
if(!modifiedGroups.isEmpty()){
Boolean flag = false;
for(String group: modifiedGroups.keySet()){
+ InvocationResult result = null;
try{
InvocationRequest request = new DefaultInvocationRequest();
setVersion(group);
request.setPomFile(new File(PROJECTSLOCATION+File.separator+getArtifactID(group)+File.separator+"pom.xml"));
request.setGoals(Arrays.asList(GOALS));
Invoker invoker = new DefaultInvoker();
- InvocationResult result = invoker.execute(request);
+ result = invoker.execute(request);
if(result.getExecutionException()!=null){
LOGGER.error(result.getExecutionException());
}else if(result.getExitCode()!=0){
LOGGER.error("Maven Invocation failure..!");
}
- if(result.getExitCode()==0){
- LOGGER.info("Build Completed..!");
- if (createFlag) {
- addNotification(group, "create");
- }else{
- addNotification(group, modifiedGroups.get(group));
- }
- flag = true;
- }
}catch(Exception e){
LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Maven Invocation issue for "+getArtifactID(group) + e.getMessage());
}
+ if(result!=null && result.getExitCode()==0){
+ LOGGER.info("Build Completed..!");
+ if (createFlag) {
+ addNotification(group, "create");
+ }else{
+ addNotification(group, modifiedGroups.get(group));
+ }
+ flag = true;
+ }else{
+ throw new PolicyException(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Maven Invocation failure!");
+ }
}
if(flag){
sendNotification(controllers);
ControllerPOJO controllerPOJO = new ControllerPOJO();
controllerPOJO.setName(controllerName);
controllerPOJO.setOperation(operation);
- HashMap<String, String> drools = new HashMap<String, String>();
+ HashMap<String, String> drools = new HashMap<>();
drools.put("groupId", getGroupID(controllerName));
drools.put("artifactId", getArtifactID(controllerName));
drools.put("version", getVersion(controllerName));
ControllerPOJO controllerPOJO = new ControllerPOJO();
controllerPOJO.setName(controllerName);
controllerPOJO.setOperation("lock");
- List<ControllerPOJO> controllers = new ArrayList<ControllerPOJO>();
+ List<ControllerPOJO> controllers = new ArrayList<>();
controllers.add(controllerPOJO);
sendNotification(controllers);
}
DeploymentRepository repository = new DeploymentRepository();
repository.setId(repID);
repository.setName(repName);
- repository.setUrl(repURL);
+ repository.setUrl(repURLs.get(0));
distributionManagement.setRepository(repository);
model.setDistributionManagement(distributionManagement);
- // Depenendency Mangement goes here.
- List<Dependency> dependencyList= new ArrayList<Dependency>();
+ // Dependency Management goes here.
+ List<Dependency> dependencyList= new ArrayList<>();
if(groupMap.get(name).size()>1){
@SuppressWarnings("unchecked")
ArrayList<PEDependency> dependencies = (ArrayList<PEDependency>) groupMap.get(name).get(1);
if(dependencyInfo.getDependencies().containsKey(controllerName)){
controller = controllerName;
}
- List<Dependency> dependencyList = new ArrayList<Dependency>();
+ List<Dependency> dependencyList = new ArrayList<>();
for(PEDependency dependency: dependencyInfo.getDependencies().get(controller)){
dependencyList.add(dependency.getDependency());
}
// Default Dependency Section. Can be changed as required.
public List<Dependency> defaultDependencies(String controllerName) {
- List<Dependency> dependencyList = new ArrayList<Dependency>();
+ List<Dependency> dependencyList = new ArrayList<>();
String version= StringEscapeUtils.escapeJava(brmsdependencyversion);
Dependency demoDependency = new Dependency();
- demoDependency.setGroupId("org.openecomp.policy.drools-applications");
+ demoDependency.setGroupId("org.onap.policy.drools-applications");
demoDependency.setArtifactId("demo");
demoDependency.setVersion(version);
dependencyList.add(demoDependency);
Dependency controlloopDependency = new Dependency();
- controlloopDependency.setGroupId("org.openecomp.policy.drools-applications");
- controlloopDependency.setArtifactId("controlloop");
+ controlloopDependency.setGroupId("org.onap.policy.drools-applications");
+ controlloopDependency.setArtifactId("events");
controlloopDependency.setVersion(version);
dependencyList.add(controlloopDependency);
Dependency restDependency = new Dependency();
- restDependency.setGroupId("org.openecomp.policy.drools-applications");
+ restDependency.setGroupId("org.onap.policy.drools-applications");
restDependency.setArtifactId("rest");
restDependency.setVersion(version);
dependencyList.add(restDependency);
Dependency appcDependency = new Dependency();
- appcDependency.setGroupId("org.openecomp.policy.drools-applications");
+ appcDependency.setGroupId("org.onap.policy.drools-applications");
appcDependency.setArtifactId("appc");
appcDependency.setVersion(version);
dependencyList.add(appcDependency);
Dependency aaiDependency = new Dependency();
- aaiDependency.setGroupId("org.openecomp.policy.drools-applications");
+ aaiDependency.setGroupId("org.onap.policy.drools-applications");
aaiDependency.setArtifactId("aai");
aaiDependency.setVersion(version);
dependencyList.add(aaiDependency);
Dependency msoDependency = new Dependency();
- msoDependency.setGroupId("org.openecomp.policy.drools-applications");
+ msoDependency.setGroupId("org.onap.policy.drools-applications");
msoDependency.setArtifactId("mso");
msoDependency.setVersion(version);
dependencyList.add(msoDependency);
Dependency trafficgeneratorDependency = new Dependency();
- trafficgeneratorDependency.setGroupId("org.openecomp.policy.drools-applications");
+ trafficgeneratorDependency.setGroupId("org.onap.policy.drools-applications");
trafficgeneratorDependency.setArtifactId("trafficgenerator");
trafficgeneratorDependency.setVersion(version);
dependencyList.add(trafficgeneratorDependency);
LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "groupNames property is missing or empty from the property file ");
throw new PolicyException(XACMLErrorConstants.ERROR_DATA_ISSUE + "groupNames property is missing or empty from the property file ");
}
- groupMap = new HashMap<String, ArrayList<Object>>();
+ groupMap = new HashMap<>();
for(int counter=0; counter < groupNames.length ;counter++){
String name = groupNames[counter];
String groupID = config.getProperty(name+".groupID");
}
private void addToGroup(String name, PEDependency dependency) {
- ArrayList<Object> values = new ArrayList<Object>();
+ ArrayList<Object> values = new ArrayList<>();
values.add(dependency);
groupMap.put(name, values);
EntityTransaction et = em.getTransaction();
File file = new File(ruleFolder+File.separator+ policyName +".drl");
if(file.delete()){
LOGGER.info("Deleted File.. " + file.getAbsolutePath());
+ removePolicyFromGroup(policyName, controllerName);
}
if(new File(ruleFolder).listFiles().length == 0) {
removedRuleModifiedGroup(controllerName);
}
}
+ // Removes Policy from Memory and Database.
+ private void removePolicyFromGroup(String policyName, String controllerName) {
+ policyMap.remove(policyName);
+ EntityTransaction et = em.getTransaction();
+ et.begin();
+ Query query = em.createQuery("select b from BRMSPolicyInfo as b where b.policyName = :pn");
+ query.setParameter("pn", policyName);
+ List<?> pList = query.getResultList();
+ BRMSPolicyInfo brmsPolicyInfo = new BRMSPolicyInfo();
+ if(pList.size()>0){
+ // Already exists.
+ brmsPolicyInfo = (BRMSPolicyInfo) pList.get(0);
+ if(brmsPolicyInfo.getControllerName().getControllerName().equals(controllerName)){
+ em.remove(brmsPolicyInfo);
+ em.flush();
+ }
+ }
+ et.commit();
+ }
+
private void setVersion(String newVersion, String controllerName) {
PEDependency userController = (PEDependency) groupMap.get(controllerName).get(0);
userController.setVersion(newVersion);
public static BackUpMonitor getBackUpMonitor(){
return bm;
}
+
+ public void rotateURLs() {
+ if(repURLs!=null){
+ Collections.rotate(repURLs, -1);
+ }
+ }
+
+ public int URLListSize() {
+ if(repURLs!=null){
+ return repURLs.size();
+ }else return 0;
+ }
}