summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/core/GenericExternalSystemInfoProvider.java
blob: 38665877f7c6a97ada1796c666c8772184f4ad37 (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
83
84
85
86
87
88
89
90
91
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * 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
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;

import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.TimeUnit;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VimInfoProvider;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
import org.onap.vnfmdriver.model.VnfmInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.env.Environment;

import static java.lang.Long.valueOf;

import static com.google.common.cache.CacheBuilder.newBuilder;
import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * Responsible for providing access to core systems
 */
public abstract class GenericExternalSystemInfoProvider extends IpMappingProvider implements VnfmInfoProvider, VimInfoProvider, InitializingBean {
    /**
     * The name of the VNFM info cache eviction in the properties file
     */
    public static final String VNFM_INFO_CACHE_EVICTION_IN_MS = "vnfmInfoCacheEvictionInMs";
    /**
     * The default VNFM info cache eviction in milliseconds
     */
    public static final int DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS = 10 * 60 * 1000;
    private static Logger logger = getLogger(GenericExternalSystemInfoProvider.class);
    private final Environment environment;
    private LoadingCache<String, VnfmInfo> vnfmInfoCache;

    public GenericExternalSystemInfoProvider(Environment environment) {
        super(environment);
        this.environment = environment;
    }

    /**
     * After the Bean has been initialized the IP mapping and the VMFM cache is initialized
     * It is done in this phase because the logic requires the the @Value anoted fields to
     * be specified
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
        vnfmInfoCache = newBuilder().expireAfterWrite(environment.getProperty(VNFM_INFO_CACHE_EVICTION_IN_MS, Long.class, valueOf(DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS)), TimeUnit.MILLISECONDS).concurrencyLevel(1).build(new CacheLoader<String, VnfmInfo>() {
            @Override
            public VnfmInfo load(String vnfmId) throws Exception {
                logger.info("Querying VNFM info from source with " + vnfmId + " identifier");
                return queryVnfmInfoFromSource(vnfmId);
            }
        });
    }

    /*
     * @param vnfmId the identifier of the VNFM
     * @return the cached VNFM
     */
    public VnfmInfo getVnfmInfo(String vnfmId) {
        try {
            return vnfmInfoCache.get(vnfmId);
        } catch (Exception e) {
            throw buildFatalFailure(logger, "Unable to query VNFM info for " + vnfmId, e);
        }
    }

    /**
     * Load the information related to the VNFM from the remote source
     *
     * @param vnfmId the identifier of the VNFM
     * @return the description of the VNFM
     */
    public abstract VnfmInfo queryVnfmInfoFromSource(String vnfmId);
}