Merge "Custom mailbox that is bounded and instrumented."
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / frm / FlowCookieProducer.java
1 /**
2  * Copyright (c) 2014 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.frm;
10
11 import java.math.BigInteger;
12
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
14 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15
16 import com.google.common.base.Preconditions;
17 import com.google.common.util.concurrent.AtomicLongMap;
18
19 /**
20  * forwardingrules-manager
21  * org.opendaylight.controller.frm
22  *
23  * Singleton FlowCookieProducer contains a FlowCookie generator which is generated unique
24  * flowCookie identifier for every flow in same Table. That could help with quick
25  * identification of flow statistic because DataStore/CONFIGURATION could contains
26  * a lot of flows with same flowCookie. So we are replacing original flowCookie
27  * with unique and we are building final FlowCookieMap in DataStore/OPERATIONAL
28  *
29  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
30  *
31  * Created: Jun 13, 2014
32  */
33 public enum FlowCookieProducer {
34
35     INSTANCE;
36
37     /* Flow_Cookie_Key and Flow_Ids MapHolder */
38     private static final AtomicLongMap<InstanceIdentifier<Table>> cookieKeys = AtomicLongMap.create();
39
40     /**
41      * Method returns the unique cookie for a node table.
42      * Flow Cookie Key signs List<FlowId> for a right flow statistic identification
43      * in the DataStore/operational.
44      * We need a List<FlowId> because system doesn't guarantee unique mapping
45      * from flow_cookie to flow_id. REST Operations doesn't used FRM yet, so
46      * cookie from user input could have a user input flow ID and an alien system ID
47      * which is generated by system.
48      *
49      * @param InstanceIdentifier<Table> tableIdentifier
50      * @return unique BigInteger flowCookie for a node table
51      */
52     public BigInteger getNewCookie(final InstanceIdentifier<Table> tableIdentifier) {
53         FlowCookieProducer.validationTableIdentifier(tableIdentifier);
54         if ( cookieKeys.containsKey(tableIdentifier)) {
55             /* new identifier always starts from ONE because
56              * ZERO is reserved for the NO_COOKIES flows */
57             return BigInteger.valueOf(cookieKeys.addAndGet(tableIdentifier, 1L));
58         } else {
59             return BigInteger.valueOf(cookieKeys.incrementAndGet(tableIdentifier));
60         }
61     }
62
63     /**
64      * Method cleans the node table flow_cookie_key for the disconnected Node.
65      *
66      * @param InstanceIdentifier<Table> tableIdentifier
67      */
68     public void clean(final InstanceIdentifier<Table> tableIdentifier) {
69         FlowCookieProducer.validationTableIdentifier(tableIdentifier);
70         cookieKeys.remove(tableIdentifier);
71     }
72
73     /*
74      * Help the TableIdentifer input validation method
75      */
76     private static void validationTableIdentifier(final InstanceIdentifier<Table> tableIdent) {
77         Preconditions.checkArgument(tableIdent != null, "Input validation exception: TableIdentifier can not be null !");
78     }
79 }