Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / GenericVnfManager.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.nokia.cbam.lcm.v32.model.VnfInfo;
19 import java.util.ArrayList;
20 import java.util.NoSuchElementException;
21 import org.onap.aai.model.GenericVnf;
22 import org.onap.aai.model.Relationship;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
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 org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
34
35 /**
36  * Responsible for managing the {@link GenericVnf} in AAI
37  */
38 @Component
39 @Conditional(value = Conditions.UseForDirect.class)
40 class GenericVnfManager extends AbstractManager {
41     private static final long MAX_MS_TO_WAIT_FOR_VNF_TO_APPEAR = 30 * 1000L;
42     private static Logger logger = org.slf4j.LoggerFactory.getLogger(GenericVnfManager.class);
43
44     @Autowired
45     GenericVnfManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
46         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
47     }
48
49     static Relationship linkTo(String vnfId) {
50         Relationship relationship = new Relationship();
51         relationship.setRelatedTo("generic-vnf");
52         relationship.setRelationshipData(new ArrayList<>());
53         relationship.getRelationshipData().add(buildRelationshipData("generic-vnf.vnf-id", vnfId));
54         return relationship;
55     }
56
57     @Override
58     protected Logger getLogger() {
59         return logger;
60     }
61
62     void createOrUpdate(String vnfId, boolean inMaintenance) {
63         try {
64             GenericVnf vnf = waitForVnfToAppearInAai(vnfId);
65             updateFields(vnf, vnfId, inMaintenance);
66         } catch (NoSuchElementException e) {
67             try {
68                 logger.warn("The VNF with " + vnfId + " identifier did not appear in time", e);
69                 updateFields(new GenericVnf(), vnfId, inMaintenance);
70             } catch (Exception e2) {
71                 logger.warn("The VNF with " + vnfId + " identifier has been created since after the maximal wait for VNF to appear timeout", e2);
72                 //the VNF might have been created since the last poll
73                 updateFields(getExistingVnf(vnfId), vnfId, inMaintenance);
74             }
75         }
76     }
77
78     private GenericVnf getExistingVnf(String vnfId) {
79         return aaiRestApiProvider.getNetworkApi().getNetworkGenericVnfsGenericVnf(vnfId, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null).blockingFirst();
80     }
81
82     private void updateFields(GenericVnf vnf, String vnfId, boolean inMaintenance) {
83         try {
84             VnfInfo vnfInfo = cbamRestApiProvider.getCbamLcmApi(driverProperties.getVnfmId()).vnfsVnfInstanceIdGet(vnfId, CbamRestApiProvider.NOKIA_LCM_API_VERSION).blockingFirst();
85             vnf.setVnfName(vnfInfo.getName());
86         } catch (RuntimeException e) {
87             throw buildFatalFailure(logger, "Unable to query VNF with " + vnfId + " identifier from CBAM", e);
88         }
89         vnf.setVnfId(vnfId);
90         vnf.setInMaint(inMaintenance);
91         //FIXME whould be good to know if this parameter is relevant or not? (mandatory)
92         vnf.setVnfType("NokiaVNF");
93         vnf.setIsClosedLoopDisabled(inMaintenance);
94         aaiRestApiProvider.getNetworkApi().createOrUpdateNetworkGenericVnfsGenericVnf(vnf.getVnfId(), vnf).blockingFirst();
95     }
96
97     private GenericVnf waitForVnfToAppearInAai(String vnfId) {
98         long timeoutInMs = systemFunctions().currentTimeMillis() + MAX_MS_TO_WAIT_FOR_VNF_TO_APPEAR;
99         while (timeoutInMs - systemFunctions().currentTimeMillis() > 0) {
100             try {
101                 return getExistingVnf(vnfId);
102             } catch (NoSuchElementException e) {
103                 logger.debug("Unable to get VNF with " + vnfId + " identifier", e);
104             }
105             systemFunctions().sleep(3 * 1000L);
106         }
107         throw new NoSuchElementException();
108     }
109
110 }