TAPI topology creation for 100GE Transponder
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / utils / TapiListener.java
1 /*
2  * Copyright © 2018 Orange and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.tapi.utils;
9
10 import java.util.Collection;
11 import java.util.Iterator;
12 import java.util.List;
13
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.api.DataObjectModification;
16 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
17 import org.opendaylight.mdsal.binding.api.DataTreeModification;
18 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.ServiceInterfacePoints;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.service._interface.points.ServiceEndPoint;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class TapiListener implements DataTreeChangeListener<ServiceInterfacePoints> {
26
27     private static final Logger LOG = LoggerFactory.getLogger(TapiListener.class);
28
29     @Override
30     public void onDataTreeChanged(@NonNull Collection<DataTreeModification<ServiceInterfacePoints>> changes) {
31         LOG.info("onDataTreeChanged in TapiListener");
32         for (DataTreeModification<ServiceInterfacePoints> change : changes) {
33             DataObjectModification<ServiceInterfacePoints> rootSIP = change.getRootNode();
34             switch (rootSIP.getModificationType()) {
35                 case WRITE:
36                     LOG.info("onDataTreeChanged in TapiListener : WRITE");
37                     ServiceInterfacePoints data = rootSIP.getDataAfter();
38                     List<ServiceEndPoint> listSEP = data.getServiceEndPoint();
39                     MappingUtils.deleteMap();
40                     for (ServiceEndPoint sep : listSEP) {
41                         MappingUtils.addMapSEP(sep);
42                     }
43                     MappingUtils.afficheMap();
44                     break;
45                 case SUBTREE_MODIFIED:
46                     LOG.info("onDataTreeChanged in TapiListener : SUBTREE_MODIFIED");
47                     Iterator<? extends DataObjectModification<? extends DataObject>> iterator = rootSIP
48                         .getModifiedChildren().iterator();
49                     while (iterator.hasNext()) {
50                         DataObjectModification<? extends DataObject> dom = iterator.next();
51                         // to delete existing child entry
52                         if (dom.getDataAfter() == null) {
53                             DataObject dataObject = dom.getDataBefore();
54                             ServiceEndPoint sep = null;
55                             sep = (ServiceEndPoint) dataObject;
56                             Uuid uuid = sep.getUuid();
57                             MappingUtils.deleteMapEntry(uuid);
58                             MappingUtils.afficheMap();
59                             break;
60                         }
61
62                         // to add new child entry
63                         if (dom.getDataType().toString().compareTo("interface org.opendaylight.yang.gen.v1.urn.opendayl"
64                             + "ight.params.xml.ns.yang.tapi.rev180928.service._interface.points.ServiceEndPoint") == 0
65                             && dom.getDataBefore() == null) {
66                             DataObject dataObject = dom.getDataAfter();
67                             ServiceEndPoint sep = null;
68                             sep = (ServiceEndPoint) dataObject;
69                             MappingUtils.addMapSEP(sep);
70                             MappingUtils.afficheMap();
71                         } else {
72                             LOG.error("data input type is not a valid 'service-end-point'");
73                         }
74                         break;
75                     }
76                     break;
77                 case DELETE:
78                     LOG.info("onDataTreeChanged in TapiListener : DELETE");
79                     MappingUtils.deleteMap();
80                     break;
81                 default:
82                     LOG.error("Error of API REST modification type");
83                     break;
84             }
85
86         }
87     }
88
89 }