2 * Copyright © 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.sfc.workaround.services;
11 import java.util.HashMap;
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;
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<>();
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<>();
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);
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,
47 flowCache.put(rspName, flowMap);
50 LOG.info("removeFlow: removed {}({}) from cache size {}", rspName, flowId, flowCache.size());
53 public Map<Integer, InstanceIdentifier<Flow>> getFlows(String rspName) {
54 return flowCache.get(rspName);