3b30c3e8c5332a9e2302cbd91c6a311c15fcb0a7
[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 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.interfaces.service.executorImpl;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.onap.appc.aai.client.aai.AaiService;
33 import org.onap.appc.interfaces.service.data.Request;
34 import org.onap.appc.interfaces.service.data.ScopeOverlap;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
37
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.google.common.base.Strings;
42
43 public class ServiceExecutorImpl {
44
45     private static final EELFLogger log = EELFManager.getInstance().getLogger(ServiceExecutorImpl.class);
46     private AAIClient aaiClient;
47     public String isRequestOverLap(String requestData) throws Exception {
48         String response = "\"requestOverlap\"  : ";
49         log.info("Response from ServiceExecutorImpl");
50         ScopeOverlap scopeOverlap = new ScopeOverlap();
51         ObjectMapper mapper = new ObjectMapper();
52         scopeOverlap = mapper.readValue(requestData, ScopeOverlap.class);
53         // return response + String.valueOf(checkForOverLap(scopeOverlap));
54         boolean isOverlap = checkForOverLap(scopeOverlap);
55         scopeOverlap.setOverlap(String.valueOf(isOverlap));
56         if (scopeOverlap.getOverlap() != null && scopeOverlap.getOverlap().equalsIgnoreCase("false")){
57             log.info(response + "false");
58             return response + "false";
59         }
60         else{
61             log.info(response + "true");
62             return response + "true";
63         }
64     }
65
66     private boolean checkForOverLap(ScopeOverlap scopeOverlap) throws Exception {
67         log.info("Checking for isScopeOverlap");
68         if (scopeOverlap.getInProgressRequest() == null) {
69             return Boolean.FALSE;
70         }else if ( scopeOverlap.getInProgressRequest().isEmpty()){
71             return Boolean.FALSE;
72         }
73         else if (scopeOverlap.getInProgressRequest().size() == 0) {
74             return Boolean.FALSE;
75         }
76         if (scopeOverlap.getCurrentRequest().getActionIdentifiers().getVnfId() != null) {
77             return Boolean.TRUE;
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 =new AaiService(aaiClient);
92         SvcLogicContext ctx = new SvcLogicContext();
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                             inProgressVServerIds.add(ctx.getAttribute("vm[" + i + "].vserver-id"));
109                         log.debug("Received vserver-id from AAI: "+ inProgressVServerIds);
110                     }
111                 }
112             }
113             for(Request inProgVserverIds:inProgressRequests)
114                 if(inProgVserverIds.getActionIdentifiers().getvServerId()!=null)
115                     inProgressVServerIds.add(inProgVserverIds.getActionIdentifiers().getvServerId());
116             if(currentRequestVnfcName != null){
117                 for (int i = 0; i < vm_count; i++)
118                     if (ctx.getAttribute("vm[" + i + "].vnfc-name") != null && ctx.getAttribute("vm[" + i + "].vnfc-name")
119                             .equals(currentRequestVnfcName))
120                  currentVnfcVserverId = ctx.getAttribute("vm[" + i + "].vserver-id");
121                 log.debug("Received vserver-id from AAI: "+ currentVnfcVserverId);
122                 return inProgressVServerIds.contains(currentVnfcVserverId);
123             }
124             for (Request request : inProgressRequests) {
125                 if(!Strings.isNullOrEmpty(currentRequestVServerId)  && currentRequestVServerId.equalsIgnoreCase(request.getActionIdentifiers().getvServerId()))
126                     return Boolean.TRUE;
127             }
128             if(currentRequestVServerId != null)    {
129                 return  inProgressVServerIds.contains(currentRequestVServerId);
130             }
131             return Boolean.FALSE;
132         } catch (Exception e) {
133             e.printStackTrace();
134             log.debug(e.getMessage());
135             throw e;
136         }
137     }
138
139     private boolean isVserverOrVnfcIdOverLap(ScopeOverlap scopeOverlap) throws Exception {
140         List<Request> inProgressRequests = scopeOverlap.getInProgressRequest();
141         for (Request request : inProgressRequests) {
142             if(request.getActionIdentifiers().getVnfId()!= null)
143             return Boolean.TRUE ;
144             }
145         for (Request request : inProgressRequests) {
146             if(request.getActionIdentifiers().getVfModuleId()!= null)
147                 return Boolean.TRUE ;
148         }
149         String currentVserverID = scopeOverlap.getCurrentRequest().getActionIdentifiers().getvServerId();
150         for (Request request : inProgressRequests) {
151             if(currentVserverID != null && currentVserverID.equalsIgnoreCase(request.getActionIdentifiers().getvServerId()))
152                 return Boolean.TRUE ;
153         }
154         return isVnfcNameOverLap(scopeOverlap);
155     }
156 }