2 * Build PCMM gate requests from API QoS Gate objects
4 package org.opendaylight.controller.packetcable.provider;
6 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.ServiceFlowDirection;
7 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.TosByte;
8 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.ccap.attributes.AmId;
9 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.classifier.Classifier;
10 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.ext.classifier.ExtClassifier;
11 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.gate.spec.GateSpec;
12 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.ipv6.classifier.Ipv6Classifier;
13 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.traffic.profile.TrafficProfile;
14 import org.pcmm.gates.*;
15 import org.pcmm.gates.IGateSpec.DSCPTOS;
16 import org.pcmm.gates.IGateSpec.Direction;
17 import org.pcmm.gates.impl.DOCSISServiceClassNameTrafficProfile;
18 import org.pcmm.gates.impl.PCMMGateReq;
19 import org.pcmm.gates.impl.SubscriberID;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
28 * PacketCable data processor
31 public class PCMMGateReqBuilder {
33 private Logger logger = LoggerFactory.getLogger(PCMMGateReqBuilder.class);
35 private PCMMGateReq gateReq = null;
37 public PCMMGateReqBuilder() {
38 gateReq = new org.pcmm.gates.impl.PCMMGateReq();
41 public PCMMGateReq getGateReq() {
45 public void build(AmId qosAmId){
46 IAMID amId = new org.pcmm.gates.impl.AMID();
47 amId.setApplicationMgrTag(qosAmId.getAmTag().shortValue());
48 amId.setApplicationType(qosAmId.getAmType().shortValue());
49 gateReq.setAMID(amId);
52 public void build(InetAddress qosSubId){
53 ISubscriberID subId = new SubscriberID();
54 subId.setSourceIPAddress(qosSubId);
55 gateReq.setSubscriberID(subId);
58 public void build(GateSpec qosGateSpec, ServiceFlowDirection scnDirection) {
59 IGateSpec gateSpec = new org.pcmm.gates.impl.GateSpec();
60 // service flow direction
61 ServiceFlowDirection qosDir = null;
62 Direction gateDir = null;
63 if (scnDirection != null) {
64 qosDir = scnDirection;
65 } else if (qosGateSpec.getDirection() != null) {
66 qosDir = qosGateSpec.getDirection();
68 if (qosDir == ServiceFlowDirection.Ds) {
69 gateDir = Direction.DOWNSTREAM;
70 } else if (qosDir == ServiceFlowDirection.Us) {
71 gateDir = Direction.UPSTREAM;
73 gateSpec.setDirection(gateDir);
75 TosByte tosOverwrite = qosGateSpec.getDscpTosOverwrite();
76 if (tosOverwrite != null) {
77 byte gateTos = tosOverwrite.getValue().byteValue();
78 gateSpec.setDSCP_TOSOverwrite(DSCPTOS.ENABLE);
79 gateSpec.setDSCP_TOSOverwrite(gateTos);
80 TosByte tosMask = qosGateSpec.getDscpTosMask();
81 if (tosMask != null) {
82 byte gateTosMask = tosMask.getValue().byteValue();
83 gateSpec.setDSCP_TOSMask(gateTosMask);
85 gateSpec.setDSCP_TOSMask((byte)0xff);
88 gateReq.setGateSpec(gateSpec);
91 public void build(TrafficProfile qosTrafficProfile) {
92 if (qosTrafficProfile.getServiceClassName() != null) {
93 String scn = qosTrafficProfile.getServiceClassName().getValue();
94 DOCSISServiceClassNameTrafficProfile trafficProfile = new DOCSISServiceClassNameTrafficProfile();
95 if (scn.length() <= 16) { // NB.16 char SCN is max length per PCMM spec
96 trafficProfile.setServiceClassName(scn);
97 gateReq.setTrafficProfile(trafficProfile);
102 private InetAddress getByName(String ipAddressStr){
103 InetAddress ipAddress = null;
105 ipAddress = InetAddress.getByName(ipAddressStr);
106 } catch (UnknownHostException e) {
107 logger.error(e.getMessage());
112 public void build(Classifier qosClassifier) {
114 IClassifier classifier = new org.pcmm.gates.impl.Classifier();
115 classifier.setPriority((byte) 64);
116 if (qosClassifier.getProtocol() != null){
117 classifier.setProtocol(qosClassifier.getProtocol().getValue().shortValue());
119 if (qosClassifier.getSrcIp() != null) {
120 InetAddress sip = getByName(qosClassifier.getSrcIp().getValue());
122 classifier.setSourceIPAddress(sip);
125 if (qosClassifier.getDstIp() != null) {
126 InetAddress dip = getByName(qosClassifier.getDstIp().getValue());
128 classifier.setDestinationIPAddress(dip);
131 if (qosClassifier.getSrcPort() != null) {
132 classifier.setSourcePort(qosClassifier.getSrcPort().getValue().shortValue());
134 if (qosClassifier.getDstPort() != null) {
135 classifier.setDestinationPort(qosClassifier.getDstPort().getValue().shortValue());
137 if (qosClassifier.getTosByte() != null) {
138 classifier.setDSCPTOS(qosClassifier.getTosByte().getValue().byteValue());
139 if (qosClassifier.getTosMask() != null) {
140 classifier.setDSCPTOSMask(qosClassifier.getTosMask().getValue().byteValue());
142 // set default TOS mask
143 classifier.setDSCPTOSMask((byte)0xff);
146 // push the classifier to the gate request
147 gateReq.setClassifier(classifier);
150 public void build(ExtClassifier qosExtClassifier) {
151 // Extended classifier
152 IExtendedClassifier extClassifier = new org.pcmm.gates.impl.ExtendedClassifier();
153 extClassifier.setPriority((byte) 64);
154 extClassifier.setActivationState((byte) 0x01);
155 // Protocol -- zero is match any
156 if (qosExtClassifier.getProtocol() != null){
157 extClassifier.setProtocol(qosExtClassifier.getProtocol().getValue().shortValue());
159 extClassifier.setProtocol((short)0);
161 // Source IP address & mask
162 if (qosExtClassifier.getSrcIp() != null) {
163 InetAddress sip = getByName(qosExtClassifier.getSrcIp().getValue());
165 extClassifier.setSourceIPAddress(sip);
166 if (qosExtClassifier.getSrcIpMask() != null) {
167 InetAddress sipMask = getByName(qosExtClassifier.getSrcIpMask().getValue());
168 extClassifier.setIPSourceMask(sipMask);
170 // default mask is /32
171 extClassifier.setIPSourceMask(getByName("255.255.255.255"));
175 // Destination IP address & mask
176 if (qosExtClassifier.getDstIp() != null) {
177 InetAddress dip = getByName(qosExtClassifier.getDstIp().getValue());
179 extClassifier.setDestinationIPAddress(dip);
180 if (qosExtClassifier.getDstIpMask() != null) {
181 InetAddress dipMask = getByName(qosExtClassifier.getDstIpMask().getValue());
182 extClassifier.setIPDestinationMask(dipMask);
184 // default mask is /32
185 extClassifier.setIPDestinationMask(getByName("255.255.255.255"));
189 // default source port range must be set to match any even if qosExtClassifier has no range
190 // match any port range is 0-65535, NOT 0-0
191 short startPort = (short)0;
192 short endPort = (short)65535;
193 if (qosExtClassifier.getSrcPortStart() != null) {
194 startPort = qosExtClassifier.getSrcPortStart().getValue().shortValue();
196 if (qosExtClassifier.getSrcPortEnd() != null) {
197 endPort = qosExtClassifier.getSrcPortEnd().getValue().shortValue();
199 if (startPort > endPort) {
200 logger.warn("Start port %d > End port %d in ext-classifier source port range -- forcing to same", startPort, endPort);
204 extClassifier.setSourcePortStart(startPort);
205 extClassifier.setSourcePortEnd(endPort);
206 // default destination port range must be set to match any even if qosExtClassifier has no range
207 // match any port range is 0-65535, NOT 0-0
208 startPort = (short)0;
209 endPort = (short)65535;
210 if (qosExtClassifier.getDstPortStart() != null) {
211 startPort = qosExtClassifier.getDstPortStart().getValue().shortValue();
213 if (qosExtClassifier.getDstPortEnd() != null) {
214 endPort = qosExtClassifier.getDstPortEnd().getValue().shortValue();
216 if (startPort > endPort) {
217 logger.warn("Start port %d > End port %d in ext-classifier destination port range -- forcing to same", startPort, endPort);
221 extClassifier.setDestinationPortStart(startPort);
222 extClassifier.setDestinationPortEnd(endPort);
224 if (qosExtClassifier.getTosByte() != null) {
225 // OR in the DSCP/TOS enable bit 0x01
226 extClassifier.setDSCPTOS((byte) (qosExtClassifier.getTosByte().getValue().byteValue() | 0x01));
227 if (qosExtClassifier.getTosMask() != null) {
228 extClassifier.setDSCPTOSMask(qosExtClassifier.getTosMask().getValue().byteValue());
230 // set default TOS mask
231 extClassifier.setDSCPTOSMask((byte)0xff);
234 // push the extended classifier to the gate request
235 gateReq.setClassifier(extClassifier);
238 public void build(Ipv6Classifier qosIpv6Classifier) {
240 IIPv6Classifier ipv6Classifier = new org.pcmm.gates.impl.IPv6Classifier();
241 ipv6Classifier.setPriority((byte) 64);
242 ipv6Classifier.setActivationState((byte) 0x01);
244 if (qosIpv6Classifier.getFlowLabel() != null){
245 ipv6Classifier.setFlowLabel(qosIpv6Classifier.getFlowLabel());
246 ipv6Classifier.setFlowLabelEnableFlag((byte)0x01);
249 if (qosIpv6Classifier.getNextHdr() != null){
250 ipv6Classifier.setNextHdr(qosIpv6Classifier.getNextHdr().getValue().shortValue());
252 // default: match any nextHdr is 256 because nextHdr 0 is Hop-by-Hop option
253 ipv6Classifier.setNextHdr((short)256);
255 // Source IPv6 address & prefix len
257 if (qosIpv6Classifier.getSrcIp6() != null) {
258 String[] parts = qosIpv6Classifier.getSrcIp6().getValue().split("/");
259 String Ipv6AddressStr = parts[0];
260 InetAddress sip6 = getByName(Ipv6AddressStr);
262 ipv6Classifier.setSourceIPAddress(sip6);
265 if (parts.length > 1) {
266 prefLen = (byte)Integer.parseInt(parts[1]);
268 ipv6Classifier.setSourcePrefixLen(prefLen);
270 // Destination IPv6 address & prefix len
271 if (qosIpv6Classifier.getDstIp6() != null) {
272 String[] parts = qosIpv6Classifier.getDstIp6().getValue().split("/");
273 String Ipv6AddressStr = parts[0];
274 InetAddress dip6 = getByName(Ipv6AddressStr);
276 ipv6Classifier.setDestinationIPAddress(dip6);
279 if (parts.length > 1) {
280 prefLen = (byte)Integer.parseInt(parts[1]);
282 ipv6Classifier.setDestinationPrefixLen(prefLen);
284 // default source port range must be set to match any -- even if qosExtClassifier has no range value
285 // match any port range is 0-65535, NOT 0-0
286 short startPort = (short)0;
287 short endPort = (short)65535;
288 if (qosIpv6Classifier.getSrcPortStart() != null) {
289 startPort = qosIpv6Classifier.getSrcPortStart().getValue().shortValue();
291 if (qosIpv6Classifier.getSrcPortEnd() != null) {
292 endPort = qosIpv6Classifier.getSrcPortEnd().getValue().shortValue();
294 if (startPort > endPort) {
295 logger.warn("Start port %d > End port %d in ipv6-classifier source port range -- forcing to same", startPort, endPort);
299 ipv6Classifier.setSourcePortStart(startPort);
300 ipv6Classifier.setSourcePortEnd(endPort);
301 // default destination port range must be set to match any -- even if qosExtClassifier has no range value
302 // match any port range is 0-65535, NOT 0-0
303 startPort = (short)0;
304 endPort = (short)65535;
305 if (qosIpv6Classifier.getDstPortStart() != null) {
306 startPort = qosIpv6Classifier.getDstPortStart().getValue().shortValue();
308 if (qosIpv6Classifier.getDstPortEnd() != null) {
309 endPort = qosIpv6Classifier.getDstPortEnd().getValue().shortValue();
311 if (startPort > endPort) {
312 logger.warn("Start port %d > End port %d in ipv6-classifier destination port range -- forcing to same", startPort, endPort);
316 ipv6Classifier.setDestinationPortStart(startPort);
317 ipv6Classifier.setDestinationPortEnd(endPort);
318 // TC low, high, mask
319 if (qosIpv6Classifier.getTcLow() != null) {
320 ipv6Classifier.setTcLow(qosIpv6Classifier.getTcLow().getValue().byteValue());
321 if (qosIpv6Classifier.getTcHigh() != null) {
322 ipv6Classifier.setTcHigh(qosIpv6Classifier.getTcHigh().getValue().byteValue());
324 if (qosIpv6Classifier.getTcMask() != null) {
325 ipv6Classifier.setTcMask(qosIpv6Classifier.getTcMask().getValue().byteValue());
327 // set default TOS mask
328 ipv6Classifier.setTcMask((byte)0xff);
331 // mask 0x00 is match any
332 ipv6Classifier.setTcMask((byte)0x00);
334 // push the IPv6 classifier to the gate request
335 gateReq.setClassifier(ipv6Classifier);