Seven more Equals/HashCode/StringBuilder replacements
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / ContainerFlow.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.core;
11
12 import java.io.Serializable;
13
14 import org.apache.commons.lang3.builder.EqualsBuilder;
15 import org.apache.commons.lang3.builder.HashCodeBuilder;
16 import org.opendaylight.controller.sal.action.Action;
17 import org.opendaylight.controller.sal.action.SetDlType;
18 import org.opendaylight.controller.sal.action.SetNwDst;
19 import org.opendaylight.controller.sal.action.SetNwSrc;
20 import org.opendaylight.controller.sal.action.SetTpDst;
21 import org.opendaylight.controller.sal.action.SetTpSrc;
22 import org.opendaylight.controller.sal.flowprogrammer.Flow;
23 import org.opendaylight.controller.sal.match.Match;
24 import org.opendaylight.controller.sal.match.MatchType;
25
26 /**
27  * Express a container flow
28  *
29  *
30  *
31  */
32 public class ContainerFlow implements Serializable {
33     private static final long serialVersionUID = 1L;
34     private Match match;
35
36     public ContainerFlow(Match match) {
37         this.match = match;
38     }
39
40     /**
41      * Returns a copy of the Match defined by this Container Flow
42      *
43      * @return Match
44      */
45     public Match getMatch() {
46         return match.clone();
47     }
48
49     @Override
50     public int hashCode() {
51         return HashCodeBuilder.reflectionHashCode(this);
52     }
53
54     @Override
55     public boolean equals(Object obj) {
56         return EqualsBuilder.reflectionEquals(this, obj);
57     }
58
59     @Override
60     public String toString() {
61         return "Container Flow [" + match.toString() + "]";
62     }
63
64     /**
65      * Returns whether the specified flow is allowed
66      *
67      * @return true if the flow is allowed, false otherwise
68      */
69     public boolean allowsFlow(Flow flow) {
70         Match target = flow.getMatch();
71
72         // Check if flow's match is allowed
73         if (!this.allowsMatch(target)) {
74             return false;
75         }
76
77         // Now check if the flow's actions are not allowed
78         // Create a Match which summarizes the list of actions
79         if (flow.getActions() == null) {
80             return true;
81         }
82         Match actionMatch = new Match();
83         for (Action action : flow.getActions()) {
84             switch (action.getType()) {
85             case SET_DL_TYPE:
86                 actionMatch.setField(MatchType.DL_TYPE,
87                         ((Integer) ((SetDlType) action).getDlType())
88                                 .shortValue());
89                 break;
90             case SET_NW_SRC:
91                 actionMatch.setField(MatchType.NW_SRC, ((SetNwSrc) action)
92                         .getAddress());
93                 break;
94             case SET_NW_DST:
95                 actionMatch.setField(MatchType.NW_DST, ((SetNwDst) action)
96                         .getAddress());
97                 break;
98             case SET_TP_SRC:
99                 actionMatch.setField(MatchType.TP_SRC,
100                         ((Integer) ((SetTpSrc) action).getPort()).shortValue());
101                 break;
102             case SET_TP_DST:
103                 actionMatch.setField(MatchType.TP_DST,
104                         ((Integer) ((SetTpDst) action).getPort()).shortValue());
105                 break;
106             default:
107                 // This action cannot conflict
108             }
109         }
110
111         return this.allowsMatch(actionMatch);
112     }
113
114     /**
115      * Returns whether the specified match is allowed
116      *
117      * @param match     the match to test
118      * @return true if the match is allowed, false otherwise
119      */
120     public boolean allowsMatch(Match target) {
121         return !target.conflictWithFilter(this.match);
122     }
123 }