Update MRI projects for Aluminium
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / util / FlowCapableNodeLookups.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 package org.opendaylight.openflowplugin.applications.frsync.util;
9
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
22 import org.opendaylight.yangtools.yang.common.Uint32;
23 import org.opendaylight.yangtools.yang.common.Uint8;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Helpers for flow lookups in {@link FlowCapableNode}.
29  */
30 public final class FlowCapableNodeLookups {
31
32     private static final Logger LOG = LoggerFactory.getLogger(FlowCapableNodeLookups.class);
33
34     private FlowCapableNodeLookups() {
35         throw new IllegalAccessError("non instantiable util class");
36     }
37
38     @NonNull
39     public static Map<Uint8, Table> wrapTablesToMap(@Nullable final Collection<Table> tables) {
40         final Map<Uint8, Table> tableMap;
41
42         if (tables == null) {
43             tableMap = Collections.emptyMap();
44         } else {
45             LOG.trace("tables found: {}", tables.size());
46             tableMap = new HashMap<>();
47             for (Table table : tables) {
48                 tableMap.put(table.getId(), table);
49             }
50         }
51
52         return tableMap;
53     }
54
55     @NonNull
56     public static Map<FlowDescriptor, Flow> wrapFlowsToMap(@Nullable final Collection<Flow> flows) {
57         final Map<FlowDescriptor, Flow> flowMap;
58
59         if (flows == null) {
60             flowMap = Collections.emptyMap();
61         } else {
62             LOG.trace("flows found: {}", flows.size());
63             flowMap = new HashMap<>();
64             for (Flow flow : flows) {
65                 flowMap.put(new FlowDescriptor(flow), flow);
66             }
67         }
68
69         return flowMap;
70     }
71
72     public static Flow flowMapLookupExisting(Flow flow, Map<FlowDescriptor, Flow> flowConfigMap) {
73         return flowConfigMap.get(new FlowDescriptor(flow));
74     }
75
76     @NonNull
77     public static Map<MeterId, Meter> wrapMetersToMap(@Nullable final Collection<Meter> meters) {
78         final Map<MeterId, Meter> meterMap;
79
80         if (meters == null) {
81             meterMap = Collections.emptyMap();
82         } else {
83             LOG.trace("meters found: {}", meters.size());
84             meterMap = new HashMap<>();
85             for (Meter meter : meters) {
86                 meterMap.put(meter.getMeterId(), meter);
87             }
88         }
89
90         return meterMap;
91     }
92
93     @NonNull
94     public static Map<Uint32, Group> wrapGroupsToMap(@Nullable final Collection<Group> groups) {
95         final Map<Uint32, Group> groupMap;
96
97         if (groups == null) {
98             groupMap = Collections.emptyMap();
99         } else {
100             LOG.trace("groups found: {}", groups.size());
101             groupMap = new HashMap<>();
102             for (Group group : groups) {
103                 groupMap.put(group.getGroupId().getValue(), group);
104             }
105         }
106
107         return groupMap;
108     }
109 }