summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-core/src/main/java/org/onap/portalsdk/core/service/LdapServiceImpl.java
blob: 9f0784035f02e9dc3c48e6f722dd24f8b54120c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * ============LICENSE_START==========================================
 * ONAP Portal SDK
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.onap.portalsdk.core.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;

import org.onap.portalsdk.core.command.support.SearchResult;
import org.onap.portalsdk.core.domain.User;
import org.onap.portalsdk.core.domain.support.DomainVo;
import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.onap.portalsdk.core.service.support.FusionService;
import org.onap.portalsdk.core.service.support.ServiceLocator;
import org.onap.portalsdk.core.util.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.onap.portalsdk.core.logging.aspect.MetricsLog;

@Service("ldapService")
@Transactional
public class LdapServiceImpl extends FusionService implements LdapService {

	private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(LdapServiceImpl.class);

	@Autowired
	private ServiceLocator serviceLocator;

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public SearchResult searchPost(DomainVo searchCriteria, String sortBy1, String sortBy2, String sortBy3, int pageNo,
			int dataSize, int userId) throws NamingException {

		// initialize the directory context to access POST
		DirContext dirContext = serviceLocator.getDirContext(
				SystemProperties.getProperty(SystemProperties.POST_INITIAL_CONTEXT_FACTORY),
				SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL),
				SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL));

		SearchResult searchResult = new SearchResult();

		String[] postAttributes = { "nickname", "givenName", "initials", "sn", "employeeNumber", "mail",
				"telephoneNumber", "departmentNumber", "a1", "street", "roomNumber", "l", "st", "postalCode", "zip4",
				"physicalDeliveryOfficeName", "bc", "friendlyCountryName", "bd", "bdname", "bu", "buname", "jtname",
				"mgrid", "a2", "compcode", "compdesc", "costcenter", "silo", "b2" };

		SearchControls searchControls = new SearchControls();
		searchControls.setTimeLimit(5000);
		searchControls.setReturningAttributes(postAttributes);

		StringBuilder filterClause = new StringBuilder("(&(objectClass=*)");
		User user = (User) searchCriteria;

		if (Utilities.nvl(user.getFirstName()).length() > 0) {
			filterClause.append("(givenName=").append(user.getFirstName()).append("*)");
		}
		if (Utilities.nvl(user.getLastName()).length() > 0) {
			filterClause.append("(sn=").append(user.getLastName()).append("*)");
		}
		if (Utilities.nvl(user.getHrid()).length() > 0) {
			filterClause.append("(employeeNumber=").append(user.getHrid()).append("*)");
		}
		if (Utilities.nvl(user.getOrgManagerUserId()).length() > 0) {
			filterClause.append("(mgrid=").append(user.getOrgManagerUserId()).append("*)");
		}
		if (Utilities.nvl(user.getOrgCode()).length() > 0) {
			filterClause.append("(departmentNumber=").append(user.getOrgCode()).append("*)");
		}
		if (Utilities.nvl(user.getEmail()).length() > 0) {
			filterClause.append("(mail=").append(user.getEmail()).append("*)");
		}
		if (Utilities.nvl(user.getOrgUserId()).length() > 0) {
			filterClause.append("(a1=").append(user.getOrgUserId()).append("*)");
		}
		filterClause.append("(c3=N)"); // this has been added to filter CP09 entries on the LDAP server that are
										// duplicates of existing individuals
		filterClause.append(")");

		List list = new ArrayList();
		if (!"(&(objectClass=*))".equals(filterClause.toString())) {
			NamingEnumeration e = dirContext.search(
					SystemProperties.getProperty(SystemProperties.POST_PROVIDER_URL) + "/"
							+ SystemProperties.getProperty(SystemProperties.POST_SECURITY_PRINCIPAL),
					filterClause.toString(), searchControls);

			list = processResults(e);
		}

		Collections.sort(list);

		searchResult = new SearchResult(list);
		searchResult.setPageNo(pageNo);
		if (dataSize >= 0) {
			searchResult.setDataSize(dataSize);
		} else {
			searchResult.setDataSize(list.size());
		} // else

		dirContext.close();

		return searchResult;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@MetricsLog
	private ArrayList processResults(NamingEnumeration e) throws NamingException {
		ArrayList results = new ArrayList();
		int count = 0;

		while (e.hasMore()) {
			javax.naming.directory.SearchResult searchResult = (javax.naming.directory.SearchResult) e.next();
			results.add(processAttributes(searchResult.getAttributes()));
			count++;

			if (count > Integer.parseInt(SystemProperties.getProperty(SystemProperties.POST_MAX_RESULT_SIZE))) {
				break;
			}

		}

		return results;
	}

	@SuppressWarnings("rawtypes")
	@MetricsLog
	private DomainVo processAttributes(Attributes resultAttributes) {
		User user = new User();
		try {
			if (resultAttributes == null) {
				logger.debug(EELFLoggerDelegate.debugLogger, "processAttributes: no attributes");
			} else {
				for (NamingEnumeration e = resultAttributes.getAll(); e.hasMore();) { // why the nested loop?
					Attribute attribute = (Attribute) e.next();
					for (NamingEnumeration ie = attribute.getAll(); ie.hasMore();) {
						if (attribute.getID().equalsIgnoreCase("nickname")) {
							user.setFirstName((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("initials")) {
							user.setMiddleInitial((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("sn")) {
							user.setLastName((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("employeeNumber")) {
							user.setHrid((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("mail")) {
							user.setEmail((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("telephoneNumber")) {
							user.setPhone((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("departmentNumber")) {
							user.setOrgCode((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("a1")) {
							user.setOrgUserId((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("street")) {
							user.setAddress1((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("roomNumber")) {
							user.setAddress2((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("l")) {
							user.setCity((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("st")) {
							user.setState((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("postalCode")) {
							user.setZipCode((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("zip4")) {
							user.setZipCodeSuffix((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("physicalDeliveryOfficeName")) {
							user.setLocationClli((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("bc")) {
							user.setBusinessCountryCode((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("friendlyCountryName")) {
							user.setBusinessCountryName((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("bd")) {
							user.setDepartment((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("bdname")) {
							user.setDepartmentName((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("jtname")) {
							user.setJobTitle((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("mgrid")) {
							user.setOrgManagerUserId((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("a2")) {
							user.setCommandChain((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("compcode")) {
							user.setCompanyCode((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("compdesc")) {
							user.setCompany((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("bu")) {
							user.setBusinessUnit((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("buname")) {
							user.setBusinessUnitName((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("silo")) {
							user.setSiloStatus((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("costcenter")) {
							user.setCostCenter((String) ie.next());
						} else if (attribute.getID().equalsIgnoreCase("b2")) {
							user.setFinancialLocCode((String) ie.next());
						} else { // we don't care about returned attribute, let's move on
							ie.next();
						}

					}
				}
			}
		} catch (NamingException e) {
			logger.error(EELFLoggerDelegate.errorLogger,
					"An error occurred while processing the following user from POST with an ORGUSERID of "
							+ user.getOrgUserId() + e.getMessage());
		}

		return user;

	}

}