d9f3acc3df0c0f06138d37cef2c7ee0a30d3edde
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SO
4  * ================================================================================
5  * Copyright (C) 2020 Samsung. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vevnfm.service;
22
23 import java.util.LinkedList;
24 import java.util.List;
25 import org.onap.aai.domain.yang.EsrSystemInfo;
26 import org.onap.so.adapters.vevnfm.aai.EsrId;
27 import org.onap.so.adapters.vevnfm.exception.VeVnfmException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.scheduling.annotation.EnableScheduling;
32 import org.springframework.scheduling.annotation.Scheduled;
33 import org.springframework.stereotype.Service;
34
35 @Service
36 @EnableScheduling
37 public class SubscriptionScheduler {
38
39     private static final Logger logger = LoggerFactory.getLogger(SubscriptionScheduler.class);
40
41     @Autowired
42     private SubscriberService subscriberService;
43
44     private List<EsrId> esrIds;
45
46     public void setInfos(final List<EsrSystemInfo> infos) {
47         esrIds = new LinkedList<>();
48
49         for (final EsrSystemInfo info : infos) {
50             final EsrId esrId = new EsrId();
51             esrId.setInfo(info);
52             esrIds.add(esrId);
53         }
54     }
55
56     List<EsrId> getEsrIds() {
57         return esrIds;
58     }
59
60     @Scheduled(fixedRate = 5000, initialDelay = 2000)
61     void subscribeTask() throws VeVnfmException {
62         if (isEsrIdsValid()) {
63             for (final EsrId esrId : esrIds) {
64                 singleSubscribe(esrId);
65             }
66         }
67     }
68
69     @Scheduled(fixedRate = 20000)
70     void checkSubscribeTask() throws VeVnfmException {
71         if (isEsrIdsValid()) {
72             for (final EsrId esrId : esrIds) {
73                 singleCheckSubscription(esrId);
74             }
75         }
76     }
77
78     private boolean isEsrIdsValid() {
79         return esrIds != null && !esrIds.isEmpty();
80     }
81
82     private void singleSubscribe(final EsrId esrId) throws VeVnfmException {
83         if (esrId.getId() == null) {
84             logger.info("Single subscribe task");
85             esrId.setId(subscriberService.subscribe(esrId.getInfo()));
86         }
87     }
88
89     private void singleCheckSubscription(final EsrId esrId) throws VeVnfmException {
90         if (esrId.getId() != null) {
91             logger.info("Checking subscription: {}", esrId.getId());
92             if (!subscriberService.checkSubscription(esrId.getInfo(), esrId.getId())) {
93                 logger.info("Subscription {} not available", esrId.getId());
94                 esrId.setId(null);
95             }
96         }
97     }
98 }