lower code smells
[appc.git] / appc-inbound / appc-interfaces-service / bundle / src / main / java / org / onap / appc / interfaces / service / executorImpl / ServiceExecutorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.interfaces.service.executorImpl;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
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;
38
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;
43
44 public class ServiceExecutorImpl {
45
46     private static final EELFLogger log = EELFManager.getInstance().getLogger(ServiceExecutorImpl.class);
47     private AAIClient aaiClient;
48
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";
60         }
61         else{
62             log.info(response + "true");
63             return response + "true";
64         }
65     }
66
67     private boolean checkForOverLap(ScopeOverlap scopeOverlap) throws Exception {
68         log.info("Checking for isScopeOverlap");
69         if (scopeOverlap.getInProgressRequest() == null) {
70             return Boolean.FALSE;
71         }else if ( scopeOverlap.getInProgressRequest().isEmpty()){
72             return Boolean.FALSE;
73         }
74         if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfId() != null) {
75             return Boolean.TRUE;
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) {
81             return Boolean.TRUE;
82         } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId() != null) {
83             return isVserverOrVnfcIdOverLap(scopeOverlap);
84         } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfcName() != null) {
85                 return isVserverOrVnfcIdOverLap(scopeOverlap);
86         } else {
87             throw new Exception(" Action Identifier doesn't have VnfId, VfModuleId, VServerId, VnfcName ");
88         }
89     }
90
91     private boolean isVnfcNameOverLap(ScopeOverlap scopeOverlap) throws Exception {
92
93         AaiService aaiService = getAaiService(aaiClient);
94         SvcLogicContext ctx = getSvcLogicContext();
95         Map<String, String> params = new HashMap<>();
96         List<String> inProgressVServerIds = new ArrayList<>();
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());
102         try {
103             aaiService.getGenericVnfInfo(params, ctx);
104             int vmCount = Integer.parseInt(ctx.getAttribute("vm-count"));
105                 for(Request inprogressRequest:inProgressRequests){
106                     if(inprogressRequest.getActionIdentifiers().getVnfcName() != null){
107                         for (int i = 0; i < vmCount; 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);
113                             }
114                         }
115                     }
116             }
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 < vmCount; 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);
127                     }
128                 }
129             }
130             for (Request request : inProgressRequests) {
131                 if(!Strings.isNullOrEmpty(currentRequestVServerId)  && currentRequestVServerId.equalsIgnoreCase(request.getActionIdentifiers().getvServerId()))
132                     return Boolean.TRUE;
133             }
134             if(currentRequestVServerId != null)    {
135                 return  inProgressVServerIds.contains(currentRequestVServerId);
136             }
137             return Boolean.FALSE;
138         } catch (Exception e) {
139             log.debug(e.getMessage());
140             throw e;
141         }
142     }
143
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 ;
149             }
150         for (Request request : inProgressRequests) {
151             if(request.getActionIdentifiers().getVfModuleId() != null)
152                 return Boolean.TRUE ;
153         }
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 ;
158         }
159         return isVnfcNameOverLap(scopeOverlap);
160     }
161
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())))
171                 return Boolean.TRUE;
172         }
173         return Boolean.FALSE;
174     }
175
176     protected ObjectMapper getObjectMapper() {
177         return new ObjectMapper();
178     }
179
180     protected AaiService getAaiService(AAIClient aaiClient) {
181         return new AaiService(aaiClient);
182     }
183
184     protected SvcLogicContext getSvcLogicContext() {
185         return new SvcLogicContext();
186     }
187 }