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<Ccaps, 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 {
90 if (dataBroker != null) {
91 // remove our config datastore instances
92 final AsyncReadWriteTransaction<InstanceIdentifier<?>, ?> tx = dataBroker.newReadWriteTransaction();
93 tx.delete(LogicalDatastoreType.CONFIGURATION, ccapIID);
94 tx.delete(LogicalDatastoreType.CONFIGURATION, qosIID);
99 public InetAddress getInetAddress(final String subId){
101 return InetAddress.getByName(subId);
102 } catch (UnknownHostException e) {
103 logger.error("getInetAddress: {} FAILED: {}", subId, e.getMessage());
108 private String getIpPrefixStr(final IpPrefix ipPrefix) {
109 final Ipv4Prefix ipv4 = ipPrefix.getIpv4Prefix();
111 return ipv4.getValue();
113 return ipPrefix.getIpv6Prefix().getValue();
117 private void updateCcapMaps(final Ccaps ccap) {
118 // add ccap to the subscriberSubnets map
119 for (final IpPrefix ipPrefix : ccap.getSubscriberSubnets()) {
121 subscriberSubnetsMap.put(Subnet.createInstance(getIpPrefixStr(ipPrefix)), ccap);
122 } catch (UnknownHostException e) {
123 logger.error("updateSubscriberSubnets: {}:{} FAILED: {}", ipPrefix, ccap, e.getMessage());
126 // ccap to upstream SCN map
127 for (final ServiceClassName scn : ccap.getUpstreamScns()) {
128 if (upstreamScnMap.containsKey(scn)) {
129 upstreamScnMap.get(scn).add(ccap);
131 final List<Ccaps> ccapList = new ArrayList<>();
133 upstreamScnMap.put(scn, ccapList);
136 // ccap to downstream SCN map
137 for (final ServiceClassName scn : ccap.getDownstreamScns()) {
138 if (downstreamScnMap.containsKey(scn)) {
139 downstreamScnMap.get(scn).add(ccap);
141 final List<Ccaps> ccapList = new ArrayList<>();
143 downstreamScnMap.put(scn, ccapList);
148 private void removeCcapFromAllMaps(final Ccaps ccap) {
149 // remove the ccap from all maps
150 // subscriberSubnets map
151 for (final Map.Entry<Subnet, Ccaps> entry : subscriberSubnetsMap.entrySet()) {
152 if (entry.getValue() == ccap) {
153 subscriberSubnetsMap.remove(entry.getKey());
156 // ccap to upstream SCN map
157 for (final Map.Entry<ServiceClassName, List<Ccaps>> entry : upstreamScnMap.entrySet()) {
158 final List<Ccaps> ccapList = entry.getValue();
159 ccapList.remove(ccap);
160 if (ccapList.isEmpty()) {
161 upstreamScnMap.remove(entry.getKey());
164 // ccap to downstream SCN map
165 for (final Map.Entry<ServiceClassName, List<Ccaps>> entry : downstreamScnMap.entrySet()) {
166 final List<Ccaps> ccapList = entry.getValue();
167 ccapList.remove(ccap);
168 if (ccapList.isEmpty()) {
169 downstreamScnMap.remove(entry.getKey());
173 final PCMMService service = pcmmServiceMap.remove(ccap);
174 if (service != null) service.disconect();
177 private Ccaps findCcapForSubscriberId(final InetAddress inetAddr) {
178 Ccaps matchedCcap = null;
179 int longestPrefixLen = -1;
180 for (final Map.Entry<Subnet, Ccaps> entry : subscriberSubnetsMap.entrySet()) {
181 final Subnet subnet = entry.getKey();
182 if (subnet.isInNet(inetAddr)) {
183 int prefixLen = subnet.getPrefixLen();
184 if (prefixLen > longestPrefixLen) {
185 matchedCcap = entry.getValue();
186 longestPrefixLen = prefixLen;
193 private ServiceFlowDirection findScnOnCcap(final ServiceClassName scn, final Ccaps ccap) {
194 if (upstreamScnMap.containsKey(scn)) {
195 final List<Ccaps> ccapList = upstreamScnMap.get(scn);
196 if (ccapList.contains(ccap)) {
197 return ServiceFlowDirection.Us;
199 } else if (downstreamScnMap.containsKey(scn)) {
200 final List<Ccaps> ccapList = downstreamScnMap.get(scn);
201 if (ccapList.contains(ccap)) {
202 return ServiceFlowDirection.Ds;
209 * Implemented from the DataChangeListener interface.
212 private class InstanceData {
214 public final Map<InstanceIdentifier<Ccaps>, Ccaps> ccapIidMap = new HashMap<>();
217 public final Map<String, String> gatePathMap = new HashMap<>();
218 public String gatePath;
219 public final Map<InstanceIdentifier<Gates>, Gates> gateIidMap = new HashMap<>();
220 // remove path for either CCAP or Gates
221 public final Set<String> removePathList = new HashSet<>();
223 public InstanceData(final Map<InstanceIdentifier<?>, DataObject> thisData) {
224 // only used to parse createdData or updatedData
226 if (ccapIidMap.isEmpty()) {
228 if (! gateIidMap.isEmpty()){
229 gatePath = gatePathMap.get("appId") + "/" + gatePathMap.get("subId");
234 public InstanceData(final Set<InstanceIdentifier<?>> thisData) {
235 // only used to parse the removedData paths
236 for (final InstanceIdentifier<?> removeThis : thisData) {
237 getGatePathMap(removeThis);
238 if (gatePathMap.containsKey("ccapId")) {
239 gatePath = gatePathMap.get("ccapId");
240 removePathList.add(gatePath);
241 } else if (gatePathMap.containsKey("gateId")) {
242 gatePath = gatePathMap.get("appId") + "/" + gatePathMap.get("subId") + "/" + gatePathMap.get("gateId");
243 removePathList.add(gatePath);
247 private void getGatePathMap(final InstanceIdentifier<?> thisInstance) {
248 logger.info("onDataChanged().getGatePathMap(): " + thisInstance);
250 final InstanceIdentifier<Ccaps> ccapInstance = thisInstance.firstIdentifierOf(Ccaps.class);
251 if (ccapInstance != null) {
252 final CcapsKey ccapKey = InstanceIdentifier.keyOf(ccapInstance);
253 if (ccapKey != null) {
254 gatePathMap.put("ccapId", ccapKey.getCcapId());
257 // get the gate path keys from the InstanceIdentifier Map key set if they are there
258 final InstanceIdentifier<Apps> appsInstance = thisInstance.firstIdentifierOf(Apps.class);
259 if (appsInstance != null) {
260 final AppsKey appKey = InstanceIdentifier.keyOf(appsInstance);
261 if (appKey != null) {
262 gatePathMap.put("appId", appKey.getAppId());
265 final InstanceIdentifier<Subs> subsInstance = thisInstance.firstIdentifierOf(Subs.class);
266 if (subsInstance != null) {
267 final SubsKey subKey = InstanceIdentifier.keyOf(subsInstance);
268 if (subKey != null) {
269 subId = subKey.getSubId();
270 gatePathMap.put("subId", subId);
273 final InstanceIdentifier<Gates> gatesInstance = thisInstance.firstIdentifierOf(Gates.class);
274 if (gatesInstance != null) {
275 final GatesKey gateKey = InstanceIdentifier.keyOf(gatesInstance);
276 if (gateKey != null) {
277 gatePathMap.put("gateId", gateKey.getGateId());
281 } catch (ClassCastException err) {
282 logger.warn("Unexpected exception", err);
286 private void getCcaps(final Map<InstanceIdentifier<?>, DataObject> thisData) {
287 logger.info("onDataChanged().getCcaps(): " + thisData);
288 for (final Map.Entry<InstanceIdentifier<?>, DataObject> entry : thisData.entrySet()) {
289 if (entry.getValue() instanceof Ccaps) {
290 ccapIidMap.put((InstanceIdentifier<Ccaps>)entry.getKey(), (Ccaps)entry.getValue());
295 private void getGates(final Map<InstanceIdentifier<?>, DataObject> thisData) {
296 logger.info("onDataChanged().getGates(): " + thisData);
297 for (final Map.Entry<InstanceIdentifier<?>, DataObject> entry : thisData.entrySet()) {
298 if (entry.getValue() instanceof Gates) {
299 final Gates gate = (Gates)entry.getValue();
300 final InstanceIdentifier<Gates> gateIID = (InstanceIdentifier<Gates>)entry.getKey();
301 getGatePathMap(gateIID);
302 gateIidMap.put(gateIID, gate);
309 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
310 logger.info("onDataChanged");
311 // Determine what change action took place by looking at the change object's InstanceIdentifier sets
312 // and validate all instance data
313 if (!change.getCreatedData().isEmpty()) {
314 if (!new ValidateInstanceData(dataBroker, change.getCreatedData()).validateYang()) {
315 // leave now -- a bad yang object has been detected and a response object has been inserted
318 onCreate(new InstanceData(change.getCreatedData()));
319 } else if (!change.getRemovedPaths().isEmpty()) {
320 onRemove(new InstanceData(change.getRemovedPaths()));
321 } else if (!change.getUpdatedData().isEmpty()) {
322 if (new ValidateInstanceData(dataBroker, change.getUpdatedData()).isResponseEcho()) {
323 // leave now -- this is an echo of the inserted response object
326 onUpdate(new InstanceData(change.getUpdatedData()));
328 // we should not be here -- complain bitterly and return
329 logger.error("onDataChanged(): Unknown change action: " + change);
333 private void onCreate(final InstanceData thisData) {
334 logger.info("onCreate(): " + thisData);
336 // get the CCAP parameters
338 if (! thisData.ccapIidMap.isEmpty()) {
339 for (Map.Entry<InstanceIdentifier<Ccaps>, Ccaps> entry : thisData.ccapIidMap.entrySet()) {
340 final Ccaps thisCcap = entry.getValue();
341 // get the CCAP node identity from the Instance Data
342 final String ccapId = thisCcap.getCcapId();
344 if (pcmmServiceMap.get(thisCcap) == null) {
345 final PCMMService pcmmService = new PCMMService(IPCMMClient.CLIENT_TYPE, thisCcap);
346 // TODO - may want to use the AMID but for the client type but probably not???
348 final PCMMService pcmmService = new PCMMService(
349 thisCcap.getAmId().getAmType().shortValue(), thisCcap);
351 pcmmServiceMap.put(thisCcap, pcmmService);
352 message = pcmmService.addCcap();
353 if (message.contains("200 OK")) {
354 ccapMap.put(ccapId, thisCcap);
355 updateCcapMaps(thisCcap);
356 logger.info("onDataChanged(): created CCAP: {}/{} : {}", thisData.gatePath, thisCcap, message);
357 logger.info("onDataChanged(): created CCAP: {} : {}", thisData.gatePath, message);
359 logger.error("onDataChanged(): create CCAP Failed: {} : {}", thisData.gatePath, message);
361 // set the response string in the config ccap object using a new thread
362 executor.execute(new Response(dataBroker, entry.getKey(), thisCcap, message));
364 logger.error("Already monitoring CCAP - " + thisCcap);
369 // get the PCMM gate parameters from the ccapId/appId/subId/gateId path in the Maps entry (if new gate)
370 for (final Map.Entry<InstanceIdentifier<Gates>, Gates> entry : thisData.gateIidMap.entrySet()) {
372 final Gates gate = entry.getValue();
373 final String gateId = gate.getGateId();
374 final String gatePathStr = thisData.gatePath + "/" + gateId ;
375 final InetAddress subId = getInetAddress(thisData.subId);
377 final Ccaps thisCcap = findCcapForSubscriberId(subId);
378 if (thisCcap != null) {
379 final String ccapId = thisCcap.getCcapId();
380 // verify SCN exists on CCAP and force gateSpec.Direction to align with SCN direction
381 final ServiceClassName scn = gate.getTrafficProfile().getServiceClassName();
383 final ServiceFlowDirection scnDir = findScnOnCcap(scn, thisCcap);
384 if (scnDir != null) {
385 if (pcmmServiceMap.get(thisCcap) != null) {
386 message = pcmmServiceMap.get(thisCcap).sendGateSet(gatePathStr, subId, gate, scnDir);
387 if (message.contains("200 OK")) {
388 gateMap.put(gatePathStr, gate);
389 gateCcapMap.put(gatePathStr, thisCcap.getCcapId());
390 logger.info("onDataChanged(): created QoS gate {} for {}/{}/{} - {}",
391 gateId, ccapId, gatePathStr, gate, message);
392 logger.info("onDataChanged(): created QoS gate {} for {}/{} - {}",
393 gateId, ccapId, gatePathStr, message);
395 logger.info("onDataChanged(): Unable to create QoS gate {} for {}/{}/{} - {}",
396 gateId, ccapId, gatePathStr, gate, message);
397 logger.error("onDataChanged(): Unable to create QoS gate {} for {}/{} - {}",
398 gateId, ccapId, gatePathStr, message);
401 logger.error("Unable to locate PCMM Service for CCAP - " + thisCcap);
405 logger.error("PCMMService: sendGateSet(): SCN {} not found on CCAP {} for {}/{}",
406 scn.getValue(), thisCcap, gatePathStr, gate);
407 message = String.format("404 Not Found - SCN %s not found on CCAP %s for %s",
408 scn.getValue(), thisCcap.getCcapId(), gatePathStr);
412 final String subIdStr = thisData.subId;
413 message = String.format("404 Not Found - no CCAP found for subscriber %s in %s",
414 subIdStr, gatePathStr);
415 logger.info("onDataChanged(): create QoS gate {} FAILED: no CCAP found for subscriber {}: @ {}/{}",
416 gateId, subIdStr, gatePathStr, gate);
417 logger.error("onDataChanged(): create QoS gate {} FAILED: no CCAP found for subscriber {}: @ {}",
418 gateId, subIdStr, gatePathStr);
421 final String subIdStr = thisData.subId;
422 message = String.format("400 Bad Request - subId must be a valid IP address for subscriber %s in %s",
423 subIdStr, gatePathStr);
424 logger.info("onDataChanged(): create QoS gate {} FAILED: subId must be a valid IP address for subscriber {}: @ {}/{}",
425 gateId, subIdStr, gatePathStr, gate);
426 logger.error("onDataChanged(): create QoS gate {} FAILED: subId must be a valid IP address for subscriber {}: @ {}",
427 gateId, subIdStr, gatePathStr);
429 // set the response message in the config gate object using a new thread
430 executor.execute(new Response(dataBroker, entry.getKey(), gate, message));
435 private void onRemove(final InstanceData thisData) {
436 logger.info("onRemove(): " + thisData);
437 for (final String gatePathStr: thisData.removePathList) {
438 if (gateMap.containsKey(gatePathStr)) {
439 final Gates thisGate = gateMap.remove(gatePathStr);
440 final String gateId = thisGate.getGateId();
441 final String ccapId = gateCcapMap.remove(gatePathStr);
442 final Ccaps thisCcap = ccapMap.get(ccapId);
443 final PCMMService service = pcmmServiceMap.get(thisCcap);
444 if (service != null) {
445 service.sendGateDelete(thisCcap, gatePathStr);
446 logger.info("onDataChanged(): removed QoS gate {} for {}/{}/{}: ", gateId, ccapId, gatePathStr, thisGate);
447 logger.info("onDataChanged(): removed QoS gate {} for {}/{}: ", gateId, ccapId, gatePathStr);
449 logger.warn("Unable to send to locate PCMMService to send gate delete message with CCAP - "
453 for (final String ccapIdStr: thisData.removePathList) {
454 if (ccapMap.containsKey(ccapIdStr)) {
455 final Ccaps thisCcap = ccapMap.remove(ccapIdStr);
456 removeCcapFromAllMaps(thisCcap);
461 private void onUpdate(final InstanceData oldData) {
462 logger.info("onUpdate(): " + oldData);
463 // update operation not allowed -- restore the original config object and complain
464 if (! oldData.ccapIidMap.isEmpty()) {
465 for (final Map.Entry<InstanceIdentifier<Ccaps>, Ccaps> entry : oldData.ccapIidMap.entrySet()) {
466 final Ccaps ccap = entry.getValue();
467 final String ccapId = ccap.getCcapId();
468 String message = String.format("405 Method Not Allowed - %s: CCAP update not permitted (use delete); ",
470 // push new error message onto existing response
471 message += ccap.getResponse();
472 // set the response message in the config object using a new thread -- also restores the original data
473 executor.execute(new Response(dataBroker, entry.getKey(), ccap, message));
474 logger.error("onDataChanged(): CCAP update not permitted {}/{}", ccapId, ccap);
477 for (final Map.Entry<InstanceIdentifier<Gates>, Gates> entry : oldData.gateIidMap.entrySet()) {
478 final Gates gate = entry.getValue();
479 final String gatePathStr = oldData.gatePath + "/" + gate.getGateId() ;
480 String message = String.format("405 Method Not Allowed - %s: QoS Gate update not permitted (use delete); ", gatePathStr);
481 // push new error message onto existing response
482 message += gate.getResponse();
483 // set the response message in the config object using a new thread -- also restores the original data
484 executor.execute(new Response(dataBroker, entry.getKey(), gate, message));
485 logger.error("onDataChanged(): QoS Gate update not permitted: {}/{}", gatePathStr, gate);