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 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;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.NoSuchElementException;
33 import static com.google.common.collect.Iterables.find;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
37 * Handles the common management of changing entities in AAI
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;
45 AbstractManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
46 this.aaiRestApiProvider = aaiRestApiProvider;
47 this.cbamRestApiProvider = cbamRestApiProvider;
48 this.driverProperties = driverProperties;
52 * @param key the key of the relationship
53 * @param value the value of the relationship
54 * @return the relationship
56 protected static RelationshipData buildRelationshipData(String key, String value) {
57 RelationshipData data = new RelationshipData();
58 data.setRelationshipKey(key);
59 data.setRelationshipValue(value);
64 * Extract mandatory value from the additional data on LCN resources
66 * @param additionalData the additional data
67 * @param key the key of the additional data
68 * @return the value of the additional data
70 protected static String extractMandatoryValue(Object additionalData, String key) {
71 return new Gson().toJsonTree(additionalData).getAsJsonObject().get(key).getAsString();
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
78 * @param relationships the list of relationships
79 * @param relationship the expected relationship
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())) {
89 relationships.getRelationship().add(relationship);
91 Relationship existingRelationShip = find(relationships.getRelationship(), currentRelationShip -> currentRelationShip.getRelatedTo().equals(relationship.getRelatedTo()));
92 existingRelationShip.getRelationshipData().clear();
93 existingRelationShip.getRelationshipData().addAll(relationship.getRelationshipData());
98 * Add the given relationship if it is already not part of the relationships
100 * @param relationships the relationships
101 * @param relationship the relationship to be added
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()))) {
110 relationships.getRelationship().add(relationship);
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());
122 * @return the concrete logger to be used
124 protected abstract Logger getLogger();
127 * Creates or returns a REST resource instance
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
135 protected <T> T createOrGet(AAIRestApiProvider.AAIService service, String url, T newInstance) {
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);