1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
/*-
* ============LICENSE_START=======================================================
* SOActorServiceProvider
* ================================================================================
* Copyright (C) 2017-2018 AT&T Intellectual Property. 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 com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.drools.core.WorkingMemory;
import org.onap.policy.aai.AaiManager;
import org.onap.policy.aai.AaiNqExtraProperty;
import org.onap.policy.aai.AaiNqInstanceFilters;
import org.onap.policy.aai.AaiNqInventoryResponseItem;
import org.onap.policy.aai.AaiNqNamedQuery;
import org.onap.policy.aai.AaiNqQueryParameters;
import org.onap.policy.aai.AaiNqRequest;
import org.onap.policy.aai.AaiNqResponse;
import org.onap.policy.aai.AaiNqResponseWrapper;
import org.onap.policy.controlloop.ControlLoopOperation;
import org.onap.policy.controlloop.VirtualControlLoopEvent;
import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
import org.onap.policy.controlloop.policy.Policy;
import org.onap.policy.drools.system.PolicyEngine;
import org.onap.policy.rest.RESTManager;
import org.onap.policy.so.SOCloudConfiguration;
import org.onap.policy.so.SOManager;
import org.onap.policy.so.SOModelInfo;
import org.onap.policy.so.SORelatedInstance;
import org.onap.policy.so.SORelatedInstanceListElement;
import org.onap.policy.so.SORequest;
import org.onap.policy.so.SORequestDetails;
import org.onap.policy.so.SORequestInfo;
import org.onap.policy.so.SORequestParameters;
import org.onap.policy.so.util.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SOActorServiceProvider implements Actor {
private static final Logger logger = LoggerFactory.getLogger(SOActorServiceProvider.class);
// Strings for SO Actor
private static final String SO_ACTOR = "SO";
// Strings for targets
private static final String TARGET_VFC = "VFC";
// Strings for recipes
private static final String RECIPE_VF_MODULE_CREATE = "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(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_ACTOR;
}
@Override
public List<String> recipes() {
return ImmutableList.copyOf(recipes);
}
@Override
public List<String> recipeTargets(String recipe) {
return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
}
@Override
public List<String> recipePayloads(String recipe) {
return Collections.emptyList();
}
/**
* 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
* @return a SO request conforming to the lcm API using the DMAAP wrapper
*/
public SORequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy) {
String modelNamePropertyKey = "model-ver.model-name";
String modelVersionPropertyKey = "model-ver.model-version";
String modelVersionIdPropertyKey = "model-ver.model-version-id";
if (!SO_ACTOR.equals(policy.getActor()) || !RECIPE_VF_MODULE_CREATE.equals(policy.getRecipe())) {
// for future extension
return null;
}
// 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;
}
AaiNqInventoryResponseItem vnfItem;
AaiNqInventoryResponseItem vnfServiceItem;
AaiNqInventoryResponseItem tenantItem;
// 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),
e);
return null;
}
try {
vnfServiceItem = vnfItem.getItems().getInventoryResponseItems().get(0);
} catch (Exception e) {
logger.error("VNF Service Item not found in AAI response {}",
Serialization.gsonPretty.toJson(aaiResponseWrapper), e);
return null;
}
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), e);
return null;
}
// 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);
// 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;
}
// 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);
//
// cloudConfiguration
//
request.getRequestDetails().getCloudConfiguration().setTenantId(tenantItem.getTenant().getTenantId());
request.getRequestDetails().getCloudConfiguration().setLcpCloudRegionId(
tenantItem.getItems().getInventoryResponseItems().get(0).getCloudRegion().getCloudRegionId());
//
// modelInfo
//
AaiNqInventoryResponseItem vfModuleItem = vnfItem.getItems().getInventoryResponseItems().get(nonBaseIndex);
request.getRequestDetails().getModelInfo().setModelType("vfModule");
request.getRequestDetails().getModelInfo()
.setModelInvariantId(vfModuleItem.getVfModule().getModelInvariantId());
request.getRequestDetails().getModelInfo().setModelVersionId(vfModuleItem.getVfModule().getModelVersionId());
for (AaiNqExtraProperty prop : vfModuleItem.getExtraProperties().getExtraProperty()) {
if (prop.getPropertyName().equals(modelNamePropertyKey)) {
request.getRequestDetails().getModelInfo().setModelName(prop.getPropertyValue());
} else if (prop.getPropertyName().equals(modelVersionPropertyKey)) {
request.getRequestDetails().getModelInfo().setModelVersion(prop.getPropertyValue());
}
}
//
// requestInfo
//
String instanceName = vnfItem.getItems().getInventoryResponseItems().get(baseIndex).getVfModule()
.getVfModuleName().replace("Vfmodule", "vDNS");
int numberOfNonBaseModules = findNonBaseModules(vnfItem.getItems().getInventoryResponseItems());
// Code to create unique VF Module names across the invocations.
if (numberOfNonBaseModules == 1) {
int instanceNumber = 1;
instanceName = instanceName.concat("-").concat(String.valueOf(instanceNumber));
request.getRequestDetails().getRequestInfo().setInstanceName(instanceName);
} else if (numberOfNonBaseModules > 1) {
int instanceNumber = numberOfNonBaseModules + 1;
instanceName = instanceName.concat("-").concat(String.valueOf(instanceNumber));
request.getRequestDetails().getRequestInfo().setInstanceName(instanceName);
} else {
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");
//
// 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());
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());
}
}
// 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());
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());
} else if (prop.getPropertyName().equals(modelVersionIdPropertyKey)) {
relatedInstanceListElement2.getRelatedInstance().getModelInfo()
.setModelVersionId(prop.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));
}
return request;
}
/**
* 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
* @param request the request
*/
public static void sendRequest(String requestId, WorkingMemory wm, Object request) {
SOManager soManager = new SOManager();
soManager.asyncSORestCall(requestId, wm, lastServiceItemServiceInstanceId, lastVNFItemVnfId,
(SORequest) request);
}
/**
* Constructs and sends an AAI vserver Named Query.
*
* @param onset the virtial control loop event
* @returns the response to the AAI Named Query
*/
private AaiNqResponseWrapper performAaiNamedQueryRequest(VirtualControlLoopEvent onset) {
// create AAI named-query request with UUID started with ""
AaiNqRequest aaiNqRequest = new AaiNqRequest();
AaiNqQueryParameters aaiNqQueryParam = new AaiNqQueryParameters();
AaiNqNamedQuery aaiNqNamedQuery = new AaiNqNamedQuery();
final AaiNqInstanceFilters aaiNqInstanceFilter = new AaiNqInstanceFilters();
// queryParameters
// UUID.fromString($params.getAaiNamedQueryUUID()) TO DO: AaiNamedQueryUUID
aaiNqNamedQuery.setNamedQueryUuid(UUID.fromString("4ff56a54-9e3f-46b7-a337-07a1d3c6b469"));
aaiNqQueryParam.setNamedQuery(aaiNqNamedQuery);
aaiNqRequest.setQueryParameters(aaiNqQueryParam);
//
// instanceFilters
//
Map<String, Map<String, String>> aaiNqInstanceFilterMap = new HashMap<>();
Map<String, String> aaiNqInstanceFilterMapItem = new HashMap<>();
// TO DO: get vserver.vname from dcae onset.AAI.get("vserver.vserver-name")
aaiNqInstanceFilterMapItem.put("vserver-name", onset.getAai().get("vserver.vserver-name"));
aaiNqInstanceFilterMap.put("vserver", aaiNqInstanceFilterMapItem);
aaiNqInstanceFilter.getInstanceFilter().add(aaiNqInstanceFilterMap);
aaiNqRequest.setInstanceFilters(aaiNqInstanceFilter);
if (logger.isDebugEnabled()) {
logger.debug("AAI Request sent: {}", Serialization.gsonPretty.toJson(aaiNqRequest));
}
AaiNqResponse aaiNqResponse = new AaiManager(new RESTManager()).postQuery(getPeManagerEnvProperty("aai.url"),
getPeManagerEnvProperty("aai.username"), getPeManagerEnvProperty("aai.password"), aaiNqRequest,
onset.getRequestId());
// Check AAI response
if (aaiNqResponse == null) {
logger.warn("No response received from AAI for request {}", aaiNqRequest);
return null;
}
// Create AAINQResponseWrapper
AaiNqResponseWrapper aaiNqResponseWrapper = new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
if (logger.isDebugEnabled()) {
logger.debug("AAI Named Query Response: ");
logger.debug(Serialization.gsonPretty.toJson(aaiNqResponseWrapper.getAaiNqResponse()));
}
return aaiNqResponseWrapper;
}
/**
* Find the base index or non base index in a list of inventory response items.
*
* @param inventoryResponseItems the list of inventory response items
* @param baseIndexFlag true if we are searching for the base index, false if we are searching
* for the non base index
* @return the base or non base index or -1 if the index was not found
*/
private int findIndex(List<AaiNqInventoryResponseItem> inventoryResponseItems, boolean baseIndexFlag) {
for (AaiNqInventoryResponseItem invenoryResponseItem : inventoryResponseItems) {
if (invenoryResponseItem.getVfModule() != null
&& baseIndexFlag == invenoryResponseItem.getVfModule().getIsBaseVfModule()) {
return inventoryResponseItems.indexOf(invenoryResponseItem);
}
}
return -1;
}
/**
* Find the number of non base modules present in API response object.
*
* @param inventoryResponseItems the list of inventory response items
* @return number of non base index modules
*/
private int findNonBaseModules(List<AaiNqInventoryResponseItem> inventoryResponseItems) {
int nonBaseModuleCount = 0;
for (AaiNqInventoryResponseItem invenoryResponseItem : inventoryResponseItems) {
if (invenoryResponseItem.getVfModule() != null
&& (!invenoryResponseItem.getVfModule().getIsBaseVfModule())) {
nonBaseModuleCount++;
}
}
return nonBaseModuleCount;
}
/**
* 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
*/
private static void preserveInstanceIds(final String vnfInstanceId, final String serviceInstanceId) {
lastVNFItemVnfId = vnfInstanceId;
lastServiceItemServiceInstanceId = serviceInstanceId;
}
/**
* 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 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;
}
}
|