13eca8f257fb61b93f4d70ace3ba24960b6b2658
[ccsdk/features.git] /
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.impl.util;
19
20 import java.util.HashSet;
21 import java.util.Optional;
22 import javax.annotation.Nonnull;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InternalConnectionStatus;
24 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.KeyAuth;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPw;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPwUnencrypted;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.ConnectionLogStatus;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementConnectionEntity;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.network.element.connection.entity.NodeDetailsBuilder;
38 import org.opendaylight.yangtools.yang.common.Uint32;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 @SuppressWarnings("deprecation")
43 public class NetworkElementConnectionEntitiyUtil {
44
45     private static final Logger LOG = LoggerFactory.getLogger(NetworkElementConnectionEntitiyUtil.class);
46
47     /**
48      * Update devicetype and let all other field emptys
49      *
50      * @param deviceType that should be updated
51      * @return NetworkElementConnectionEntity with related parameter
52      */
53     public static NetworkElementConnectionEntity getNetworkConnectionDeviceTpe(NetworkElementDeviceType deviceType) {
54         NetworkElementConnectionBuilder eb = new NetworkElementConnectionBuilder();
55         eb.setDeviceType(deviceType);
56         return eb.build();
57     }
58
59     /**
60      * Provide device specific data
61      *
62      * @param nodeId mountpoint id
63      * @param nNode data
64      * @return NetworkElementConnectionEntity specific information
65      */
66     public static NetworkElementConnectionEntity getNetworkConnection(String nodeId, @Nonnull NetconfNode nNode) {
67         return getNetworkConnection(nodeId, nNode, Optional.empty());
68     }
69
70     public static NetworkElementConnectionEntity getNetworkConnection(String nodeId, @Nonnull NetconfNode nNode,
71             Optional<NetconfNode> configNetconfNode) {
72
73         NetworkElementConnectionBuilder eb = new NetworkElementConnectionBuilder();
74         // -- basics
75         eb.setId(nodeId).setNodeId(nodeId).setDeviceType(NetworkElementDeviceType.Unknown).setIsRequired(false);
76
77         // -- connection status
78         ConnectionLogStatus status = InternalConnectionStatus.statusFromNodeStatus(nNode.getConnectionStatus());
79         eb.setStatus(status);
80
81         // -- capabilites
82         Capabilities availableCapabilities = Capabilities.getAvailableCapabilities(nNode);
83         Capabilities unAvailableCapabilities = Capabilities.getUnavailableCapabilities(nNode);
84
85         NodeDetailsBuilder nodeDetails =
86                 new NodeDetailsBuilder().setAvailableCapabilities(new HashSet<>(availableCapabilities.getCapabilities()))
87                         .setUnavailableCapabilities(new HashSet<>(unAvailableCapabilities.getCapabilities()));
88         eb.setNodeDetails(nodeDetails.build());
89         // -- host information
90         Host host = nNode.getHost();
91         PortNumber portNumber = nNode.getPort();
92         if (host != null && portNumber != null) {
93             eb.setHost(host.stringValue()).setPort(Uint32.valueOf(portNumber.getValue()));
94         }
95
96         Credentials credentials =
97                 configNetconfNode.isPresent() ? configNetconfNode.get().getCredentials() : nNode.getCredentials();
98         if (credentials instanceof LoginPassword) {
99             LoginPassword loginPassword = (LoginPassword) credentials;
100             eb.setUsername(loginPassword.getUsername()).setPassword(loginPassword.getPassword())
101                     .setMountMethod(LoginPassword.class.getSimpleName());
102         } else if (credentials instanceof LoginPwUnencrypted) {
103             LoginPwUnencrypted loginPassword = (LoginPwUnencrypted) credentials;
104             eb.setUsername(loginPassword.getLoginPasswordUnencrypted().getUsername())
105                     .setPassword(loginPassword.getLoginPasswordUnencrypted().getPassword())
106                     .setMountMethod(LoginPwUnencrypted.class.getSimpleName());
107         } else if (credentials instanceof LoginPw) {
108             LoginPw loginPassword = (LoginPw) credentials;
109             eb.setUsername(loginPassword.getLoginPassword().getUsername())
110                     .setPassword(loginPassword.getLoginPassword().getPassword())
111                     .setMountMethod(LoginPw.class.getSimpleName());
112         } else if (credentials instanceof KeyAuth) {
113             KeyAuth keyAuth = (KeyAuth) credentials;
114             eb.setUsername(keyAuth.getKeyBased().getUsername()).setTlsKey(keyAuth.getKeyBased().getKeyId())
115                     .setMountMethod(KeyAuth.class.getSimpleName());
116         }
117         LOG.debug("mount-method: {} ({})", eb.getMountMethod(),
118                 credentials == null ? null : credentials.getClass().getName());
119         // Default value. Specific value (if any) is set in the specific devicemanagers
120         eb.setCoreModelCapability("Unsupported");
121         return eb.build();
122     }
123 }