nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / service / EPLdapServiceImpl.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 import javax.naming.NamingEnumeration;
27 import javax.naming.NamingException;
28 import javax.naming.directory.Attribute;
29 import javax.naming.directory.Attributes;
30 import javax.naming.directory.DirContext;
31 import javax.naming.directory.SearchControls;
32
33 import org.openecomp.portalapp.portal.domain.EPUser;
34 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
35 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
36 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
37 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
38 import org.openecomp.portalsdk.core.command.support.SearchResult;
39 import org.openecomp.portalsdk.core.domain.support.DomainVo;
40 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
41 import org.openecomp.portalsdk.core.service.support.FusionService;
42 import org.openecomp.portalsdk.core.service.support.ServiceLocator;
43 import org.openecomp.portalsdk.core.util.SystemProperties;
44 import org.slf4j.MDC;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.context.annotation.EnableAspectJAutoProxy;
47 import org.springframework.stereotype.Service;
48 import org.springframework.transaction.annotation.Transactional;
49
50 @Service("epLdapService")
51 @Transactional
52 @org.springframework.context.annotation.Configuration
53 @EnableAspectJAutoProxy
54 public class EPLdapServiceImpl extends FusionService implements EPLdapService {
55         @Autowired
56         private ServiceLocator serviceLocator;
57         
58         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLdapServiceImpl.class);
59
60         @EPAuditLog
61     @SuppressWarnings({ "rawtypes", "unchecked" })
62         public SearchResult searchPost(DomainVo searchCriteria, String sortBy1, String sortBy2, String sortBy3, 
63                 int pageNo, int dataSize, int orgUserId) throws Exception {
64
65         String remoteHost = "";
66                         
67                 // initialize the directory context to access POST
68         DirContext dirContext = serviceLocator.getDirContext(SystemProperties.getProperty(SystemProperties.POST_INITIAL_CONTEXT_FACTORY),
69                                                                   SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
70                                                                   SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
71         
72         SearchResult searchResult = new SearchResult();
73
74         try {
75         
76                 remoteHost = String.format("%s/%s", SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL), 
77                                                                         SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));
78                 MDC.put(EPSystemProperties.FULL_URL, remoteHost);
79                 
80           String[] postAttributes =  {"nickname","givenName","initials","sn","employeeNumber","mail","telephoneNumber",
81                                 "departmentNumber","a1","street","roomNumber","l","st","postalCode","zip4","physicalDeliveryOfficeName","bc",
82                                 "friendlyCountryName","bd","bdname","bu","buname","jtname","mgrid","a2","compcode","compdesc",
83                                 "costcenter","silo","b2"};
84
85           SearchControls searchControls = new SearchControls();
86           searchControls.setTimeLimit(5000);
87           searchControls.setReturningAttributes(postAttributes);
88
89           StringBuffer filterClause = new StringBuffer("(&(objectClass=*)");
90
91           EPUser user = (EPUser)searchCriteria;
92
93           if(Utilities.nvl(user.getFirstName()).length() > 0) {
94             filterClause.append("(givenName=").append(user.getFirstName()).append("*)");
95           }
96           if(Utilities.nvl(user.getLastName()).length() > 0) {
97             filterClause.append("(sn=").append(user.getLastName()).append("*)");
98           }
99           if(Utilities.nvl(user.getHrid()).length() > 0) {
100             filterClause.append("(employeeNumber=").append(user.getHrid()).append("*)");
101           }
102           if(Utilities.nvl(user.getOrgManagerUserId()).length() > 0) {
103               filterClause.append("(mgrid=").append(user.getOrgManagerUserId()).append("*)");
104             }
105           if(Utilities.nvl(user.getOrgCode()).length() > 0) {
106             filterClause.append("(departmentNumber=").append(user.getOrgCode()).append("*)");
107           }
108           if(Utilities.nvl(user.getEmail()).length() > 0) {
109             filterClause.append("(mail=").append(user.getEmail()).append("*)");
110           }
111           if(Utilities.nvl(user.getOrgUserId()).length() > 0) {
112             filterClause.append("(a1=").append(user.getOrgUserId()).append("*)");
113           }
114           filterClause.append("(c3=N)"); // this has been added to filter CP09 entries on the LDAP server that are duplicates of existing individuals
115           filterClause.append(")");
116
117           List list = new ArrayList();
118           if (!filterClause.toString().equals("(&(objectClass=*))")) {
119                   NamingEnumeration e = dirContext.search(SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL) + "/" +
120                                                       SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL),
121                                                       filterClause.toString(),
122                                                       searchControls);
123                   list = processResults(e);
124           }
125           
126           Collections.sort(list);
127
128           searchResult = new SearchResult(list);
129           searchResult.setPageNo(pageNo);
130           if(dataSize >= 0) {
131             searchResult.setDataSize(dataSize);
132           }
133           else {
134             searchResult.setDataSize(list.size());
135           }
136         } catch(NamingException ne) {
137                 String stackTrace = EcompPortalUtils.getStackTrace(ne);
138                 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the LDAP search. Details: " + stackTrace);
139         } catch(Exception e) {
140                 String stackTrace = EcompPortalUtils.getStackTrace(e);
141                 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing the LDAP search. Details: " + stackTrace);
142         }
143         finally {
144                 dirContext.close();
145         }
146
147         return searchResult;
148     }
149
150
151     @SuppressWarnings({ "rawtypes", "unchecked" })
152     @EPMetricsLog
153         private ArrayList processResults(NamingEnumeration e) throws NamingException {
154       ArrayList results = new ArrayList();
155       int count = 0;
156
157       while (e.hasMore()) {
158         javax.naming.directory.SearchResult searchResult = (javax.naming.directory.SearchResult)e.next();
159         results.add(processAttributes(searchResult.getAttributes()));
160         count++;
161
162         if(count > Integer.parseInt(SystemProperties.getProperty(SystemProperties.POST_MAX_RESULT_SIZE))) {
163           break;
164         }
165       }
166       return results;
167     }
168
169
170     @SuppressWarnings("rawtypes")
171     @EPMetricsLog
172         private DomainVo processAttributes(Attributes resultAttributes) throws NamingException {
173       EPUser user = new EPUser();
174
175       try {
176         if (resultAttributes == null) {
177           logger.debug(EELFLoggerDelegate.debugLogger, "This result has no attributes");
178         } else {
179           for (NamingEnumeration e = resultAttributes.getAll(); e.hasMore();) { //why the nested loop?
180             Attribute attribute = (Attribute)e.next();
181             for (NamingEnumeration ie = attribute.getAll(); ie.hasMore();) {
182               if (attribute.getID().equalsIgnoreCase("nickname")) {
183                   user.setFirstName((String) ie.next());
184               }
185               else if (attribute.getID().equalsIgnoreCase("initials")) {
186                   user.setMiddleInitial((String) ie.next());
187               }
188               else if (attribute.getID().equalsIgnoreCase("sn")) {
189                   user.setLastName((String) ie.next());
190               }
191               else if (attribute.getID().equalsIgnoreCase("employeeNumber")) {
192                   user.setHrid((String) ie.next());
193               }
194               else if (attribute.getID().equalsIgnoreCase("mail")) {
195                   user.setEmail((String) ie.next());
196               }
197               else if (attribute.getID().equalsIgnoreCase("telephoneNumber")) {
198                   user.setPhone((String) ie.next());
199               }
200               else if (attribute.getID().equalsIgnoreCase("departmentNumber")) {
201                   user.setOrgCode((String) ie.next());
202               }
203               else if (attribute.getID().equalsIgnoreCase("a1")) {
204                   user.setOrgUserId((String) ie.next());
205               }
206               else if (attribute.getID().equalsIgnoreCase("street")) {
207                   user.setAddress1((String) ie.next());
208               }
209               else if (attribute.getID().equalsIgnoreCase("roomNumber")) {
210                   user.setAddress2((String) ie.next());
211               }
212               else if (attribute.getID().equalsIgnoreCase("l")) {
213                   user.setCity((String) ie.next());
214               }
215               else if (attribute.getID().equalsIgnoreCase("st")) {
216                   user.setState((String) ie.next());
217               }
218               else if (attribute.getID().equalsIgnoreCase("postalCode")) {
219                   user.setZipCode((String) ie.next());
220               }
221               else if (attribute.getID().equalsIgnoreCase("zip4")) {
222                   user.setZipCodeSuffix((String) ie.next());
223               }
224               else if (attribute.getID().equalsIgnoreCase("physicalDeliveryOfficeName")) {
225                   user.setLocationClli((String) ie.next());
226               }
227               else if (attribute.getID().equalsIgnoreCase("bc")) {
228                   user.setBusinessCountryCode((String) ie.next());
229               }
230               else if (attribute.getID().equalsIgnoreCase("friendlyCountryName")) {
231                   user.setBusinessCountryName((String) ie.next());
232               }
233               else if (attribute.getID().equalsIgnoreCase("bd")) {
234                   user.setDepartment((String) ie.next());
235               }
236               else if (attribute.getID().equalsIgnoreCase("bdname")) {
237                   user.setDepartmentName((String) ie.next());
238               }
239               else if (attribute.getID().equalsIgnoreCase("jtname")) {
240                   user.setJobTitle((String) ie.next());
241               }
242               else if (attribute.getID().equalsIgnoreCase("mgrid")) {
243                   user.setOrgManagerUserId((String) ie.next());
244               }
245               else if (attribute.getID().equalsIgnoreCase("a2")) {
246                   user.setCommandChain((String) ie.next());
247               }
248               else if (attribute.getID().equalsIgnoreCase("compcode")) {
249                   user.setCompanyCode((String) ie.next());
250               }
251               else if (attribute.getID().equalsIgnoreCase("compdesc")) {
252                   user.setCompany((String) ie.next());
253               }
254               else if (attribute.getID().equalsIgnoreCase("bu")) {
255                   user.setBusinessUnit((String)ie.next());
256               }
257               else if (attribute.getID().equalsIgnoreCase("buname")) {
258                   user.setBusinessUnitName((String)ie.next());
259               }
260               else if (attribute.getID().equalsIgnoreCase("silo")) {
261                   user.setSiloStatus((String)ie.next());
262               }
263               else if (attribute.getID().equalsIgnoreCase("costcenter")) {
264                   user.setCostCenter((String)ie.next());
265               }
266               else if (attribute.getID().equalsIgnoreCase("b2")) {
267                   user.setFinancialLocCode((String)ie.next());
268               }
269               else { //we don't care about returned attribute, let's move on
270                 ie.next();
271               }
272
273             }
274           }
275         }
276       } catch (NamingException e) {
277           String stackTrace = EcompPortalUtils.getStackTrace(e);
278           logger.error(EELFLoggerDelegate.errorLogger, "An error occurred while processing the following user from POST with an userId of " + user.getOrgUserId());
279           logger.error(EELFLoggerDelegate.errorLogger, "Exception occcurred while processing LDAP search results, Details: " + stackTrace);
280       } catch(Exception e) {
281           String stackTrace = EcompPortalUtils.getStackTrace(e);
282           logger.error(EELFLoggerDelegate.errorLogger, "Exception occcurred while processing LDAP search results, Details: " + stackTrace);
283       }
284       
285       return user;
286     }
287 }