blob: 0360b44178960b589f3b9db0c1084d946e6a7e5e [file] [log] [blame]
Jian Li136fd6c2017-02-10 14:54:59 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.mapping.instructions;
17
18import org.onosproject.net.DeviceId;
19import org.onosproject.net.flow.instructions.ExtensionTreatment;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24import static org.onosproject.mapping.instructions.MulticastMappingInstruction.*;
25import static org.onosproject.mapping.instructions.UnicastMappingInstruction.*;
26
27/**
28 * Factory class for creating various mapping instructions.
29 */
30public final class MappingInstructions {
31
32 private static final String SEPARATOR = ":";
33
34 /**
35 * Prevents instantiation from external.
36 */
37 private MappingInstructions() {}
38
39 /**
40 * Creates an unicast weight instruction.
41 *
42 * @param weight weight value
43 * @return an unicast mapping instruction
44 */
45 public static UnicastMappingInstruction unicastWeight(int weight) {
46 return new UnicastMappingInstruction.WeightMappingInstruction(
47 UnicastType.WEIGHT, weight);
48 }
49
50 /**
51 * Creates an unicast priority instruction.
52 *
53 * @param priority priority value
54 * @return an unicast mapping instruction
55 */
56 public static UnicastMappingInstruction unicastPriority(int priority) {
57 return new UnicastMappingInstruction.PriorityMappingInstruction(
58 UnicastType.PRIORITY, priority);
59 }
60
61 /**
62 * Creates a multicast weight instruction.
63 *
64 * @param weight weight value
65 * @return a multicast mapping instruction
66 */
67 public static MulticastMappingInstruction multicastWeight(int weight) {
68 return new MulticastMappingInstruction.WeightMappingInstruction(
69 MulticastType.WEIGHT, weight);
70 }
71
72 /**
73 * Creates a multicast priority instruction.
74 *
75 * @param priority priority value
76 * @return a multicast mapping instruction
77 */
78 public static MulticastMappingInstruction multicastPriority(int priority) {
79 return new MulticastMappingInstruction.PriorityMappingInstruction(
80 MulticastType.PRIORITY, priority);
81 }
82
83 /**
84 * Creates no action mapping instruction.
85 *
86 * @return an action mapping instruction
87 */
88 public static ActionMappingInstruction noAction() {
89 return new ActionMappingInstruction.NoActionMappingInstruction();
90 }
91
92 /**
93 * Creates native forward mapping instruction.
94 *
95 * @return an action mapping instruction
96 */
97 public static ActionMappingInstruction nativeForwardAction() {
98 return new ActionMappingInstruction.NativeForwardMappingInstruction();
99 }
100
101 /**
102 * Creates forward mapping instruction.
103 *
104 * @return an action mapping instruction
105 */
106 public static ActionMappingInstruction forwardAction() {
107 return new ActionMappingInstruction.ForwardMappingInstruction();
108 }
109
110 /**
111 * Creates drop mapping instruction.
112 *
113 * @return an action mapping instruction
114 */
115 public static ActionMappingInstruction dropAction() {
116 return new ActionMappingInstruction.DropMappingInstruction();
117 }
118
119 /**
120 * Creates an extension mapping instruction.
121 *
122 * @param extension extension mapping instruction
123 * @param deviceId device identifier
124 * @return extension mapping instruction
125 */
126 public static ExtensionMappingInstructionWrapper extension(ExtensionTreatment extension,
127 DeviceId deviceId) {
128 checkNotNull(extension, "Extension instruction cannot be null");
129 checkNotNull(deviceId, "Device ID cannot be null");
130 return new ExtensionMappingInstructionWrapper(extension, deviceId);
131 }
132
133 /**
134 * Extension mapping instruction.
135 */
136 public static class ExtensionMappingInstructionWrapper implements MappingInstruction {
137
138 private final ExtensionTreatment extensionTreatment;
139 private final DeviceId deviceId;
140
141 ExtensionMappingInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
142 this.extensionTreatment = extension;
143 this.deviceId = deviceId;
144 }
145
146 public ExtensionTreatment extensionMappingInstruction() {
147 return extensionTreatment;
148 }
149
150 public DeviceId deviceId() {
151 return deviceId;
152 }
153
154 @Override
155 public Type type() {
156 return Type.EXTENSION;
157 }
158
159 @Override
160 public String toString() {
161 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
162 }
163
164 @Override
165 public int hashCode() {
166 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
167 }
168
169 @Override
170 public boolean equals(Object obj) {
171 if (this == obj) {
172 return true;
173 }
174 if (obj instanceof ExtensionMappingInstructionWrapper) {
175 ExtensionMappingInstructionWrapper that =
176 (ExtensionMappingInstructionWrapper) obj;
177 return Objects.equals(extensionTreatment, that.extensionTreatment)
178 && Objects.equals(deviceId, that.deviceId);
179
180 }
181 return false;
182 }
183 }
184}