a09391baf3d08ae1de9c31672b0bb3f14019ef0b
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / openecomp / 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.openecomp.policy.std;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25
26 import org.openecomp.policy.api.LoadedPolicy;
27 import org.openecomp.policy.api.RemovedPolicy;
28 import org.openecomp.policy.api.UpdateType;
29 import org.openecomp.policy.std.StdLoadedPolicy;
30 import org.openecomp.policy.std.StdPDPNotification;
31
32 import com.fasterxml.jackson.databind.ObjectMapper;
33
34 public class NotificationUnMarshal {
35         private static StdPDPNotification notification;
36         
37         public static StdPDPNotification notificationJSON(String json) throws Exception{
38                 ObjectMapper mapper = new ObjectMapper();
39                 notification = mapper.readValue(json, StdPDPNotification.class);
40                 if(notification!=null){
41                         if(notification.getLoadedPolicies()!=null){
42                                 Collection<StdLoadedPolicy> stdLoadedPolicies = new ArrayList<StdLoadedPolicy>();
43                                 for(LoadedPolicy loadedPolicy: notification.getLoadedPolicies()){
44                                         StdLoadedPolicy stdLoadedPolicy = (StdLoadedPolicy) loadedPolicy;
45                                         if(notification.getRemovedPolicies()!=null){
46                                                 Boolean updated = false;
47                                                 for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){
48                                                         String regex = ".(\\d)*.xml";
49                                                         if(removedPolicy.getPolicyName().replaceAll(regex, "").equals(stdLoadedPolicy.getPolicyName().replaceAll(regex, ""))){
50                                                                 updated  = true;
51                                                                 break;
52                                                         }
53                                                 }
54                                                 if(updated){
55                                                         stdLoadedPolicy.setUpdateType(UpdateType.UPDATE);
56                                                 }else{
57                                                         stdLoadedPolicy.setUpdateType(UpdateType.NEW);
58                                                 }
59                                         }else{
60                                                 stdLoadedPolicy.setUpdateType(UpdateType.NEW);
61                                         }
62                                         stdLoadedPolicies.add(stdLoadedPolicy);
63                                 }
64                                 notification.setLoadedPolicies(stdLoadedPolicies);
65                         }
66                 }
67                 return notification;
68         }
69 }