4eaec246ea89aea60479c19dd6291b1993cc14d0
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom;
23
24 import com.google.common.util.concurrent.FluentFuture;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Map.Entry;
28 import java.util.Objects;
29 import java.util.Optional;
30 import java.util.concurrent.ExecutionException;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33 import org.eclipse.jdt.annotation.NonNull;
34 import org.eclipse.jdt.annotation.Nullable;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfAccessorImpl;
37 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
38 //import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
39 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
40 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
42 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
43 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
44 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class NetconfDomAccessorImpl extends NetconfAccessorImpl implements NetconfDomAccessor {
55
56     private static final Logger LOG = LoggerFactory.getLogger(NetconfDomAccessorImpl.class);
57
58     private final DOMDataBroker dataBroker;
59     private final DOMMountPoint mountpoint;
60     private final DomContext domContext;
61
62     public NetconfDomAccessorImpl(NetconfAccessorImpl accessor, DOMDataBroker domDataBroker, DOMMountPoint mountPoint,
63             DomContext domContext) {
64         super(accessor);
65         this.dataBroker = Objects.requireNonNull(domDataBroker);
66         this.mountpoint = Objects.requireNonNull(mountPoint);
67         this.domContext = Objects.requireNonNull(domContext);
68     }
69
70     @Override
71     public DOMDataBroker getDataBroker() {
72         return dataBroker;
73     }
74
75     @Override
76     public DOMMountPoint getMountpoint() {
77         return mountpoint;
78     }
79
80     @Override
81     public @NonNull <T extends DOMNotificationListener> ListenerRegistration<DOMNotificationListener> doRegisterNotificationListener(
82             @NonNull T listener, Collection<SchemaPath> types) {
83         LOG.info("Begin register listener for Mountpoint {}", mountpoint.getIdentifier().toString());
84         final Optional<DOMNotificationService> optionalNotificationService =
85                 mountpoint.getService(DOMNotificationService.class);
86         if (optionalNotificationService.isPresent()) {
87             final ListenerRegistration<DOMNotificationListener> ranListenerRegistration =
88                     optionalNotificationService.get().registerNotificationListener(listener, types);
89             LOG.info("End registration listener for Mountpoint {} Listener: {} Result: {}",
90                     mountpoint.getIdentifier().toString(), optionalNotificationService, ranListenerRegistration);
91             return ranListenerRegistration;
92         }
93         throw new IllegalArgumentException("Can not get notification service");
94     }
95
96     @Override
97     public @NonNull <T extends DOMNotificationListener> ListenerRegistration<DOMNotificationListener> doRegisterNotificationListener(
98             @NonNull T listener, SchemaPath[] types) {
99         return doRegisterNotificationListener(listener, Arrays.asList(types));
100     }
101
102     @Override
103     public <T extends DataObject> Optional<T> readData(LogicalDatastoreType dataStoreType, YangInstanceIdentifier path,
104             Class<T> clazz) {
105         LOG.debug("Read to object datastore:{} path:{}", dataStoreType, path);
106
107         try {
108             return convertNormalizedNode(domContext.getBindingNormalizedNodeSerializer(),
109                     readDataNode(dataStoreType, path), path, clazz);
110         } catch (CanNotConvertException e) {
111             LOG.info("Incomplete read to class transaction {} {}", dataStoreType, path, e);
112             return Optional.empty();
113         }
114     }
115
116     @Override
117     public Optional<NormalizedNode<?, ?>> readDataNode(LogicalDatastoreType dataStoreType,
118             YangInstanceIdentifier path) {
119         LOG.debug("Read to node datastore:{} path:{}", dataStoreType, path);
120
121         try (DOMDataTreeReadTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction()) {
122             FluentFuture<Optional<NormalizedNode<?, ?>>> foData = readOnlyTransaction.read(dataStoreType, path);
123             // RAVI - Add a few debug here, like what ? Speak to Micha....
124             
125             Optional<NormalizedNode<?, ?>> data = foData.get(120, TimeUnit.SECONDS);
126             LOG.info("read is done - {} ", foData.isDone());
127             return data;
128             
129         } catch (InterruptedException | ExecutionException | TimeoutException e) {
130             LOG.info("Incomplete read to node transaction {} {}", dataStoreType, path, e);
131             return Optional.empty();
132         }
133     }
134
135     @SuppressWarnings("unchecked")
136     private static <T extends DataObject> Optional<T> convertNormalizedNode(BindingNormalizedNodeSerializer serializer,
137             Optional<NormalizedNode<?, ?>> oData, YangInstanceIdentifier path, Class<T> clazz)
138             throws CanNotConvertException {
139         if (oData.isPresent()) {
140             NormalizedNode<?, ?> data = oData.get();
141             LOG.debug("data identifier: {}", data.getIdentifier());
142             @Nullable
143             Entry<InstanceIdentifier<?>, DataObject> entry = serializer.fromNormalizedNode(path, data);
144             if (entry != null) {
145                 LOG.debug("object identifier: {}", entry.getKey());
146                 DataObject value = entry.getValue();
147                 if (clazz.isInstance(value)) {
148                     return Optional.of((T) value);
149                 } else {
150                     throw new CanNotConvertException("Unexpected class. Expected:" + clazz.getName() + " provided:"
151                             + value.getClass().getName() + " Nodetype:" + data.getNodeType());
152                 }
153             } else {
154                 throw new CanNotConvertException(
155                         "No object created for path:" + path + " Nodetype:" + data.getNodeType());
156             }
157         } else {
158             throw new CanNotConvertException("No data received for path:" + path);
159         }
160     }
161 }