diff options
author | Jim Hahn <jrh3@att.com> | 2018-08-27 15:32:14 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2018-08-28 10:56:27 -0400 |
commit | f43b3e32fdf4cdb3361c584491730af18ef46baf (patch) | |
tree | d3e6434d66c3e5e8d6371581d64039b79c6ad584 /controlloop/common/actors | |
parent | 57ac9eac8ac0f8236ddb52e5226f475368d4de33 (diff) |
use new SO API for scale-out
Updated licenses and serialization IDs.
Moved hashCode() and equals() methods to be in same location as in the
original code so it can be more easily compared.
Changed vDNS yaml timeout and retry to 1200 and 0, respectively, to match
the original values.
Removed extra newline from yaml, to match original yaml.
Reformatted source to wrap lines at 120 characters and removing trailing
white space.
Change-Id: I3f5e59a1f44750347de49164f587d4ffefdee8b4
Issue-ID: POLICY-962
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'controlloop/common/actors')
2 files changed, 178 insertions, 37 deletions
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 f68c6f676..ccc13a557 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 @@ -20,8 +20,11 @@ package org.onap.policy.controlloop.actor.so; +import java.lang.reflect.Type; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.UUID; import org.drools.core.WorkingMemory; import org.onap.policy.aai.AaiNqExtraProperty; import org.onap.policy.aai.AaiNqInventoryResponseItem; @@ -44,6 +47,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.gson.reflect.TypeToken; public class SOActorServiceProvider implements Actor { private static final Logger logger = LoggerFactory.getLogger(SOActorServiceProvider.class); @@ -59,9 +63,19 @@ public class SOActorServiceProvider implements Actor { 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(RECIPE_VF_MODULE_CREATE, ImmutableList.of(TARGET_VFC)).build(); + .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 + // name of request parameters within policy payload + public static final String REQ_PARAM_NM = "requestParameters"; + + // name of configuration parameters within policy payload + public static final String CONFIG_PARAM_NM = "configurationParameters"; + + // used to decode configuration parameters via gson + public static Type CONFIG_TYPE = new TypeToken<List<Map<String, String>>>() {}.getType(); + + // 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; @@ -87,17 +101,19 @@ public class SOActorServiceProvider implements Actor { } /** - * 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. + * 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 the policy the was specified from the yaml generated by CLAMP or through the - * Policy GUI/API + * @param operation the control loop operation specifying the actor, operation, + * target, etc. + * @param policy the policy the was specified from the yaml generated by CLAMP or + * through the Policy GUI/API * @param aaiResponseWrapper wrapper for AAI vserver named-query response * @return a SO request conforming to the lcm API using the DMAAP wrapper */ - public SORequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy, AaiNqResponseWrapper aaiResponseWrapper) { + public SORequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy, + AaiNqResponseWrapper aaiResponseWrapper) { String modelNamePropertyKey = "model-ver.model-name"; String modelVersionPropertyKey = "model-ver.model-version"; String modelVersionIdPropertyKey = "model-ver.model-version-id"; @@ -120,10 +136,10 @@ public class SOActorServiceProvider implements Actor { // Extract the items we're interested in from the response try { vnfItem = aaiResponseWrapper.getAaiNqResponse().getInventoryResponseItems().get(0).getItems() - .getInventoryResponseItems().get(0); + .getInventoryResponseItems().get(0); } catch (Exception e) { logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiResponseWrapper), - e); + e); return null; } @@ -131,16 +147,16 @@ public class SOActorServiceProvider implements Actor { vnfServiceItem = vnfItem.getItems().getInventoryResponseItems().get(0); } catch (Exception e) { logger.error("VNF Service Item not found in AAI response {}", - Serialization.gsonPretty.toJson(aaiResponseWrapper), e); + Serialization.gsonPretty.toJson(aaiResponseWrapper), e); return null; } try { tenantItem = aaiResponseWrapper.getAaiNqResponse().getInventoryResponseItems().get(0).getItems() - .getInventoryResponseItems().get(1); + .getInventoryResponseItems().get(1); } catch (Exception e) { logger.error("Tenant Item not found in AAI response {}", - Serialization.gsonPretty.toJson(aaiResponseWrapper), e); + Serialization.gsonPretty.toJson(aaiResponseWrapper), e); return null; } @@ -172,15 +188,17 @@ public class SOActorServiceProvider implements Actor { // request.getRequestDetails().getCloudConfiguration().setTenantId(tenantItem.getTenant().getTenantId()); request.getRequestDetails().getCloudConfiguration().setLcpCloudRegionId( - tenantItem.getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId()); + tenantItem.getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId()); // // modelInfo // request.getRequestDetails().getModelInfo().setModelType("vfModule"); request.getRequestDetails().getModelInfo() - .setModelInvariantId(vfModuleItem.getVfModule().getModelInvariantId()); + .setModelInvariantId(vfModuleItem.getVfModule().getModelInvariantId()); request.getRequestDetails().getModelInfo().setModelVersionId(vfModuleItem.getVfModule().getModelVersionId()); + request.getRequestDetails().getModelInfo() + .setModelCustomizationId(vfModuleItem.getVfModule().getModelCustomizationId()); for (AaiNqExtraProperty prop : vfModuleItem.getExtraProperties().getExtraProperty()) { if (prop.getPropertyName().equals(modelNamePropertyKey)) { @@ -193,7 +211,8 @@ public class SOActorServiceProvider implements Actor { // // requestInfo // - request.getRequestDetails().getRequestInfo().setInstanceName(aaiResponseWrapper.genVfModuleName()); + String vfModuleName = aaiResponseWrapper.genVfModuleName(); + request.getRequestDetails().getRequestInfo().setInstanceName(vfModuleName); request.getRequestDetails().getRequestInfo().setSource("POLICY"); request.getRequestDetails().getRequestInfo().setSuppressRollback(false); request.getRequestDetails().getRequestInfo().setRequestorId("policy"); @@ -201,27 +220,35 @@ public class SOActorServiceProvider implements Actor { // // relatedInstanceList // + SORelatedInstanceListElement relatedInstanceListElement0 = new SORelatedInstanceListElement(); SORelatedInstanceListElement relatedInstanceListElement1 = new SORelatedInstanceListElement(); SORelatedInstanceListElement relatedInstanceListElement2 = new SORelatedInstanceListElement(); relatedInstanceListElement1.setRelatedInstance(new SORelatedInstance()); relatedInstanceListElement2.setRelatedInstance(new SORelatedInstance()); + // Volume Group Item + relatedInstanceListElement0.setRelatedInstance(new SORelatedInstance()); + relatedInstanceListElement0.getRelatedInstance().setInstanceId(UUID.randomUUID().toString()); + relatedInstanceListElement0.getRelatedInstance().setInstanceName(vfModuleName + "_vol"); + relatedInstanceListElement0.getRelatedInstance().setModelInfo(new SOModelInfo()); + relatedInstanceListElement0.getRelatedInstance().getModelInfo().setModelType("volumeGroup"); + // Service Item relatedInstanceListElement1.getRelatedInstance() - .setInstanceId(vnfServiceItem.getServiceInstance().getServiceInstanceId()); + .setInstanceId(vnfServiceItem.getServiceInstance().getServiceInstanceId()); relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SOModelInfo()); relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service"); relatedInstanceListElement1.getRelatedInstance().getModelInfo() - .setModelInvariantId(vnfServiceItem.getServiceInstance().getModelInvariantId()); + .setModelInvariantId(vnfServiceItem.getServiceInstance().getModelInvariantId()); for (AaiNqExtraProperty prop : vnfServiceItem.getExtraProperties().getExtraProperty()) { if (prop.getPropertyName().equals(modelNamePropertyKey)) { relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelName(prop.getPropertyValue()); } else if (prop.getPropertyName().equals(modelVersionPropertyKey)) { relatedInstanceListElement1.getRelatedInstance().getModelInfo() - .setModelVersion(prop.getPropertyValue()); + .setModelVersion(prop.getPropertyValue()); } else if (prop.getPropertyName().equals(modelVersionIdPropertyKey)) { - relatedInstanceListElement1.getRelatedInstance().getModelInfo() - .setModelVersionId(prop.getPropertyValue()); + relatedInstanceListElement1.getRelatedInstance().getModelInfo() + .setModelVersionId(prop.getPropertyValue()); } } @@ -230,28 +257,38 @@ public class SOActorServiceProvider implements Actor { relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SOModelInfo()); relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf"); relatedInstanceListElement2.getRelatedInstance().getModelInfo() - .setModelInvariantId(vnfItem.getGenericVnf().getModelInvariantId()); + .setModelInvariantId(vnfItem.getGenericVnf().getModelInvariantId()); for (AaiNqExtraProperty prop : vnfItem.getExtraProperties().getExtraProperty()) { if (prop.getPropertyName().equals(modelNamePropertyKey)) { relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelName(prop.getPropertyValue()); } else if (prop.getPropertyName().equals(modelVersionPropertyKey)) { relatedInstanceListElement2.getRelatedInstance().getModelInfo() - .setModelVersion(prop.getPropertyValue()); + .setModelVersion(prop.getPropertyValue()); } else if (prop.getPropertyName().equals(modelVersionIdPropertyKey)) { relatedInstanceListElement2.getRelatedInstance().getModelInfo() - .setModelVersionId(prop.getPropertyValue()); + .setModelVersionId(prop.getPropertyValue()); } } - relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelCustomizationName(vnfItem - .getGenericVnf().getVnfType().substring(vnfItem.getGenericVnf().getVnfType().lastIndexOf('/') + 1)); + relatedInstanceListElement2.getRelatedInstance().getModelInfo() + .setModelCustomizationName(vnfItem.getGenericVnf().getVnfType() + .substring(vnfItem.getGenericVnf().getVnfType().lastIndexOf('/') + 1)); + relatedInstanceListElement2.getRelatedInstance().getModelInfo() + .setModelCustomizationId(vnfItem.getGenericVnf().getModelCustomizationId()); // Insert the Service Item and VNF Item + request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement0); request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1); request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2); + // Request Parameters + request.getRequestDetails().setRequestParameters(buildRequestParameters(policy)); + + // Configuration Parameters + request.getRequestDetails().setConfigurationParameters(buildConfigurationParameters(policy)); + // Save the instance IDs for the VNF and service to static fields preserveInstanceIds(vnfItem.getGenericVnf().getVnfId(), - vnfServiceItem.getServiceInstance().getServiceInstanceId()); + vnfServiceItem.getServiceInstance().getServiceInstanceId()); if (logger.isDebugEnabled()) { logger.debug("SO request sent: {}", Serialization.gsonPretty.toJson(request)); @@ -261,8 +298,8 @@ public class SOActorServiceProvider implements Actor { } /** - * This method is needed to get the serviceInstanceId and vnfInstanceId which is used in the - * asyncSORestCall. + * This method is needed to get the serviceInstanceId and vnfInstanceId which is used + * in the asyncSORestCall. * * @param requestId the request Id * @param wm the working memory @@ -271,7 +308,7 @@ public class SOActorServiceProvider implements Actor { public static void sendRequest(String requestId, WorkingMemory wm, Object request) { SOManager soManager = new SOManager(); soManager.asyncSORestCall(requestId, wm, lastServiceItemServiceInstanceId, lastVNFItemVnfId, - (SORequest) request); + (SORequest) request); } /** @@ -288,8 +325,47 @@ public class SOActorServiceProvider implements Actor { } /** - * 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 + * Builds the request parameters from the policy payload. + * + * @param policy the policy + * @return the request parameters, or {@code null} if the payload is {@code null} + */ + private SORequestParameters buildRequestParameters(Policy policy) { + if (policy.getPayload() == null) { + return null; + } + + String json = policy.getPayload().get(REQ_PARAM_NM); + if (json == null) { + return null; + } + + return Serialization.gsonPretty.fromJson(json, SORequestParameters.class); + } + + /** + * Builds the configuration parameters from the policy payload. + * + * @param policy the policy + * @return the configuration parameters, or {@code null} if the payload is + * {@code null} + */ + private List<Map<String, String>> buildConfigurationParameters(Policy policy) { + if (policy.getPayload() == null) { + return null; + } + + String json = policy.getPayload().get(CONFIG_PARAM_NM); + if (json == null) { + return null; + } + + return Serialization.gsonPretty.fromJson(json, CONFIG_TYPE); + } + + /** + * 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 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 index 25bbda0f4..c3155367b 100644 --- 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 @@ -9,9 +9,9 @@ * 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. @@ -28,6 +28,10 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; import java.util.UUID; import org.apache.commons.io.IOUtils; import org.junit.Test; @@ -37,6 +41,7 @@ import org.onap.policy.controlloop.ControlLoopOperation; import org.onap.policy.controlloop.VirtualControlLoopEvent; import org.onap.policy.controlloop.policy.Policy; import org.onap.policy.so.SORequest; +import org.onap.policy.so.SORequestParameters; import org.onap.policy.so.util.Serialization; public class TestSOActorServiceProvider { @@ -59,6 +64,8 @@ public class TestSOActorServiceProvider { assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp)); policy.setRecipe("VF Module Create"); + + // empty policy payload SORequest request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp); assertNotNull(request); @@ -66,6 +73,15 @@ public class TestSOActorServiceProvider { assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId()); assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId()); + // non-empty policy payload + policy.setPayload(makePayload()); + request = new SOActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp); + assertNotNull(request); + assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload()); + assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey")); + assertEquals(1, request.getRequestDetails().getConfigurationParameters().size()); + assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey")); + // null response assertNull(new SOActorServiceProvider().constructRequest(onset, operation, policy, null)); @@ -98,15 +114,64 @@ public class TestSOActorServiceProvider { } /** + * Creates a policy payload containing request & configuration parameters. + * + * @return the payload + */ + private Map<String, String> makePayload() { + Map<String, String> payload = new TreeMap<>(); + + payload.put(SOActorServiceProvider.REQ_PARAM_NM, makeReqParams()); + payload.put(SOActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams()); + + return payload; + } + + /** + * Creates request parameters. + * + * @return request parameters, encoded as JSON + */ + private String makeReqParams() { + SORequestParameters params = new SORequestParameters(); + + params.setUsePreload(true); + + Map<String, String> map = new TreeMap<>(); + map.put("akey", "avalue"); + + List<Map<String, String>> lst = new LinkedList<>(); + lst.add(map); + + params.setUserParams(lst); + + return Serialization.gsonPretty.toJson(params); + } + + /** + * Creates configuration parameters. + * + * @return configuration parameters, encoded as JSON + */ + private String makeConfigParams() { + Map<String, String> map = new TreeMap<>(); + map.put("ckey", "cvalue"); + + List<Map<String, String>> lst = new LinkedList<>(); + lst.add(map); + + return Serialization.gsonPretty.toJson(lst); + } + + /** * Reads an AAI vserver named-query response from a file. - * + * * @param onset the ONSET event * @param fileName name of the file containing the JSON response * @return output from the AAI vserver named-query * @throws IOException if the file cannot be read */ - private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) - throws IOException { + private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException { String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8); AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class); |