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