2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
18 import com.google.gson.Gson;
19 import io.reactivex.Observable;
20 import java.util.HashSet;
21 import java.util.List;
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;
30 import static com.google.common.collect.Iterables.find;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
34 * Handles the common management of changing entities in AAI
36 abstract class AbstractManager {
37 protected final AAIRestApiProvider aaiRestApiProvider;
38 protected final CbamRestApiProvider cbamRestApiProvider;
39 protected final DriverProperties driverProperties;
41 AbstractManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
42 this.aaiRestApiProvider = aaiRestApiProvider;
43 this.cbamRestApiProvider = cbamRestApiProvider;
44 this.driverProperties = driverProperties;
48 * @param key the key of the relationship
49 * @param value the value of the relationship
50 * @return the relationship
52 protected static RelationshipData buildRelationshipData(String key, String value) {
53 RelationshipData data = new RelationshipData();
54 data.setRelationshipKey(key);
55 data.setRelationshipValue(value);
60 * Extract mandatory value from the additional data on LCN resources
62 * @param additionalData the additional data
63 * @param key the key of the additional data
64 * @return the value of the additional data
66 protected static String extractMandatoryValue(Object additionalData, String key) {
67 return new Gson().toJsonTree(additionalData).getAsJsonObject().get(key).getAsString();
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
74 * @param relationships the list of relationships
75 * @param relationship the expected relationship
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())) {
85 relationships.add(relationship);
87 Relationship existingRelationShip = find(relationships, currentRelationShip -> currentRelationShip.getRelatedTo().equals(relationship.getRelatedTo()));
88 existingRelationShip.getRelationshipData().clear();
89 existingRelationShip.getRelationshipData().addAll(relationship.getRelationshipData());
94 * Add the given relationship if it is already not part of the relationships
96 * @param relationships the relationships
97 * @param relationship the relationship to be added
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()))) {
106 relationships.add(relationship);
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());
118 * @return the concrete logger to be used
120 protected abstract Logger getLogger();
123 * Creates or returns a REST resource instance
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
130 protected <T> T createOrGet(Observable<T> get, T newInstance) {
132 return get.blockingFirst();
133 } catch (Exception e) {
134 getLogger().debug("The resource was not found in AAI", e);