org.onap migration
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / WebConfig.java
1 package org.onap.vid.controller;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import org.apache.commons.io.IOUtils;
5 import org.json.JSONObject;
6 import org.json.JSONTokener;
7 import org.onap.vid.aai.AaiClient;
8 import org.onap.vid.aai.AaiClientInterface;
9 import org.onap.vid.asdc.AsdcClient;
10 import org.onap.vid.asdc.local.LocalAsdcClient;
11 import org.onap.vid.asdc.memory.InMemoryAsdcClient;
12 import org.onap.vid.asdc.parser.ToscaParserImpl2;
13 import org.onap.vid.asdc.rest.RestfulAsdcClient;
14 import org.onap.vid.properties.AsdcClientConfiguration;
15 import org.onap.vid.properties.AsdcClientConfiguration.AsdcClientType;
16 import org.onap.vid.services.AaiService;
17 import org.onap.vid.services.AaiServiceImpl;
18 import org.onap.vid.services.VidService;
19 import org.onap.vid.services.VidServiceImpl;
20 import org.springframework.context.annotation.Bean;
21 import org.springframework.context.annotation.Configuration;
22
23 import javax.net.ssl.SSLContext;
24 import javax.ws.rs.client.Client;
25 import javax.ws.rs.client.ClientBuilder;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.security.KeyManagementException;
31 import java.security.NoSuchAlgorithmException;
32 import java.util.Arrays;
33
34 @Configuration
35 public class WebConfig {
36
37     /**
38      * Gets the object mapper.
39      *
40      * @return the object mapper
41      */
42     @Bean
43     public ObjectMapper getObjectMapper() {
44         return new ObjectMapper();
45     }
46
47
48
49
50     @Bean
51     public VidService vidService(AsdcClient asdcClient) {
52         return new VidServiceImpl(asdcClient);
53     }
54
55     @Bean
56     public AaiService getAaiService(){
57         return new AaiServiceImpl();
58     }
59
60     @Bean
61     public AaiClientInterface getAaiClientInterface(){
62         return new AaiClient();
63     }
64
65     @Bean
66     public AsdcClient asdcClient(AsdcClientConfiguration asdcClientConfig) throws IOException {
67         switch (asdcClientConfig.getAsdcClientType()) {
68             case IN_MEMORY:
69                 final InputStream asdcCatalogFile = VidController.class.getClassLoader().getResourceAsStream("catalog.json");
70                 final JSONTokener tokener = new JSONTokener(asdcCatalogFile);
71                 final JSONObject catalog = new JSONObject(tokener);
72
73                 return new InMemoryAsdcClient.Builder().catalog(catalog).build();
74             case REST:
75
76                 final String protocol = asdcClientConfig.getAsdcClientProtocol();
77                 final String host = asdcClientConfig.getAsdcClientHost();
78                 final int port = asdcClientConfig.getAsdcClientPort();
79                 final String auth = asdcClientConfig.getAsdcClientAuth();
80                 Client cl = null;
81                 if (protocol.equalsIgnoreCase("https")) {
82                     try {
83                         SSLContext ctx = SSLContext.getInstance("TLSv1.2");
84                         ctx.init(null, null, null);
85                         cl = ClientBuilder.newBuilder().sslContext(ctx).build();
86                     } catch (NoSuchAlgorithmException n) {
87                         throw new RuntimeException("SDC Client could not be instantiated due to unsupported protocol TLSv1.2", n);
88                     } catch (KeyManagementException k) {
89                         throw new RuntimeException("SDC Client could not be instantiated due to a key management exception", k);
90                     }
91                 } else {
92                     cl = ClientBuilder.newBuilder().build();
93                 }
94
95                 try {
96                     final URI uri = new URI(protocol + "://" + host + ":" + port + "/");
97                     return new RestfulAsdcClient.Builder(cl, uri)
98                             .auth(auth)
99                             .build();
100                 } catch (URISyntaxException e) {
101                     throw new RuntimeException("SDC Client could not be instantiated due to a syntax error in the URI", e);
102                 }
103
104             case LOCAL:
105
106                 final InputStream asdcServicesFile = VidController.class.getClassLoader().getResourceAsStream("sdcservices.json");
107
108                 final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
109                 final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
110
111                 return new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
112
113             default:
114                 throw new RuntimeException(asdcClientConfig.getAsdcClientType() + " is invalid; must be one of " + Arrays.toString(AsdcClientType.values()));
115         }
116     }
117
118     @Bean
119     public ToscaParserImpl2 getToscaParser() {
120         return new ToscaParserImpl2();
121     }
122
123 }