Merge "Sonar issues"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / FlowRegistryKeyFactory.java
1 /*
2  * Copyright (c) 2015 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.impl.registry.flow;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import java.math.BigInteger;
14 import org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
16 import org.opendaylight.openflowplugin.impl.util.MatchComparatorFactory;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20
21 /**
22  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
23  */
24 public class FlowRegistryKeyFactory {
25
26     private FlowRegistryKeyFactory() {
27         // Hide implicit constructor
28     }
29
30     public static FlowRegistryKey create(final Flow flow) {
31         return new FlowRegistryKeyDto(flow);
32     }
33
34     private static final class FlowRegistryKeyDto implements FlowRegistryKey {
35         private final short tableId;
36         private final int priority;
37         private final BigInteger cookie;
38         private final Match match;
39
40         private FlowRegistryKeyDto(final Flow flow) {
41             //TODO: mandatory flow input values (or default values) should be specified via yang model
42             tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
43             priority = MoreObjects.firstNonNull(flow.getPriority(), OFConstants.DEFAULT_FLOW_PRIORITY);
44             match = MoreObjects.firstNonNull(flow.getMatch(), OFConstants.EMPTY_MATCH);
45             cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
46         }
47
48         @Override
49         public boolean equals(final Object o) {
50             if (this == o) {
51                 return true;
52             }
53
54             if (o == null || !(o instanceof FlowRegistryKey)) {
55                 return false;
56             }
57
58             final FlowRegistryKey that = (FlowRegistryKey) o;
59
60             return getPriority() == that.getPriority() &&
61                     getTableId() == that.getTableId() &&
62                     getCookie().equals(that.getCookie()) &&
63                     MatchComparatorFactory.createMatch().areObjectsEqual(getMatch(), that.getMatch());
64         }
65
66         @Override
67         public int hashCode() {
68             int result = tableId;
69             result = 31 * result + priority;
70             result = 31 * result + cookie.hashCode();
71             result = 31 * result + match.hashCode();
72             return result;
73         }
74
75         @Override
76         public short getTableId() {
77             return tableId;
78         }
79
80         @Override
81         public int getPriority() {
82             return priority;
83         }
84
85         @Override
86         public BigInteger getCookie() {
87             return cookie;
88         }
89
90         @Override
91         public Match getMatch() {
92             return match;
93         }
94     }
95 }