Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / controller / WebConfig.java
1 package org.openecomp.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.openecomp.vid.aai.AaiClient;
8 import org.openecomp.vid.aai.AaiClientInterface;
9 import org.openecomp.vid.asdc.AsdcClient;
10 import org.openecomp.vid.asdc.local.LocalAsdcClient;
11 import org.openecomp.vid.asdc.memory.InMemoryAsdcClient;
12 import org.openecomp.vid.asdc.parser.ToscaParserImpl2;
13 import org.openecomp.vid.asdc.rest.RestfulAsdcClient;
14 import org.openecomp.vid.properties.AsdcClientConfiguration;
15 import org.openecomp.vid.properties.AsdcClientConfiguration.AsdcClientType;
16 import org.openecomp.vid.services.AaiService;
17 import org.openecomp.vid.services.AaiServiceImpl;
18 import org.openecomp.vid.services.VidService;
19 import org.openecomp.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     @Bean
50     public VidService vidService(AsdcClient asdcClient) {
51         return new VidServiceImpl(asdcClient);
52     }
53
54     @Bean
55     public AaiService getAaiService(){
56         return new AaiServiceImpl();
57     }
58
59     @Bean
60     public AaiClientInterface getAaiClientInterface(){
61         return new AaiClient();
62     }
63
64     @Bean
65     public AsdcClient asdcClient(AsdcClientConfiguration asdcClientConfig) throws IOException {
66         switch (asdcClientConfig.getAsdcClientType()) {
67             case IN_MEMORY:
68                 final InputStream asdcCatalogFile = VidController.class.getClassLoader().getResourceAsStream("catalog.json");
69                 final JSONTokener tokener = new JSONTokener(asdcCatalogFile);
70                 final JSONObject catalog = new JSONObject(tokener);
71
72                 return new InMemoryAsdcClient.Builder().catalog(catalog).build();
73             case REST:
74
75                 final String protocol = asdcClientConfig.getAsdcClientProtocol();
76                 final String host = asdcClientConfig.getAsdcClientHost();
77                 final int port = asdcClientConfig.getAsdcClientPort();
78                 final String auth = asdcClientConfig.getAsdcClientAuth();
79                 Client cl = null;
80                 if (protocol.equalsIgnoreCase("https")) {
81                     try {
82                         SSLContext ctx = SSLContext.getInstance("TLSv1.2");
83                         ctx.init(null, null, null);
84                         cl = ClientBuilder.newBuilder().sslContext(ctx).build();
85                     } catch (NoSuchAlgorithmException n) {
86                         throw new RuntimeException("SDC Client could not be instantiated due to unsupported protocol TLSv1.2", n);
87                     } catch (KeyManagementException k) {
88                         throw new RuntimeException("SDC Client could not be instantiated due to a key management exception", k);
89                     }
90                 } else {
91                     cl = ClientBuilder.newBuilder().build();
92                 }
93
94                 try {
95                     final URI uri = new URI(protocol + "://" + host + ":" + port + "/");
96                     return new RestfulAsdcClient.Builder(cl, uri)
97                             .auth(auth)
98                             .build();
99                 } catch (URISyntaxException e) {
100                     throw new RuntimeException("SDC Client could not be instantiated due to a syntax error in the URI", e);
101                 }
102
103             case LOCAL:
104
105                 final InputStream asdcServicesFile = VidController.class.getClassLoader().getResourceAsStream("sdcservices.json");
106
107                 final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
108                 final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
109
110                 return new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
111
112             default:
113                 throw new RuntimeException(asdcClientConfig.getAsdcClientType() + " is invalid; must be one of " + Arrays.toString(AsdcClientType.values()));
114         }
115     }
116
117     @Bean
118     public ToscaParserImpl2 getToscaParser() {
119         return new ToscaParserImpl2();
120     }
121
122 }