diff options
author | liamfallon <liam.fallon@ericsson.com> | 2018-01-26 14:31:09 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@ericsson.com> | 2018-01-28 20:21:43 +0000 |
commit | 7767c5643349e5e6691a07afd5c4acd3ef9d5617 (patch) | |
tree | f5d4441392f44a91cf23de6f275bfa127c9bc8f6 /controlloop | |
parent | e285764b0eddab0d4195128f55a9f524beee3f05 (diff) |
Fix Tech Debt/JUnit on SO actor
Unit test for SO actor added. SO actor service provider class refactored to remove technical debt.
Change-Id: I6186ede5661ce483917184f03ee44d57a124b6a4
Issue-ID: POLICY-455
Signed-off-by: liamfallon <liam.fallon@ericsson.com>
Diffstat (limited to 'controlloop')
3 files changed, 338 insertions, 565 deletions
diff --git a/controlloop/common/actors/actor.so/pom.xml b/controlloop/common/actors/actor.so/pom.xml index 39397bf92..3246e8929 100644 --- a/controlloop/common/actors/actor.so/pom.xml +++ b/controlloop/common/actors/actor.so/pom.xml @@ -1,17 +1,16 @@ <?xml version="1.0"?> -<project - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" - xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> - <groupId>org.onap.policy.drools-applications</groupId> + <groupId>org.onap.policy.drools-applications</groupId> <artifactId>actors</artifactId> <version>1.2.0-SNAPSHOT</version> </parent> <artifactId>actor.so</artifactId> <dependencies> <dependency> - <groupId>org.onap.policy.drools-applications</groupId> + <groupId>org.onap.policy.drools-applications</groupId> <artifactId>actorServiceProvider</artifactId> <version>${project.version}</version> <scope>provided</scope> @@ -47,10 +46,22 @@ <scope>provided</scope> </dependency> <dependency> - <groupId>org.onap.policy.drools-pdp</groupId> - <artifactId>policy-management</artifactId> - <version>${project.version}</version> - <scope>provided</scope> + <groupId>org.onap.policy.drools-pdp</groupId> + <artifactId>policy-management</artifactId> + <version>${project.version}</version> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.onap.policy.drools-applications</groupId> + <artifactId>simulators</artifactId> + <version>${project.version}</version> + <scope>test</scope> </dependency> </dependencies> </project> diff --git a/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java b/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java index eb2bc1731..cb31b7458 100644 --- a/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java +++ b/controlloop/common/actors/actor.so/src/main/java/org/onap/policy/controlloop/actor/so/SOActorServiceProvider.java @@ -58,58 +58,29 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; public class SOActorServiceProvider implements Actor { - private static final Logger logger = LoggerFactory.getLogger(SOActorServiceProvider.class); - private static String vnfItemVnfId; + // Strings for SO Actor + private static final String SO_ACTOR = "SO"; - private String vnfItemVnfType; + // Strings for targets + private static final String TARGET_VFC = "VFC"; - private String vnfItemModelInvariantId; + // Strings for recipes + private static final String RECIPE_VF_MODULE_CREATE = "VF Module Create"; - private String vnfItemModelVersionId; - - private String vnfItemModelName; - - private String vnfItemModelVersion; - - private String vnfItemModelNameVersionId; - - private static String serviceItemServiceInstanceId; - - private String serviceItemModelInvariantId; - - private String serviceItemModelName; - - private String serviceItemModelType; - - private String serviceItemModelVersion; - - private String serviceItemModelNameVersionId; - - private String vfModuleItemVfModuleName; - - private String vfModuleItemModelInvariantId; - - private String vfModuleItemModelVersionId; - - private String vfModuleItemModelName; - - private String vfModuleItemModelNameVersionId; - - private String tenantItemTenantId; - - private String cloudRegionItemCloudRegionId; - - private static final ImmutableList<String> recipes = ImmutableList.of( - "VF Module Create"); + private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_VF_MODULE_CREATE); private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>() - .put("VF Module Create", ImmutableList.of("VFC")) + .put(RECIPE_VF_MODULE_CREATE, ImmutableList.of(TARGET_VFC)) .build(); - + + // Static variables required to hold the IDs of the last service item and VNF item. Note that in a multithreaded deployment this WILL break + private static String lastVNFItemVnfId; + private static String lastServiceItemServiceInstanceId; + @Override public String actor() { - return "SO"; + return SO_ACTOR; } @Override @@ -126,572 +97,255 @@ public class SOActorServiceProvider implements Actor { public List<String> recipePayloads(String recipe) { return Collections.emptyList(); } - - /** - * SOActorServiceProvider Constructor - * - */ - public SOActorServiceProvider() { - - } - - /** - * Constructs and sends an AAI vserver Named Query - * - * @param onset - * @returns the response to the AAI Named Query - */ - private AAINQResponseWrapper AaiNamedQueryRequest(VirtualControlLoopEvent onset) { - - // create AAI named-query request with UUID started with "" - AAINQRequest aainqrequest = new AAINQRequest(); - AAINQQueryParameters aainqqueryparam = new AAINQQueryParameters(); - AAINQNamedQuery aainqnamedquery = new AAINQNamedQuery(); - AAINQInstanceFilters aainqinstancefilter = new AAINQInstanceFilters(); - - // queryParameters - aainqnamedquery.setNamedQueryUUID(UUID.fromString("4ff56a54-9e3f-46b7-a337-07a1d3c6b469")); // UUID.fromString($params.getAaiNamedQueryUUID()) TO DO: AaiNamedQueryUUID - aainqqueryparam.setNamedQuery(aainqnamedquery); - aainqrequest.setQueryParameters(aainqqueryparam); - // - // instanceFilters - // - Map<String, Map<String, String>> aainqinstancefiltermap = new HashMap<>(); - Map<String, String> aainqinstancefiltermapitem = new HashMap<>(); - aainqinstancefiltermapitem.put("vserver-name", onset.getAAI().get("vserver.vserver-name")); // TO DO: get vserver.vname from dcae onset.AAI.get("vserver.vserver-name") - aainqinstancefiltermap.put("vserver", aainqinstancefiltermapitem); - aainqinstancefilter.getInstanceFilter().add(aainqinstancefiltermap); - aainqrequest.setInstanceFilters(aainqinstancefilter); - // - // print aainqrequest for debug - // - logger.debug("AAI Request sent:"); - logger.debug(Serialization.gsonPretty.toJson(aainqrequest)); - // - // Create AAINQRequestWrapper - // -// AAINQRequestWrapper aainqRequestWrapper = new AAINQRequestWrapper(onset.requestID, aainqrequest); - // - // insert aainqrequest into memory - // -// insert(aainqRequestWrapper); - - /* - * Obtain A&AI credentials from properties.environment file - * TODO: What if these are null? - */ - String aaiUrl = PolicyEngine.manager.getEnvironmentProperty("aai.url"); - String aaiUsername = PolicyEngine.manager.getEnvironmentProperty("aai.username"); - String aaiPassword = PolicyEngine.manager.getEnvironmentProperty("aai.password"); - - //***** send the request *****\\ - AAINQResponse aainqresponse = new AAIManager(new RESTManager()).postQuery(aaiUrl, aaiUsername, aaiPassword, - aainqrequest, onset.getRequestID()); - - // Check AAI response - if (aainqresponse == null) { - System.err.println("Failed to get AAI response"); - - // Fail and retract everything - return null; - } else { - // Create AAINQResponseWrapper - AAINQResponseWrapper aainqResponseWrapper = new AAINQResponseWrapper(onset.getRequestID(), aainqresponse); - - // insert aainqResponseWrapper to memory -- Is this needed? -// insert(aainqResponseWrapper); - - if (logger.isDebugEnabled()) { - logger.debug("AAI Named Query Response: "); - logger.debug(Serialization.gsonPretty.toJson(aainqResponseWrapper.getAainqresponse())); - } - - extractSOFieldsFromNamedQuery(aainqResponseWrapper, onset); - return aainqResponseWrapper; - } - } - /** - * Extract the required fields from the named query response - * @param namedQueryResponseWrapper - * @param onset - */ - private void extractSOFieldsFromNamedQuery(AAINQResponseWrapper namedQueryResponseWrapper, VirtualControlLoopEvent onset) { - - try { - // vnfItem - setVnfItemVnfId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getGenericVNF().getVnfID()); - setVnfItemVnfType(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getGenericVNF().getVnfType()); - setVnfItemVnfType(vnfItemVnfType.substring(vnfItemVnfType.lastIndexOf("/")+1)); - setVnfItemModelInvariantId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getGenericVNF().getModelInvariantId()); - setVnfItemModelVersionId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getGenericVNF().getModelVersionId()); - setVnfItemModelName(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getExtraProperties().getExtraProperty().get(1).getPropertyValue()); - setVnfItemModelNameVersionId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getExtraProperties().getExtraProperty().get(0).getPropertyValue()); - setVnfItemModelVersion(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getExtraProperties().getExtraProperty().get(4).getPropertyValue()); - - // serviceItem - setServiceItemServiceInstanceId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getServiceInstance().getServiceInstanceID()); - setServiceItemModelInvariantId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getServiceInstance().getModelInvariantId()); - setServiceItemModelName(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getExtraProperties().getExtraProperty().get(1).getPropertyValue()); - setServiceItemModelType(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getExtraProperties().getExtraProperty().get(1).getPropertyValue()); - setServiceItemModelNameVersionId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getServiceInstance().getModelVersionId()); - setServiceItemModelVersion(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getExtraProperties().getExtraProperty().get(4).getPropertyValue()); - - // Find the index for base vf module and non-base vf module - int baseIndex = -1; - int nonBaseIndex = -1; - List<AAINQInventoryResponseItem> inventoryItems = namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems(); - for (AAINQInventoryResponseItem m : inventoryItems) { - if (m.getVfModule() != null) { - if (m.getVfModule().getIsBaseVfModule()) { - baseIndex = inventoryItems.indexOf(m); - } else if (m.getVfModule().getIsBaseVfModule() == false) { - nonBaseIndex = inventoryItems.indexOf(m); - } - } - // - if (baseIndex != -1 && nonBaseIndex != -1) { - break; - } - } - - // Report the error if either base vf module or non-base vf module is not found - if (baseIndex == -1 || nonBaseIndex == -1) { - logger.error("Either base or non-base vf module is not found from AAI response."); - return; - } - - // This comes from the base module - setVfModuleItemVfModuleName(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(baseIndex).getVfModule().getVfModuleName()); - setVfModuleItemVfModuleName(vfModuleItemVfModuleName.replace("Vfmodule", "vDNS")); - - // vfModuleItem - NOT the base module - setVfModuleItemModelInvariantId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(nonBaseIndex).getVfModule().getModelInvariantId()); - setVfModuleItemModelNameVersionId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(nonBaseIndex).getVfModule().getModelVersionId()); - setVfModuleItemModelName(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(nonBaseIndex).getExtraProperties().getExtraProperty().get(1).getPropertyValue()); - setVfModuleItemModelVersionId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(nonBaseIndex).getExtraProperties().getExtraProperty().get(4).getPropertyValue()); - - // tenantItem - setTenantItemTenantId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(1).getTenant().getTenantId()); - - // cloudRegionItem - setCloudRegionItemCloudRegionId(namedQueryResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(1).getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId()); - - } catch (Exception e) { - logger.warn("Problem extracting SO data from AAI query response because of {}", e.getMessage(), e); - return; - } - } - - /** - * Checks whether extracted fields from AAI Named Query are null or not - * @return false if some extracted fields are missing, true otherwise - */ - private boolean checkExtractedFields() { - - if ((getVnfItemVnfId() == null) || (getVnfItemVnfType() == null) || - (getVnfItemModelInvariantId() == null) || (getVnfItemModelName() == null) || - (getVnfItemModelVersion() == null) || (getVnfItemModelNameVersionId() == null) || - (getServiceItemServiceInstanceId() == null) || (getServiceItemModelName() == null) || - (getServiceItemModelType() == null) || (getServiceItemModelVersion() == null) || - (getServiceItemModelNameVersionId() == null) || (getVfModuleItemVfModuleName() == null) || - (getVfModuleItemModelInvariantId() == null) || (getVfModuleItemModelVersionId() == null) || - (getVfModuleItemModelName() == null) || (getVfModuleItemModelNameVersionId() == null) || - (getTenantItemTenantId() == null) || (getCloudRegionItemCloudRegionId() == null)) { - return false; - } - return true; - } - - /** - * Construct SO Request + * Constructs a SO request conforming to the lcm API. + * The actual request is constructed and then placed in a + * wrapper object used to send through DMAAP. * * @param onset + * the event that is reporting the alert for policy + * to perform an action * @param operation + * the control loop operation specifying the actor, + * operation, target, etc. * @param policy - * @return SORequest - * @throws IllegalAccessException + * the policy the was specified from the yaml generated + * by CLAMP or through the Policy GUI/API + * @return a SO request conforming to the lcm API using the DMAAP wrapper */ public SORequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) { - - if ("SO".equals(policy.getActor()) && "VF Module Create".equals(policy.getRecipe())) { - // perform named query request and handle response - AaiNamedQueryRequest(onset); - } else { + if (!SO_ACTOR.equals(policy.getActor()) || !RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) { // for future extension return null; - }; - - // check if the fields extracted from named query response are - // not null so we can proceed with SO request - if (!checkExtractedFields()) { - logger.warn("AAI response is missing some required fields. Cannot proceed with SO Request construction."); - return null; - - } else { - - // Construct SO Request - SORequest request = new SORequest(); -// request.requestId = onset.requestID; - request.setRequestDetails(new SORequestDetails()); - request.getRequestDetails().setModelInfo(new SOModelInfo()); - request.getRequestDetails().setCloudConfiguration(new SOCloudConfiguration()); - request.getRequestDetails().setRequestInfo(new SORequestInfo()); - request.getRequestDetails().setRequestParameters(new SORequestParameters()); - request.getRequestDetails().getRequestParameters().setUserParams(null); - // - // cloudConfiguration - // - request.getRequestDetails().getCloudConfiguration().setLcpCloudRegionId(getCloudRegionItemCloudRegionId()); - request.getRequestDetails().getCloudConfiguration().setTenantId(getTenantItemTenantId()); - // - // modelInfo - // - request.getRequestDetails().getModelInfo().setModelType("vfModule"); - request.getRequestDetails().getModelInfo().setModelInvariantId(getVfModuleItemModelInvariantId()); - request.getRequestDetails().getModelInfo().setModelVersionId(getVfModuleItemModelNameVersionId()); - request.getRequestDetails().getModelInfo().setModelName(getVfModuleItemModelName()); - request.getRequestDetails().getModelInfo().setModelVersion(getVfModuleItemModelVersionId()); - // - // requestInfo - // - request.getRequestDetails().getRequestInfo().setInstanceName(getVfModuleItemVfModuleName()); - request.getRequestDetails().getRequestInfo().setSource("POLICY"); - request.getRequestDetails().getRequestInfo().setSuppressRollback(false); - request.getRequestDetails().getRequestInfo().setRequestorId("policy"); - // - // relatedInstanceList - // - SORelatedInstanceListElement relatedInstanceListElement1 = new SORelatedInstanceListElement(); - SORelatedInstanceListElement relatedInstanceListElement2 = new SORelatedInstanceListElement(); - relatedInstanceListElement1.setRelatedInstance(new SORelatedInstance()); - relatedInstanceListElement2.setRelatedInstance(new SORelatedInstance()); - // - relatedInstanceListElement1.getRelatedInstance().setInstanceId(getServiceItemServiceInstanceId()); - relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SOModelInfo()); - relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service"); - relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelInvariantId(getServiceItemModelInvariantId()); - relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersionId(getServiceItemModelNameVersionId()); - relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(getServiceItemModelName()); - relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(getServiceItemModelVersion()); - // - relatedInstanceListElement2.getRelatedInstance().setInstanceId(getVnfItemVnfId()); - relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SOModelInfo()); - relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf"); - relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelInvariantId(getVnfItemModelInvariantId()); - relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(getVnfItemModelNameVersionId()); - relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(getVnfItemModelName()); - relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(getVnfItemModelVersion()); - relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelCustomizationName(getVnfItemVnfType()); - // - request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1); - request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2); - // - // print SO request for debug - // - logger.debug("SO request sent:"); - logger.debug(Serialization.gsonPretty.toJson(request)); - - return request; } - } - - /** - * This method is needed to get the serviceInstanceId and vnfInstanceId which is used - * in the asyncSORestCall - * - * @param wm - * @param request - */ - public static void sendRequest(String requestID, WorkingMemory wm, Object request) { - SOManager Mm = new SOManager(); - Mm.asyncSORestCall(requestID, wm, getServiceItemServiceInstanceId(), getVnfItemVnfId(), (SORequest)request); - } - - /** - * @return the vnfItemVnfId - */ - public static String getVnfItemVnfId() { - return vnfItemVnfId; - } - - /** - * @param vnfItemVnfId the vnfItemVnfId to set - */ - private static void setVnfItemVnfId(String vnfItemVnfId) { - SOActorServiceProvider.vnfItemVnfId = vnfItemVnfId; - } - - /** - * @return the vnfItemVnfType - */ - public String getVnfItemVnfType() { - return this.vnfItemVnfType; - } - /** - * @param vnfItemVnfType the vnfItemVnfType to set - */ - private void setVnfItemVnfType(String vnfItemVnfType) { - this.vnfItemVnfType = vnfItemVnfType; - } - - /** - * @return the vnfItemModelInvariantId - */ - public String getVnfItemModelInvariantId() { - return this.vnfItemModelInvariantId; - } - - /** - * @param vnfItemModelInvariantId the vnfItemModelInvariantId to set - */ - private void setVnfItemModelInvariantId(String vnfItemModelInvariantId) { - this.vnfItemModelInvariantId = vnfItemModelInvariantId; - } - - /** - * @return the vnfItemModelVersionId - */ - public String getVnfItemModelVersionId() { - return this.vnfItemModelVersionId; - } - - /** - * @param vnfItemModelVersionId the vnfItemModelVersionId to set - */ - private void setVnfItemModelVersionId(String vnfItemModelVersionId) { - this.vnfItemModelVersionId = vnfItemModelVersionId; - } - - /** - * @return the vnfItemModelName - */ - public String getVnfItemModelName() { - return this.vnfItemModelName; - } - - /** - * @param vnfItemModelName the vnfItemModelName to set - */ - private void setVnfItemModelName(String vnfItemModelName) { - this.vnfItemModelName = vnfItemModelName; - } - - /** - * @return the vnfItemModelVersion - */ - public String getVnfItemModelVersion() { - return this.vnfItemModelVersion; - } - - /** - * @param vnfItemModelVersion the vnfItemModelVersion to set - */ - private void setVnfItemModelVersion(String vnfItemModelVersion) { - this.vnfItemModelVersion = vnfItemModelVersion; - } - - /** - * @return the vnfItemModelNameVersionId - */ - public String getVnfItemModelNameVersionId() { - return this.vnfItemModelNameVersionId; - } + // Perform named query request and handle response + AAINQResponseWrapper aaiResponseWrapper = performAaiNamedQueryRequest(onset); + if (aaiResponseWrapper == null) { + // Tracing and error handling handied in the "performAaiNamedQueryRequest()" method + return null; + } - /** - * @param vnfItemModelNameVersionId the vnfItemModelNameVersionId to set - */ - private void setVnfItemModelNameVersionId(String vnfItemModelNameVersionId) { - this.vnfItemModelNameVersionId = vnfItemModelNameVersionId; - } + AAINQInventoryResponseItem vnfItem = null; + AAINQInventoryResponseItem vnfServiceItem = null; + AAINQInventoryResponseItem tenantItem = null; - /** - * @return the serviceItemServiceInstanceId - */ - public static String getServiceItemServiceInstanceId() { - return serviceItemServiceInstanceId; - } + // Extract the items we're interested in from the response + try { + vnfItem = aaiResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0); + } + catch (Exception e) { + logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiResponseWrapper)); + return null; + } - /** - * @param serviceItemServiceInstanceId the serviceItemServiceInstanceId to set - */ - private static void setServiceItemServiceInstanceId( - String serviceItemServiceInstanceId) { - SOActorServiceProvider.serviceItemServiceInstanceId = serviceItemServiceInstanceId; - } + try { + vnfServiceItem = vnfItem.getItems().getInventoryResponseItems().get(0); + } + catch (Exception e) { + logger.error("VNF Service Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiResponseWrapper)); + return null; + } - /** - * @return the serviceItemModelInvariantId - */ - public String getServiceItemModelInvariantId() { - return this.serviceItemModelInvariantId; - } + try { + tenantItem = aaiResponseWrapper.getAainqresponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(1); + } + catch (Exception e) { + logger.error("Tenant Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiResponseWrapper)); + return null; + } - /** - * @param serviceItemModelInvariantId the serviceItemModelInvariantId to set - */ - private void setServiceItemModelInvariantId(String serviceItemModelInvariantId) { - this.serviceItemModelInvariantId = serviceItemModelInvariantId; - } + // Find the index for base vf module and non-base vf module + int baseIndex = findIndex(vnfItem.getItems().getInventoryResponseItems(), true); + int nonBaseIndex = findIndex(vnfItem.getItems().getInventoryResponseItems(), false); - /** - * @return the serviceItemModelName - */ - public String getServiceItemModelName() { - return this.serviceItemModelName; - } + // Report the error if either base vf module or non-base vf module is not found + if (baseIndex == -1 || nonBaseIndex == -1) { + logger.error("Either base or non-base vf module is not found from AAI response."); + return null; + } - /** - * @param serviceItemModelName the serviceItemModelName to set - */ - private void setServiceItemModelName(String serviceItemModelName) { - this.serviceItemModelName = serviceItemModelName; - } + // Construct SO Request + SORequest request = new SORequest(); + request.setRequestId(onset.getRequestID()); + request.setRequestDetails(new SORequestDetails()); + request.getRequestDetails().setModelInfo(new SOModelInfo()); + request.getRequestDetails().setCloudConfiguration(new SOCloudConfiguration()); + request.getRequestDetails().setRequestInfo(new SORequestInfo()); + request.getRequestDetails().setRequestParameters(new SORequestParameters()); + request.getRequestDetails().getRequestParameters().setUserParams(null); - /** - * @return the serviceItemModelType - */ - public String getServiceItemModelType() { - return this.serviceItemModelType; - } + // + // cloudConfiguration + // + request.getRequestDetails().getCloudConfiguration().setTenantId(tenantItem.getTenant().getTenantId()); + request.getRequestDetails().getCloudConfiguration().setLcpCloudRegionId(tenantItem.getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId()); - /** - * @param serviceItemModelType the serviceItemModelType to set - */ - private void setServiceItemModelType(String serviceItemModelType) { - this.serviceItemModelType = serviceItemModelType; - } + // + // modelInfo + // + AAINQInventoryResponseItem vfModuleItem = vnfItem.getItems().getInventoryResponseItems().get(nonBaseIndex); - /** - * @return the serviceItemModelVersion - */ - public String getServiceItemModelVersion() { - return this.serviceItemModelVersion; - } + request.getRequestDetails().getModelInfo().setModelType("vfModule"); + request.getRequestDetails().getModelInfo().setModelInvariantId(vfModuleItem.getVfModule().getModelInvariantId()); + request.getRequestDetails().getModelInfo().setModelVersionId(vfModuleItem.getVfModule().getModelVersionId()); + request.getRequestDetails().getModelInfo().setModelName(vfModuleItem.getExtraProperties().getExtraProperty().get(1).getPropertyValue()); + request.getRequestDetails().getModelInfo().setModelVersion(vfModuleItem.getExtraProperties().getExtraProperty().get(4).getPropertyValue()); - /** - * @param serviceItemModelVersion the serviceItemModelVersion to set - */ - private void setServiceItemModelVersion(String serviceItemModelVersion) { - this.serviceItemModelVersion = serviceItemModelVersion; - } + // + // requestInfo + // + request.getRequestDetails().getRequestInfo().setInstanceName(vnfItem.getItems().getInventoryResponseItems().get(baseIndex).getVfModule().getVfModuleName().replace("Vfmodule", "vDNS")); + request.getRequestDetails().getRequestInfo().setSource("POLICY"); + request.getRequestDetails().getRequestInfo().setSuppressRollback(false); + request.getRequestDetails().getRequestInfo().setRequestorId("policy"); - /** - * @return the serviceItemModelNameVersionId - */ - public String getServiceItemModelNameVersionId() { - return this.serviceItemModelNameVersionId; - } + // + // relatedInstanceList + // + SORelatedInstanceListElement relatedInstanceListElement1 = new SORelatedInstanceListElement(); + SORelatedInstanceListElement relatedInstanceListElement2 = new SORelatedInstanceListElement(); + relatedInstanceListElement1.setRelatedInstance(new SORelatedInstance()); + relatedInstanceListElement2.setRelatedInstance(new SORelatedInstance()); + + // Service Item + relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstance().getServiceInstanceID()); + relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SOModelInfo()); + relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service"); + relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelInvariantId(vnfServiceItem.getServiceInstance().getModelInvariantId()); + relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersionId(vnfServiceItem.getServiceInstance().getModelVersionId()); + relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(vnfServiceItem.getExtraProperties().getExtraProperty().get(1).getPropertyValue()); + relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(vnfServiceItem.getExtraProperties().getExtraProperty().get(4).getPropertyValue()); + + // VNF Item + relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getGenericVNF().getVnfID()); + relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SOModelInfo()); + relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf"); + relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelInvariantId(vnfItem.getGenericVNF().getModelInvariantId()); + relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getExtraProperties().getExtraProperty().get(0).getPropertyValue()); + relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(vnfItem.getExtraProperties().getExtraProperty().get(1).getPropertyValue()); + relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(vnfItem.getExtraProperties().getExtraProperty().get(4).getPropertyValue()); + relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelCustomizationName(vnfItem.getGenericVNF().getVnfType().substring(vnfItem.getGenericVNF().getVnfType().lastIndexOf('/') + 1)); + + // Insert the Service Item and VNF Item + request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1); + request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2); + + // Save the instance IDs for the VNF and service to static fields + preserveInstanceIDs(vnfItem.getGenericVNF().getVnfID(), vnfServiceItem.getServiceInstance().getServiceInstanceID()); + + if (logger.isDebugEnabled()) { + logger.debug("SO request sent: {}", Serialization.gsonPretty.toJson(request)); + } - /** - * @param serviceItemModelNameVersionId the serviceItemModelNameVersionId to set - */ - private void setServiceItemModelNameVersionId( - String serviceItemModelNameVersionId) { - this.serviceItemModelNameVersionId = serviceItemModelNameVersionId; + return request; } /** - * @return the vfModuleItemVfModuleName + * This method is needed to get the serviceInstanceId and vnfInstanceId which is used + * in the asyncSORestCall + * + * @param wm + * @param request */ - public String getVfModuleItemVfModuleName() { - return this.vfModuleItemVfModuleName; + public static void sendRequest(String requestID, WorkingMemory wm, Object request) { + SOManager soManager = new SOManager(); + soManager.asyncSORestCall(requestID, wm, lastServiceItemServiceInstanceId, lastVNFItemVnfId, (SORequest)request); } /** - * @param vfModuleItemVfModuleName the vfModuleItemVfModuleName to set + * Constructs and sends an AAI vserver Named Query + * + * @param onset + * @returns the response to the AAI Named Query */ - private void setVfModuleItemVfModuleName(String vfModuleItemVfModuleName) { - this.vfModuleItemVfModuleName = vfModuleItemVfModuleName; - } + private AAINQResponseWrapper performAaiNamedQueryRequest(VirtualControlLoopEvent onset) { - /** - * @return the vfModuleItemModelInvariantId - */ - public String getVfModuleItemModelInvariantId() { - return this.vfModuleItemModelInvariantId; - } + // create AAI named-query request with UUID started with "" + AAINQRequest aainqrequest = new AAINQRequest(); + AAINQQueryParameters aainqqueryparam = new AAINQQueryParameters(); + AAINQNamedQuery aainqnamedquery = new AAINQNamedQuery(); + AAINQInstanceFilters aainqinstancefilter = new AAINQInstanceFilters(); - /** - * @param vfModuleItemModelInvariantId the vfModuleItemModelInvariantId to set - */ - private void setVfModuleItemModelInvariantId(String vfModuleItemModelInvariantId) { - this.vfModuleItemModelInvariantId = vfModuleItemModelInvariantId; - } + // queryParameters + aainqnamedquery.setNamedQueryUUID(UUID.fromString("4ff56a54-9e3f-46b7-a337-07a1d3c6b469")); // UUID.fromString($params.getAaiNamedQueryUUID()) TO DO: AaiNamedQueryUUID + aainqqueryparam.setNamedQuery(aainqnamedquery); + aainqrequest.setQueryParameters(aainqqueryparam); + // + // instanceFilters + // + Map<String, Map<String, String>> aainqinstancefiltermap = new HashMap<>(); + Map<String, String> aainqinstancefiltermapitem = new HashMap<>(); + aainqinstancefiltermapitem.put("vserver-name", onset.getAAI().get("vserver.vserver-name")); // TO DO: get vserver.vname from dcae onset.AAI.get("vserver.vserver-name") + aainqinstancefiltermap.put("vserver", aainqinstancefiltermapitem); + aainqinstancefilter.getInstanceFilter().add(aainqinstancefiltermap); + aainqrequest.setInstanceFilters(aainqinstancefilter); - /** - * @return the vfModuleItemModelVersionId - */ - public String getVfModuleItemModelVersionId() { - return this.vfModuleItemModelVersionId; - } + if (logger.isDebugEnabled()) { + logger.debug("AAI Request sent: {}", Serialization.gsonPretty.toJson(aainqrequest)); + } - /** - * @param vfModuleItemModelVersionId the vfModuleItemModelVersionId to set - */ - private void setVfModuleItemModelVersionId( - String vfModuleItemModelVersionId) { - this.vfModuleItemModelVersionId = vfModuleItemModelVersionId; - } + AAINQResponse aainqresponse = new AAIManager(new RESTManager()).postQuery( + getPEManagerEnvProperty("aai.url"), + getPEManagerEnvProperty("aai.username"), + getPEManagerEnvProperty("aai.password"), + aainqrequest, onset.getRequestID()); - /** - * @return the vfModuleItemModelName - */ - public String getVfModuleItemModelName() { - return this.vfModuleItemModelName; - } + // Check AAI response + if (aainqresponse == null) { + logger.warn("No response received from AAI for request {}", aainqrequest); + return null; + } - /** - * @param vfModuleItemModelName the vfModuleItemModelName to set - */ - private void setVfModuleItemModelName(String vfModuleItemModelName) { - this.vfModuleItemModelName = vfModuleItemModelName; - } + // Create AAINQResponseWrapper + AAINQResponseWrapper aainqResponseWrapper = new AAINQResponseWrapper(onset.getRequestID(), aainqresponse); - /** - * @return the vfModuleItemModelNameVersionId - */ - public String getVfModuleItemModelNameVersionId() { - return this.vfModuleItemModelNameVersionId; - } + if (logger.isDebugEnabled()) { + logger.debug("AAI Named Query Response: "); + logger.debug(Serialization.gsonPretty.toJson(aainqResponseWrapper.getAainqresponse())); + } - /** - * @param vfModuleItemModelNameVersionId the vfModuleItemModelNameVersionId to set - */ - private void setVfModuleItemModelNameVersionId( - String vfModuleItemModelNameVersionId) { - this.vfModuleItemModelNameVersionId = vfModuleItemModelNameVersionId; + return aainqResponseWrapper; } /** - * @return the tenantItemTenantId + * Find the base index or non base index in a list of inventory response items + * @param inventoryResponseItems + * @param baseIndexFlag true if we are searching for the base index, false if we are searching for hte non base index + * @return the base or non base index or -1 if the index was not found */ - public String getTenantItemTenantId() { - return this.tenantItemTenantId; - } + private int findIndex(List<AAINQInventoryResponseItem> inventoryResponseItems, boolean baseIndexFlag) { + for (AAINQInventoryResponseItem invenoryResponseItem : inventoryResponseItems) { + if (invenoryResponseItem.getVfModule() != null && baseIndexFlag == invenoryResponseItem.getVfModule().getIsBaseVfModule()) { + return inventoryResponseItems.indexOf(invenoryResponseItem); + } + } - /** - * @param tenantItemTenantId the tenantItemTenantId to set - */ - private void setTenantItemTenantId(String tenantItemTenantId) { - this.tenantItemTenantId = tenantItemTenantId; + return -1; } /** - * @return the cloudRegionItemCloudRegionId + * This method is called to remember the last service instance ID and VNF Item VNF ID. Note these fields are static, beware for multithreaded deployments + * @param vnfInstanceID update the last VNF instance ID to this value + * @param serviceInstanceID update the last service instance ID to this value */ - public String getCloudRegionItemCloudRegionId() { - return this.cloudRegionItemCloudRegionId; + private static void preserveInstanceIDs(final String vnfInstanceID, final String serviceInstanceID) { + lastVNFItemVnfId = vnfInstanceID; + lastServiceItemServiceInstanceId = serviceInstanceID; } /** - * @param cloudRegionItemCloudRegionId the cloudRegionItemCloudRegionId to set + * This method reads and validates environmental properties coming from the policy engine. Null properties cause + * an {@link IllegalArgumentException} runtime exception to be thrown + * @param string the name of the parameter to retrieve + * @return the property value */ - private void setCloudRegionItemCloudRegionId( - String cloudRegionItemCloudRegionId) { - this.cloudRegionItemCloudRegionId = cloudRegionItemCloudRegionId; + private static String getPEManagerEnvProperty(String enginePropertyName) { + String enginePropertyValue = PolicyEngine.manager.getEnvironmentProperty(enginePropertyName); + if (enginePropertyValue == null) { + throw new IllegalArgumentException("The value of policy engine manager environment property \"" + enginePropertyName + "\" may not be null"); + } + return enginePropertyValue; } - } diff --git a/controlloop/common/actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/TestSOActorServiceProvider.java b/controlloop/common/actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/TestSOActorServiceProvider.java new file mode 100644 index 000000000..5d2134ec1 --- /dev/null +++ b/controlloop/common/actors/actor.so/src/test/java/org/onap/policy/controlloop/actor/so/TestSOActorServiceProvider.java @@ -0,0 +1,108 @@ +/*- + * ============LICENSE_START======================================================= + * TestSOActorServiceProvider + * ================================================================================ + * Copyright (C) 2018 Ericsson. 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.controlloop.actor.so; + +import static org.junit.Assert.*; + +import java.util.UUID; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.controlloop.ControlLoopOperation; +import org.onap.policy.controlloop.VirtualControlLoopEvent; +import org.onap.policy.controlloop.policy.Policy; +import org.onap.policy.drools.http.server.HttpServletServer; +import org.onap.policy.drools.system.PolicyEngine; +import org.onap.policy.simulators.Util; +import org.onap.policy.so.SORequest; + +public class TestSOActorServiceProvider { + @BeforeClass + public static void setUpSimulator() { + try { + Util.buildAaiSim(); + } catch (Exception e) { + fail(e.getMessage()); + } + } + + @AfterClass + public static void tearDownSimulator() { + HttpServletServer.factory.destroy(); + } + + @Test + public void testConstructRequest() { + VirtualControlLoopEvent onset = new VirtualControlLoopEvent(); + ControlLoopOperation operation = new ControlLoopOperation(); + + UUID requestID = UUID.randomUUID(); + onset.setRequestID(requestID); + + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI"); + PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI"); + + Policy policy = new Policy(); + policy.setActor("Dorothy"); + policy.setRecipe("GoToOz"); + assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy)); + + policy.setActor("SO"); + assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy)); + + policy.setRecipe("VF Module Create"); + assertNotNull(new SOActorServiceProvider().constructRequest(onset, operation, policy)); + + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:999999"); + assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy)); + + PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666"); + assertNotNull(new SOActorServiceProvider().constructRequest(onset, operation, policy)); + + SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy); + + assertEquals(requestID, request.getRequestId()); + assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId()); + assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId()); + } + + @Test + public void testSendRequest() { + try { + SOActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null); + } + catch (Exception e) { + fail("Test should not throw an exception"); + } + } + + @Test + public void testMethods() { + SOActorServiceProvider sp = new SOActorServiceProvider(); + + assertEquals("SO", sp.actor()); + assertEquals(1, sp.recipes().size()); + assertEquals("VF Module Create", sp.recipes().get(0)); + assertEquals(0, sp.recipePayloads("VF Module Create").size()); + } +} |