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.List;
25 import java.util.UUID;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import org.modelmapper.ModelMapper;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201.InstantiationStateEnum;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfo;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
37 import org.onap.svnfm.simulator.config.ApplicationConfig;
38 import org.onap.svnfm.simulator.constants.Constant;
39 import org.onap.svnfm.simulator.model.VnfInstance;
40 import org.onap.svnfm.simulator.model.VnfOperation;
41 import org.onap.svnfm.simulator.model.Vnfds;
42 import org.onap.svnfm.simulator.notifications.VnfInstantiationNotification;
43 import org.onap.svnfm.simulator.notifications.VnfmAdapterCreationNotification;
44 import org.onap.svnfm.simulator.repository.VnfOperationRepository;
45 import org.onap.svnfm.simulator.repository.VnfmRepository;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.cache.Cache;
50 import org.springframework.cache.CacheManager;
51 import org.springframework.cache.annotation.CachePut;
52 import org.springframework.cache.support.SimpleValueWrapper;
53 import org.springframework.stereotype.Service;
57 * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
58 * @author Ronan Kenny (ronan.kenny@est.tech)
61 public class SvnfmService {
64 VnfmRepository vnfmRepository;
67 VnfOperationRepository vnfOperationRepository;
70 private VnfmHelper vnfmHelper;
73 ApplicationConfig applicationConfig;
76 CacheManager cacheManager;
82 SubscriptionService subscriptionService;
84 private final ExecutorService executor = Executors.newCachedThreadPool();
86 private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmService.class);
90 * @param createVNFRequest
91 * @return inlineResponse201
93 public InlineResponse201 createVnf(final CreateVnfRequest createVNFRequest, final String id) {
94 InlineResponse201 inlineResponse201 = null;
96 final VnfInstance vnfInstance = vnfmHelper.createVnfInstance(createVNFRequest, id);
97 vnfmRepository.save(vnfInstance);
98 final Thread creationNotification = new Thread(new VnfmAdapterCreationNotification());
99 creationNotification.start();
100 inlineResponse201 = vnfmHelper.getInlineResponse201(vnfInstance);
101 LOGGER.debug("Response from Create VNF {}", inlineResponse201);
102 } catch (IllegalAccessException | InvocationTargetException e) {
103 LOGGER.error("Failed in Create Vnf", e);
105 return inlineResponse201;
108 @CachePut(value = Constant.IN_LINE_RESPONSE_201_CACHE, key = "#id")
109 public InlineResponse201 updateVnf(final InstantiationStateEnum instantiationState,
110 final InlineResponse201InstantiatedVnfInfo instantiatedVnfInfo, final String id,
111 final List<InlineResponse201VimConnectionInfo> vimConnectionInfo) {
112 final InlineResponse201 vnf = getVnf(id);
113 vnf.setInstantiatedVnfInfo(instantiatedVnfInfo);
114 vnf.setInstantiationState(instantiationState);
115 vnf.setVimConnectionInfo(vimConnectionInfo);
122 * @param instantiateVNFRequest
124 * @throws InterruptedException
126 public String instantiateVnf(final String vnfId, final InstantiateVnfRequest instantiateVNFRequest) {
127 final VnfOperation vnfOperation = buildVnfOperation(InlineResponse200.OperationEnum.INSTANTIATE, vnfId);
128 vnfOperationRepository.save(vnfOperation);
129 executor.submit(new InstantiateOperationProgressor(vnfOperation, this, vnfOperationRepository,
130 applicationConfig, vnfds, subscriptionService));
131 return vnfOperation.getId();
135 * vnfOperationRepository
138 * @param instantiateOperationId
140 public VnfOperation buildVnfOperation(final InlineResponse200.OperationEnum operation, final String vnfId) {
141 final VnfOperation vnfOperation = new VnfOperation();
142 vnfOperation.setId(UUID.randomUUID().toString());
143 vnfOperation.setOperation(operation);
144 vnfOperation.setOperationState(InlineResponse200.OperationStateEnum.STARTING);
145 vnfOperation.setVnfInstanceId(vnfId);
152 * @throws InterruptedException
154 public InlineResponse200 getOperationStatus(final String operationId) {
155 LOGGER.info("Getting operation status with id: {}", operationId);
156 final Thread instantiationNotification = new Thread(new VnfInstantiationNotification());
157 instantiationNotification.start();
158 for (final VnfOperation operation : vnfOperationRepository.findAll()) {
159 LOGGER.info("Operation found: {}", operation);
160 if (operation.getId().equals(operationId)) {
161 final ModelMapper modelMapper = new ModelMapper();
162 return modelMapper.map(operation, InlineResponse200.class);
171 * @return inlineResponse201
173 public InlineResponse201 getVnf(final String vnfId) {
174 final Cache ca = cacheManager.getCache(Constant.IN_LINE_RESPONSE_201_CACHE);
175 final SimpleValueWrapper wrapper = (SimpleValueWrapper) ca.get(vnfId);
176 final InlineResponse201 inlineResponse201 = (InlineResponse201) wrapper.get();
177 if (inlineResponse201 != null) {
178 LOGGER.info("Cache Read Successful");
179 return inlineResponse201;
188 public String terminateVnf(final String vnfId) {
189 final VnfOperation vnfOperation = buildVnfOperation(InlineResponse200.OperationEnum.TERMINATE, vnfId);
190 vnfOperationRepository.save(vnfOperation);
191 executor.submit(new TerminateOperationProgressor(vnfOperation, this, vnfOperationRepository, applicationConfig,
192 vnfds, subscriptionService));
193 return vnfOperation.getId();
196 public void registerSubscription(final LccnSubscriptionRequest subscription) {
197 subscriptionService.registerSubscription(subscription);