summaryrefslogtreecommitdiffstats
path: root/engine-d/src/main/java/org/onap/holmes/engine/manager/status/EntityStatusRefreshTask.java
blob: 0b159997bad6e0158f407ae58a9a4d95aa267796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
 * Copyright 2020 ZTE Corporation.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.holmes.engine.manager.status;

import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.engine.entity.EngineEntity;
import org.onap.holmes.common.engine.service.EngineEntityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Optional;
import java.util.Timer;
import java.util.TimerTask;

import static java.util.concurrent.TimeUnit.SECONDS;

@Service
public class EntityStatusRefreshTask extends TimerTask {
    private static final Logger logger = LoggerFactory.getLogger(EntityStatusRefreshTask.class);
    private final long INTERVAL = SECONDS.toMillis(15);
    private EngineEntity engineEntity;
    private Timer timer = new Timer("EntityStatusRefreshTimer", true);
    private EngineEntityService engineEntityService;

    @Inject
    public EntityStatusRefreshTask(EngineEntityService engineEntityService) {
        this.engineEntityService = engineEntityService;
    }

    @PostConstruct
    private void initialize() {
        String[] serviceAddrInfo = MicroServiceConfig.getMicroServiceIpAndPort();
        if (null != serviceAddrInfo) {
            String ip = serviceAddrInfo[0];
            String port = Optional.ofNullable(serviceAddrInfo[1]).orElse("9201");
            port = port.equals("80") ? "9201" : port;
            engineEntity = new EngineEntity(ip, Integer.parseInt(port));

            timer.schedule(this, SECONDS.toMillis(1), INTERVAL);
        } else {
            logger.warn("Failed to get the address info of current engine. " +
                    "Problems will be caused when it comes to a multi-instance scenario!");
        }
    }

    @Override
    public void run() {
        if (engineEntity == null) {
            logger.warn("No engine entity is specified. The status refresh task will return immediately.");
            return;
        }

        engineEntity.setLastModified(System.currentTimeMillis());

        try {
            if (engineEntityService.getEntity(engineEntity.getId()) == null) {
                engineEntityService.insertEntity(engineEntity);
            } else {
                engineEntityService.updateEntity(engineEntity);
            }
        } catch (Exception e) {
            logger.warn("Failed to update engine instance information.", e);
        }
    }
}