-
Notifications
You must be signed in to change notification settings - Fork 477
/
ApplicationServiceImpl.java
295 lines (257 loc) · 12.3 KB
/
ApplicationServiceImpl.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file 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.
*/
package org.kaaproject.kaa.server.common.dao.service;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.kaaproject.kaa.server.common.dao.impl.DaoUtil.convertDtoList;
import static org.kaaproject.kaa.server.common.dao.impl.DaoUtil.getDto;
import static org.kaaproject.kaa.server.common.dao.service.Validator.isValidSqlId;
import static org.kaaproject.kaa.server.common.dao.service.Validator.isValidSqlObject;
import org.apache.commons.lang.RandomStringUtils;
import org.kaaproject.kaa.common.Constants;
import org.kaaproject.kaa.common.dto.ApplicationDto;
import org.kaaproject.kaa.common.dto.ConfigurationDto;
import org.kaaproject.kaa.common.dto.ConfigurationSchemaDto;
import org.kaaproject.kaa.common.dto.EndpointGroupDto;
import org.kaaproject.kaa.common.dto.EndpointProfileSchemaDto;
import org.kaaproject.kaa.common.dto.KaaAuthorityDto;
import org.kaaproject.kaa.common.dto.NotificationSchemaDto;
import org.kaaproject.kaa.common.dto.NotificationTypeDto;
import org.kaaproject.kaa.common.dto.ServerProfileSchemaDto;
import org.kaaproject.kaa.common.dto.ctl.CtlSchemaDto;
import org.kaaproject.kaa.common.dto.logs.LogSchemaDto;
import org.kaaproject.kaa.server.common.dao.ApplicationService;
import org.kaaproject.kaa.server.common.dao.CTLService;
import org.kaaproject.kaa.server.common.dao.ConfigurationService;
import org.kaaproject.kaa.server.common.dao.EndpointService;
import org.kaaproject.kaa.server.common.dao.LogSchemaService;
import org.kaaproject.kaa.server.common.dao.NotificationService;
import org.kaaproject.kaa.server.common.dao.ProfileService;
import org.kaaproject.kaa.server.common.dao.ServerProfileService;
import org.kaaproject.kaa.server.common.dao.TopicService;
import org.kaaproject.kaa.server.common.dao.exception.IncorrectParameterException;
import org.kaaproject.kaa.server.common.dao.impl.ApplicationDao;
import org.kaaproject.kaa.server.common.dao.impl.UserDao;
import org.kaaproject.kaa.server.common.dao.model.sql.Application;
import org.kaaproject.kaa.server.common.dao.model.sql.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
@Service
@Transactional
public class ApplicationServiceImpl implements ApplicationService {
private static final Logger LOG = LoggerFactory.getLogger(ApplicationServiceImpl.class);
private static final String GROUP_ALL = "All";
private static final String DEFAULT_FILTER_BODY = "true";
private static final String DEFAULT_CONFIGURATION_SCHEMA_FILE = "/default_configuration_schema.avsc";
private static final String DEFAULT_NOTIFICATION_SCHEMA_FILE = "/default_notification_schema.avsc";
private static final String DEFAULT_LOG_SCHEMA_FILE = "/default_log_schema.avsc";
private static final String DEFAULT_SCHEMA_NAME = "Generated";
private static final String DEFAULT_CREDENTIALS_SERVICE_NAME = "Trustful";
@Autowired
private ApplicationDao<Application> applicationDao;
@Autowired
private EndpointService endpointService;
@Autowired
private CTLService ctlService;
@Autowired
private ConfigurationService configurationService;
@Autowired
private ProfileService profileService;
@Autowired
private ServerProfileService serverProfileService;
@Autowired
private NotificationService notificationService;
@Autowired
private LogSchemaService logSchemaService;
@Autowired
private TopicService topicService;
@Autowired
private UserDao<User> userDao;
@Override
public List<ApplicationDto> findAppsByTenantId(String tenantId) {
List<ApplicationDto> applicationDto;
if (isValidSqlId(tenantId)) {
LOG.debug("Find applications by tenant id [{}]", tenantId);
applicationDto = convertDtoList(applicationDao.findByTenantId(tenantId));
} else {
throw new IncorrectParameterException("Incorrect tenant id: " + tenantId);
}
return applicationDto;
}
@Override
public void removeAppsByTenantId(String tenantId) {
LOG.debug("Remove applications by tenant id [{}]", tenantId);
if (isValidSqlId(tenantId)) {
List<ApplicationDto> applications = findAppsByTenantId(tenantId);
for (ApplicationDto application : applications) {
removeAppById(application.getId());
}
}
}
@Override
public ApplicationDto findAppById(String id) {
ApplicationDto applicationDto = null;
if (isValidSqlId(id)) {
applicationDto = getDto(applicationDao.findById(id));
}
return applicationDto;
}
@Override
public void removeAppById(String id) {
if (isValidSqlId(id)) {
removeCascadeApplication(id);
}
}
@Override
public ApplicationDto findAppByApplicationToken(String applicationToken) {
ApplicationDto applicationDto = null;
if (isNotBlank(applicationToken)) {
applicationDto = getDto(applicationDao.findByApplicationToken(applicationToken));
}
return applicationDto;
}
@Override
public ApplicationDto saveApp(ApplicationDto applicationDto) {
ApplicationDto appDto = null;
if (isValidSqlObject(applicationDto)) {
if (isBlank(applicationDto.getName())) {
throw new IncorrectParameterException("Can't save/update application with null name");
}
Application checkApplication = applicationDao.findByNameAndTenantId(applicationDto.getName(), applicationDto.getTenantId());
if (checkApplication != null && !Objects.equals(checkApplication.getStringId(), applicationDto.getId())) {
throw new IncorrectParameterException("Can't save application with same name within one tenant");
}
if (isBlank(applicationDto.getCredentialsServiceName())) {
applicationDto.setCredentialsServiceName(DEFAULT_CREDENTIALS_SERVICE_NAME);
}
if (isNotBlank(applicationDto.getId())) {
LOG.debug("Update application with id [{}]", applicationDto.getId());
return getDto(applicationDao.save(new Application(applicationDto)));
}
String appToken = RandomStringUtils.randomNumeric(Constants.APP_TOKEN_SIZE);
applicationDto.setApplicationToken(appToken);
Application application = new Application(applicationDto);
appDto = getDto(applicationDao.save(application));
if (appDto != null) {
String appId = appDto.getId();
List<User> users = userDao.findByTenantIdAndAuthority(appDto.getTenantId(), KaaAuthorityDto.TENANT_ADMIN.name());
String createdUsername = null;
if (!users.isEmpty()) {
createdUsername = users.get(0).getUsername();
}
LOG.debug("Saved application with id [{}]", appId);
EndpointGroupDto groupDto = createDefaultGroup(appId, createdUsername);
if (groupDto != null) {
String groupId = groupDto.getId();
LOG.debug("Saved endpoint group with id [{}]", groupId);
EndpointProfileSchemaDto profileSchema = createDefaultProfileSchema(appId, createdUsername);
ConfigurationDto configuration = createDefaultConfigurationWithSchema(appId, groupId, createdUsername);
if (profileSchema == null || configuration == null) {
LOG.warn("Got error during creation application. Deleted application with id [{}]", appId);
removeCascadeApplication(appId);
}
LOG.debug("Creating default server profile schema");
createDefaultServerProfileSchema(appId, createdUsername);
LOG.debug("Creating default notification schema");
createDefaultNotificationSchema(appId, createdUsername);
LOG.debug("Creating default log schema");
createDefaultLogSchema(appId, createdUsername);
} else {
LOG.warn("Cant save default group for application with id [{}]", appId);
removeCascadeApplication(appId);
}
}
LOG.debug("Inserted new application with");
}
return appDto;
}
private EndpointGroupDto createDefaultGroup(String appId, String createdUsername) {
EndpointGroupDto endpointGroup = new EndpointGroupDto();
endpointGroup.setName(GROUP_ALL);
endpointGroup.setCreatedUsername(createdUsername);
endpointGroup.setApplicationId(appId);
return endpointService.saveEndpointGroup(endpointGroup);
}
private EndpointProfileSchemaDto createDefaultProfileSchema(String appId, String createdUsername) {
EndpointProfileSchemaDto profileSchemaDto = new EndpointProfileSchemaDto();
profileSchemaDto.setApplicationId(appId);
CtlSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername);
profileSchemaDto.setCtlSchemaId(ctlSchema.getId());
profileSchemaDto.setName(DEFAULT_SCHEMA_NAME);
profileSchemaDto.setCreatedUsername(createdUsername);
profileSchemaDto = profileService.saveProfileSchema(profileSchemaDto);
if (profileSchemaDto == null) {
throw new RuntimeException("Can't save default profile schema "); //NOSONAR
}
return profileSchemaDto;
}
private ConfigurationDto createDefaultConfigurationWithSchema(String appId, String groupId, String createdUsername) {
ConfigurationSchemaDto schema = new ConfigurationSchemaDto();
schema.setApplicationId(appId);
CtlSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername);
schema.setCtlSchemaId(ctlSchema.getId());
schema.setName(DEFAULT_SCHEMA_NAME);
schema.setCreatedUsername(createdUsername);
ConfigurationSchemaDto savedSchema = configurationService.saveConfSchema(schema, groupId);
ConfigurationDto config = configurationService.findConfigurationByAppIdAndVersion(savedSchema.getApplicationId(), savedSchema.getVersion());
if (config == null) {
throw new RuntimeException("Can't find default configuration by schema id " + savedSchema.getId()); //NOSONAR
} else {
return config;
}
}
private ServerProfileSchemaDto createDefaultServerProfileSchema(String appId, String createdUsername) {
ServerProfileSchemaDto serverProfileSchemaDto = new ServerProfileSchemaDto();
serverProfileSchemaDto.setApplicationId(appId);
CtlSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername);
serverProfileSchemaDto.setCtlSchemaId(ctlSchema.getId());
serverProfileSchemaDto.setName(DEFAULT_SCHEMA_NAME);
serverProfileSchemaDto.setCreatedUsername(createdUsername);
return serverProfileService.saveServerProfileSchema(serverProfileSchemaDto);
}
private NotificationSchemaDto createDefaultNotificationSchema(String appId, String createdUsername) {
NotificationSchemaDto notificationSchemaDto = new NotificationSchemaDto();
notificationSchemaDto.setApplicationId(appId);
CtlSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername);
notificationSchemaDto.setCtlSchemaId(ctlSchema.getId());
notificationSchemaDto.setName(DEFAULT_SCHEMA_NAME);
notificationSchemaDto.setCreatedUsername(createdUsername);
notificationSchemaDto.setType(NotificationTypeDto.USER);
notificationSchemaDto = notificationService.saveNotificationSchema(notificationSchemaDto);
if (notificationSchemaDto == null) {
throw new RuntimeException("Can't save default notification schema "); //NOSONAR
}
return notificationSchemaDto;
}
private LogSchemaDto createDefaultLogSchema(String appId, String createdUsername) {
LogSchemaDto schema = new LogSchemaDto();
schema.setApplicationId(appId);
CtlSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername);
schema.setCtlSchemaId(ctlSchema.getId());
schema.setName(DEFAULT_SCHEMA_NAME);
schema.setCreatedUsername(createdUsername);
schema = logSchemaService.saveLogSchema(schema);
return schema;
}
private void removeCascadeApplication(String id) {
applicationDao.removeById(id);
}
}