Update vulnerable package dependencies
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / auditing / impl / AuditBaseEventFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.auditing.impl;
21
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Objects;
26 import org.apache.commons.lang3.StringUtils;
27 import org.javatuples.Pair;
28 import org.openecomp.sdc.be.auditing.api.AuditEventFactory;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
31 import org.openecomp.sdc.common.api.Constants;
32
33 public abstract class AuditBaseEventFactory implements AuditEventFactory {
34
35     private AuditingActionEnum action;
36
37     protected AuditBaseEventFactory(AuditingActionEnum action) {
38         this.action = Objects.requireNonNull(action);
39     }
40
41     protected AuditBaseEventFactory() {
42     }
43
44     public static String buildUserNameExtended(User user) {
45         if (user == null) {
46             return null;
47         }
48         StringBuilder builder = new StringBuilder();
49         appendIfNotEmpty(user.getUserId(), builder);
50         String firstName = replaceNullNameWithEmpty(user.getFirstName());
51         String lastName = replaceNullNameWithEmpty(user.getLastName());
52         if (appendIfNotEmpty(firstName, builder)) {
53             appendIfNotEmpty(lastName, builder, " ");
54         } else {
55             appendIfNotEmpty(lastName, builder);
56         }
57         appendIfNotEmpty(user.getEmail(), builder);
58         appendIfNotEmpty(user.getRole(), builder);
59         return builder.toString();
60     }
61
62     private static boolean appendIfNotEmpty(String value, StringBuilder builder) {
63         return appendIfNotEmpty(value, builder, ", ");
64     }
65
66     protected static boolean appendIfNotEmpty(String value, StringBuilder builder, String delimiter) {
67         if (StringUtils.isEmpty(value)) {
68             return false;
69         }
70         addDelimiterIfNeeded(builder, delimiter);
71         builder.append(value);
72         return true;
73     }
74
75     private static void addDelimiterIfNeeded(StringBuilder builder, String delimiter) {
76         if (builder.length() > 0) {
77             builder.append(delimiter);
78         }
79     }
80
81     protected static String buildUserName(User user) {
82         if (user == null || StringUtils.isEmpty(user.getUserId())) {
83             return StringUtils.EMPTY;
84         }
85         StringBuilder sb = new StringBuilder();
86         String firstName = replaceNullNameWithEmpty(user.getFirstName());
87         if (!firstName.isEmpty()) {
88             sb.append(firstName);
89             sb.append(" ");
90         }
91         String lastName = replaceNullNameWithEmpty(user.getLastName());
92         if (!lastName.isEmpty()) {
93             sb.append(lastName);
94         }
95         sb.append("(").append(user.getUserId()).append(")");
96         return sb.toString();
97     }
98
99     private static String buildValue(String value) {
100         if (value == null) {
101             return StringUtils.EMPTY;
102         }
103         return value;
104     }
105
106     protected static String replaceNullNameWithEmpty(String name) {
107         if (name != null && !name.trim().contains(Constants.NULL_STRING)) {
108             return name;
109         }
110         return StringUtils.EMPTY;
111     }
112
113     public AuditingActionEnum getAction() {
114         return action;
115     }
116
117     @Override
118     //TODO implement in derived classes for ci testing
119     public List<Pair<String, String>> getQueryParams() {
120         return Collections.emptyList();
121     }
122
123     @Override
124     public final String getLogMessage() {
125         return String.format(getLogPattern(), getLogArgs());
126     }
127
128     private Object[] getLogArgs() {
129         return Arrays.stream(getLogMessageParams()).map(AuditBaseEventFactory::buildValue).toArray(String[]::new);
130     }
131
132     public abstract String getLogPattern();
133
134     public abstract String[] getLogMessageParams();
135 }