SECARSP-268 *Implement device policies state request mechanism: update data type...
[platform/core/security/suspicious-activity-monitor.git] / server / samserver / src / main / java / com / samsung / samserver / web / rest / service / vm / UIPolicies.java
1 /**
2  * In Samsung Ukraine R&D Center (SRK under a contract between)
3  * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
5  */
6 package com.samsung.samserver.web.rest.service.vm;
7
8 import com.fasterxml.jackson.annotation.*;
9 import javax.validation.Valid;
10 import javax.validation.constraints.*;
11 import lombok.*;
12 import org.slf4j.*;
13 import java.util.*;
14
15 import static com.samsung.samserver.web.rest.service.vm.DPolicy.*;
16 import static com.samsung.samserver.web.rest.service.vm.UIPoliciesUpdate.*;
17 import static com.samsung.samserver.web.rest.service.vm.UIPolicies.InputType.*;
18
19 /**
20  * View Model for UI Policies.
21  *
22  * @author <A HREF="mailto:m.dalakov@samsung.com">Mykhailo Dalakov</A>
23  * @version 1.0
24  */
25
26 @Getter @Setter @ToString
27 public class UIPolicies {
28
29     @NotNull
30     private String group;
31
32     @Valid
33     @NotNull
34     @JsonProperty("policies")
35     private List<UIPolicy> uiPolicyList;
36
37     static final class InputType {
38         private InputType() {}
39         public static final String FLAG = "flag";
40         public static final String INTEGER = "input-number";
41         public static final String FLAG_SET = "select";
42         public static final String MULT_CHOICE = "multiselect";
43     }
44
45     @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true)
46     @JsonSubTypes({
47             @JsonSubTypes.Type(value = Flag.class, name = FLAG),
48             @JsonSubTypes.Type(value = UIInteger.class, name = INTEGER),
49             @JsonSubTypes.Type(value = FlagSet.class, name = FLAG_SET),
50             @JsonSubTypes.Type(value = MultChoice.class, name = MULT_CHOICE)
51     })
52     public interface UIPolicy {
53
54         String getName();
55         String toJSONDevice();
56         String toJSONDeviceWithoutValue();
57
58         UIPolicy bind(DPolicy dPolicy, HashMap<String, UIPolicy> map);
59         UIPolicy bind(UIPolicyUpdate uiPolicyUpdate);
60
61         String getUiname();
62         String getDescr();
63         void setUiname(String type);
64         void setDescr(String descr);
65
66         Logger log = LoggerFactory.getLogger(UIPolicy.class);
67
68         static UIPolicy make(String uiType) {
69             UIPolicy uiPolicy = null;
70             switch (uiType) {
71                 case FLAG:
72                     uiPolicy = new Flag();
73                     break;
74                 case INTEGER:
75                     uiPolicy = new UIInteger();
76                     break;
77                 case FLAG_SET:
78                     uiPolicy = new FlagSet();
79                     break;
80                 case MULT_CHOICE:
81                     uiPolicy = new MultChoice();
82                     break;
83                 default:
84                     log.debug("policy: {}","mapping not found");
85             }
86             return uiPolicy;
87         }
88     }
89
90     public static final String NAME = "\"name\":\"";
91     public static final String VALUE = "\"value\":";
92
93     @JsonRootName(FLAG)
94     @Getter @Setter @ToString
95     public static class Flag implements UIPolicy {
96         @NotNull
97         private String type;
98         @NotNull
99         private String name;
100         @NotNull
101         @Max(1) @Min(0)
102         private Integer value;
103         @NotNull
104         private String uiname;
105         @NotNull
106         private String descr;
107         @NotNull
108         private Integer state;
109         @Override
110         public UIPolicy bind(DPolicy dPolicy, HashMap<String, UIPolicy> map){
111             DPolicyInteger dPolicyInteger = (DPolicyInteger)dPolicy;
112             name = dPolicyInteger.getName() ;
113             value = dPolicyInteger.getValue();
114             state = (dPolicyInteger.getError()!=null) ? 0:1;
115             type = FLAG;
116             return this;
117         }
118         @Override
119         public UIPolicy bind(UIPolicyUpdate update){
120             FlagUpdate flagUpdate = (FlagUpdate)update;
121             name = flagUpdate.getName() ;
122             value = flagUpdate.getValue();
123             return this;
124         }
125         @Override
126         public String toJSONDevice() {
127             return  "{" +
128                     NAME + name + "\"," +
129                     VALUE + value +
130                     "}";
131         }
132         @Override
133         public String toJSONDeviceWithoutValue() {
134             return  "{" +
135                     NAME + name + "\"" +
136                     "}";
137         }
138     }
139
140     @JsonRootName(INTEGER)
141     @Getter @Setter @ToString
142     public static class UIInteger implements UIPolicy {
143         @NotNull
144         private String type;
145         @NotNull
146         private String name;
147         @Min(0)
148         private Integer value;
149         @NotNull
150         private String uiname;
151         @NotNull
152         private String descr;
153         @NotNull
154         private Integer state;
155         @Override
156         public UIInteger bind(DPolicy dPolicy, HashMap<String, UIPolicy> map){
157             DPolicyInteger dPolicyInteger = (DPolicyInteger)dPolicy;
158             name = dPolicyInteger.getName() ;
159             value = dPolicyInteger.getValue();
160             state = (dPolicyInteger.getError()!=null) ? 0:1;
161             type = INTEGER;
162             return this;
163         }
164         @Override
165         public UIPolicy bind(UIPolicyUpdate update){
166             UIIntegerUpdate uiIntegerUpdate = (UIIntegerUpdate)update;
167             name = uiIntegerUpdate.getName() ;
168             value = uiIntegerUpdate.getValue();
169             return this;
170         }
171         @Override
172         public String toJSONDevice() {
173             return  "{" +
174                     NAME + name + "\"," +
175                     VALUE + value +
176                     "}";
177         }
178         @Override
179         public String toJSONDeviceWithoutValue() {
180             return  "{" +
181                     NAME + name + "\"" +
182                     "}";
183         }
184     }
185
186     @JsonRootName(FLAG_SET)
187     @Getter @Setter @ToString
188     public static class FlagSet implements UIPolicy {
189
190         @Getter @Setter @ToString
191         @NoArgsConstructor @AllArgsConstructor
192         public static class FlagSetObject {
193             @NotNull
194             String key;
195             @NotNull
196             @Max(1) @Min(0)
197             Integer value;
198         }
199
200         @NotNull
201         private String type;
202         @NotNull
203         private String name;
204         @NotNull
205         private List<FlagSetObject> items;
206         @NotNull
207         private String uiname;
208         @NotNull
209         private String descr;
210         @NotNull
211         private Integer state;
212
213         @Override
214         public FlagSet bind(DPolicy dPolicy, HashMap<String, UIPolicy> map){
215             DPolicyIndexed dPolicyIndexed = (DPolicyIndexed)dPolicy;
216             name = dPolicyIndexed.getName();
217             state = (dPolicyIndexed.getError()!=null) ? 0:1;
218             type = FLAG_SET;
219
220             UIPolicy entry = map.get(name);
221             if (entry!=null) {
222                 items = ((FlagSet)entry).getItems();
223             } else {
224                 items = new ArrayList<>();
225                 map.put(name, this);
226             }
227             items.add(new FlagSetObject(
228                 dPolicyIndexed.getKey(), dPolicyIndexed.getValue())
229             );
230             return this;
231         }
232         @Override
233         public UIPolicy bind(UIPolicyUpdate update){
234             FlagSetUpdate flagSetUpdate = (FlagSetUpdate)update;
235             name = flagSetUpdate.getName() ;
236             items = flagSetUpdate.getItems();
237             return this;
238         }
239         @Override
240         public String toJSONDevice() {
241             int c = 0;
242             int size = items.size();
243             StringBuilder s = new StringBuilder();
244             for (FlagSetObject fso: items) {
245                 s.append("{")
246                     .append(NAME).append(name).append("\",")
247                     .append("\"key\":\"").append(fso.getKey()).append("\",")
248                     .append(VALUE).append(fso.getValue()).append("}");
249                 c++;
250                 if (c!=size) s.append(",");
251             }
252             return s.toString();
253         }
254         @Override
255         public String toJSONDeviceWithoutValue() {
256             int c = 0;
257             int size = items.size();
258             StringBuilder s = new StringBuilder();
259             for (FlagSetObject fso: items) {
260                 s.append("{")
261                         .append(NAME).append(name).append("\",")
262                         .append("\"key\":\"").append(fso.getKey()).append("\"")
263                         .append("}");
264                 c++;
265                 if (c!=size) s.append(",");
266             }
267             return s.toString();
268         }
269     }
270
271     @JsonRootName(MULT_CHOICE)
272     @Getter @Setter @ToString
273     public static class MultChoice implements UIPolicy {
274
275         static final List<String> STRINGS = new ArrayList<>(Arrays.asList(
276                 "SIMPLE_PASSWORD","","","","SOMETHING","NUMERIC","ALPHABETIC","ALPHANUMERIC"));
277
278         @Getter @Setter @ToString
279         @NoArgsConstructor @AllArgsConstructor
280         public static class MultChoiceObject {
281             @NotNull
282             String key;
283             @NotNull
284             @Max(1) @Min(0)
285             Integer value;
286         }
287
288         @NotNull
289         private String type;
290         @NotNull
291         private String name;
292         private List<MultChoiceObject> items;
293         @NotNull
294         private String uiname;
295         @NotNull
296         private String descr;
297         @NotNull
298         private Integer state;
299
300         @Override
301         public MultChoice bind(DPolicy dPolicy, HashMap<String, UIPolicy> map){
302             DPolicyInteger dPolicyInteger = (DPolicyInteger)dPolicy;
303             name = dPolicyInteger.getName();
304             state = (dPolicyInteger.getError()!=null) ? 0:1;
305             type = MULT_CHOICE;
306             items = new ArrayList<>();
307             int value = dPolicyInteger.getValue();
308             for (int i=0; i<8; i++) {
309                 items.add(new MultChoiceObject(
310                     STRINGS.get(i), value & (1 << i)
311                 ));
312             }
313             return this;
314         }
315         @Override
316         public UIPolicy bind(UIPolicyUpdate update){
317             MultChoiceUpdate multChoiceUpdate = (MultChoiceUpdate)update;
318             name = multChoiceUpdate.getName() ;
319             items = multChoiceUpdate.getItems();
320             return this;
321         }
322         @Override
323         public String toJSONDevice() {
324             Integer value = 0;
325             for (MultChoiceObject choice: items) {
326                 if (choice.value != 0) {
327                     int bit = STRINGS.indexOf(choice.key);
328                     if (bit > -1) value = value + (1 << bit);
329                 }
330             }
331             return  "{" +
332                     NAME + name + "\"," +
333                     VALUE + value +
334                     "}";
335         }
336         @Override
337         public String toJSONDeviceWithoutValue() {
338             return  "{" +
339                     NAME + name + "\"" +
340                     "}";
341         }
342     }
343
344 }