f13e2bbec913741fd678c2544698635dd9ed1070
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / GenericExternalSystemInfoProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
17
18 import com.google.common.cache.CacheLoader;
19 import com.google.common.cache.LoadingCache;
20 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VimInfoProvider;
21 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
22 import org.onap.vnfmdriver.model.VnfmInfo;
23 import org.slf4j.Logger;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.core.env.Environment;
26
27 import java.util.concurrent.TimeUnit;
28
29 import static com.google.common.cache.CacheBuilder.newBuilder;
30 import static java.lang.Long.valueOf;
31 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.fatalFailure;
32 import static org.slf4j.LoggerFactory.getLogger;
33
34 /**
35  * Responsible for providing access to core systems
36  */
37 public abstract class GenericExternalSystemInfoProvider extends IpMappingProvider implements VnfmInfoProvider, VimInfoProvider, InitializingBean {
38     /**
39      * The name of the VNFM info cache eviction in the properties file
40      */
41     public static final String VNFM_INFO_CACHE_EVICTION_IN_MS = "vnfmInfoCacheEvictionInMs";
42     /**
43      * The default VNFM info cache eviction in milliseconds
44      */
45     public static final int DEFAULT_CACHE_EVICTION_TIMEOUT_IN_MS = 10 * 60 * 1000;
46     private static Logger logger = getLogger(GenericExternalSystemInfoProvider.class);
47     private final Environment environment;
48     private LoadingCache<String, VnfmInfo> vnfmInfoCache;
49
50     public GenericExternalSystemInfoProvider(Environment environment) {
51         super(environment);
52         this.environment = environment;
53     }
54
55     /**
56      * After the Bean has been initialized the IP mapping and the VMFM cache is initialized
57      * It is done in this phase because the logic requires the the @Value anoted fields to
58      * be specified
59      */
60     @Override
61     public void afterPropertiesSet() throws Exception {
62         super.afterPropertiesSet();
63         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>() {
64             @Override
65             public VnfmInfo load(String vnfmId) throws Exception {
66                 logger.info("Quering VNFM info from source with " + vnfmId + " identifier");
67                 return queryVnfmInfoFromSource(vnfmId);
68             }
69         });
70     }
71
72     /*
73      * @param vnfmId the identifier of the VNFM
74      * @return the cached VNFM
75      */
76     public VnfmInfo getVnfmInfo(String vnfmId) {
77         try {
78             return vnfmInfoCache.get(vnfmId);
79         } catch (Exception e) {
80             throw fatalFailure(logger, "Unable to query VNFM info for " + vnfmId, e);
81         }
82     }
83
84     /**
85      * Load the information related to the VNFM from the remote source
86      *
87      * @param vnfmId the identifier of the VNFM
88      * @return the description of the VNFM
89      */
90     public abstract VnfmInfo queryVnfmInfoFromSource(String vnfmId);
91 }