2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Ericsson
8 * ================================================================================
9 * Modifications Copyright (C) 2019 IBM
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.interfaces.service.executorImpl;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 import org.onap.appc.aai.client.aai.AaiService;
34 import org.onap.appc.interfaces.service.data.Request;
35 import org.onap.appc.interfaces.service.data.ScopeOverlap;
36 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.google.common.base.Strings;
44 public class ServiceExecutorImpl {
46 private static final EELFLogger log = EELFManager.getInstance().getLogger(ServiceExecutorImpl.class);
47 private AAIClient aaiClient;
49 public String isRequestOverLap(String requestData) throws Exception {
50 String response = "\"requestOverlap\" : ";
51 log.info("Response from ServiceExecutorImpl");
52 ObjectMapper mapper = getObjectMapper();
53 ScopeOverlap scopeOverlap = mapper.readValue(requestData, ScopeOverlap.class);
54 // return response + String.valueOf(checkForOverLap(scopeOverlap));
55 boolean isOverlap = checkForOverLap(scopeOverlap);
56 scopeOverlap.setOverlap(String.valueOf(isOverlap));
57 if (scopeOverlap.getOverlap() != null && scopeOverlap.getOverlap().equalsIgnoreCase("false")){
58 log.info(response + "false");
59 return response + "false";
62 log.info(response + "true");
63 return response + "true";
67 private boolean checkForOverLap(ScopeOverlap scopeOverlap) throws Exception {
68 log.info("Checking for isScopeOverlap");
69 if (scopeOverlap.getInProgressRequest() == null) {
71 }else if ( scopeOverlap.getInProgressRequest().isEmpty()){
74 if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfId() != null) {
76 } else if (!Strings.isNullOrEmpty(scopeOverlap.getVnfId())
77 && scopeOverlap.getInProgressRequest().size() > 0) {
78 log.info("Checking overlap for similar vnfid :" + isVnfIdOverlap(scopeOverlap));
79 return isVnfIdOverlap(scopeOverlap);
80 } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVfModuleId() != null) {
82 } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId() != null) {
83 return isVserverOrVnfcIdOverLap(scopeOverlap);
84 } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfcName() != null) {
85 return isVserverOrVnfcIdOverLap(scopeOverlap);
87 throw new Exception(" Action Identifier doesn't have VnfId, VfModuleId, VServerId, VnfcName ");
91 private boolean isVnfcNameOverLap(ScopeOverlap scopeOverlap) throws Exception {
93 AaiService aaiService = getAaiService(aaiClient);
94 SvcLogicContext ctx = getSvcLogicContext();
95 Map<String, String> params = new HashMap<String, String>();
96 List<String> inProgressVServerIds = new ArrayList<String>();
97 String currentVnfcVserverId = new String();
98 String currentRequestVnfcName = scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfcName();
99 String currentRequestVServerId = scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId();
100 List<Request> inProgressRequests = scopeOverlap.getInProgressRequest();
101 params.put("vnfId", scopeOverlap.getVnfId());
103 aaiService.getGenericVnfInfo(params, ctx);
104 int vm_count = Integer.parseInt(ctx.getAttribute("vm-count"));
105 for(Request inprogressRequest:inProgressRequests){
106 if(inprogressRequest.getActionIdentifiers().getVnfcName() != null){
107 for (int i = 0; i < vm_count; i++) {
108 if (ctx.getAttribute("vm[" + i + "].vnfc-name") != null && ctx.getAttribute("vm[" + i + "].vnfc-name")
109 .equals(inprogressRequest.getActionIdentifiers().getVnfcName())) {
110 String newInProgressVserverId = ctx.getAttribute("vm[" + i + "].vserver-id");
111 inProgressVServerIds.add(newInProgressVserverId);
112 log.debug("Received vserver-id from AAI: " + newInProgressVserverId);
117 for(Request inProgVserverIds:inProgressRequests)
118 if(inProgVserverIds.getActionIdentifiers().getvServerId() != null)
119 inProgressVServerIds.add(inProgVserverIds.getActionIdentifiers().getvServerId());
120 if(currentRequestVnfcName != null){
121 for (int i = 0; i < vm_count; i++) {
122 if (ctx.getAttribute("vm[" + i + "].vnfc-name") != null && ctx.getAttribute("vm[" + i + "].vnfc-name")
123 .equals(currentRequestVnfcName)) {
124 currentVnfcVserverId = ctx.getAttribute("vm[" + i + "].vserver-id");
125 log.debug("Received vserver-id from AAI: " + currentVnfcVserverId);
126 return inProgressVServerIds.contains(currentVnfcVserverId);
130 for (Request request : inProgressRequests) {
131 if(!Strings.isNullOrEmpty(currentRequestVServerId) && currentRequestVServerId.equalsIgnoreCase(request.getActionIdentifiers().getvServerId()))
134 if(currentRequestVServerId != null) {
135 return inProgressVServerIds.contains(currentRequestVServerId);
137 return Boolean.FALSE;
138 } catch (Exception e) {
139 log.debug(e.getMessage());
144 private boolean isVserverOrVnfcIdOverLap(ScopeOverlap scopeOverlap) throws Exception {
145 List<Request> inProgressRequests = scopeOverlap.getInProgressRequest();
146 for (Request request : inProgressRequests) {
147 if(request.getActionIdentifiers().getVnfId() != null)
148 return Boolean.TRUE ;
150 for (Request request : inProgressRequests) {
151 if(request.getActionIdentifiers().getVfModuleId() != null)
152 return Boolean.TRUE ;
154 String currentVserverID = scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId();
155 for (Request request : inProgressRequests) {
156 if(currentVserverID != null && currentVserverID.equalsIgnoreCase(request.getActionIdentifiers().getvServerId()))
157 return Boolean.TRUE ;
159 return isVnfcNameOverLap(scopeOverlap);
162 private boolean isVnfIdOverlap(ScopeOverlap scopeOverlap) throws Exception {
163 List<Request> inProgressRequests = scopeOverlap.getInProgressRequest();
164 log.info("inProgressRequests list" + inProgressRequests.toString());
165 for (Request request : inProgressRequests) {
166 log.info("request list" + request.getTargetId());
167 if (!Strings.isNullOrEmpty(scopeOverlap.getVnfId())
168 && !Strings.isNullOrEmpty(request.getTargetId())
169 && (request.getTargetId()
170 .equals(scopeOverlap.getVnfId())))
173 return Boolean.FALSE;
176 protected ObjectMapper getObjectMapper() {
177 return new ObjectMapper();
180 protected AaiService getAaiService(AAIClient aaiClient) {
181 return new AaiService(aaiClient);
184 protected SvcLogicContext getSvcLogicContext() {
185 return new SvcLogicContext();