YANG Model update for A1 Adapter
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / base / netconf / ONFCoreNetworkElementFactory.java
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf;
19
20 import com.google.common.base.Optional;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container.Capabilities;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.wrapperc.OnfMicrowaveModel;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.wrapperc.WrapperMicrowaveModelRev170324;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.wrapperc.WrapperMicrowaveModelRev180907;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.wrapperc.WrapperMicrowaveModelRev181010;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.toggleAlarmFilter.NotificationDelayService;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.ProviderClient;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database.service.HtDatabaseEventsService;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.WebSocketServiceClient;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.maintenance.MaintenanceService;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
36 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
37 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
38 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.NetworkElement;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Create a Network Element representation according to the capability
48  * information. The capabilities are more than an ODL-QName. After the ? other
49  * terms than "revision" are provided.
50  *
51  */
52 @SuppressWarnings("deprecation")
53 public class ONFCoreNetworkElementFactory {
54
55     private static final Logger LOG = LoggerFactory.getLogger(ONFCoreNetworkElementFactory.class);
56
57     public static @Nonnull ONFCoreNetworkElementRepresentation create(String mountPointNodeName, DataBroker dataBroker,
58             WebSocketServiceClient webSocketService, HtDatabaseEventsService databaseService,
59             InstanceIdentifier<Node> instanceIdentifier, DataBroker mountpointDataBroker, ProviderClient dcaeProvider,
60             @Nullable ProviderClient aotsmClient, MaintenanceService maintenanceService,
61             NotificationDelayService<ProblemNotificationXml> notificationDelayService) {
62
63         ONFCoreNetworkElementRepresentation res = null;
64         try (ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();){
65             Optional<Node> nodeOption = tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).checkedGet();
66             if (nodeOption.isPresent()) {
67                 Node node = nodeOption.get();
68                 NetconfNode nnode = node.augmentation(NetconfNode.class);
69                 if (nnode != null) {
70                     ConnectionStatus csts = nnode.getConnectionStatus();
71                     if (csts == ConnectionStatus.Connected) {
72                         Capabilities capabilities = Capabilities.getAvailableCapabilities(nnode);
73                         LOG.info("Mountpoint {} capabilities {}", mountPointNodeName, capabilities);
74                         res = build(mountPointNodeName, capabilities, mountpointDataBroker,
75                                 webSocketService, databaseService, dcaeProvider, aotsmClient, maintenanceService,
76                                 notificationDelayService);
77                         LOG.info("ONFCoreNetworkElementRepresentation12 value is not null? " + (res != null));
78                     }
79                 }
80             }
81             tx.close();
82         } catch (ReadFailedException | IllegalArgumentException e) {
83             LOG.warn("Can not generate specific NE Version representation. ", e);
84         }
85         if (res == null) {
86             res = new ONFCoreNetworkElementEmpty(mountPointNodeName);
87         }
88         LOG.info("Mointpoint {} started as {}", mountPointNodeName, res.getClass().getSimpleName());
89         return res;
90     }
91
92     public static @Nonnull ONFCoreNetworkElementRepresentation getEmpty(String mountPointNodeName) {
93         return new ONFCoreNetworkElementEmpty(mountPointNodeName);
94     }
95
96     /**
97      * Check capabilities are matching the this specific implementation and create network element
98      * representation if so.
99      *
100      * @param mountPointNodeName as String
101      * @param capabilities of the specific network element
102      * @param netconfNodeDataBroker for the network element specific data
103      * @param webSocketService to forward event notifications
104      * @param databaseService to access the database
105      * @param dcaeProvider to forward problem / change notifications
106      * @return created Object if conditions are OK or null if not.
107      */
108     private static @Nullable ONFCoreNetworkElementRepresentation build(String mountPointNodeName, Capabilities capabilities,
109             DataBroker netconfNodeDataBroker, WebSocketServiceClient webSocketService,
110             HtDatabaseEventsService databaseService, ProviderClient dcaeProvider, @Nullable ProviderClient aotsmClient,
111             MaintenanceService maintenanceService,
112             NotificationDelayService<ProblemNotificationXml> notificationDelayService) {
113
114         if (capabilities.isSupportingNamespaceAndRevision(NetworkElement.QNAME)) {
115             OnfMicrowaveModel onfMicrowaveModel = null;
116
117             if (capabilities.isSupportingNamespaceAndRevision(WrapperMicrowaveModelRev170324.QNAME)) {
118                 onfMicrowaveModel = new WrapperMicrowaveModelRev170324();
119             } else if (capabilities.isSupportingNamespaceAndRevision(WrapperMicrowaveModelRev180907.QNAME)) {
120                 onfMicrowaveModel = new WrapperMicrowaveModelRev180907();
121             } else if (capabilities.isSupportingNamespaceAndRevision(WrapperMicrowaveModelRev181010.QNAME)) {
122                 onfMicrowaveModel = new WrapperMicrowaveModelRev181010();
123             }
124
125             if (onfMicrowaveModel != null) {
126                 return new ONFCoreNetworkElement12Microwave(mountPointNodeName, capabilities, netconfNodeDataBroker,
127                         webSocketService, databaseService, dcaeProvider, aotsmClient, maintenanceService,
128                         notificationDelayService, onfMicrowaveModel);
129             } else {
130                 return new ONFCoreNetworkElement12(mountPointNodeName, capabilities, netconfNodeDataBroker,
131                         webSocketService, databaseService, dcaeProvider, aotsmClient, maintenanceService,
132                         notificationDelayService);
133             }
134         }
135         return null;
136
137     }
138
139 }