Bug 1029: Remove dead code: samples/clustersession
[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.opendaylight.controller.sal.action.Action;
15 import org.opendaylight.controller.sal.action.SetDlType;
16 import org.opendaylight.controller.sal.action.SetNwDst;
17 import org.opendaylight.controller.sal.action.SetNwSrc;
18 import org.opendaylight.controller.sal.action.SetTpDst;
19 import org.opendaylight.controller.sal.action.SetTpSrc;
20 import org.opendaylight.controller.sal.action.SetVlanId;
21 import org.opendaylight.controller.sal.flowprogrammer.Flow;
22 import org.opendaylight.controller.sal.match.Match;
23 import org.opendaylight.controller.sal.match.MatchType;
24
25 /**
26  * Express a container flow
27  *
28  *
29  *
30  */
31 public class ContainerFlow implements Serializable {
32     private static final long serialVersionUID = 1L;
33     private Match match;
34
35     public ContainerFlow(Match match) {
36         this.match = match;
37     }
38
39     /**
40      * Returns a copy of the Match defined by this Container Flow
41      *
42      * @return Match
43      */
44     public Match getMatch() {
45         return match.clone();
46     }
47
48     @Override
49     public int hashCode() {
50         final int prime = 31;
51         int result = 1;
52         result = prime * result + ((match == null) ? 0 : match.hashCode());
53         return result;
54     }
55
56     @Override
57     public boolean equals(Object obj) {
58         if (this == obj) {
59             return true;
60         }
61         if (obj == null) {
62             return false;
63         }
64         if (getClass() != obj.getClass()) {
65             return false;
66         }
67         ContainerFlow other = (ContainerFlow) obj;
68         if (match == null) {
69             if (other.match != null) {
70                 return false;
71             }
72         } else if (!match.equals(other.match)) {
73             return false;
74         }
75         return true;
76     }
77
78     @Override
79     public String toString() {
80         return "Container Flow [" + match.toString() + "]";
81     }
82
83     /**
84      * Returns whether the specified flow is allowed
85      *
86      * @return true if the flow is allowed, false otherwise
87      */
88     public boolean allowsFlow(Flow flow) {
89         Match target = flow.getMatch();
90
91         // Check if flow's match is allowed
92         if (!this.allowsMatch(target)) {
93             return false;
94         }
95
96         // Now check if the flow's actions are not allowed
97         // Create a Match which summarizes the list of actions
98         if (flow.getActions() == null) {
99             return true;
100         }
101         Match actionMatch = new Match();
102         for (Action action : flow.getActions()) {
103             switch (action.getType()) {
104             case SET_VLAN_ID:
105                 actionMatch.setField(MatchType.DL_VLAN,
106                         ((Integer) ((SetVlanId) action).getVlanId())
107                                 .shortValue());
108                 break;
109             case SET_DL_TYPE:
110                 actionMatch.setField(MatchType.DL_TYPE,
111                         ((Integer) ((SetDlType) action).getDlType())
112                                 .shortValue());
113                 break;
114             case SET_NW_SRC:
115                 actionMatch.setField(MatchType.NW_SRC, ((SetNwSrc) action)
116                         .getAddress());
117                 break;
118             case SET_NW_DST:
119                 actionMatch.setField(MatchType.NW_DST, ((SetNwDst) action)
120                         .getAddress());
121                 break;
122             case SET_TP_SRC:
123                 actionMatch.setField(MatchType.TP_SRC,
124                         ((Integer) ((SetTpSrc) action).getPort()).shortValue());
125                 break;
126             case SET_TP_DST:
127                 actionMatch.setField(MatchType.TP_DST,
128                         ((Integer) ((SetTpDst) action).getPort()).shortValue());
129                 break;
130             default:
131                 // This action cannot conflict
132             }
133         }
134
135         return this.allowsMatch(actionMatch);
136     }
137
138     /**
139      * Returns whether the specified match is allowed
140      *
141      * @param match the match to test
142      * @return true if the match is allowed, false otherwise
143      */
144     public boolean allowsMatch(Match target) {
145         return !target.conflictWithFilter(this.match);
146     }
147 }