Merge "Remove unused exception"
[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 com.google.common.base.Splitter;
19 import com.nokia.cbam.lcm.v32.model.AffectedVnfc;
20 import org.onap.aai.domain.yang.v11.RelationshipList;
21 import org.onap.aai.domain.yang.v11.Vnfc;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
27 import org.slf4j.Logger;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.Conditional;
30 import org.springframework.stereotype.Component;
31
32 import static java.lang.String.format;
33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.NETWORK;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
35
36 /**
37  * Responsible for managing {@link Vnfc} in AAI
38  */
39 @Component
40 @Conditional(value = Conditions.UseForDirect.class)
41 public class VnfcManager extends AbstractManager {
42     private static Logger logger = org.slf4j.LoggerFactory.getLogger(VnfcManager.class);
43
44     @Autowired
45     VnfcManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
46         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
47     }
48
49     public static String buildUrl(String vnfId, String cbamVnfcId) {
50         return format("/vnfcs/vnfc/%s", buildId(vnfId, cbamVnfcId));
51     }
52
53     public static String getCbamVnfcId(String vnfcId){
54         String vnfId = Splitter.on(CbamUtils.SEPARATOR).split(vnfcId).iterator().next();
55         return vnfcId.replaceFirst(vnfId + SEPARATOR, "");
56     }
57
58     private static String buildId(String vnfId, String cbamVnfcId) {
59         return vnfId + SEPARATOR + cbamVnfcId;
60     }
61
62     @Override
63     protected Logger getLogger() {
64         return logger;
65     }
66
67     void delete(String vnfId, com.nokia.cbam.lcm.v32.model.AffectedVnfc cbamVnfc) {
68         aaiRestApiProvider.delete(logger, NETWORK, buildUrl(vnfId, cbamVnfc.getId()));
69     }
70
71     void update(String vimId, String tenantId, String vnfId, com.nokia.cbam.lcm.v32.model.AffectedVnfc cbamVnfc, boolean inMaintenance) {
72         String url = buildUrl(vnfId, cbamVnfc.getId());
73         Vnfc vnfc = createOrGet(NETWORK, url, OBJECT_FACTORY.createVnfc());
74         updateFields(vimId, tenantId, vnfc, cbamVnfc, vnfId, url, inMaintenance);
75     }
76
77     private void updateFields(String vimId, String tenantId, Vnfc aaiVnfc, com.nokia.cbam.lcm.v32.model.AffectedVnfc cbamVnfc, String vnfId, String url, boolean inMaintenance) {
78         aaiVnfc.setInMaint(inMaintenance);
79         aaiVnfc.setIsClosedLoopDisabled(inMaintenance);
80         //FIXME would be good to know what is this mandatory parameter
81         aaiVnfc.setNfcFunction(cbamVnfc.getId());
82         //FIXME would be good to know what is this mandatory parameter
83         aaiVnfc.setNfcNamingCode(cbamVnfc.getId());
84         aaiVnfc.setVnfcName(buildId(vnfId, cbamVnfc.getId()));
85         aaiVnfc.setRelationshipList(new RelationshipList());
86         addSingletonRelation(aaiVnfc.getRelationshipList(), VserverManager.linkTo(vimId, tenantId, cbamVnfc.getComputeResource().getResourceId()));
87         addSingletonRelation(aaiVnfc.getRelationshipList(), GenericVnfManager.linkTo(vnfId));
88         aaiRestApiProvider.put(logger, NETWORK, url, aaiVnfc, Void.class);
89     }
90 }