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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.svnfm.simulator.services;
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;
40 * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
41 * @author Ronan Kenny (ronan.kenny@est.tech)
44 public class SvnfmService {
47 VnfmRepository vnfmRepository;
50 VnfJobRepository vnfJobRepository;
53 private VnfmHelper vnfmHelper;
55 private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmService.class);
59 * @param createVNFRequest
60 * @return inlineResponse201
62 public InlineResponse201 createVnf(final CreateVnfRequest createVNFRequest) {
63 InlineResponse201 inlineResponse201 = null;
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);
74 return inlineResponse201;
80 * @param instantiateJobId
81 * @throws InterruptedException
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());
93 * @param instantiateJobId
95 public VnfJob buildVnfInstantiation(final String vnfId, final String instantiateJobId) {
96 final VnfJob vnfJob = new VnfJob();
97 final Optional<VnfInstance> vnfInstance = vnfmRepository.findById(vnfId);
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());
106 vnfJob.setVnfId(vnfId);
107 vnfJob.setStatus("STARTING");
115 * @throws InterruptedException
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");
122 for (final VnfJob job : vnfJobRepository.findAll()) {
123 if (job.getJobId().equals(jobId)) {
124 job.setStatus("RUNNING");
125 vnfJobRepository.save(job);
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);
143 * @return inlineResponse201
145 public InlineResponse201 getVnf(final String vnfId) {
146 InlineResponse201 inlineResponse201 = null;
148 final Optional<VnfInstance> vnfInstance = vnfmRepository.findById(vnfId);
150 if (vnfInstance.isPresent()) {
151 inlineResponse201 = vnfmHelper.getInlineResponse201(vnfInstance.get());
152 LOGGER.debug("Response from get VNF", inlineResponse201);
154 } catch (IllegalAccessException | InvocationTargetException e) {
155 LOGGER.error("Failed in get Vnf", e);
157 return inlineResponse201;
164 public Object terminateVnf(String vnfId) {