Bug 5540 - Remove ConvertorManager singleton
[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     private FlowRegistryKeyFactory() {
26         // Hide implicit constructor
27     }
28
29     public static FlowRegistryKey create(final Flow flow) {
30         return new FlowRegistryKeyDto(flow);
31     }
32
33     private static final class FlowRegistryKeyDto implements FlowRegistryKey {
34         private final short tableId;
35         private final int priority;
36         private final BigInteger cookie;
37         private final Match match;
38
39         private FlowRegistryKeyDto(final Flow flow) {
40             //TODO: mandatory flow input values (or default values) should be specified via yang model
41             tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
42             priority = MoreObjects.firstNonNull(flow.getPriority(), OFConstants.DEFAULT_FLOW_PRIORITY);
43             match = MoreObjects.firstNonNull(flow.getMatch(), OFConstants.EMPTY_MATCH);
44             cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
45         }
46
47         @Override
48         public boolean equals(final Object o) {
49             if (this == o) {
50                 return true;
51             }
52
53             if (o == null || !(o instanceof FlowRegistryKey)) {
54                 return false;
55             }
56
57             final FlowRegistryKey that = (FlowRegistryKey) o;
58
59             return getPriority() == that.getPriority() &&
60                     getTableId() == that.getTableId() &&
61                     getCookie().equals(that.getCookie()) &&
62                     MatchComparatorFactory.createMatch().areObjectsEqual(getMatch(), that.getMatch());
63         }
64
65         @Override
66         public int hashCode() {
67             int result = tableId;
68             result = 31 * result + priority;
69             result = 31 * result + cookie.hashCode();
70             result = 31 * result + match.hashCode();
71             return result;
72         }
73
74         @Override
75         public short getTableId() {
76             return tableId;
77         }
78
79         @Override
80         public int getPriority() {
81             return priority;
82         }
83
84         @Override
85         public BigInteger getCookie() {
86             return cookie;
87         }
88
89         @Override
90         public Match getMatch() {
91             return match;
92         }
93     }
94 }