d64996c6547243e42ab513fcd9a9f9d1a8f7f306
[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 /**
19  * Convert capabilities of netconfnode into internal format. Boron and Carbon are providing
20  * different versions
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.text.DateFormat;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Date;
31 import java.util.List;
32 import java.util.Optional;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Wrapper class for capabilites for Boron and later releases. Uses generics because yang model was
41  * changed from Boron to later version. Interface class:
42  * org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability
43  */
44 public class Capabilities {
45
46     private static final Logger LOG = LoggerFactory.getLogger(Capabilities.class);
47
48     private static final String METHODNAME = "getCapability";
49     private final List<String> capabilities = new ArrayList<>();
50     private final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
51
52     private Capabilities() {}
53
54     public static Capabilities getAvailableCapabilities(NetconfNode nnode) {
55         LOG.info("GetAvailableCapabilities for node");
56         Capabilities capabilities = new Capabilities();
57         if (nnode != null) {
58             AvailableCapabilities availableCapabilites = nnode.getAvailableCapabilities();
59             if (availableCapabilites != null) {
60                 capabilities.constructor(availableCapabilites.getAvailableCapability());
61             } else {
62                 LOG.debug("empty capabilites");
63             }
64         } else {
65             LOG.debug("No node provided");
66         }
67         return capabilities;
68     }
69
70     /**
71      * Does all construction steps
72      *
73      * @param pcapabilities with a list of capabilities. <br>
74      *        Type could be <br>
75      *        - Boron: List<code><String></code> <br>
76      *        - Carbon: List<AvailableCapability>
77      */
78     private void constructor(List<?> pcapabilities) {
79         if (pcapabilities != null) {
80             Method methodGetCapability;
81
82             for (Object capability : pcapabilities) {
83
84                 if (capability instanceof String) { // ODL Boron specific
85                     this.capabilities.add((String) capability);
86                 } else { // Carbon specific part .. handled via generics
87                     try {
88                         methodGetCapability = capability.getClass().getDeclaredMethod(METHODNAME);
89                         methodGetCapability.setAccessible(true);
90                         this.capabilities.add(methodGetCapability.invoke(capability).toString());
91                     } catch (NoSuchMethodException | SecurityException | IllegalAccessException
92                             | IllegalArgumentException | InvocationTargetException e) {
93                         LOG.warn("Capability class with missing interface method {}: {} {} {}", METHODNAME,
94                                 e.getMessage(), capability.getClass(),
95                                 Arrays.toString(capability.getClass().getInterfaces()));
96                     }
97                 }
98             }
99         }
100     }
101
102     /**
103      * check if the namespace and its revision are supported by the given capabilities
104      *
105      * @param qCapability capability from the model
106      * @return true if supporting the model
107      */
108     public boolean isSupportingNamespaceAndRevision(QName qCapability) {
109         String namespace = qCapability.getNamespace().toString();
110         String revision;
111         Object revisionObject = qCapability.getRevision();
112         if (revisionObject instanceof Optional) {
113             if (((Optional<?>) revisionObject).isPresent()) {
114                 revisionObject = ((Optional<?>) revisionObject).get();
115                 LOG.info("Unwrapp Optional: {}", revisionObject.getClass());
116             }
117         }
118         if (revisionObject instanceof String) {
119             revision = (String) revisionObject;
120         } else if (revisionObject instanceof Date) {
121             revision = formatter.format((Date) revisionObject);
122         } else {
123             revision = revisionObject.toString();
124             LOG.debug("Revision number type not supported. Use toString().String:{} Class:{} ", revisionObject,
125                     revisionObject.getClass().getName());
126         }
127         LOG.trace("isSupportingNamespaceAndRevision: Model namespace {}?[revision {}]", namespace, revision);
128         for (String capability : capabilities) {
129             if (capability.contains(namespace) && capability.contains(revision)) {
130                 LOG.trace("Verify true with: {}", capability);
131                 return true;
132             } else {
133                 LOG.trace("Verify false with: {}", capability);
134             }
135         }
136         return false;
137     }
138
139
140     @Override
141     public String toString() {
142         return "Capabilities [capabilities=" + capabilities + "]";
143     }
144
145 }