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