VNFM simulator implementation for delete flow
[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.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;
54
55 /**
56  *
57  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
58  * @author Ronan Kenny (ronan.kenny@est.tech)
59  */
60 @Service
61 public class SvnfmService {
62
63     @Autowired
64     VnfmRepository vnfmRepository;
65
66     @Autowired
67     VnfOperationRepository vnfOperationRepository;
68
69     @Autowired
70     private VnfmHelper vnfmHelper;
71
72     @Autowired
73     ApplicationConfig applicationConfig;
74
75     @Autowired
76     CacheManager cacheManager;
77
78     @Autowired
79     Vnfds vnfds;
80
81     @Autowired
82     SubscriptionService subscriptionService;
83
84     private final ExecutorService executor = Executors.newCachedThreadPool();
85
86     private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmService.class);
87
88     /**
89      *
90      * @param createVNFRequest
91      * @return inlineResponse201
92      */
93     public InlineResponse201 createVnf(final CreateVnfRequest createVNFRequest, final String id) {
94         InlineResponse201 inlineResponse201 = null;
95         try {
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);
104         }
105         return inlineResponse201;
106     }
107
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);
116         return vnf;
117     }
118
119     /**
120      *
121      * @param vnfId
122      * @param instantiateVNFRequest
123      * @param operationId
124      * @throws InterruptedException
125      */
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();
132     }
133
134     /**
135      * vnfOperationRepository
136      *
137      * @param vnfId
138      * @param instantiateOperationId
139      */
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);
146         return vnfOperation;
147     }
148
149     /**
150      *
151      * @param operationId
152      * @throws InterruptedException
153      */
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);
163             }
164         }
165         return null;
166     }
167
168     /**
169      *
170      * @param vnfId
171      * @return inlineResponse201
172      */
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;
180         }
181         return null;
182     }
183
184     /**
185      * @param vnfId
186      * @return
187      */
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();
194     }
195
196     public void registerSubscription(final LccnSubscriptionRequest subscription) {
197         subscriptionService.registerSubscription(subscription);
198     }
199 }