f7f4eaa4a28b01e039b2db735702cf46e2bf7532
[so.git] / vnfm-simulator / vnfm-service / src / main / java / org / onap / svnfm / simulator / services / SvnfmService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.svnfm.simulator.services;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.Optional;
25 import org.onap.svnfm.simulator.model.VnfInstance;
26 import org.onap.svnfm.simulator.model.VnfJob;
27 import org.onap.svnfm.simulator.notifications.VnfInstantiationNotification;
28 import org.onap.svnfm.simulator.notifications.VnfmAdapterCreationNotification;
29 import org.onap.svnfm.simulator.repository.VnfJobRepository;
30 import org.onap.svnfm.simulator.repository.VnfmRepository;
31 import org.onap.vnfm.v1.model.CreateVnfRequest;
32 import org.onap.vnfm.v1.model.InlineResponse201;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 /**
39  * 
40  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
41  * @author Ronan Kenny (ronan.kenny@est.tech)
42  */
43 @Service
44 public class SvnfmService {
45
46     @Autowired
47     VnfmRepository vnfmRepository;
48
49     @Autowired
50     VnfJobRepository vnfJobRepository;
51
52     @Autowired
53     private VnfmHelper vnfmHelper;
54
55     private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmService.class);
56
57     /**
58      * 
59      * @param createVNFRequest
60      * @return inlineResponse201
61      */
62     public InlineResponse201 createVnf(final CreateVnfRequest createVNFRequest) {
63         InlineResponse201 inlineResponse201 = null;
64         try {
65             final VnfInstance vnfInstance = vnfmHelper.createVnfInstance(createVNFRequest);
66             vnfmRepository.save(vnfInstance);
67             final Thread creationNotification = new Thread(new VnfmAdapterCreationNotification());
68             creationNotification.start();
69             inlineResponse201 = vnfmHelper.getInlineResponse201(vnfInstance);
70             LOGGER.debug("Response from Create VNF", inlineResponse201);
71         } catch (IllegalAccessException | InvocationTargetException e) {
72             LOGGER.error("Failed in Create Vnf", e);
73         }
74         return inlineResponse201;
75     }
76
77     /**
78      * 
79      * @param vnfId
80      * @param instantiateJobId
81      * @throws InterruptedException
82      */
83     public Object instatiateVnf(final String vnfId, final String instantiateJobId) throws InterruptedException {
84         final VnfJob vnfJob = buildVnfInstantiation(vnfId, instantiateJobId);
85         vnfJobRepository.save(vnfJob);
86         getJobStatus(vnfJob.getJobId());
87         return null;
88     }
89
90     /**
91      * 
92      * @param vnfId
93      * @param instantiateJobId
94      */
95     public VnfJob buildVnfInstantiation(final String vnfId, final String instantiateJobId) {
96         final VnfJob vnfJob = new VnfJob();
97         final Optional<VnfInstance> vnfInstance = vnfmRepository.findById(vnfId);
98
99         if (vnfInstance.isPresent()) {
100             vnfJob.setJobId(instantiateJobId);
101             for (final VnfInstance instance : vnfmRepository.findAll()) {
102                 if (instance.getId().equals(vnfId)) {
103                     vnfJob.setVnfInstanceId(instance.getVnfInstanceDescription());
104                 }
105             }
106             vnfJob.setVnfId(vnfId);
107             vnfJob.setStatus("STARTING");
108         }
109         return vnfJob;
110     }
111
112     /**
113      * 
114      * @param jobId
115      * @throws InterruptedException
116      */
117     public Object getJobStatus(final String jobId) throws InterruptedException {
118         LOGGER.info("Getting job status with id: " + jobId);
119         for (int i = 0; i < 5; i++) {
120             LOGGER.info("Instantiation status: RUNNING");
121             Thread.sleep(5000);
122             for (final VnfJob job : vnfJobRepository.findAll()) {
123                 if (job.getJobId().equals(jobId)) {
124                     job.setStatus("RUNNING");
125                     vnfJobRepository.save(job);
126                 }
127             }
128         }
129         final Thread instantiationNotification = new Thread(new VnfInstantiationNotification());
130         instantiationNotification.start();
131         for (final VnfJob job : vnfJobRepository.findAll()) {
132             if (job.getJobId().equals(jobId)) {
133                 job.setStatus("COMPLETE");
134                 vnfJobRepository.save(job);
135             }
136         }
137         return null;
138     }
139
140     /**
141      * 
142      * @param vnfId
143      * @return inlineResponse201
144      */
145     public InlineResponse201 getVnf(final String vnfId) {
146         InlineResponse201 inlineResponse201 = null;
147
148         final Optional<VnfInstance> vnfInstance = vnfmRepository.findById(vnfId);
149         try {
150             if (vnfInstance.isPresent()) {
151                 inlineResponse201 = vnfmHelper.getInlineResponse201(vnfInstance.get());
152                 LOGGER.debug("Response from get VNF", inlineResponse201);
153             }
154         } catch (IllegalAccessException | InvocationTargetException e) {
155             LOGGER.error("Failed in get Vnf", e);
156         }
157         return inlineResponse201;
158     }
159
160     /**
161      * @param vnfId
162      * @return
163      */
164     public Object terminateVnf(String vnfId) {
165         // TODO
166         return null;
167     }
168 }