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