Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / aaiTree / FailureAAITreeNode.java
1 package org.onap.vid.model.aaiTree;
2
3 import org.apache.commons.proxy.Invoker;
4 import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
5 import org.jetbrains.annotations.NotNull;
6 import org.onap.vid.exceptions.GenericUncheckedException;
7
8 public class FailureAAITreeNode extends AAITreeNode {
9
10     private static final Class[] classes = {AAITreeNode.class};
11     private static final JavassistProxyFactory proxyFactory = new JavassistProxyFactory(); // can proxy a class without an interface
12
13     private FailureAAITreeNode() {
14         // don't instantiate this class
15     }
16
17     /*
18     Returns a new AAITreeNode instance that throws an exception when
19     invoking any of its methods
20      */
21     public static AAITreeNode of(Exception failureException) {
22         return (AAITreeNode) proxyFactory.createInvokerProxy(exceptionThrower(failureException), classes);
23     }
24
25     @NotNull
26     private static Invoker exceptionThrower(Exception exception) {
27         return (self, thisMethod, args) -> {
28             throw new GenericUncheckedException("AAI node fetching failed.", exception);
29         };
30     }
31 }