Store yang store snapshot cache using soft reference.
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / YangStoreServiceImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.osgi;
10
11 import java.lang.ref.SoftReference;
12 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
13
14 import javax.annotation.concurrent.GuardedBy;
15
16 public class YangStoreServiceImpl implements YangStoreService {
17     private final SchemaContextProvider service;
18     @GuardedBy("this")
19     private SoftReference<YangStoreSnapshotImpl> cache = new SoftReference<>(null);
20
21     public YangStoreServiceImpl(SchemaContextProvider service) {
22         this.service = service;
23     }
24
25     @Override
26     public synchronized YangStoreSnapshotImpl getYangStoreSnapshot() throws YangStoreException {
27         YangStoreSnapshotImpl yangStoreSnapshot = cache.get();
28         if (yangStoreSnapshot == null) {
29             yangStoreSnapshot = new YangStoreSnapshotImpl(service.getSchemaContext());
30             cache = new SoftReference<>(yangStoreSnapshot);
31         }
32         return yangStoreSnapshot;
33     }
34
35     /**
36      * Called when schema context changes, invalidates cache.
37      */
38     public synchronized void refresh() {
39         cache.clear();
40     }
41 }