2 * ============LICENSE_START=======================================================
3 * SOActorServiceProvider
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.so;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26 import com.google.gson.reflect.TypeToken;
27 import java.lang.reflect.Type;
28 import java.util.Collections;
29 import java.util.List;
31 import org.onap.aai.domain.yang.CloudRegion;
32 import org.onap.aai.domain.yang.GenericVnf;
33 import org.onap.aai.domain.yang.ServiceInstance;
34 import org.onap.aai.domain.yang.Tenant;
35 import org.onap.policy.aai.AaiCqResponse;
36 import org.onap.policy.controlloop.ControlLoopOperation;
37 import org.onap.policy.controlloop.VirtualControlLoopEvent;
38 import org.onap.policy.controlloop.actorserviceprovider.impl.ActorImpl;
39 import org.onap.policy.controlloop.policy.Policy;
40 import org.onap.policy.so.SoCloudConfiguration;
41 import org.onap.policy.so.SoManager;
42 import org.onap.policy.so.SoModelInfo;
43 import org.onap.policy.so.SoOperationType;
44 import org.onap.policy.so.SoRelatedInstance;
45 import org.onap.policy.so.SoRelatedInstanceListElement;
46 import org.onap.policy.so.SoRequest;
47 import org.onap.policy.so.SoRequestDetails;
48 import org.onap.policy.so.SoRequestInfo;
49 import org.onap.policy.so.SoRequestParameters;
50 import org.onap.policy.so.util.Serialization;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class SoActorServiceProvider extends ActorImpl {
55 private static final Logger logger = LoggerFactory.getLogger(SoActorServiceProvider.class);
57 public static final String NAME = "SO";
59 // TODO old code: remove lines down to **HERE**
61 private static final String TENANT_NOT_FOUND = "Tenant Item not found in AAI response {}";
62 private static final String CONSTRUCTED_SO_MSG = "Constructed SO request: {}";
64 // Strings for targets
65 private static final String TARGET_VFC = "VFC";
67 // Strings for recipes
68 private static final String RECIPE_VF_MODULE_CREATE = "VF Module Create";
69 private static final String RECIPE_VF_MODULE_DELETE = "VF Module Delete";
71 private static final ImmutableList<String> recipes =
72 ImmutableList.of(RECIPE_VF_MODULE_CREATE, RECIPE_VF_MODULE_DELETE);
73 private static final ImmutableMap<String, List<String>> targets =
74 new ImmutableMap.Builder<String, List<String>>().put(RECIPE_VF_MODULE_CREATE, ImmutableList.of(TARGET_VFC))
75 .put(RECIPE_VF_MODULE_DELETE, ImmutableList.of(TARGET_VFC)).build();
77 // name of request parameters within policy payload
78 public static final String REQ_PARAM_NM = "requestParameters";
80 // name of configuration parameters within policy payload
81 public static final String CONFIG_PARAM_NM = "configurationParameters";
83 // used to decode configuration parameters via gson
84 private static final Type CONFIG_TYPE = new TypeToken<List<Map<String, String>>>() {}.getType();
86 // Static variables required to hold the IDs of the last service item, VNF item and VF Module.
88 // a multithreaded deployment this WILL break
89 private static String lastVNFItemVnfId;
90 private static String lastServiceItemServiceInstanceId;
91 private static String lastVfModuleItemVfModuleInstanceId;
95 public SoActorServiceProvider() {
99 // TODO old code: remove lines down to **HERE**
102 public String actor() {
107 public List<String> recipes() {
108 return ImmutableList.copyOf(recipes);
112 public List<String> recipeTargets(String recipe) {
113 return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
117 public List<String> recipePayloads(String recipe) {
118 return Collections.emptyList();
121 private SoModelInfo prepareSoModelInfo(Policy policy) {
123 if (policy.getTarget() == null || policy.getTarget().getModelCustomizationId() == null
124 || policy.getTarget().getModelInvariantId() == null) {
128 if (policy.getTarget().getModelName() == null || policy.getTarget().getModelVersion() == null
129 || policy.getTarget().getModelVersionId() == null) {
133 SoModelInfo soModelInfo = new SoModelInfo();
134 soModelInfo.setModelCustomizationId(policy.getTarget().getModelCustomizationId());
135 soModelInfo.setModelInvariantId(policy.getTarget().getModelInvariantId());
136 soModelInfo.setModelName(policy.getTarget().getModelName());
137 soModelInfo.setModelVersion(policy.getTarget().getModelVersion());
138 soModelInfo.setModelVersionId(policy.getTarget().getModelVersionId());
139 soModelInfo.setModelType("vfModule");
144 * Construct requestInfo for the SO requestDetails.
146 * @return SO request information
148 private SoRequestInfo constructRequestInfo() {
149 SoRequestInfo soRequestInfo = new SoRequestInfo();
150 soRequestInfo.setSource("POLICY");
151 soRequestInfo.setSuppressRollback(false);
152 soRequestInfo.setRequestorId("policy");
153 return soRequestInfo;
157 * This method is needed to get the serviceInstanceId and vnfInstanceId which is used in the asyncSORestCall.
159 * @param requestId the request Id
160 * @param callback callback method
161 * @param request the request
162 * @param url SO REST URL
163 * @param user username
164 * @param password password
166 public static void sendRequest(String requestId, SoManager.SoCallback callback, Object request, String url,
167 String user, String password) {
168 SoManager soManager = new SoManager(url, user, password);
169 soManager.asyncSoRestCall(requestId, callback, lastServiceItemServiceInstanceId, lastVNFItemVnfId,
170 lastVfModuleItemVfModuleInstanceId, (SoRequest) request);
175 * Builds the request parameters from the policy payload.
177 * @param policy the policy
178 * @param request request into which to stick the request parameters
180 private void buildRequestParameters(Policy policy, SoRequestDetails request) {
181 // assume null until proven otherwise
182 request.setRequestParameters(null);
184 if (policy.getPayload() == null) {
188 String json = policy.getPayload().get(REQ_PARAM_NM);
193 request.setRequestParameters(Serialization.gsonPretty.fromJson(json, SoRequestParameters.class));
197 * Builds the configuration parameters from the policy payload.
199 * @param policy the policy
200 * @param request request into which to stick the configuration parameters
202 private void buildConfigurationParameters(Policy policy, SoRequestDetails request) {
203 // assume null until proven otherwise
204 request.setConfigurationParameters(null);
206 if (policy.getPayload() == null) {
210 String json = policy.getPayload().get(CONFIG_PARAM_NM);
215 request.setConfigurationParameters(Serialization.gsonPretty.fromJson(json, CONFIG_TYPE));
219 * This method is called to remember the last service instance ID, VNF Item VNF ID and vf module ID. Note these
220 * fields are static, beware for multithreaded deployments
222 * @param vnfInstanceId update the last VNF instance ID to this value
223 * @param serviceInstanceId update the last service instance ID to this value
224 * @param vfModuleId update the vfModule instance ID to this value
226 private static void preserveInstanceIds(final String vnfInstanceId, final String serviceInstanceId,
227 final String vfModuleId) {
228 lastVNFItemVnfId = vnfInstanceId;
229 lastServiceItemServiceInstanceId = serviceInstanceId;
230 lastVfModuleItemVfModuleInstanceId = vfModuleId;
234 * Constructs a SO request conforming to the lcm API. The actual request is constructed and then placed in a wrapper
235 * object used to send through DMAAP.
237 * @param onset the event that is reporting the alert for policy to perform an action
238 * @param operation the control loop operation specifying the actor, operation, target, etc.
239 * @param policy the policy the was specified from the yaml generated by CLAMP or through the Policy GUI/API
240 * @param aaiCqResponse response from A&AI custom query
241 * @return a SO request conforming to the lcm API using the DMAAP wrapper
243 public SoRequest constructRequestCq(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
244 AaiCqResponse aaiCqResponse) {
245 if (!NAME.equals(policy.getActor()) || !recipes().contains(policy.getRecipe())) {
249 // A&AI named query should have been performed by now. If not, return null
250 if (aaiCqResponse == null) {
254 SoModelInfo soModelInfo = prepareSoModelInfo(policy);
256 // Report the error vf module is not found
257 if (soModelInfo == null) {
258 logger.error("vf module is not found.");
263 ServiceInstance vnfServiceItem;
265 CloudRegion cloudRegionItem;
267 // Extract the items we're interested in from the response
269 vnfItem = aaiCqResponse.getGenericVnfByVfModuleModelInvariantId(soModelInfo.getModelInvariantId());
270 //Report VNF not found
271 if (vnfItem == null) {
272 logger.error("Generic Vnf is not found.");
275 } catch (Exception e) {
276 logger.error("VNF Item not found in AAI response {}", Serialization.gsonPretty.toJson(aaiCqResponse), e);
281 vnfServiceItem = aaiCqResponse.getServiceInstance();
282 } catch (Exception e) {
283 logger.error("VNF Service Item not found in AAI response {}",
284 Serialization.gsonPretty.toJson(aaiCqResponse), e);
289 tenantItem = aaiCqResponse.getDefaultTenant();
290 } catch (Exception e) {
291 logger.error(TENANT_NOT_FOUND, Serialization.gsonPretty.toJson(aaiCqResponse), e);
296 cloudRegionItem = aaiCqResponse.getDefaultCloudRegion();
297 } catch (Exception e) {
298 logger.error(TENANT_NOT_FOUND, Serialization.gsonPretty.toJson(aaiCqResponse), e);
304 // Construct SO Request for a policy's recipe
305 if (RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) {
306 return constructCreateRequestCq(aaiCqResponse, policy, tenantItem, vnfItem, vnfServiceItem, soModelInfo,
308 } else if (RECIPE_VF_MODULE_DELETE.equals(policy.getRecipe())) {
309 return constructDeleteRequestCq(tenantItem, vnfItem, vnfServiceItem, policy, cloudRegionItem);
316 * Construct the So request, based on Custom Query response from A&AI.
318 * @param aaiCqResponse Custom query response from A&AI
319 * @param policy policy information
320 * @param tenantItem Tenant from CQ response
321 * @param vnfItem Generic VNF from CQ response
322 * @param vnfServiceItem Service Instance from CQ response
323 * @param vfModuleItem VF Module from CustomQuery response
324 * @param cloudRegionItem Cloud Region from Custom query response
325 * @return SoRequest well formed So Request
327 private SoRequest constructCreateRequestCq(AaiCqResponse aaiCqResponse, Policy policy, Tenant tenantItem,
328 GenericVnf vnfItem, ServiceInstance vnfServiceItem, SoModelInfo vfModuleItem, CloudRegion cloudRegionItem) {
329 SoRequest request = new SoRequest();
330 request.setOperationType(SoOperationType.SCALE_OUT);
333 // Do NOT send So the requestId, they do not support this field
335 request.setRequestDetails(new SoRequestDetails());
336 request.getRequestDetails().setRequestParameters(new SoRequestParameters());
337 request.getRequestDetails().getRequestParameters().setUserParams(null);
339 // cloudConfiguration
340 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
342 request.getRequestDetails().setModelInfo(vfModuleItem);
346 request.getRequestDetails().setRequestInfo(constructRequestInfo());
347 request.getRequestDetails().getRequestInfo().setInstanceName("vfModuleName");
349 // relatedInstanceList
350 SoRelatedInstanceListElement relatedInstanceListElement1 = new SoRelatedInstanceListElement();
351 SoRelatedInstanceListElement relatedInstanceListElement2 = new SoRelatedInstanceListElement();
352 relatedInstanceListElement1.setRelatedInstance(new SoRelatedInstance());
353 relatedInstanceListElement2.setRelatedInstance(new SoRelatedInstance());
356 relatedInstanceListElement1.getRelatedInstance().setInstanceId(vnfServiceItem.getServiceInstanceId());
357 relatedInstanceListElement1.getRelatedInstance().setModelInfo(new SoModelInfo());
358 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelType("service");
359 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
360 .setModelInvariantId(vnfServiceItem.getModelInvariantId());
361 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
362 .setModelVersionId(vnfServiceItem.getModelVersionId());
363 relatedInstanceListElement1.getRelatedInstance().getModelInfo()
364 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelName());
365 relatedInstanceListElement1.getRelatedInstance().getModelInfo().setModelVersion(
366 aaiCqResponse.getModelVerByVersionId(vnfServiceItem.getModelVersionId()).getModelVersion());
370 relatedInstanceListElement2.getRelatedInstance().setInstanceId(vnfItem.getVnfId());
371 relatedInstanceListElement2.getRelatedInstance().setModelInfo(new SoModelInfo());
372 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelType("vnf");
373 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
374 .setModelInvariantId(vnfItem.getModelInvariantId());
375 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersionId(vnfItem.getModelVersionId());
377 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
378 .setModelName(aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelName());
379 relatedInstanceListElement2.getRelatedInstance().getModelInfo().setModelVersion(
380 aaiCqResponse.getModelVerByVersionId(vnfItem.getModelVersionId()).getModelVersion());
383 relatedInstanceListElement2.getRelatedInstance().getModelInfo()
384 .setModelCustomizationId(vnfItem.getModelCustomizationId());
387 // Insert the Service Item and VNF Item
388 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement1);
389 request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
391 // Request Parameters
392 buildRequestParameters(policy, request.getRequestDetails());
394 // Configuration Parameters
395 buildConfigurationParameters(policy, request.getRequestDetails());
396 // Save the instance IDs for the VNF and service to static fields
397 // vfModuleId is not required for the create vf-module
398 preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
399 if (logger.isDebugEnabled()) {
400 logger.debug(CONSTRUCTED_SO_MSG, Serialization.gsonPretty.toJson(request));
406 * constructs delete request for So.
408 * @param tenantItem Tenant from A&AI CQ request
409 * @param vnfItem Generic VNF from A&AI CQ request
410 * @param vnfServiceItem ServiceInstance from A&AI CQ request
411 * @param policy policy information
412 * @param cloudRegionItem CloudRegion from A&AI CQ request
413 * @return SoRequest deleted
415 private SoRequest constructDeleteRequestCq(Tenant tenantItem, GenericVnf vnfItem, ServiceInstance vnfServiceItem,
416 Policy policy, CloudRegion cloudRegionItem) {
417 SoRequest request = new SoRequest();
418 request.setOperationType(SoOperationType.DELETE_VF_MODULE);
419 request.setRequestDetails(new SoRequestDetails());
420 request.getRequestDetails().setRelatedInstanceList(null);
421 request.getRequestDetails().setConfigurationParameters(null);
423 // cloudConfiguration
424 request.getRequestDetails().setCloudConfiguration(constructCloudConfigurationCq(tenantItem, cloudRegionItem));
426 request.getRequestDetails().setModelInfo(prepareSoModelInfo(policy));
428 request.getRequestDetails().setRequestInfo(constructRequestInfo());
429 // Save the instance IDs for the VNF, service and vfModule to static fields
430 preserveInstanceIds(vnfItem.getVnfId(), vnfServiceItem.getServiceInstanceId(), null);
432 if (logger.isDebugEnabled()) {
433 logger.debug(CONSTRUCTED_SO_MSG, Serialization.gsonPretty.toJson(request));
440 * Construct cloudConfiguration for the SO requestDetails. Overridden for custom query.
442 * @param tenantItem tenant item from A&AI named-query response
443 * @return SO cloud configuration
445 private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
446 SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
447 cloudConfiguration.setTenantId(tenantItem.getTenantId());
448 cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
449 return cloudConfiguration;