a696336011ede7a3580f6e0df3a85f9cb09f67e1
[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     private final SubscriberService subscriberService;
42     private List<EsrId> esrIds;
43
44     @Autowired
45     public SubscriptionScheduler(final SubscriberService subscriberService) {
46         this.subscriberService = subscriberService;
47     }
48
49     public void setInfos(final List<EsrSystemInfo> infos) {
50         esrIds = new LinkedList<>();
51
52         for (final EsrSystemInfo info : infos) {
53             final EsrId esrId = new EsrId();
54             esrId.setInfo(info);
55             esrIds.add(esrId);
56         }
57     }
58
59     List<EsrId> getEsrIds() {
60         return esrIds;
61     }
62
63     @Scheduled(fixedRate = 5000, initialDelay = 2000)
64     void subscribeTask() throws VeVnfmException {
65         if (isEsrIdsValid()) {
66             for (final EsrId esrId : esrIds) {
67                 singleSubscribe(esrId);
68             }
69         }
70     }
71
72     @Scheduled(fixedRate = 20000)
73     void checkSubscribeTask() throws VeVnfmException {
74         if (isEsrIdsValid()) {
75             for (final EsrId esrId : esrIds) {
76                 singleCheckSubscription(esrId);
77             }
78         }
79     }
80
81     private boolean isEsrIdsValid() {
82         return esrIds != null && !esrIds.isEmpty();
83     }
84
85     private void singleSubscribe(final EsrId esrId) throws VeVnfmException {
86         if (esrId.getId() == null) {
87             logger.info("Single subscribe task");
88             esrId.setId(subscriberService.subscribe(esrId.getInfo()));
89         }
90     }
91
92     private void singleCheckSubscription(final EsrId esrId) throws VeVnfmException {
93         if (esrId.getId() != null) {
94             logger.info("Checking subscription: {}", esrId.getId());
95             if (!subscriberService.checkSubscription(esrId.getInfo(), esrId.getId())) {
96                 logger.info("Subscription {} not available", esrId.getId());
97                 esrId.setId(null);
98             }
99         }
100     }
101 }