1 package org.opendaylight.controller.packetcable.provider;
3 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
4 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
5 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
6 import org.opendaylight.controller.md.sal.common.api.data.AsyncReadWriteTransaction;
7 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
8 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
9 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
10 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.Ccap;
11 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.Qos;
12 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.ServiceClassName;
13 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.ServiceFlowDirection;
14 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.ccap.Ccaps;
15 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.ccap.CcapsKey;
16 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.gates.Apps;
17 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.gates.AppsKey;
18 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.gates.apps.Subs;
19 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.gates.apps.SubsKey;
20 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.gates.apps.subs.Gates;
21 import org.opendaylight.yang.gen.v1.urn.packetcable.rev150327.pcmm.qos.gates.apps.subs.GatesKey;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.pcmm.rcd.IPCMMClient;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import javax.annotation.concurrent.ThreadSafe;
29 import java.net.InetAddress;
30 import java.net.UnknownHostException;
32 import java.util.concurrent.ConcurrentHashMap;
33 import java.util.concurrent.ExecutionException;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.Executors;
38 * Called by ODL framework to start this bundle.
40 * This class is responsible for processing messages received from ODL's restconf interface.
41 * TODO - Remove some of these state maps and move some of this into the PCMMService
44 public class PacketcableProvider implements DataChangeListener, AutoCloseable {
46 private static final Logger logger = LoggerFactory.getLogger(PacketcableProvider.class);
48 // keys to the /restconf/config/packetcable:ccap and /restconf/config/packetcable:qos config datastore
49 public static final InstanceIdentifier<Ccap> ccapIID = InstanceIdentifier.builder(Ccap.class).build();
50 public static final InstanceIdentifier<Qos> qosIID = InstanceIdentifier.builder(Qos.class).build();
53 * The ODL object used to broker messages throughout the framework
55 private final DataBroker dataBroker;
58 * The thread pool executor
60 private final ExecutorService executor;
62 // TODO - Revisit these maps and remove the ones no longer necessary
63 private final Map<String, Ccaps> ccapMap = new ConcurrentHashMap<>();
64 private final Map<String, Gates> gateMap = new ConcurrentHashMap<>();
65 private final Map<String, String> gateCcapMap = new ConcurrentHashMap<>();
66 private final Map<Subnet, Ccaps> subscriberSubnetsMap = new ConcurrentHashMap<>();
67 private final Map<ServiceClassName, List<Ccaps>> downstreamScnMap = new ConcurrentHashMap<>();
68 private final Map<ServiceClassName, List<Ccaps>> upstreamScnMap = new ConcurrentHashMap<>();
71 * Holds a PCMMService object for each CCAP being managed.
73 private final Map<String, PCMMService> pcmmServiceMap = new ConcurrentHashMap<>();
78 public PacketcableProvider(final DataBroker dataBroker) {
79 logger.info("Starting provider");
80 this.dataBroker = dataBroker;
81 executor = Executors.newCachedThreadPool();
85 * Implemented from the AutoCloseable interface.
88 public void close() throws ExecutionException, InterruptedException {
92 public InetAddress getInetAddress(final String subId){
94 return InetAddress.getByName(subId);
95 } catch (UnknownHostException e) {
96 logger.error("getInetAddress: {} FAILED: {}", subId, e.getMessage());
101 private String getIpPrefixStr(final IpPrefix ipPrefix) {
102 final Ipv4Prefix ipv4 = ipPrefix.getIpv4Prefix();
104 return ipv4.getValue();
106 return ipPrefix.getIpv6Prefix().getValue();
110 private void updateCcapMaps(final Ccaps ccap) {
111 // add ccap to the subscriberSubnets map
112 for (final IpPrefix ipPrefix : ccap.getSubscriberSubnets()) {
114 subscriberSubnetsMap.put(Subnet.createInstance(getIpPrefixStr(ipPrefix)), ccap);
115 } catch (UnknownHostException e) {
116 logger.error("updateSubscriberSubnets: {}:{} FAILED: {}", ipPrefix, ccap, e.getMessage());
119 // ccap to upstream SCN map
120 for (final ServiceClassName scn : ccap.getUpstreamScns()) {
121 if (upstreamScnMap.containsKey(scn)) {
122 upstreamScnMap.get(scn).add(ccap);
124 final List<Ccaps> ccapList = new ArrayList<>();
126 upstreamScnMap.put(scn, ccapList);
129 // ccap to downstream SCN map
130 for (final ServiceClassName scn : ccap.getDownstreamScns()) {
131 if (downstreamScnMap.containsKey(scn)) {
132 downstreamScnMap.get(scn).add(ccap);
134 final List<Ccaps> ccapList = new ArrayList<>();
136 downstreamScnMap.put(scn, ccapList);
141 private void removeCcapFromAllMaps(final Ccaps ccap) {
142 // remove the ccap from all maps
143 // subscriberSubnets map
144 for (final Map.Entry<Subnet, Ccaps> entry : subscriberSubnetsMap.entrySet()) {
145 if (entry.getValue() == ccap) {
146 subscriberSubnetsMap.remove(entry.getKey());
149 // ccap to upstream SCN map
150 for (final Map.Entry<ServiceClassName, List<Ccaps>> entry : upstreamScnMap.entrySet()) {
151 final List<Ccaps> ccapList = entry.getValue();
152 ccapList.remove(ccap);
153 if (ccapList.isEmpty()) {
154 upstreamScnMap.remove(entry.getKey());
157 // ccap to downstream SCN map
158 for (final Map.Entry<ServiceClassName, List<Ccaps>> entry : downstreamScnMap.entrySet()) {
159 final List<Ccaps> ccapList = entry.getValue();
160 ccapList.remove(ccap);
161 if (ccapList.isEmpty()) {
162 downstreamScnMap.remove(entry.getKey());
166 final PCMMService service = pcmmServiceMap.remove(ccap.getCcapId());
167 if (service != null) service.disconect();
170 private Ccaps findCcapForSubscriberId(final InetAddress inetAddr) {
171 Ccaps matchedCcap = null;
172 int longestPrefixLen = -1;
173 for (final Map.Entry<Subnet, Ccaps> entry : subscriberSubnetsMap.entrySet()) {
174 final Subnet subnet = entry.getKey();
175 if (subnet.isInNet(inetAddr)) {
176 int prefixLen = subnet.getPrefixLen();
177 if (prefixLen > longestPrefixLen) {
178 matchedCcap = entry.getValue();
179 longestPrefixLen = prefixLen;
186 private ServiceFlowDirection findScnOnCcap(final ServiceClassName scn, final Ccaps ccap) {
187 if (upstreamScnMap.containsKey(scn)) {
188 final List<Ccaps> ccapList = upstreamScnMap.get(scn);
189 if (ccapList.contains(ccap)) {
190 return ServiceFlowDirection.Us;
192 } else if (downstreamScnMap.containsKey(scn)) {
193 final List<Ccaps> ccapList = downstreamScnMap.get(scn);
194 if (ccapList.contains(ccap)) {
195 return ServiceFlowDirection.Ds;
202 * Implemented from the DataChangeListener interface.
205 private class InstanceData {
207 public final Map<InstanceIdentifier<Ccaps>, Ccaps> ccapIidMap = new HashMap<>();
210 public final Map<String, String> gatePathMap = new HashMap<>();
211 public String gatePath;
212 public final Map<InstanceIdentifier<Gates>, Gates> gateIidMap = new HashMap<>();
213 // remove path for either CCAP or Gates
214 public final Set<String> removePathList = new HashSet<>();
216 public InstanceData(final Map<InstanceIdentifier<?>, DataObject> thisData) {
217 // only used to parse createdData or updatedData
219 if (ccapIidMap.isEmpty()) {
221 if (! gateIidMap.isEmpty()){
222 gatePath = gatePathMap.get("appId") + "/" + gatePathMap.get("subId");
227 public InstanceData(final Set<InstanceIdentifier<?>> thisData) {
228 // only used to parse the removedData paths
229 for (final InstanceIdentifier<?> removeThis : thisData) {
230 getGatePathMap(removeThis);
231 if (gatePathMap.containsKey("ccapId")) {
232 gatePath = gatePathMap.get("ccapId");
233 removePathList.add(gatePath);
234 } else if (gatePathMap.containsKey("gateId")) {
235 gatePath = gatePathMap.get("appId") + "/" + gatePathMap.get("subId") + "/" + gatePathMap.get("gateId");
236 removePathList.add(gatePath);
240 private void getGatePathMap(final InstanceIdentifier<?> thisInstance) {
241 logger.info("onDataChanged().getGatePathMap(): " + thisInstance);
243 final InstanceIdentifier<Ccaps> ccapInstance = thisInstance.firstIdentifierOf(Ccaps.class);
244 if (ccapInstance != null) {
245 final CcapsKey ccapKey = InstanceIdentifier.keyOf(ccapInstance);
246 if (ccapKey != null) {
247 gatePathMap.put("ccapId", ccapKey.getCcapId());
250 // get the gate path keys from the InstanceIdentifier Map key set if they are there
251 final InstanceIdentifier<Apps> appsInstance = thisInstance.firstIdentifierOf(Apps.class);
252 if (appsInstance != null) {
253 final AppsKey appKey = InstanceIdentifier.keyOf(appsInstance);
254 if (appKey != null) {
255 gatePathMap.put("appId", appKey.getAppId());
258 final InstanceIdentifier<Subs> subsInstance = thisInstance.firstIdentifierOf(Subs.class);
259 if (subsInstance != null) {
260 final SubsKey subKey = InstanceIdentifier.keyOf(subsInstance);
261 if (subKey != null) {
262 subId = subKey.getSubId();
263 gatePathMap.put("subId", subId);
266 final InstanceIdentifier<Gates> gatesInstance = thisInstance.firstIdentifierOf(Gates.class);
267 if (gatesInstance != null) {
268 final GatesKey gateKey = InstanceIdentifier.keyOf(gatesInstance);
269 if (gateKey != null) {
270 gatePathMap.put("gateId", gateKey.getGateId());
274 } catch (ClassCastException err) {
275 logger.warn("Unexpected exception", err);
279 private void getCcaps(final Map<InstanceIdentifier<?>, DataObject> thisData) {
280 logger.info("onDataChanged().getCcaps(): " + thisData);
281 for (final Map.Entry<InstanceIdentifier<?>, DataObject> entry : thisData.entrySet()) {
282 if (entry.getValue() instanceof Ccaps) {
283 // TODO FIXME - Potential ClassCastException thrown here!!!
284 ccapIidMap.put((InstanceIdentifier<Ccaps>)entry.getKey(), (Ccaps)entry.getValue());
289 private void getGates(final Map<InstanceIdentifier<?>, DataObject> thisData) {
290 logger.info("onDataChanged().getGates(): " + thisData);
291 for (final Map.Entry<InstanceIdentifier<?>, DataObject> entry : thisData.entrySet()) {
292 if (entry.getValue() instanceof Gates) {
293 final Gates gate = (Gates)entry.getValue();
295 // TODO FIXME - Potential ClassCastException thrown here!!!
296 final InstanceIdentifier<Gates> gateIID = (InstanceIdentifier<Gates>)entry.getKey();
297 getGatePathMap(gateIID);
298 gateIidMap.put(gateIID, gate);
305 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
306 logger.info("onDataChanged");
307 // Determine what change action took place by looking at the change object's InstanceIdentifier sets
308 // and validate all instance data
309 if (!change.getCreatedData().isEmpty()) {
310 if (!new ValidateInstanceData(dataBroker, change.getCreatedData()).validateYang()) {
311 // leave now -- a bad yang object has been detected and a response object has been inserted
314 onCreate(new InstanceData(change.getCreatedData()));
315 } else if (!change.getRemovedPaths().isEmpty()) {
316 onRemove(new InstanceData(change.getRemovedPaths()));
317 } else if (!change.getUpdatedData().isEmpty()) {
318 if (new ValidateInstanceData(dataBroker, change.getUpdatedData()).isResponseEcho()) {
319 // leave now -- this is an echo of the inserted response object
322 onUpdate(new InstanceData(change.getUpdatedData()));
324 // we should not be here -- complain bitterly and return
325 logger.error("onDataChanged(): Unknown change action: " + change);
329 private void onCreate(final InstanceData thisData) {
330 logger.info("onCreate(): " + thisData);
332 // get the CCAP parameters
334 if (! thisData.ccapIidMap.isEmpty()) {
335 for (Map.Entry<InstanceIdentifier<Ccaps>, Ccaps> entry : thisData.ccapIidMap.entrySet()) {
336 final Ccaps thisCcap = entry.getValue();
337 // get the CCAP node identity from the Instance Data
338 final String ccapId = thisCcap.getCcapId();
340 if (pcmmServiceMap.get(thisCcap.getCcapId()) == null) {
341 final PCMMService pcmmService = new PCMMService(IPCMMClient.CLIENT_TYPE, thisCcap);
342 // TODO - may want to use the AMID but for the client type but probably not???
344 final PCMMService pcmmService = new PCMMService(
345 thisCcap.getAmId().getAmType().shortValue(), thisCcap);
347 pcmmServiceMap.put(thisCcap.getCcapId(), pcmmService);
348 message = pcmmService.addCcap();
349 if (message.contains("200 OK")) {
350 ccapMap.put(ccapId, thisCcap);
351 updateCcapMaps(thisCcap);
352 logger.info("Created CCAP: {}/{} : {}", thisData.gatePath, thisCcap, message);
353 logger.info("Created CCAP: {} : {}", thisData.gatePath, message);
355 // TODO - when a connection cannot be made, need to remove CCAP from ODL cache.
356 logger.error("Create CCAP Failed: {} : {}", thisData.gatePath, message);
358 // set the response string in the config ccap object using a new thread
359 executor.execute(new Response(dataBroker, entry.getKey(), thisCcap, message));
361 logger.error("Already monitoring CCAP - " + thisCcap);
366 // get the PCMM gate parameters from the ccapId/appId/subId/gateId path in the Maps entry (if new gate)
367 for (final Map.Entry<InstanceIdentifier<Gates>, Gates> entry : thisData.gateIidMap.entrySet()) {
369 final Gates gate = entry.getValue();
370 final String gateId = gate.getGateId();
371 final String gatePathStr = thisData.gatePath + "/" + gateId ;
372 final InetAddress subId = getInetAddress(thisData.subId);
374 final Ccaps thisCcap = findCcapForSubscriberId(subId);
375 if (thisCcap != null) {
376 final String ccapId = thisCcap.getCcapId();
377 // verify SCN exists on CCAP and force gateSpec.Direction to align with SCN direction
378 final ServiceClassName scn = gate.getTrafficProfile().getServiceClassName();
380 final ServiceFlowDirection scnDir = findScnOnCcap(scn, thisCcap);
381 if (scnDir != null) {
382 if (pcmmServiceMap.get(thisCcap.getCcapId()) != null) {
383 message = pcmmServiceMap.get(thisCcap.getCcapId()).sendGateSet(gatePathStr, subId, gate, scnDir);
384 gateMap.put(gatePathStr, gate);
385 gateCcapMap.put(gatePathStr, thisCcap.getCcapId());
387 if (message.contains("200 OK")) {
388 logger.info("Created QoS gate {} for {}/{}/{} - {}",
389 gateId, ccapId, gatePathStr, gate, message);
390 logger.info("Created QoS gate {} for {}/{} - {}",
391 gateId, ccapId, gatePathStr, message);
393 logger.info("Unable to create QoS gate {} for {}/{}/{} - {}",
394 gateId, ccapId, gatePathStr, gate, message);
395 logger.error("Unable to create QoS gate {} for {}/{} - {}",
396 gateId, ccapId, gatePathStr, message);
399 logger.error("Unable to locate PCMM Service for CCAP - " + thisCcap);
403 logger.error("PCMMService: sendGateSet(): SCN {} not found on CCAP {} for {}/{}",
404 scn.getValue(), thisCcap, gatePathStr, gate);
405 message = String.format("404 Not Found - SCN %s not found on CCAP %s for %s",
406 scn.getValue(), thisCcap.getCcapId(), gatePathStr);
410 final String subIdStr = thisData.subId;
411 message = String.format("404 Not Found - no CCAP found for subscriber %s in %s",
412 subIdStr, gatePathStr);
413 logger.info("Create QoS gate {} FAILED: no CCAP found for subscriber {}: @ {}/{}",
414 gateId, subIdStr, gatePathStr, gate);
415 logger.error("Create QoS gate {} FAILED: no CCAP found for subscriber {}: @ {}",
416 gateId, subIdStr, gatePathStr);
419 final String subIdStr = thisData.subId;
420 message = String.format("400 Bad Request - subId must be a valid IP address for subscriber %s in %s",
421 subIdStr, gatePathStr);
422 logger.info("Create QoS gate {} FAILED: subId must be a valid IP address for subscriber {}: @ {}/{}",
423 gateId, subIdStr, gatePathStr, gate);
424 logger.error("Create QoS gate {} FAILED: subId must be a valid IP address for subscriber {}: @ {}",
425 gateId, subIdStr, gatePathStr);
427 // set the response message in the config gate object using a new thread
428 executor.execute(new Response(dataBroker, entry.getKey(), gate, message));
433 private void onRemove(final InstanceData thisData) {
434 logger.info("onRemove(): " + thisData);
435 for (final String gatePathStr: thisData.removePathList) {
436 if (gateMap.containsKey(gatePathStr)) {
437 final Gates thisGate = gateMap.remove(gatePathStr);
438 final String gateId = thisGate.getGateId();
439 final String ccapId = gateCcapMap.remove(gatePathStr);
440 final Ccaps thisCcap = ccapMap.get(ccapId);
441 final PCMMService service = pcmmServiceMap.get(thisCcap.getCcapId());
442 if (service != null) {
443 service.sendGateDelete(gatePathStr);
444 logger.info("onDataChanged(): removed QoS gate {} for {}/{}/{}: ", gateId, ccapId, gatePathStr, thisGate);
445 logger.info("onDataChanged(): removed QoS gate {} for {}/{}: ", gateId, ccapId, gatePathStr);
447 logger.warn("Unable to send to locate PCMMService to send gate delete message with CCAP - "
451 for (final String ccapIdStr: thisData.removePathList) {
452 if (ccapMap.containsKey(ccapIdStr)) {
453 final Ccaps thisCcap = ccapMap.remove(ccapIdStr);
454 removeCcapFromAllMaps(thisCcap);
459 private void onUpdate(final InstanceData oldData) {
460 logger.info("onUpdate(): " + oldData);
461 // update operation not allowed -- restore the original config object and complain
462 if (! oldData.ccapIidMap.isEmpty()) {
463 for (final Map.Entry<InstanceIdentifier<Ccaps>, Ccaps> entry : oldData.ccapIidMap.entrySet()) {
464 final Ccaps ccap = entry.getValue();
465 final String ccapId = ccap.getCcapId();
466 String message = String.format("405 Method Not Allowed - %s: CCAP update not permitted (use delete); ",
468 // push new error message onto existing response
469 message += ccap.getResponse();
470 // set the response message in the config object using a new thread -- also restores the original data
471 executor.execute(new Response(dataBroker, entry.getKey(), ccap, message));
472 logger.error("onDataChanged(): CCAP update not permitted {}/{}", ccapId, ccap);
475 for (final Map.Entry<InstanceIdentifier<Gates>, Gates> entry : oldData.gateIidMap.entrySet()) {
476 final Gates gate = entry.getValue();
477 final String gatePathStr = oldData.gatePath + "/" + gate.getGateId() ;
478 String message = String.format("405 Method Not Allowed - %s: QoS Gate update not permitted (use delete); ", gatePathStr);
479 // push new error message onto existing response
480 message += gate.getResponse();
481 // set the response message in the config object using a new thread -- also restores the original data
482 executor.execute(new Response(dataBroker, entry.getKey(), gate, message));
483 logger.error("onDataChanged(): QoS Gate update not permitted: {}/{}", gatePathStr, gate);