blob: e38b7952ac5d5988786885cd189eb83b493b10d2 [file] [log] [blame]
Jian Li136fd6c2017-02-10 14:54:59 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Li136fd6c2017-02-10 14:54:59 +09003 *
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 /**
Jian Li136fd6c2017-02-10 14:54:59 +090084 * Creates an extension mapping instruction.
85 *
86 * @param extension extension mapping instruction
87 * @param deviceId device identifier
88 * @return extension mapping instruction
89 */
90 public static ExtensionMappingInstructionWrapper extension(ExtensionTreatment extension,
91 DeviceId deviceId) {
92 checkNotNull(extension, "Extension instruction cannot be null");
93 checkNotNull(deviceId, "Device ID cannot be null");
94 return new ExtensionMappingInstructionWrapper(extension, deviceId);
95 }
96
97 /**
98 * Extension mapping instruction.
99 */
100 public static class ExtensionMappingInstructionWrapper implements MappingInstruction {
101
102 private final ExtensionTreatment extensionTreatment;
103 private final DeviceId deviceId;
104
Jian Licc169f32017-02-24 20:13:23 +0900105 /**
106 * Defaults constructor for extension mapping instruction wrapper.
107 *
108 * @param extension extension treatment
109 * @param deviceId device identifier
110 */
Jian Li136fd6c2017-02-10 14:54:59 +0900111 ExtensionMappingInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
112 this.extensionTreatment = extension;
113 this.deviceId = deviceId;
114 }
115
Jian Licc169f32017-02-24 20:13:23 +0900116 /**
117 * Obtains the extension treatment.
118 *
119 * @return extension treatment
120 */
Jian Li136fd6c2017-02-10 14:54:59 +0900121 public ExtensionTreatment extensionMappingInstruction() {
122 return extensionTreatment;
123 }
124
Jian Licc169f32017-02-24 20:13:23 +0900125 /**
126 * Obtains the device identifier.
127 *
128 * @return device identifer
129 */
Jian Li136fd6c2017-02-10 14:54:59 +0900130 public DeviceId deviceId() {
131 return deviceId;
132 }
133
134 @Override
135 public Type type() {
136 return Type.EXTENSION;
137 }
138
139 @Override
140 public String toString() {
141 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
142 }
143
144 @Override
145 public int hashCode() {
146 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
147 }
148
149 @Override
150 public boolean equals(Object obj) {
151 if (this == obj) {
152 return true;
153 }
154 if (obj instanceof ExtensionMappingInstructionWrapper) {
155 ExtensionMappingInstructionWrapper that =
156 (ExtensionMappingInstructionWrapper) obj;
157 return Objects.equals(extensionTreatment, that.extensionTreatment)
158 && Objects.equals(deviceId, that.deviceId);
159
160 }
161 return false;
162 }
163 }
164}