Sync: md-sal augmentation re-model with broker. Traffic profile: Best Effort hooked up.
[packetcable.git] / packetcable-provider / src / main / java / org / opendaylight / controller / packetcable / provider / processors / PCMMDataProcessor.java
1 /**
2  * 
3  */
4 package org.opendaylight.controller.packetcable.provider.processors;
5
6 import java.net.InetAddress;
7 import java.net.UnknownHostException;
8
9 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
10 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
11 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.traffic.profile.rev140808.TrafficProfileBestEffortAttributes;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.traffic.profile.rev140808.TrafficProfileDocsisServiceClassNameAttributes;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.traffic.profile.rev140808.TrafficProfileFlowspecAttributes;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.traffic.profile.rev140808.traffic.profile.best.effort.attributes.BeAuthorizedEnvelope;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.traffic.profile.rev140808.traffic.profile.best.effort.attributes.BeCommittedEnvelope;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.traffic.profile.rev140808.traffic.profile.best.effort.attributes.BereservedEnvelope;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140120.SubscriberIdRpcAddFlow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140120.TcpMatchRangesAttributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140120.TcpMatchRangesRpcAddFlow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140120.UdpMatchRangesAttributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140120.UdpMatchRangesRpcAddFlow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140120.tcp.match.ranges.attributes.TcpMatchRanges;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packetcable.match.types.rev140120.udp.match.ranges.attributes.UpdMatchRanges;
26 import org.pcmm.gates.IClassifier;
27 import org.pcmm.gates.IExtendedClassifier;
28 import org.pcmm.gates.ITrafficProfile;
29 import org.pcmm.gates.impl.BestEffortService;
30 import org.pcmm.gates.impl.DOCSISServiceClassNameTrafficProfile;
31 import org.pcmm.gates.impl.ExtendedClassifier;
32 import org.pcmm.gates.impl.BestEffortService.BEEnvelop;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * 
38  * PacketCable data processor
39  * 
40  */
41 public class PCMMDataProcessor {
42
43         private Logger logger = LoggerFactory.getLogger(PCMMDataProcessor.class);
44
45         public ITrafficProfile process(TrafficProfileBestEffortAttributes bestEffort) {
46                 BestEffortService trafficProfile = new BestEffortService(BestEffortService.DEFAULT_ENVELOP);
47                 getBEAuthorizedEnvelop(bestEffort, trafficProfile);
48                 getBEReservedEnvelop(bestEffort, trafficProfile);
49                 getBECommittedEnvelop(bestEffort, trafficProfile);
50                 return trafficProfile;
51         }
52
53         public ITrafficProfile process(TrafficProfileDocsisServiceClassNameAttributes docsis) {
54                 DOCSISServiceClassNameTrafficProfile trafficProfile = new DOCSISServiceClassNameTrafficProfile();
55                 trafficProfile.setServiceClassName(docsis.getServiceClassName());
56                 return trafficProfile;
57         }
58
59         // TODO
60         public ITrafficProfile process(TrafficProfileFlowspecAttributes flowSpec) {
61                 throw new UnsupportedOperationException("Not impelemnted yet");
62         }
63
64         public IClassifier process(Match match) {
65                 ExtendedClassifier classifier = new ExtendedClassifier();
66                 classifier.setProtocol(IClassifier.Protocol.NONE);
67                 getUdpMatchRangeValues(match.getAugmentation(UdpMatchRangesRpcAddFlow.class), classifier);
68                 getTcpMatchRangesValues(match.getAugmentation(TcpMatchRangesRpcAddFlow.class), classifier);
69                 SubscriberIdRpcAddFlow subId = match.getAugmentation(SubscriberIdRpcAddFlow.class);
70                 Ipv6Address ipv6Address = subId.getSubscriberId().getIpv6Address();
71                 if (ipv6Address != null)
72                         try {
73                                 classifier.setDestinationIPAddress(InetAddress.getByName(ipv6Address.getValue()));
74                         } catch (UnknownHostException e) {
75                                 logger.error(e.getMessage());
76                         }
77
78                 Ipv4Address ipv4Address = subId.getSubscriberId().getIpv4Address();
79                 if (ipv4Address != null)
80                         try {
81                                 classifier.setDestinationIPAddress(InetAddress.getByName(ipv4Address.getValue()));
82                         } catch (UnknownHostException e) {
83                                 logger.error(e.getMessage());
84                         }
85                 return classifier;
86         }
87
88         private void getBECommittedEnvelop(TrafficProfileBestEffortAttributes bestEffort, BestEffortService trafficProfile) {
89                 BEEnvelop committedEnvelop = trafficProfile.getCommittedEnvelop();
90                 BeCommittedEnvelope beCommittedEnvelope = bestEffort.getBeCommittedEnvelope();
91                 if (beCommittedEnvelope.getTrafficPriority() != null)
92                         committedEnvelop.setTrafficPriority(beCommittedEnvelope.getTrafficPriority().byteValue());
93                 else
94                         committedEnvelop.setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
95                 if (beCommittedEnvelope.getMaximumTrafficBurst() != null)
96                         committedEnvelop.setMaximumTrafficBurst(beCommittedEnvelope.getMaximumTrafficBurst().intValue());
97                 else
98                         committedEnvelop.setMaximumTrafficBurst(BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
99                 if (beCommittedEnvelope.getRequestTransmissionPolicy() != null)
100                         committedEnvelop.setRequestTransmissionPolicy(beCommittedEnvelope.getRequestTransmissionPolicy().intValue());
101                 // else
102                 // committedEnvelop.setRequestTransmissionPolicy(PCMMGlobalConfig.BETransmissionPolicy);
103                 if (beCommittedEnvelope.getMaximumSustainedTrafficRate() != null)
104                         committedEnvelop.setMaximumSustainedTrafficRate(beCommittedEnvelope.getMaximumSustainedTrafficRate().intValue());
105                 // else
106                 // committedEnvelop.setMaximumSustainedTrafficRate(PCMMGlobalConfig.DefaultLowBestEffortTrafficRate);
107         }
108
109         private void getBEReservedEnvelop(TrafficProfileBestEffortAttributes bestEffort, BestEffortService trafficProfile) {
110                 BEEnvelop reservedEnvelop = trafficProfile.getReservedEnvelop();
111                 BereservedEnvelope beReservedEnvelope = bestEffort.getBereservedEnvelope();
112                 if (beReservedEnvelope.getTrafficPriority() != null)
113                         reservedEnvelop.setTrafficPriority(beReservedEnvelope.getTrafficPriority().byteValue());
114                 else
115                         reservedEnvelop.setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
116                 if (beReservedEnvelope.getMaximumTrafficBurst() != null)
117                         reservedEnvelop.setMaximumTrafficBurst(beReservedEnvelope.getMaximumTrafficBurst().intValue());
118                 else
119                         reservedEnvelop.setMaximumTrafficBurst(BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
120                 if (beReservedEnvelope.getRequestTransmissionPolicy() != null)
121                         reservedEnvelop.setRequestTransmissionPolicy(beReservedEnvelope.getRequestTransmissionPolicy().intValue());
122                 if (beReservedEnvelope.getMaximumSustainedTrafficRate() != null)
123                         reservedEnvelop.setMaximumSustainedTrafficRate(beReservedEnvelope.getMaximumSustainedTrafficRate().intValue());
124         }
125
126         private void getBEAuthorizedEnvelop(TrafficProfileBestEffortAttributes bestEffort, BestEffortService trafficProfile) {
127                 BEEnvelop authorizedEnvelop = trafficProfile.getAuthorizedEnvelop();
128                 BeAuthorizedEnvelope beAuthorizedEnvelope = bestEffort.getBeAuthorizedEnvelope();
129                 if (beAuthorizedEnvelope.getTrafficPriority() != null)
130                         authorizedEnvelop.setTrafficPriority(beAuthorizedEnvelope.getTrafficPriority().byteValue());
131                 else
132                         authorizedEnvelop.setTrafficPriority(BestEffortService.DEFAULT_TRAFFIC_PRIORITY);
133                 if (beAuthorizedEnvelope.getMaximumTrafficBurst() != null)
134                         authorizedEnvelop.setMaximumTrafficBurst(beAuthorizedEnvelope.getMaximumTrafficBurst().intValue());
135                 else
136                         authorizedEnvelop.setMaximumTrafficBurst(BestEffortService.DEFAULT_MAX_TRAFFIC_BURST);
137                 if (beAuthorizedEnvelope.getRequestTransmissionPolicy() != null)
138                         authorizedEnvelop.setRequestTransmissionPolicy(beAuthorizedEnvelope.getRequestTransmissionPolicy().intValue());
139                 if (beAuthorizedEnvelope.getMaximumSustainedTrafficRate() != null)
140                         authorizedEnvelop.setMaximumSustainedTrafficRate(beAuthorizedEnvelope.getMaximumSustainedTrafficRate().intValue());
141         }
142
143         private void getTcpMatchRangesValues(TcpMatchRangesAttributes tcpRange, IExtendedClassifier classifier) {
144                 short srcPortStart, srcPortEnd, dstPortStart, dstPortEnd;
145                 srcPortStart = srcPortEnd = dstPortStart = dstPortEnd = 0;
146                 if (tcpRange != null) {
147                         classifier.setProtocol(IClassifier.Protocol.TCP);
148                         TcpMatchRanges tcpMatchRanges = tcpRange.getTcpMatchRanges();
149                         PortNumber tcpDestinationPortStart = tcpMatchRanges.getTcpDestinationPortBegin();
150                         if (tcpDestinationPortStart != null && tcpDestinationPortStart.getValue() != null)
151                                 dstPortStart = tcpDestinationPortStart.getValue().shortValue();
152                         PortNumber tcpSourcePortStart = tcpMatchRanges.getTcpSourcePortStart();
153                         if (tcpSourcePortStart != null && tcpSourcePortStart.getValue() != null)
154                                 srcPortStart = tcpSourcePortStart.getValue().shortValue();
155                         PortNumber tcpDestinationPortEnd = tcpMatchRanges.getTcpDestinationPortEnd();
156                         if (tcpDestinationPortEnd != null && tcpDestinationPortEnd.getValue() != null)
157                                 dstPortEnd = tcpDestinationPortEnd.getValue().shortValue();
158                         PortNumber tcpSourcePortEnd = tcpMatchRanges.getTcpSourcePortEnd();
159                         if (tcpSourcePortEnd != null && tcpSourcePortEnd.getValue() != null)
160                                 srcPortEnd = tcpSourcePortEnd.getValue().shortValue();
161                 }
162                 classifier.setDestinationPortStart(dstPortStart);
163                 classifier.setSourcePortStart(srcPortStart);
164                 classifier.setDestinationPortEnd(dstPortEnd);
165                 classifier.setSourcePortEnd(srcPortEnd);
166         }
167
168         private void getUdpMatchRangeValues(UdpMatchRangesAttributes updRange, IExtendedClassifier classifier) {
169                 short srcPortStart, srcPortEnd, dstPortStart, dstPortEnd;
170                 srcPortStart = srcPortEnd = dstPortStart = dstPortEnd = 0;
171                 if (updRange != null) {
172                         classifier.setProtocol(IClassifier.Protocol.UDP);
173                         UpdMatchRanges updMatchRanges = updRange.getUpdMatchRanges();
174                         PortNumber udpDestinationPortStart = updMatchRanges.getUdpDestinationPortStart();
175                         if (udpDestinationPortStart != null && udpDestinationPortStart.getValue() != null)
176                                 dstPortStart = udpDestinationPortStart.getValue().shortValue();
177                         PortNumber udpSourcePortStart = updMatchRanges.getUdpSourcePortStart();
178                         if (udpSourcePortStart != null && udpSourcePortStart.getValue() != null)
179                                 srcPortStart = udpSourcePortStart.getValue().shortValue();
180                         PortNumber udpDestinationPortEnd = updMatchRanges.getUdpDestinationPortEnd();
181                         if (udpDestinationPortEnd != null && udpDestinationPortEnd.getValue() != null)
182                                 dstPortEnd = udpDestinationPortEnd.getValue().shortValue();
183                         PortNumber udpSourcePortEnd = updMatchRanges.getUdpSourcePortEnd();
184                         if (udpSourcePortEnd != null && udpSourcePortEnd.getValue() != null)
185                                 srcPortEnd = udpSourcePortEnd.getValue().shortValue();
186                 }
187                 classifier.setDestinationPortStart(dstPortStart);
188                 classifier.setSourcePortStart(srcPortStart);
189                 classifier.setDestinationPortEnd(dstPortEnd);
190                 classifier.setSourcePortEnd(srcPortEnd);
191         }
192
193 }