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