2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Huawei Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.so.bpmn.common.resource;
22 import com.google.common.reflect.TypeToken;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import java.lang.reflect.Type;
28 import java.util.ArrayList;
29 import java.util.List;
31 import java.util.Optional;
32 import org.apache.commons.lang3.StringUtils;
33 import org.onap.so.bpmn.core.domain.GroupResource;
34 import org.onap.so.bpmn.core.domain.Resource;
35 import org.onap.so.bpmn.core.domain.VnfResource;
37 public class InstanceResourceList {
39 private InstanceResourceList() {
40 throw new IllegalStateException("Utility class");
43 // this method returns key from resource input
44 // e.g. {\"sdwansite_emails\" : \"[sdwansiteresource_list(PK), INDEX, sdwansite_emails]|default\",
46 // it will return sdwansiteresource_list
47 private static String getPrimaryKey(Resource resource) {
48 String resourceInput = "";
49 if (resource instanceof VnfResource) {
50 resourceInput = ((VnfResource) resource).getResourceInput();
51 } else if (resource instanceof GroupResource) {
52 resourceInput = ((GroupResource) resource).getVnfcs().get(0).getResourceInput();
55 Gson gson = new Gson();
56 Type type = new TypeToken<Map<String, String>>() {}.getType();
57 Map<String, String> map = gson.fromJson(resourceInput, type);
60 Optional<String> pkOpt = map.values().stream().filter(e -> e.contains("[")).map(e -> e.replace("[", ""))
61 .map(e -> e.split(",")[0]).findFirst();
63 return pkOpt.isPresent() ? pkOpt.get() : "";
70 public static List<Resource> getInstanceResourceList(final VnfResource vnfResource, final String uuiRequest) {
71 List<Resource> sequencedResourceList = new ArrayList<Resource>();
72 Gson gson = new Gson();
73 JsonObject servJsonObject = gson.fromJson(uuiRequest, JsonObject.class);
74 JsonObject reqInputJsonObj = servJsonObject.getAsJsonObject("service").getAsJsonObject("parameters")
75 .getAsJsonObject("requestInputs");
77 String pk = getPrimaryKey(vnfResource);
78 // if pk is not empty that means it can contain list of VNF
80 JsonElement vfNode = reqInputJsonObj.get(pk);
81 if (vfNode.isJsonArray()) {
82 // multiple instance of VNF
83 JsonArray vfNodeList = vfNode.getAsJsonArray();
84 for (JsonElement vf : vfNodeList) {
85 JsonObject vfObj = vf.getAsJsonObject();
87 // Add VF first before adding groups
88 sequencedResourceList.add(vnfResource);
89 List<Resource> sequencedGroupResourceList = getGroupResourceInstanceList(vnfResource, vfObj);
90 if (!sequencedGroupResourceList.isEmpty()) {
91 sequencedResourceList.addAll(sequencedGroupResourceList);
96 // if pk is empty that means it has only one VNF Node
97 // Add VF first before adding groups
98 sequencedResourceList.add(vnfResource);
99 // check the groups for this VNF and add into resource list
100 List<Resource> sequencedGroupResourceList = getGroupResourceInstanceList(vnfResource, reqInputJsonObj);
101 if (!sequencedGroupResourceList.isEmpty()) {
102 sequencedResourceList.addAll(sequencedGroupResourceList);
106 // In negative case consider only VNF resource only
107 if (sequencedResourceList.isEmpty()) {
108 sequencedResourceList.add(vnfResource);
111 return sequencedResourceList;
114 private static List<Resource> getGroupResourceInstanceList(VnfResource vnfResource, JsonObject vfObj) {
115 List<Resource> sequencedResourceList = new ArrayList<Resource>();
116 if (vnfResource.getGroupOrder() != null && !StringUtils.isEmpty(vnfResource.getGroupOrder())) {
117 String[] grpSequence = vnfResource.getGroupOrder().split(",");
118 for (String grpType : grpSequence) {
119 for (GroupResource gResource : vnfResource.getGroups()) {
120 if (StringUtils.containsIgnoreCase(gResource.getModelInfo().getModelName(), grpType)) {
121 // check the number of group instances from UUI to be added
122 String sk = getPrimaryKey(gResource);
124 // if sk is empty that means it is not list type
125 // only one group / vnfc to be considered
127 sequencedResourceList.add(gResource);
129 // check the number of list size of VNFC of a group
130 JsonElement vfcNode = vfObj.get(sk);
131 if (vfcNode.isJsonArray()) {
132 JsonArray vfcList = vfcNode.getAsJsonArray();
133 for (JsonElement vfc : vfcList) {
134 sequencedResourceList.add(gResource);
137 // consider only one vnfc/group if not an array
138 sequencedResourceList.add(gResource);
146 return sequencedResourceList;