c8008f3877ca5429a1724e47f3ea0248fd80039a
[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 org.onap.aai.domain.yang.v11.ObjectFactory;
20 import org.onap.aai.domain.yang.v11.Relationship;
21 import org.onap.aai.domain.yang.v11.RelationshipData;
22 import org.onap.aai.domain.yang.v11.RelationshipList;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
26 import org.slf4j.Logger;
27
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.NoSuchElementException;
31 import java.util.Set;
32
33 import static com.google.common.collect.Iterables.find;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
35
36 /**
37  * Handles the common management of changing entities in AAI
38  */
39 abstract class AbstractManager {
40     protected static final ObjectFactory OBJECT_FACTORY = new ObjectFactory();
41     protected final AAIRestApiProvider aaiRestApiProvider;
42     protected final CbamRestApiProvider cbamRestApiProvider;
43     protected final DriverProperties driverProperties;
44
45     AbstractManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
46         this.aaiRestApiProvider = aaiRestApiProvider;
47         this.cbamRestApiProvider = cbamRestApiProvider;
48         this.driverProperties = driverProperties;
49     }
50
51     /**
52      * @param key   the key of the relationship
53      * @param value the value of the relationship
54      * @return the relationship
55      */
56     protected static RelationshipData buildRelationshipData(String key, String value) {
57         RelationshipData data = new RelationshipData();
58         data.setRelationshipKey(key);
59         data.setRelationshipValue(value);
60         return data;
61     }
62
63     /**
64      * Extract mandatory value from the additional data on LCN resources
65      *
66      * @param additionalData the additional data
67      * @param key            the key of the additional data
68      * @return the value of the additional data
69      */
70     protected static String extractMandatoryValue(Object additionalData, String key) {
71         return new Gson().toJsonTree(additionalData).getAsJsonObject().get(key).getAsString();
72     }
73
74     /**
75      * Create or update the singleton relationship. Singleton means that relationships can only have a
76      * single {@link Relationship} with the given {@link Relationship#getRelatedTo} value
77      *
78      * @param relationships the list of relationships
79      * @param relationship  the expected relationship
80      */
81     protected static void addSingletonRelation(RelationshipList relationships, Relationship relationship) {
82         boolean found = false;
83         for (Relationship currentRelationShip : relationships.getRelationship()) {
84             if (relationship.getRelatedTo().equals(currentRelationShip.getRelatedTo())) {
85                 found = true;
86             }
87         }
88         if (!found) {
89             relationships.getRelationship().add(relationship);
90         } else {
91             Relationship existingRelationShip = find(relationships.getRelationship(), currentRelationShip -> currentRelationShip.getRelatedTo().equals(relationship.getRelatedTo()));
92             existingRelationShip.getRelationshipData().clear();
93             existingRelationShip.getRelationshipData().addAll(relationship.getRelationshipData());
94         }
95     }
96
97     /**
98      * Add the given relationship if it is already not part of the relationships
99      *
100      * @param relationships the relationships
101      * @param relationship  the relationship to be added
102      */
103     protected static void addMissingRelation(RelationshipList relationships, Relationship relationship) {
104         for (Relationship currentRelationShip : relationships.getRelationship()) {
105             if (currentRelationShip.getRelatedTo().equals(relationship.getRelatedTo())
106                     && compositeKeys(currentRelationShip.getRelationshipData()).equals(compositeKeys(relationship.getRelationshipData()))) {
107                 return;
108             }
109         }
110         relationships.getRelationship().add(relationship);
111     }
112
113     private static Set<String> compositeKeys(List<RelationshipData> data) {
114         Set<String> keys = new HashSet<>();
115         for (RelationshipData relationshipData : data) {
116             keys.add(relationshipData.getRelationshipKey() + SEPARATOR + relationshipData.getRelationshipValue());
117         }
118         return keys;
119     }
120
121     /**
122      * @return the concrete logger to be used
123      */
124     protected abstract Logger getLogger();
125
126     /**
127      * Creates or returns a REST resource instance
128      *
129      * @param service     the type of the service
130      * @param url         the URL of the resource without the service prefix
131      * @param newInstance the empty instance if the resource does not exists
132      * @param <T>         the type of the resource
133      * @return the created or queried resource
134      */
135     protected <T> T createOrGet(AAIRestApiProvider.AAIService service, String url, T newInstance) {
136         try {
137             return (T) aaiRestApiProvider.get(getLogger(), service, url, newInstance.getClass());
138         } catch (NoSuchElementException e) {
139             getLogger().debug("The resource on " + url + " URL was not found in AAI", e);
140             return newInstance;
141         }
142     }
143 }