VNFM simulator implementation for instantiate 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.UUID;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import org.modelmapper.ModelMapper;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
33 import org.onap.svnfm.simulator.config.ApplicationConfig;
34 import org.onap.svnfm.simulator.constants.Constant;
35 import org.onap.svnfm.simulator.model.VnfInstance;
36 import org.onap.svnfm.simulator.model.VnfOperation;
37 import org.onap.svnfm.simulator.model.Vnfds;
38 import org.onap.svnfm.simulator.notifications.VnfInstantiationNotification;
39 import org.onap.svnfm.simulator.notifications.VnfmAdapterCreationNotification;
40 import org.onap.svnfm.simulator.repository.VnfOperationRepository;
41 import org.onap.svnfm.simulator.repository.VnfmCacheRepository;
42 import org.onap.svnfm.simulator.repository.VnfmRepository;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.cache.Cache;
47 import org.springframework.cache.CacheManager;
48 import org.springframework.cache.support.SimpleValueWrapper;
49 import org.springframework.stereotype.Service;
50
51 /**
52  *
53  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
54  * @author Ronan Kenny (ronan.kenny@est.tech)
55  */
56 @Service
57 public class SvnfmService {
58
59     @Autowired
60     VnfmRepository vnfmRepository;
61
62     @Autowired
63     VnfmCacheRepository vnfRepository;
64
65     @Autowired
66     VnfOperationRepository vnfOperationRepository;
67
68     @Autowired
69     private VnfmHelper vnfmHelper;
70
71     @Autowired
72     ApplicationConfig applicationConfig;
73
74     @Autowired
75     CacheManager cacheManager;
76
77     @Autowired
78     Vnfds vnfds;
79
80     @Autowired
81     SubscriptionService subscriptionService;
82
83     private final ExecutorService executor = Executors.newCachedThreadPool();
84
85     private static final Logger LOGGER = LoggerFactory.getLogger(SvnfmService.class);
86
87     /**
88      *
89      * @param createVNFRequest
90      * @return inlineResponse201
91      */
92     public InlineResponse201 createVnf(final CreateVnfRequest createVNFRequest, final String id) {
93         InlineResponse201 inlineResponse201 = null;
94         try {
95             final VnfInstance vnfInstance = vnfmHelper.createVnfInstance(createVNFRequest, id);
96             vnfmRepository.save(vnfInstance);
97             final Thread creationNotification = new Thread(new VnfmAdapterCreationNotification());
98             creationNotification.start();
99             inlineResponse201 = vnfmHelper.getInlineResponse201(vnfInstance);
100             LOGGER.debug("Response from Create VNF {}", inlineResponse201);
101         } catch (IllegalAccessException | InvocationTargetException e) {
102             LOGGER.error("Failed in Create Vnf", e);
103         }
104         return inlineResponse201;
105     }
106
107     /**
108      *
109      * @param vnfId
110      * @param instantiateVNFRequest
111      * @param operationId
112      * @throws InterruptedException
113      */
114     public String instantiateVnf(final String vnfId, final InstantiateVnfRequest instantiateVNFRequest) {
115         final VnfOperation vnfOperation = buildVnfOperation(InlineResponse200.OperationEnum.INSTANTIATE, vnfId);
116         vnfOperationRepository.save(vnfOperation);
117         executor.submit(new OperationProgressor(vnfOperation, vnfRepository, vnfOperationRepository, applicationConfig,
118                 vnfds, subscriptionService));
119         return vnfOperation.getId();
120     }
121
122     /**
123      * vnfOperationRepository
124      *
125      * @param vnfId
126      * @param instantiateOperationId
127      */
128     public VnfOperation buildVnfOperation(final InlineResponse200.OperationEnum operation, final String vnfId) {
129         final VnfOperation vnfOperation = new VnfOperation();
130         vnfOperation.setId(UUID.randomUUID().toString());
131         vnfOperation.setOperation(operation);
132         vnfOperation.setOperationState(InlineResponse200.OperationStateEnum.STARTING);
133         vnfOperation.setVnfInstanceId(vnfId);
134         return vnfOperation;
135     }
136
137     /**
138      *
139      * @param operationId
140      * @throws InterruptedException
141      */
142     public InlineResponse200 getOperationStatus(final String operationId) {
143         LOGGER.info("Getting operation status with id: {}", operationId);
144         final Thread instantiationNotification = new Thread(new VnfInstantiationNotification());
145         instantiationNotification.start();
146         for (final VnfOperation operation : vnfOperationRepository.findAll()) {
147             LOGGER.info("Operation found: {}", operation);
148             if (operation.getId().equals(operationId)) {
149                 final ModelMapper modelMapper = new ModelMapper();
150                 return modelMapper.map(operation, InlineResponse200.class);
151             }
152         }
153         return null;
154     }
155
156     /**
157      *
158      * @param vnfId
159      * @return inlineResponse201
160      */
161     public InlineResponse201 getVnf(final String vnfId) {
162         final Cache ca = cacheManager.getCache(Constant.IN_LINE_RESPONSE_201_CACHE);
163         final SimpleValueWrapper wrapper = (SimpleValueWrapper) ca.get(vnfId);
164         final InlineResponse201 inlineResponse201 = (InlineResponse201) wrapper.get();
165         if (inlineResponse201 != null) {
166             LOGGER.info("Cache Read Successful");
167             return inlineResponse201;
168         }
169         return null;
170     }
171
172     /**
173      * @param vnfId
174      * @return
175      */
176     public Object terminateVnf(final String vnfId) {
177         // TODO
178         return null;
179     }
180
181     public void registerSubscription(final LccnSubscriptionRequest subscription) {
182         subscriptionService.registerSubscription(subscription);
183     }
184 }