Merge "Use ${project.version} for internal dependencies"
[ovsdb.git] / openstack / net-virt-sfc / impl / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / sfc / workaround / services / FlowCache.java
1 /*
2  * Copyright © 2015 Red Hat, 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.ovsdb.openstack.netvirt.sfc.workaround.services;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class FlowCache {
22     private static final Logger LOG = LoggerFactory.getLogger(FlowCache.class);
23     private Map<String, Map<Integer, InstanceIdentifier<Flow>>> flowCache = new HashMap<>();
24
25     public void addFlow(FlowBuilder flowBuilder, NodeBuilder nodeBuilder, String rspName, int flowId) {
26         Map<Integer, InstanceIdentifier<Flow>> flowMap = flowCache.get(rspName);
27         if (flowMap == null) {
28             LOG.info("addFlow: adding new flowMap for {}({})", rspName, flowId);
29             flowMap = new HashMap<>();
30         }
31         InstanceIdentifier<Flow> path = FlowUtils.createFlowPath(flowBuilder, nodeBuilder);
32         flowMap.put(flowId, path);
33         flowCache.put(rspName, flowMap);
34         LOG.info("addFlow: added {}({}) {} to cache size {} - {}", rspName, flowId, path,
35                 flowCache.size(), flowCache);
36     }
37
38     public void removeFlow(String rspName, int flowId) {
39         Map<Integer, InstanceIdentifier<Flow>> flowMap = flowCache.get(rspName);
40         if (flowMap != null) {
41             flowMap.remove(flowId);
42             if (flowMap.isEmpty()) {
43                 flowCache.remove(rspName);
44                 LOG.info("removeFlow: removed flowMap {}({}) from cache size {}", rspName, flowId,
45                         flowCache.size());
46             } else {
47                 flowCache.put(rspName, flowMap);
48             }
49         }
50         LOG.info("removeFlow: removed {}({}) from cache size {}", rspName, flowId, flowCache.size());
51     }
52
53     public Map<Integer, InstanceIdentifier<Flow>> getFlows(String rspName) {
54         return flowCache.get(rspName);
55     }
56 }