4f7835aaccf9c0f562f2149efef0084e1b723059
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / dao / FlowCapableNodeSnapshotDao.java
1 /**
2  * Copyright (c) 2016 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.openflowplugin.applications.frsync.dao;
10
11 import com.google.common.base.Optional;
12 import java.util.concurrent.ConcurrentHashMap;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
16
17 /**
18  * Adding cache to data access object of {@link FlowCapableNode}.
19  */
20 public class FlowCapableNodeSnapshotDao implements FlowCapableNodeDao {
21
22     private final ConcurrentHashMap<String, FlowCapableNode> cache = new ConcurrentHashMap<>();
23
24     public void updateCache(@Nonnull NodeId nodeId, Optional<FlowCapableNode> dataAfter) {
25         if (dataAfter.isPresent()) {
26             cache.put(nodeId.getValue(), dataAfter.get());
27         } else {
28             cache.remove(nodeId.getValue());
29         }
30     }
31
32     public Optional<FlowCapableNode> loadByNodeId(@Nonnull NodeId nodeId) {
33         final FlowCapableNode node = cache.get(nodeId.getValue());
34         return Optional.fromNullable(node);
35     }
36
37 }