blob: 63e65af74fe611a578f44e04a732d9d50e318cd0 [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 /**
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
105 ExtensionMappingInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
106 this.extensionTreatment = extension;
107 this.deviceId = deviceId;
108 }
109
110 public ExtensionTreatment extensionMappingInstruction() {
111 return extensionTreatment;
112 }
113
114 public DeviceId deviceId() {
115 return deviceId;
116 }
117
118 @Override
119 public Type type() {
120 return Type.EXTENSION;
121 }
122
123 @Override
124 public String toString() {
125 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
126 }
127
128 @Override
129 public int hashCode() {
130 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
131 }
132
133 @Override
134 public boolean equals(Object obj) {
135 if (this == obj) {
136 return true;
137 }
138 if (obj instanceof ExtensionMappingInstructionWrapper) {
139 ExtensionMappingInstructionWrapper that =
140 (ExtensionMappingInstructionWrapper) obj;
141 return Objects.equals(extensionTreatment, that.extensionTreatment)
142 && Objects.equals(deviceId, that.deviceId);
143
144 }
145 return false;
146 }
147 }
148}