/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.controller.netconf.confignetconfconnector.osgi;

import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;

import javax.annotation.concurrent.GuardedBy;

public class YangStoreServiceImpl implements YangStoreService {
    private final SchemaContextProvider service;
    @GuardedBy("this")
    private YangStoreSnapshotImpl cache = null;

    public YangStoreServiceImpl(SchemaContextProvider service) {
        this.service = service;
    }

    @Override
    public synchronized YangStoreSnapshotImpl getYangStoreSnapshot() throws YangStoreException {
        if (cache == null) {
            cache = new YangStoreSnapshotImpl(service.getSchemaContext());
        }
        return cache;
    }

    /**
     * Called when schema context changes, invalidates cache.
     */
    public synchronized void refresh() {
        cache = null;
    }
}

/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.controller.netconf.confignetconfconnector.osgi;

import java.lang.ref.SoftReference;
import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;

import javax.annotation.concurrent.GuardedBy;

public class YangStoreServiceImpl implements YangStoreService {
    private final SchemaContextProvider service;
    @GuardedBy("this")
    private SoftReference<YangStoreSnapshotImpl> cache = new SoftReference<>(null);

    public YangStoreServiceImpl(SchemaContextProvider service) {
        this.service = service;
    }

    @Override
    public synchronized YangStoreSnapshotImpl getYangStoreSnapshot() throws YangStoreException {
        YangStoreSnapshotImpl yangStoreSnapshot = cache.get();
        if (yangStoreSnapshot == null) {
            yangStoreSnapshot = new YangStoreSnapshotImpl(service.getSchemaContext());
            cache = new SoftReference<>(yangStoreSnapshot);
        }
        return yangStoreSnapshot;
    }

    /**
     * Called when schema context changes, invalidates cache.
     */
    public synchronized void refresh() {
        cache.clear();
    }
}

Store yang store snapshot cache using soft reference.
Store yang store snapshot cache using soft reference.

Change-Id: I9b159db83ba204b4a636f2314fd4fc2e7b6f654c
Signed-off-by: Tomas Olvecky <15c0b3ba77d9541ebd8ccf1bd003ebd96c682ed7@cisco.com>
