Merge "Remove "response" from yang."
[packetcable.git] / packetcable-policy-server / src / main / java / org / opendaylight / controller / packetcable / provider / PCMMGateReqBuilder.java
1 /**
2  * Build PCMM gate requests from API QoS Gate objects
3  */
4 package org.opendaylight.controller.packetcable.provider;
5
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;
22
23 import java.net.InetAddress;
24 import java.net.UnknownHostException;
25
26 /**
27  *
28  * PacketCable data processor
29  *
30  */
31 public class PCMMGateReqBuilder {
32
33         private Logger logger = LoggerFactory.getLogger(PCMMGateReqBuilder.class);
34
35         private PCMMGateReq gateReq = null;
36
37         public PCMMGateReqBuilder() {
38                 gateReq = new org.pcmm.gates.impl.PCMMGateReq();
39         }
40
41         public PCMMGateReq getGateReq() {
42                 return gateReq;
43         }
44
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);
50         }
51
52         public void build(InetAddress qosSubId){
53                 ISubscriberID subId = new SubscriberID();
54                 subId.setSourceIPAddress(qosSubId);
55                 gateReq.setSubscriberID(subId);
56         }
57
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();
67                 }
68                 if (qosDir == ServiceFlowDirection.Ds) {
69                         gateDir = Direction.DOWNSTREAM;
70                 } else if (qosDir == ServiceFlowDirection.Us) {
71                         gateDir = Direction.UPSTREAM;
72                 }
73                 gateSpec.setDirection(gateDir);
74                 // DSCP/TOS Overwrite
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);
84                         } else {
85                                 gateSpec.setDSCP_TOSMask((byte)0xff);
86                         }
87                 }
88                 gateReq.setGateSpec(gateSpec);
89         }
90
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);
98                         }
99                 }
100         }
101
102         private InetAddress getByName(String ipAddressStr){
103                 InetAddress ipAddress = null;
104                 try {
105                         ipAddress = InetAddress.getByName(ipAddressStr);
106                 } catch (UnknownHostException e) {
107                         logger.error(e.getMessage());
108                 }
109                 return ipAddress;
110         }
111
112         public void build(Classifier qosClassifier) {
113                 // Legacy classifier
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());
118                 }
119                 if (qosClassifier.getSrcIp() != null) {
120                         InetAddress sip = getByName(qosClassifier.getSrcIp().getValue());
121                         if (sip != null) {
122                                 classifier.setSourceIPAddress(sip);
123                         }
124                 }
125                 if (qosClassifier.getDstIp() != null) {
126                         InetAddress dip = getByName(qosClassifier.getDstIp().getValue());
127                         if (dip != null) {
128                                 classifier.setDestinationIPAddress(dip);
129                         }
130                 }
131                 if (qosClassifier.getSrcPort() != null) {
132                         classifier.setSourcePort(qosClassifier.getSrcPort().getValue().shortValue());
133                 }
134                 if (qosClassifier.getDstPort() != null) {
135                         classifier.setDestinationPort(qosClassifier.getDstPort().getValue().shortValue());
136                 }
137                 if (qosClassifier.getTosByte() != null) {
138                         classifier.setDSCPTOS(qosClassifier.getTosByte().getValue().byteValue());
139                         if (qosClassifier.getTosMask() != null) {
140                                 classifier.setDSCPTOSMask(qosClassifier.getTosMask().getValue().byteValue());
141                         } else {
142                                 // set default TOS mask
143                                 classifier.setDSCPTOSMask((byte)0xff);
144                         }
145                 }
146                 // push the classifier to the gate request
147                 gateReq.setClassifier(classifier);
148         }
149
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());
158                 } else {
159                         extClassifier.setProtocol((short)0);
160                 }
161                 // Source IP address & mask
162                 if (qosExtClassifier.getSrcIp() != null) {
163                         InetAddress sip = getByName(qosExtClassifier.getSrcIp().getValue());
164                         if (sip != null) {
165                                 extClassifier.setSourceIPAddress(sip);
166                                 if (qosExtClassifier.getSrcIpMask() != null) {
167                                         InetAddress sipMask = getByName(qosExtClassifier.getSrcIpMask().getValue());
168                                         extClassifier.setIPSourceMask(sipMask);
169                                 } else {
170                                         // default mask is /32
171                                         extClassifier.setIPSourceMask(getByName("255.255.255.255"));
172                                 }
173                         }
174                 }
175                 // Destination IP address & mask
176                 if (qosExtClassifier.getDstIp() != null) {
177                         InetAddress dip = getByName(qosExtClassifier.getDstIp().getValue());
178                         if (dip != null) {
179                                 extClassifier.setDestinationIPAddress(dip);
180                                 if (qosExtClassifier.getDstIpMask() != null) {
181                                         InetAddress dipMask = getByName(qosExtClassifier.getDstIpMask().getValue());
182                                         extClassifier.setIPDestinationMask(dipMask);
183                                 } else {
184                                         // default mask is /32
185                                         extClassifier.setIPDestinationMask(getByName("255.255.255.255"));
186                                 }
187                         }
188                 }
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();
195                         endPort = startPort;
196                         if (qosExtClassifier.getSrcPortEnd() != null) {
197                                 endPort = qosExtClassifier.getSrcPortEnd().getValue().shortValue();
198                         }
199                         if (startPort > endPort) {
200                                 logger.warn("Start port %d > End port %d in ext-classifier source port range -- forcing to same", startPort, endPort);
201                                 endPort = startPort;
202                         }
203                 }
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();
212                         endPort = startPort;
213                         if (qosExtClassifier.getDstPortEnd() != null) {
214                                 endPort = qosExtClassifier.getDstPortEnd().getValue().shortValue();
215                         }
216                         if (startPort > endPort) {
217                                 logger.warn("Start port %d > End port %d in ext-classifier destination port range -- forcing to same", startPort, endPort);
218                                 endPort = startPort;
219                         }
220                 }
221                 extClassifier.setDestinationPortStart(startPort);
222                 extClassifier.setDestinationPortEnd(endPort);
223                 // DSCP/TOP byte
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());
229                         } else {
230                                 // set default TOS mask
231                                 extClassifier.setDSCPTOSMask((byte)0xff);
232                         }
233                 }
234                 // push the extended classifier to the gate request
235                 gateReq.setClassifier(extClassifier);
236         }
237
238         public void build(Ipv6Classifier qosIpv6Classifier) {
239                 // IPv6 classifier
240                 IIPv6Classifier ipv6Classifier = new org.pcmm.gates.impl.IPv6Classifier();
241                 ipv6Classifier.setPriority((byte) 64);
242                 ipv6Classifier.setActivationState((byte) 0x01);
243                 // Flow Label
244                 if (qosIpv6Classifier.getFlowLabel() != null){
245                         ipv6Classifier.setFlowLabel(qosIpv6Classifier.getFlowLabel());
246                         ipv6Classifier.setFlowLabelEnableFlag((byte)0x01);
247                 }
248                 // Next Header
249                 if (qosIpv6Classifier.getNextHdr() != null){
250                         ipv6Classifier.setNextHdr(qosIpv6Classifier.getNextHdr().getValue().shortValue());
251                 } else {
252                         // default: match any nextHdr is 256 because nextHdr 0 is Hop-by-Hop option
253                         ipv6Classifier.setNextHdr((short)256);
254                 }
255                 // Source IPv6 address & prefix len
256                 byte prefLen;
257                 if (qosIpv6Classifier.getSrcIp6() != null) {
258                         String[] parts = qosIpv6Classifier.getSrcIp6().getValue().split("/");
259                         String Ipv6AddressStr = parts[0];
260                         InetAddress sip6 = getByName(Ipv6AddressStr);
261                         if (sip6 != null) {
262                                 ipv6Classifier.setSourceIPAddress(sip6);
263                         }
264                         prefLen = (byte)128;
265                         if (parts.length > 1) {
266                                 prefLen = (byte)Integer.parseInt(parts[1]);
267                         }
268                         ipv6Classifier.setSourcePrefixLen(prefLen);
269                 }
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);
275                         if (dip6 != null) {
276                                 ipv6Classifier.setDestinationIPAddress(dip6);
277                         }
278                         prefLen = (byte)128;
279                         if (parts.length > 1) {
280                                 prefLen = (byte)Integer.parseInt(parts[1]);
281                         }
282                         ipv6Classifier.setDestinationPrefixLen(prefLen);
283                 }
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();
290                         endPort = startPort;
291                         if (qosIpv6Classifier.getSrcPortEnd() != null) {
292                                 endPort = qosIpv6Classifier.getSrcPortEnd().getValue().shortValue();
293                         }
294                         if (startPort > endPort) {
295                                 logger.warn("Start port %d > End port %d in ipv6-classifier source port range -- forcing to same", startPort, endPort);
296                                 endPort = startPort;
297                         }
298                 }
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();
307                         endPort = startPort;
308                         if (qosIpv6Classifier.getDstPortEnd() != null) {
309                                 endPort = qosIpv6Classifier.getDstPortEnd().getValue().shortValue();
310                         }
311                         if (startPort > endPort) {
312                                 logger.warn("Start port %d > End port %d in ipv6-classifier destination port range -- forcing to same", startPort, endPort);
313                                 endPort = startPort;
314                         }
315                 }
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());
323                         }
324                         if (qosIpv6Classifier.getTcMask() != null) {
325                                 ipv6Classifier.setTcMask(qosIpv6Classifier.getTcMask().getValue().byteValue());
326                         } else {
327                                 // set default TOS mask
328                                 ipv6Classifier.setTcMask((byte)0xff);
329                         }
330                 } else {
331                         // mask 0x00 is match any
332                         ipv6Classifier.setTcMask((byte)0x00);
333                 }
334                 // push the IPv6 classifier to the gate request
335                 gateReq.setClassifier(ipv6Classifier);
336         }
337 }