* ONAP PAP
* ================================================================================
* Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2021 Nordix Foundation.
+ * Modifications Copyright (C) 2021-2022 Nordix Foundation.
+ * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
+import lombok.Getter;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PolicyNotification;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpState;
-import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter.ToscaTypedEntityFilterBuilder;
import org.onap.policy.pap.main.notification.DeploymentStatus;
+import org.onap.policy.pap.main.service.PdpGroupService;
+import org.onap.policy.pap.main.service.PolicyAuditService;
+import org.onap.policy.pap.main.service.PolicyStatusService;
+import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
*/
private static final Pattern VERSION_PREFIX_PAT = Pattern.compile("[^.]+(?:[.][^.]+)?");
- /**
- * DB provider.
- */
- private final PolicyModelsProvider dao;
-
/**
* Maps a group name to its group data. This accumulates the set of groups to be created and updated when the REST
* call completes.
/**
* Map's a policy's identifier to the policies for deployment.
*/
- private Map<ToscaConceptIdentifier, ToscaPolicy> policiesToBeDeployed = new HashMap<>();
+ private final Map<ToscaConceptIdentifier, ToscaPolicy> policiesToBeDeployed = new HashMap<>();
/**
* Set of policies to be undeployed.
*/
- private Set<ToscaConceptIdentifier> policiesToBeUndeployed = new HashSet<>();
+ private final Set<ToscaConceptIdentifier> policiesToBeUndeployed = new HashSet<>();
+
+ /**
+ * User starting requests.
+ */
+ @Getter
+ private final String user;
/**
* Tracks policy deployment status so notifications can be generated.
*/
private final DeploymentStatus deployStatus;
+ private final PolicyAuditManager auditManager;
+
+ private final ToscaServiceTemplateService toscaService;
+
+ private final PdpGroupService pdpGroupService;
/**
* Constructs the object.
*
- * @param dao DAO provider
- */
- public SessionData(PolicyModelsProvider dao) {
- this.dao = dao;
- this.deployStatus = makeDeploymentStatus(dao);
+ * @param user user triggering the request
+ * @param policyAuditService the policyAuditService
+ * @param policyStatusService the policyStatusService
+ * @param pdpGroupService the pdpGroupService
+ * @param toscaService the toscaService
+ */
+ public SessionData(String user, ToscaServiceTemplateService toscaService, PdpGroupService pdpGroupService,
+ PolicyStatusService policyStatusService, PolicyAuditService policyAuditService) {
+ this.toscaService = toscaService;
+ this.pdpGroupService = pdpGroupService;
+ this.deployStatus = makeDeploymentStatus(policyStatusService);
+ this.auditManager = makePolicyAuditManager(policyAuditService);
+ this.user = user;
}
/**
ToscaPolicyType type = typeCache.get(desiredType);
if (type == null) {
- List<ToscaPolicyType> lst = dao.getPolicyTypeList(desiredType.getName(), desiredType.getVersion());
+ List<ToscaPolicyType> lst = toscaService.getPolicyTypeList(desiredType.getName(), desiredType.getVersion());
if (lst.isEmpty()) {
return null;
}
ToscaPolicy policy = policyCache.get(desiredPolicy);
if (policy == null) {
ToscaTypedEntityFilterBuilder<ToscaPolicy> filterBuilder =
- ToscaTypedEntityFilter.<ToscaPolicy>builder().name(desiredPolicy.getName());
+ ToscaTypedEntityFilter.<ToscaPolicy>builder().name(desiredPolicy.getName());
setPolicyFilterVersion(filterBuilder, desiredPolicy.getVersion());
- List<ToscaPolicy> lst = dao.getFilteredPolicyList(filterBuilder.build());
+ List<ToscaPolicy> lst = toscaService.getFilteredPolicyList(filterBuilder.build());
if (lst.isEmpty()) {
return null;
}
}
logger.info("add update and state-change {} {} {} policies={}", update.getName(), update.getPdpGroup(),
- update.getPdpSubgroup(), update.getPolicies().size());
+ update.getPdpSubgroup(), update.getPoliciesToBeDeployed().size());
pdpRequests.put(update.getName(), Pair.of(update, change));
}
*/
public void addUpdate(PdpUpdate update) {
logger.info("add update {} {} {} policies={}", update.getName(), update.getPdpGroup(), update.getPdpSubgroup(),
- update.getPolicies().size());
+ update.getPoliciesToBeDeployed().size());
pdpRequests.compute(update.getName(), (name, data) -> Pair.of(update, (data == null ? null : data.getRight())));
}
GroupData data = groupCache.get(name);
if (data == null) {
- List<PdpGroup> lst = dao.getPdpGroups(name);
+ List<PdpGroup> lst = pdpGroupService.getPdpGroups(name);
if (lst.isEmpty()) {
logger.info("unknown group {}", name);
return null;
*
* @param type desired policy type
* @return the active groups supporting the given policy
- * @throws PfModelException if an error occurred
*/
- public List<PdpGroup> getActivePdpGroupsByPolicyType(ToscaConceptIdentifier type) throws PfModelException {
+ public List<PdpGroup> getActivePdpGroupsByPolicyType(ToscaConceptIdentifier type) {
/*
* Cannot use computeIfAbsent() because the enclosed code throws an unchecked exception and handling that would
* obfuscate the code too much, thus disabling the sonar.
PdpGroupFilter filter = PdpGroupFilter.builder().policyTypeList(Collections.singletonList(type))
.groupState(PdpState.ACTIVE).build();
- List<PdpGroup> groups = dao.getFilteredPdpGroups(filter);
+ List<PdpGroup> groups = pdpGroupService.getFilteredPdpGroups(filter);
data = groups.stream().map(this::addGroup).collect(Collectors.toList());
type2groups.put(type, data);
/**
* Gets the list of policies to be deployed to the PDPs.
*
- * @returns a list of policies to be deployed
+ * @return a list of policies to be deployed
*/
public List<ToscaPolicy> getPoliciesToBeDeployed() {
return new LinkedList<>(this.policiesToBeDeployed.values());
/**
* Gets the list of policies to be undeployed from the PDPs.
*
- * @returns a list of policies to be undeployed
+ * @return a list of policies to be undeployed
*/
public List<ToscaConceptIdentifier> getPoliciesToBeUndeployed() {
return new LinkedList<>(this.policiesToBeUndeployed);
* Update the DB with the changes.
*
* @param notification notification to which to add policy status
- *
- * @throws PfModelException if an error occurred
*/
- public void updateDb(PolicyNotification notification) throws PfModelException {
+ public void updateDb(PolicyNotification notification) {
// create new groups
List<GroupData> created = groupCache.values().stream().filter(GroupData::isNew).collect(Collectors.toList());
if (!created.isEmpty()) {
if (logger.isInfoEnabled()) {
created.forEach(group -> logger.info("creating DB group {}", group.getGroup().getName()));
}
- dao.createPdpGroups(created.stream().map(GroupData::getGroup).collect(Collectors.toList()));
+ pdpGroupService.createPdpGroups(created.stream().map(GroupData::getGroup).collect(Collectors.toList()));
}
// update existing groups
if (logger.isInfoEnabled()) {
updated.forEach(group -> logger.info("updating DB group {}", group.getGroup().getName()));
}
- dao.updatePdpGroups(updated.stream().map(GroupData::getGroup).collect(Collectors.toList()));
+ pdpGroupService.updatePdpGroups(updated.stream().map(GroupData::getGroup).collect(Collectors.toList()));
}
+ // send audits records to DB
+ auditManager.saveRecordsToDb();
+
// flush deployment status records to the DB
deployStatus.flush(notification);
}
* Deletes a group from the DB, immediately (i.e., without caching the request to be executed later).
*
* @param group the group to be deleted
- * @throws PfModelException if an error occurred
*/
- public void deleteGroupFromDb(PdpGroup group) throws PfModelException {
+ public void deleteGroupFromDb(PdpGroup group) {
logger.info("deleting DB group {}", group.getName());
- dao.deletePdpGroup(group.getName());
+ pdpGroupService.deletePdpGroup(group.getName());
}
/**
* @param pdpType PDP type (i.e., PdpSubGroup) containing the PDP of interest
* @throws PfModelException if an error occurred
*/
- protected void trackDeploy(ToscaPolicy policy, Collection<String> pdps, String pdpGroup,
- String pdpType) throws PfModelException {
+ protected void trackDeploy(ToscaPolicy policy, Collection<String> pdps, String pdpGroup, String pdpType)
+ throws PfModelException {
ToscaConceptIdentifier policyId = policy.getIdentifier();
policiesToBeDeployed.put(policyId, policy);
addData(policyId, pdps, pdpGroup, pdpType, true);
+ auditManager.addDeploymentAudit(policyId, pdpGroup, pdpType, user);
}
/**
protected void trackUndeploy(ToscaConceptIdentifier policyId, Collection<String> pdps, String pdpGroup,
String pdpType) throws PfModelException {
policiesToBeUndeployed.add(policyId);
+
addData(policyId, pdps, pdpGroup, pdpType, false);
+ auditManager.addUndeploymentAudit(policyId, pdpGroup, pdpType, user);
}
/**
// these may be overridden by junit tests
- protected DeploymentStatus makeDeploymentStatus(PolicyModelsProvider dao) {
- return new DeploymentStatus(dao);
+ protected DeploymentStatus makeDeploymentStatus(PolicyStatusService policyStatusService) {
+ return new DeploymentStatus(policyStatusService);
+ }
+
+ protected PolicyAuditManager makePolicyAuditManager(PolicyAuditService policyAuditService) {
+ return new PolicyAuditManager(policyAuditService);
}
}