SECARSP-268 +Implement device policies state request mechanism
[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.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);
59         UIPolicy bind(UIPolicyUpdate uiPolicyUpdate);
60
61         Logger log = LoggerFactory.getLogger(UIPolicy.class);
62
63         static UIPolicy make(String uiType) {
64             UIPolicy uiPolicy = null;
65             switch (uiType) {
66                 case FLAG:
67                     uiPolicy = new Flag();
68                     break;
69                 case INTEGER:
70                     uiPolicy = new UIInteger();
71                     break;
72                 case FLAG_SET:
73                     uiPolicy = new FlagSet();
74                     break;
75                 case MULT_CHOICE:
76                     uiPolicy = new MultChoice();
77                     break;
78                 default:
79                     log.debug("policy: {}","mapping not found");
80             }
81             return uiPolicy;
82         }
83     }
84
85     public static final String NAME = "\"name\":\"";
86     public static final String VALUE = "\"value\":";
87
88     @JsonRootName(FLAG)
89     @Getter @Setter @ToString
90     public static class Flag implements UIPolicy {
91         @NotNull
92         private String type;
93         @NotNull
94         private String name;
95         @NotNull
96         @Max(1) @Min(0)
97         private Integer value;
98         @NotNull
99         private String uiname;
100         @NotNull
101         private String descr;
102         @NotNull
103         private Integer state;
104         @Override
105         public UIPolicy bind(DPolicy dPolicy){
106             DPolicyInteger dPolicyInteger = (DPolicyInteger)dPolicy;
107             name = dPolicyInteger.getName() ;
108             value = dPolicyInteger.getValue();
109             state = (dPolicyInteger.getError()!=null) ? 0:1;
110             return this;
111         }
112         @Override
113         public UIPolicy bind(UIPolicyUpdate update){
114             FlagUpdate flagUpdate = (FlagUpdate)update;
115             name = flagUpdate.getName() ;
116             value = flagUpdate.getValue();
117             return this;
118         }
119         @Override
120         public String toJSONDevice() {
121             return  "{" +
122                     NAME + name + "\"," +
123                     VALUE + value +
124                     "}";
125         }
126         @Override
127         public String toJSONDeviceWithoutValue() {
128             return  "{" +
129                     NAME + name + "\"" +
130                     "}";
131         }
132     }
133
134     @JsonRootName(INTEGER)
135     @Getter @Setter @ToString
136     public static class UIInteger implements UIPolicy {
137         @NotNull
138         private String type;
139         @NotNull
140         private String name;
141         @Min(0)
142         private Integer value;
143         @NotNull
144         private String uiname;
145         @NotNull
146         private String descr;
147         @NotNull
148         private Integer state;
149         @Override
150         public UIInteger bind(DPolicy dPolicy){
151             DPolicyInteger dPolicyInteger = (DPolicyInteger)dPolicy;
152             name = dPolicyInteger.getName() ;
153             value = dPolicyInteger.getValue();
154             state = (dPolicyInteger.getError()!=null) ? 0:1;
155             return this;
156         }
157         @Override
158         public UIPolicy bind(UIPolicyUpdate update){
159             UIIntegerUpdate uiIntegerUpdate = (UIIntegerUpdate)update;
160             name = uiIntegerUpdate.getName() ;
161             value = uiIntegerUpdate.getValue();
162             return this;
163         }
164         @Override
165         public String toJSONDevice() {
166             return  "{" +
167                     NAME + name + "\"," +
168                     VALUE + value +
169                     "}";
170         }
171         @Override
172         public String toJSONDeviceWithoutValue() {
173             return  "{" +
174                     NAME + name + "\"" +
175                     "}";
176         }
177     }
178
179     @JsonRootName(FLAG_SET)
180     @Getter @Setter @ToString
181     public static class FlagSet implements UIPolicy {
182
183         static final List<String> STRINGS = new ArrayList<>(Arrays.asList("sim1","sim2"));
184
185         @Getter @Setter @ToString
186         @NoArgsConstructor @AllArgsConstructor
187         public static class FlagSetObject {
188             @NotNull
189             String key;
190             @NotNull
191             @Max(1) @Min(0)
192             Integer value;
193         }
194
195         @NotNull
196         private String type;
197         @NotNull
198         private String name;
199         @NotNull
200         private List<FlagSetObject> items;
201         @NotNull
202         private String uiname;
203         @NotNull
204         private String descr;
205         @NotNull
206         private Integer state;
207
208         @Override
209         public FlagSet bind(DPolicy dPolicy){
210
211             DPolicyIndexed dPolicyIndexed = (DPolicyIndexed)dPolicy;
212             name = dPolicyIndexed.getName();
213             state = (dPolicyIndexed.getError()!=null) ? 0:1;
214             items = new ArrayList<>();
215             int value = dPolicyIndexed.getValue();
216             for (int i=0; i<1; i++) {
217                 items.add(new FlagSetObject(
218                     STRINGS.get(i), value & (1 << i)
219                 ));
220             }
221             return this;
222         }
223         @Override
224         public UIPolicy bind(UIPolicyUpdate update){
225             FlagSetUpdate flagSetUpdate = (FlagSetUpdate)update;
226             name = flagSetUpdate.getName() ;
227             items = flagSetUpdate.getItems();
228             return this;
229         }
230         @Override
231         public String toJSONDevice() {
232             int c = 0;
233             int size = items.size();
234             StringBuilder s = new StringBuilder();
235             for (FlagSetObject fso: items) {
236                 s.append("{")
237                     .append(NAME).append(name).append("\",")
238                     .append("\"key\":\"").append(fso.getKey()).append("\",")
239                     .append(VALUE).append(fso.getValue()).append("}");
240                 c++;
241                 if (c!=size) s.append(",");
242             }
243             return s.toString();
244         }
245         @Override
246         public String toJSONDeviceWithoutValue() {
247             int c = 0;
248             int size = items.size();
249             StringBuilder s = new StringBuilder();
250             for (FlagSetObject fso: items) {
251                 s.append("{")
252                         .append(NAME).append(name).append("\",")
253                         .append("\"key\":\"").append(fso.getKey()).append("\"")
254                         .append("}");
255                 c++;
256                 if (c!=size) s.append(",");
257             }
258             return s.toString();
259         }
260     }
261
262     @JsonRootName(MULT_CHOICE)
263     @Getter @Setter @ToString
264     public static class MultChoice implements UIPolicy {
265
266         static final List<String> STRINGS = new ArrayList<>(Arrays.asList(
267                 "SIMPLE_PASSWORD","","","","SOMETHING","NUMERIC","ALPHABETIC","ALPHANUMERIC"));
268
269         @Getter @Setter @ToString
270         @NoArgsConstructor @AllArgsConstructor
271         public static class MultChoiceObject {
272             @NotNull
273             String key;
274             @NotNull
275             @Max(1) @Min(0)
276             Integer value;
277         }
278
279         @NotNull
280         private String type;
281         @NotNull
282         private String name;
283         private List<MultChoiceObject> items;
284         @NotNull
285         private String uiname;
286         @NotNull
287         private String descr;
288         @NotNull
289         private Integer state;
290
291         @Override
292         public MultChoice bind(DPolicy dPolicy){
293             DPolicyInteger dPolicyInteger = (DPolicyInteger)dPolicy;
294             name = dPolicyInteger.getName();
295             state = (dPolicyInteger.getError()!=null) ? 0:1;
296             items = new ArrayList<>();
297             int value = dPolicyInteger.getValue();
298             for (int i=0; i<8; i++) {
299                 items.add(new MultChoiceObject(
300                     STRINGS.get(i), value & (1 << i)
301                 ));
302             }
303             return this;
304         }
305         @Override
306         public UIPolicy bind(UIPolicyUpdate update){
307             MultChoiceUpdate multChoiceUpdate = (MultChoiceUpdate)update;
308             name = multChoiceUpdate.getName() ;
309             items = multChoiceUpdate.getItems();
310             return this;
311         }
312         @Override
313         public String toJSONDevice() {
314             Integer value = 0;
315             for (MultChoiceObject choice: items) {
316                 if (choice.value != 0) {
317                     int bit = STRINGS.indexOf(choice.key);
318                     if (bit > -1) value = value + (1 << bit);
319                 }
320             }
321             return  "{" +
322                     NAME + name + "\"," +
323                     VALUE + value +
324                     "}";
325         }
326         @Override
327         public String toJSONDeviceWithoutValue() {
328             return  "{" +
329                     NAME + name + "\"" +
330                     "}";
331         }
332     }
333
334 }