db65d2f1db26628aafbe53bc254b68288aed99f4
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / VnfcManager.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 io.reactivex.Observable;
19 import java.util.ArrayList;
20 import org.onap.aai.model.Vnfc;
21 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
25 import org.slf4j.Logger;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.context.annotation.Conditional;
28 import org.springframework.stereotype.Component;
29
30 import static com.google.common.base.Splitter.on;
31 import static com.google.common.collect.Lists.newArrayList;
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
33
34 /**
35  * Responsible for managing {@link Vnfc} in AAI
36  */
37 @Component
38 @Conditional(value = Conditions.UseForDirect.class)
39 public class VnfcManager extends AbstractManager {
40     private static Logger logger = org.slf4j.LoggerFactory.getLogger(VnfcManager.class);
41
42     @Autowired
43     VnfcManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
44         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
45     }
46
47     private static String buildId(String vnfId, String cbamVnfcId) {
48         return vnfId + SEPARATOR + cbamVnfcId;
49     }
50
51     /**
52      * @param onapVnfcId the identifier of the VNFC in AAI
53      * @return the identifier of the VNFC in CBAM
54      */
55     public static String buildCbamId(String onapVnfcId) {
56         return newArrayList(on(SEPARATOR).split(onapVnfcId)).get(1);
57     }
58
59     @Override
60     protected Logger getLogger() {
61         return logger;
62     }
63
64     void delete(String vnfId, com.nokia.cbam.lcm.v32.model.AffectedVnfc cbamVnfc) {
65         Vnfc vnfc = getVnfc(buildId(vnfId, cbamVnfc.getId())).blockingFirst();
66         aaiRestApiProvider.getNetworkApi().deleteNetworkVnfcsVnfc(vnfc.getVnfcName(), vnfc.getResourceVersion()).blockingFirst();
67     }
68
69     private Observable<Vnfc> getVnfc(String vnfcId) {
70         return aaiRestApiProvider.getNetworkApi().getNetworkVnfcsVnfc(vnfcId, null, null, null, null, null, null, null, null, null);
71     }
72
73     void update(String vimId, String tenantId, String vnfId, com.nokia.cbam.lcm.v32.model.AffectedVnfc cbamVnfc, boolean inMaintenance) {
74         Vnfc vnfc = createOrGet(getVnfc(buildId(vnfId, cbamVnfc.getId())), new Vnfc());
75         updateFields(vimId, tenantId, vnfc, cbamVnfc, vnfId, inMaintenance);
76     }
77
78     private void updateFields(String vimId, String tenantId, Vnfc aaiVnfc, com.nokia.cbam.lcm.v32.model.AffectedVnfc cbamVnfc, String vnfId, boolean inMaintenance) {
79         aaiVnfc.setInMaint(inMaintenance);
80         aaiVnfc.setIsClosedLoopDisabled(inMaintenance);
81         //FIXME would be good to know what is this mandatory parameter
82         aaiVnfc.setNfcFunction(cbamVnfc.getId());
83         //FIXME would be good to know what is this mandatory parameter
84         aaiVnfc.setNfcNamingCode(cbamVnfc.getId());
85         aaiVnfc.setVnfcName(buildId(vnfId, cbamVnfc.getId()));
86         if (aaiVnfc.getRelationshipList() == null) {
87             aaiVnfc.setRelationshipList(new ArrayList<>());
88         }
89         addSingletonRelation(aaiVnfc.getRelationshipList(), VserverManager.linkTo(vimId, tenantId, cbamVnfc.getComputeResource().getResourceId()));
90         addSingletonRelation(aaiVnfc.getRelationshipList(), GenericVnfManager.linkTo(vnfId));
91         aaiRestApiProvider.getNetworkApi().createOrUpdateNetworkVnfcsVnfc(aaiVnfc.getVnfcName(), aaiVnfc).blockingFirst();
92     }
93 }