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