Removing jackson to mitigate cve-2017-4995
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / AbstractManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.google.gson.Gson;
19 import io.reactivex.Observable;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import org.onap.aai.model.Relationship;
24 import org.onap.aai.model.RelationshipData;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
28 import org.slf4j.Logger;
29
30 import static com.google.common.collect.Iterables.find;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
32
33 /**
34  * Handles the common management of changing entities in AAI
35  */
36 abstract class AbstractManager {
37     protected final AAIRestApiProvider aaiRestApiProvider;
38     protected final CbamRestApiProvider cbamRestApiProvider;
39     protected final DriverProperties driverProperties;
40
41     AbstractManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
42         this.aaiRestApiProvider = aaiRestApiProvider;
43         this.cbamRestApiProvider = cbamRestApiProvider;
44         this.driverProperties = driverProperties;
45     }
46
47     /**
48      * @param key   the key of the relationship
49      * @param value the value of the relationship
50      * @return the relationship
51      */
52     protected static RelationshipData buildRelationshipData(String key, String value) {
53         RelationshipData data = new RelationshipData();
54         data.setRelationshipKey(key);
55         data.setRelationshipValue(value);
56         return data;
57     }
58
59     /**
60      * Extract mandatory value from the additional data on LCN resources
61      *
62      * @param additionalData the additional data
63      * @param key            the key of the additional data
64      * @return the value of the additional data
65      */
66     protected static String extractMandatoryValue(Object additionalData, String key) {
67         return new Gson().toJsonTree(additionalData).getAsJsonObject().get(key).getAsString();
68     }
69
70     /**
71      * Create or update the singleton relationship. Singleton means that relationships can only have a
72      * single {@link Relationship} with the given {@link Relationship#getRelatedTo} value
73      *
74      * @param relationships the list of relationships
75      * @param relationship  the expected relationship
76      */
77     protected static void addSingletonRelation(List<Relationship> relationships, Relationship relationship) {
78         boolean found = false;
79         for (Relationship currentRelationShip : relationships) {
80             if (relationship.getRelatedTo().equals(currentRelationShip.getRelatedTo())) {
81                 found = true;
82             }
83         }
84         if (!found) {
85             relationships.add(relationship);
86         } else {
87             Relationship existingRelationShip = find(relationships, currentRelationShip -> currentRelationShip.getRelatedTo().equals(relationship.getRelatedTo()));
88             existingRelationShip.getRelationshipData().clear();
89             existingRelationShip.getRelationshipData().addAll(relationship.getRelationshipData());
90         }
91     }
92
93     /**
94      * Add the given relationship if it is already not part of the relationships
95      *
96      * @param relationships the relationships
97      * @param relationship  the relationship to be added
98      */
99     protected static void addMissingRelation(List<Relationship> relationships, Relationship relationship) {
100         for (Relationship currentRelationShip : relationships) {
101             if (currentRelationShip.getRelatedTo().equals(relationship.getRelatedTo())
102                     && compositeKeys(currentRelationShip.getRelationshipData()).equals(compositeKeys(relationship.getRelationshipData()))) {
103                 return;
104             }
105         }
106         relationships.add(relationship);
107     }
108
109     private static Set<String> compositeKeys(List<RelationshipData> data) {
110         Set<String> keys = new HashSet<>();
111         for (RelationshipData relationshipData : data) {
112             keys.add(relationshipData.getRelationshipKey() + SEPARATOR + relationshipData.getRelationshipValue());
113         }
114         return keys;
115     }
116
117     /**
118      * @return the concrete logger to be used
119      */
120     protected abstract Logger getLogger();
121
122     /**
123      * Creates or returns a REST resource instance
124      *
125      * @param get         provides an existing instance
126      * @param newInstance the empty instance if the resource does not exists
127      * @param <T>         the type of the resource
128      * @return the created or queried resource
129      */
130     protected <T> T createOrGet(Observable<T> get, T newInstance) {
131         try {
132             return get.blockingFirst();
133         } catch (Exception e) {
134             getLogger().debug("The resource was not found in AAI", e);
135             return newInstance;
136         }
137     }
138 }