e9ba3200c8628e13f2c31950f62984e3dc89885f
[platform/core/security/suspicious-activity-monitor.git] / server / samserver / src / main / java / com / samsung / samserver / web / rest / service / vm / DPolicy.java
1 package com.samsung.samserver.web.rest.service.vm;
2
3 import com.fasterxml.jackson.core.JsonParser;
4 import com.fasterxml.jackson.core.type.TypeReference;
5 import com.fasterxml.jackson.databind.*;
6 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
7 import com.samsung.samserver.domain.PolicyType;
8 import com.samsung.samserver.service.PolicyTypeService;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.json.*;
13 import lombok.*;
14
15 import javax.validation.constraints.NotNull;
16 import java.io.IOException;
17 import java.util.*;
18
19 public interface DPolicy {
20
21     String getName();
22
23     @Getter @Setter @ToString
24     class DPolicyInteger implements DPolicy {
25         @NotNull
26         private String name;
27         @NotNull
28         private Integer value;
29     }
30
31     @Getter @Setter @ToString
32     class DPolicyIndexed implements DPolicy {
33         @NotNull
34         private String name;
35         @NotNull
36         private String key;
37         @NotNull
38         private Integer value;
39     }
40
41     class DPoliciesDeserializer extends StdDeserializer {
42
43         private final transient Logger log = LoggerFactory.getLogger(DPoliciesDeserializer.class);
44
45         public DPoliciesDeserializer(){
46             this(null);
47         }
48
49         public DPoliciesDeserializer(Class<?> c){
50             super(c);
51         }
52
53         @Autowired
54         private transient PolicyTypeService policyTypeService;
55
56         @Override
57         public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
58             ObjectMapper mapper = (ObjectMapper) jsonParser.getCodec();
59             JsonNode root = jsonParser.getCodec().readTree(jsonParser);
60             List<DPolicy> policies = new ArrayList<>();
61             try {
62                 Map<String, PolicyType> policyTypeMap = policyTypeService.findAllAsMap();
63                 JSONArray ja = new JSONArray(root.toString());
64                 for (int i = 0; i < ja.length(); i++) {
65                     JSONObject jo = ja.getJSONObject(i);
66                     String key = jo.get("name").toString();
67                     String mapping = policyTypeMap.get(key).getMapping();
68                     DPolicy dPolicyProp = null;
69                     switch (mapping) {
70                         case "integer":
71                             dPolicyProp = mapper.readValue(jo.toString(), new TypeReference<DPolicyInteger>(){});
72                             break;
73                         case "indexed":
74                             dPolicyProp = mapper.readValue(jo.toString(), new TypeReference<DPolicyIndexed>(){});
75                             break;
76                         default:
77                             log.debug("deserialize: {}","mapping not found");
78                     }
79                     policies.add(dPolicyProp);
80                 }
81             } catch (JSONException e) {
82                 log.error("deserialize: message: {} stacktrace: {}", e.getMessage(), e.getStackTrace());
83             }
84             return policies;
85         }
86     }
87
88 }