2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.vevnfm.service;
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;
37 public class SubscriptionScheduler {
39 private static final Logger logger = LoggerFactory.getLogger(SubscriptionScheduler.class);
41 private final SubscriberService subscriberService;
42 private List<EsrId> esrIds;
45 public SubscriptionScheduler(final SubscriberService subscriberService) {
46 this.subscriberService = subscriberService;
49 public void setInfos(final List<EsrSystemInfo> infos) {
50 esrIds = new LinkedList<>();
52 for (final EsrSystemInfo info : infos) {
53 final EsrId esrId = new EsrId();
59 List<EsrId> getEsrIds() {
63 @Scheduled(fixedRate = 5000, initialDelay = 2000)
64 void subscribeTask() throws VeVnfmException {
65 if (isEsrIdsValid()) {
66 for (final EsrId esrId : esrIds) {
67 singleSubscribe(esrId);
72 @Scheduled(fixedRate = 20000)
73 void checkSubscribeTask() throws VeVnfmException {
74 if (isEsrIdsValid()) {
75 for (final EsrId esrId : esrIds) {
76 singleCheckSubscription(esrId);
81 private boolean isEsrIdsValid() {
82 return esrIds != null && !esrIds.isEmpty();
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()));
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());