Update css file name in conf.py
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / NotificationUnMarshal.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.onap.policy.std;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26
27 import org.onap.policy.api.LoadedPolicy;
28 import org.onap.policy.api.RemovedPolicy;
29 import org.onap.policy.api.UpdateType;
30
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 public class NotificationUnMarshal {
34
35     private static final String EMPTY_STRING = "";
36
37     private static final String REGEX = ".(\\d)*.xml";
38
39     private NotificationUnMarshal() {
40         // Empty constructor
41     }
42
43     public static StdPDPNotification notificationJSON(final String json) throws IOException {
44         final ObjectMapper mapper = new ObjectMapper();
45         final StdPDPNotification notification = mapper.readValue(json, StdPDPNotification.class);
46         if (notification == null || notification.getLoadedPolicies() == null) {
47             return notification;
48         }
49         final Collection<StdLoadedPolicy> stdLoadedPolicies = new ArrayList<>();
50         for (final LoadedPolicy loadedPolicy : notification.getLoadedPolicies()) {
51             final StdLoadedPolicy stdLoadedPolicy = (StdLoadedPolicy) loadedPolicy;
52             if (notification.getRemovedPolicies() != null) {
53                 if (isUpdated(notification, stdLoadedPolicy)) {
54                     stdLoadedPolicy.setUpdateType(UpdateType.UPDATE);
55                 } else {
56                     stdLoadedPolicy.setUpdateType(UpdateType.NEW);
57                 }
58             } else {
59                 stdLoadedPolicy.setUpdateType(UpdateType.NEW);
60             }
61             stdLoadedPolicies.add(stdLoadedPolicy);
62         }
63         notification.setLoadedPolicies(stdLoadedPolicies);
64         return notification;
65     }
66
67     private static Boolean isUpdated(final StdPDPNotification notification, final StdLoadedPolicy stdLoadedPolicy) {
68         for (final RemovedPolicy removedPolicy : notification.getRemovedPolicies()) {
69             final String removedPolicyName = removedPolicy.getPolicyName();
70             final String loadedPolicyName = stdLoadedPolicy.getPolicyName();
71             if (removedPolicyName.replaceAll(REGEX, EMPTY_STRING)
72                     .equals(loadedPolicyName.replaceAll(REGEX, EMPTY_STRING))) {
73                 return true;
74             }
75         }
76         return false;
77     }
78 }