Assign image keyname and pubkey at vnf level
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / devicemanager / impl / src / main / java / org / opendaylight / mwtn / base / netconf / Capabilities.java
1 /**
2  * Convert capabilities of netconfnode into internal format.
3  * Boron and Carbon are providing different versions
4  */
5 package org.opendaylight.mwtn.base.netconf;
6
7 import java.lang.reflect.InvocationTargetException;
8 import java.lang.reflect.Method;
9 import java.lang.reflect.Proxy;
10 import java.util.ArrayList;
11 import java.util.Arrays;
12 import java.util.List;
13
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class Capabilities {
20
21     private static final Logger LOG = LoggerFactory.getLogger(Capabilities.class);
22     private static final String INTERFACE_AVAILABLECAPABILITY = "org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability";
23
24     private final List<String> capabilities = new ArrayList<String>();
25
26     public Capabilities(NetconfNode nnode) {
27         LOG.info("Create Capabilities constructor");
28
29         if (nnode != null) {
30                 constructor( nnode.getAvailableCapabilities().getAvailableCapability() );
31         }
32     }
33
34     /**
35      * Does all construction steps
36      * @param pcapabilities with a list of capabilities. <br>
37      * Type could be <br>
38      * - Boron: List<code><String></code> <br>
39      * - Carbon: List<AvailableCapability>
40      */
41     private void constructor(List<?> pcapabilities) {
42         for (Object capability : pcapabilities) {
43                 if (LOG.isTraceEnabled()) {
44                         LOG.trace("capability class: {} Interfaces: {}", capability.getClass().getName(), Arrays.toString(capability.getClass().getInterfaces()));
45                 }
46                 if (capability instanceof String) { //ODL Boron specific
47                         this.capabilities.add((String)capability);
48                 } else if (hasInterface(capability, INTERFACE_AVAILABLECAPABILITY)) {  //Carbon specific part  .. handled via generic
49                         try {
50                                 Method method = capability.getClass().getDeclaredMethod("getCapability");
51                                 this.capabilities.add(method.invoke(capability).toString());
52                         } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
53                                 LOG.warn("Unknown capability class leads to a problem", e);
54                         }
55                 } else {
56                         LOG.warn("Unknown capability class: {}", capability.getClass(), Arrays.toString(capability.getClass().getInterfaces()));
57                 }
58         }
59     }
60
61     /**
62      * check if namespace is supported by given capabilites
63      * @param theCapability Capability to search
64      * @return true if available
65      */
66     public boolean isSupportingNamespace(QName theCapability ) {
67
68         String theNameSpace = theCapability.getNamespace().toString();
69
70         for (String capability : capabilities) {
71             LOG.trace("Check {} against {}", capability, theNameSpace);
72             if (capability.contains(theNameSpace)) {
73                 return true;
74             }
75         }
76         return false;
77     }
78
79         @Override
80         public String toString() {
81                 return "Capabilities [capabilities=" + capabilities + "]";
82         }
83
84
85         /**
86          * Check if object is proxy and has specific interface
87          * @param object Name of the object to verify
88          * @param interfaceName is the name of the interface
89          * @return boolean accordingly
90          */
91         static boolean hasInterface(Object object, String interfaceName) {
92                 if (object instanceof Proxy) {
93                         Class<?>[] interfaces = object.getClass().getInterfaces();
94                         for (Class<?> i : interfaces) {
95                                 if (i.getName().equals(interfaceName)) {
96                                         return true;
97                                 }
98                         }
99                 }
100                 return false;
101         }
102
103 }