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