scopeOverlap errorfix actionrequest fr force=false
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * 
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.interfaces.service.executorImpl;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.onap.appc.aai.client.aai.AaiService;
30 import org.onap.appc.interfaces.service.data.Request;
31 import org.onap.appc.interfaces.service.data.ScopeOverlap;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
34
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.google.common.base.Strings;
39
40 public class ServiceExecutorImpl {
41
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(ServiceExecutorImpl.class);
43     private AAIClient aaiClient;
44     public String isRequestOverLap(String requestData) throws Exception {
45         String response = "\"requestOverlap\"  : ";
46         log.info("Response from ServiceExecutorImpl");
47         ScopeOverlap scopeOverlap = new ScopeOverlap();
48         ObjectMapper mapper = new ObjectMapper();
49         scopeOverlap = mapper.readValue(requestData, ScopeOverlap.class);
50         // return response + String.valueOf(checkForOverLap(scopeOverlap));
51         boolean isOverlap = checkForOverLap(scopeOverlap);
52         scopeOverlap.setOverlap(String.valueOf(isOverlap));
53         if (scopeOverlap.getOverlap() != null && scopeOverlap.getOverlap().equalsIgnoreCase("false")){
54             log.info(response + "false");
55             return response + "false";
56         }
57         else{
58             log.info(response + "true");
59             return response + "true";
60         }
61     }
62
63     private boolean checkForOverLap(ScopeOverlap scopeOverlap) throws Exception {
64         log.info("Checking for isScopeOverlap");
65         if (scopeOverlap.getInProgressRequest() == null) {
66             return Boolean.FALSE;
67         }else if ( scopeOverlap.getInProgressRequest().isEmpty()){
68             return Boolean.FALSE;
69         }
70         else if (scopeOverlap.getInProgressRequest().size() == 0) {
71             return Boolean.FALSE;
72         }
73         if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfId() != null) {
74             return Boolean.TRUE;
75         } else if (!Strings.isNullOrEmpty(scopeOverlap.getVnfId())
76                 && scopeOverlap.getInProgressRequest().size() > 0) {
77             log.info("Checking overlap for similar vnfid :" + isVnfIdOverlap(scopeOverlap));
78             return isVnfIdOverlap(scopeOverlap);
79         } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVfModuleId() != null) {
80             return Boolean.TRUE;
81         } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId() != null) {
82             return isVserverOrVnfcIdOverLap(scopeOverlap);
83         } else if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfcName() != null) {
84                 return isVserverOrVnfcIdOverLap(scopeOverlap);
85         } else {
86             throw new Exception(" Action Identifier doesn't have VnfId, VfModuleId, VServerId, VnfcName ");
87         }
88     }
89
90     private boolean isVnfcNameOverLap(ScopeOverlap scopeOverlap) throws Exception {
91         
92         AaiService aaiService =new AaiService(aaiClient);
93         SvcLogicContext ctx = new SvcLogicContext();
94         Map<String, String> params = new HashMap<String, String>();
95         List<String> inProgressVServerIds = new ArrayList<String>();
96         String currentVnfcVserverId = new String();
97         String currentRequestVnfcName = scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfcName();
98         String currentRequestVServerId = scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId();
99         List<Request> inProgressRequests = scopeOverlap.getInProgressRequest();
100         params.put("vnfId", scopeOverlap.getVnfId());
101         try {
102             aaiService.getGenericVnfInfo(params, ctx);
103             int vm_count = Integer.parseInt(ctx.getAttribute("vm-count"));
104                 for(Request inprogressRequest:inProgressRequests){    
105                     if(inprogressRequest.getActionIdentifiers().getVnfcName() != null){
106                     for (int i = 0; i < vm_count; i++){                    
107                         if (ctx.getAttribute("vm[" + i + "].vnfc-name") != null && ctx.getAttribute("vm[" + i + "].vnfc-name")
108                         .equals(inprogressRequest.getActionIdentifiers().getVnfcName()))
109                             inProgressVServerIds.add(ctx.getAttribute("vm[" + i + "].vserver-id"));
110                         log.debug("Received vserver-id from AAI: "+ inProgressVServerIds);
111                     }
112                 }
113             }
114             for(Request inProgVserverIds:inProgressRequests)
115                 if(inProgVserverIds.getActionIdentifiers().getvServerId()!=null)
116                     inProgressVServerIds.add(inProgVserverIds.getActionIdentifiers().getvServerId());
117             if(currentRequestVnfcName != null){
118                 for (int i = 0; i < vm_count; i++)
119                     if (ctx.getAttribute("vm[" + i + "].vnfc-name") != null && ctx.getAttribute("vm[" + i + "].vnfc-name")
120                             .equals(currentRequestVnfcName))
121                  currentVnfcVserverId = ctx.getAttribute("vm[" + i + "].vserver-id");
122                 log.debug("Received vserver-id from AAI: "+ currentVnfcVserverId);
123                 return inProgressVServerIds.contains(currentVnfcVserverId);
124             }
125             for (Request request : inProgressRequests) {
126                 if(!Strings.isNullOrEmpty(currentRequestVServerId)  && currentRequestVServerId.equalsIgnoreCase(request.getActionIdentifiers().getvServerId()))
127                     return Boolean.TRUE;
128             }
129             if(currentRequestVServerId != null)    {
130                 return  inProgressVServerIds.contains(currentRequestVServerId);
131             }
132             return Boolean.FALSE;
133         } catch (Exception e) {
134             e.printStackTrace();
135             log.debug(e.getMessage());
136             throw e;
137         }
138     }
139
140     private boolean isVserverOrVnfcIdOverLap(ScopeOverlap scopeOverlap) throws Exception {
141         List<Request> inProgressRequests = scopeOverlap.getInProgressRequest();
142         for (Request request : inProgressRequests) {
143             if(request.getActionIdentifiers().getVnfId()!= null)
144             return Boolean.TRUE ;
145             }
146         for (Request request : inProgressRequests) {
147             if(request.getActionIdentifiers().getVfModuleId()!= null)
148                 return Boolean.TRUE ;
149         }
150         String currentVserverID = scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId();
151         for (Request request : inProgressRequests) {
152             if(currentVserverID != null && currentVserverID.equalsIgnoreCase(request.getActionIdentifiers().getvServerId()))
153                 return Boolean.TRUE ;
154         }
155         return isVnfcNameOverLap(scopeOverlap);
156     }
157
158     private boolean isVnfIdOverlap(ScopeOverlap scopeOverlap) throws Exception {
159         List<Request> inProgressRequests = scopeOverlap.getInProgressRequest();
160         log.info("inProgressRequests list"+inProgressRequests.toString());
161         for (Request request : inProgressRequests) {
162             log.info("request list"+request.getTargetId());
163             if (!Strings.isNullOrEmpty(scopeOverlap.getVnfId())
164                     && !Strings.isNullOrEmpty(request.getTargetId())
165                     && (request.getTargetId()
166                             .equals(scopeOverlap.getVnfId())))
167                 return Boolean.TRUE;
168         }
169         return Boolean.FALSE;
170     }
171 }