Reduce technical debt and add JUnit
[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 NotificationUnMarshal() {
36                 // Empty constructor
37         }
38         
39         public static StdPDPNotification notificationJSON(String json) throws IOException{
40                 ObjectMapper mapper = new ObjectMapper();
41                 StdPDPNotification notification = mapper.readValue(json, StdPDPNotification.class);
42                 if(notification == null || notification.getLoadedPolicies() == null){
43                         return notification;
44                 }
45             Collection<StdLoadedPolicy> stdLoadedPolicies = new ArrayList<>();
46             for(LoadedPolicy loadedPolicy: notification.getLoadedPolicies()){
47                 StdLoadedPolicy stdLoadedPolicy = (StdLoadedPolicy) loadedPolicy;
48                 if(notification.getRemovedPolicies()!=null){
49                     Boolean updated = false;
50                     for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){
51                         String regex = ".(\\d)*.xml";
52                         if(removedPolicy.getPolicyName().replaceAll(regex, "").equals(stdLoadedPolicy.getPolicyName().replaceAll(regex, ""))){
53                             updated  = true;
54                             break;
55                         }
56                     }
57                     if(updated){
58                         stdLoadedPolicy.setUpdateType(UpdateType.UPDATE);
59                     }else{
60                         stdLoadedPolicy.setUpdateType(UpdateType.NEW);
61                     }
62                 }else{
63                     stdLoadedPolicy.setUpdateType(UpdateType.NEW);
64                 }
65                 stdLoadedPolicies.add(stdLoadedPolicy);
66             }
67             notification.setLoadedPolicies(stdLoadedPolicies);
68                 return notification;
69         }
70 }