Proposal to remove OSGi dependencies from the CCSDK project
[ccsdk/distribution.git] / lighty / ccsdk-lighty-module / src / main / java / org / onap / ccsdk / distribution / lighty / CcsdkLightyApplication.java
1 /*
2  * ============LICENSE_START==========================================
3  * Copyright (c) 2019 PANTHEON.tech s.r.o.
4  * ===================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
12  * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
13  * limitations under the License.
14  * ============LICENSE_END============================================
15  *
16  */
17 package org.onap.ccsdk.distribution.lighty;
18
19 import io.lighty.core.controller.api.AbstractLightyModule;
20 import io.lighty.core.controller.api.LightyController;
21 import io.lighty.core.controller.impl.LightyControllerBuilder;
22 import io.lighty.core.controller.impl.config.ConfigurationException;
23 import io.lighty.core.controller.impl.config.ControllerConfiguration;
24 import io.lighty.modules.northbound.restconf.community.impl.CommunityRestConf;
25 import io.lighty.modules.northbound.restconf.community.impl.CommunityRestConfBuilder;
26 import io.lighty.modules.northbound.restconf.community.impl.config.RestConfConfiguration;
27 import io.lighty.modules.northbound.restconf.community.impl.util.RestConfConfigUtils;
28 import java.security.InvalidAlgorithmParameterException;
29 import java.security.InvalidKeyException;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.spec.InvalidKeySpecException;
32 import java.security.spec.KeySpec;
33 import java.util.Base64;
34 import javax.crypto.Cipher;
35 import javax.crypto.NoSuchPaddingException;
36 import javax.crypto.SecretKey;
37 import javax.crypto.SecretKeyFactory;
38 import javax.crypto.spec.IvParameterSpec;
39 import javax.crypto.spec.PBEKeySpec;
40 import javax.crypto.spec.SecretKeySpec;
41 import org.onap.ccsdk.sli.core.lighty.common.CcsdkLightyUtils;
42 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
43 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
44 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
45 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
46 import org.opendaylight.yang.gen.v1.config.aaa.authn.encrypt.service.config.rev160915.AaaEncryptServiceConfig;
47 import org.opendaylight.yang.gen.v1.config.aaa.authn.encrypt.service.config.rev160915.AaaEncryptServiceConfigBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * The implementation of the {@link io.lighty.core.controller.api.LightyModule} that groups all necessary components
53  * needed to start the CCSDK lighty.io application.
54  */
55 public class CcsdkLightyApplication extends AbstractLightyModule {
56
57     private static final Logger LOG = LoggerFactory.getLogger(CcsdkLightyApplication.class);
58
59     private ControllerConfiguration controllerConfiguration;
60     private RestConfConfiguration restConfConfiguration;
61
62     private LightyController lightyController;
63     private CommunityRestConf communityRestConf;
64     private CcsdkLightyModule ccsdkLightyModule;
65
66     public CcsdkLightyApplication(ControllerConfiguration controllerConfiguration,
67             RestConfConfiguration restConfConfiguration) {
68         this.controllerConfiguration = controllerConfiguration;
69         this.restConfConfiguration = restConfConfiguration;
70     }
71
72     @Override
73     protected boolean initProcedure() {
74         // Start Lighty Controller with base OLD services
75         LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
76         try {
77             lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
78         } catch (ConfigurationException e) {
79             LOG.error("Exception thrown while starting Lighty controller!", e);
80             return false;
81         }
82         if (!CcsdkLightyUtils.startLightyModule(lightyController)) {
83             LOG.error("Unable to start Lighty controller!");
84             return false;
85         }
86
87         // Start RestConf
88         CommunityRestConfBuilder communityRestConfBuilder = new CommunityRestConfBuilder();
89         communityRestConf = communityRestConfBuilder
90                 .from(RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration,
91                         lightyController.getServices()))
92                 .build();
93         if (!CcsdkLightyUtils.startLightyModule(communityRestConf)) {
94             LOG.error("Unable to start RestConf!");
95             return false;
96         }
97
98         // Start Lighty CCSDK
99         AAAEncryptionService aaaEncryptionService = null;
100         try {
101             aaaEncryptionService = createAAAEncryptionService(getDefaultAaaEncryptServiceConfig());
102         } catch (ConfigurationException e) {
103             LOG.error("Exception thrown while initializing {}!", AAAEncryptionService.class, e);
104         }
105         DataBroker dataBroker = lightyController.getServices().getControllerBindingDataBroker();
106         NotificationPublishService notificationPublishService = lightyController.getServices()
107                 .getControllerBindingNotificationPublishService();
108         RpcProviderRegistry rpcProviderRegistry = lightyController.getServices().getControllerRpcProviderRegistry();
109         ccsdkLightyModule = new CcsdkLightyModule(dataBroker, notificationPublishService,
110                 rpcProviderRegistry, aaaEncryptionService);
111         if (!CcsdkLightyUtils.startLightyModule(ccsdkLightyModule)) {
112             LOG.error("Unable to start CCSDK Lighty module!");
113             return false;
114         }
115
116         return true;
117     }
118
119     @Override
120     protected boolean stopProcedure() {
121         boolean stopSuccessful = true;
122
123         if (!CcsdkLightyUtils.stopLightyModule(ccsdkLightyModule)) {
124             stopSuccessful = false;
125         }
126
127         if (!CcsdkLightyUtils.stopLightyModule(communityRestConf)) {
128             stopSuccessful = false;
129         }
130
131         if (!CcsdkLightyUtils.stopLightyModule(lightyController)) {
132             stopSuccessful = false;
133         }
134
135         return stopSuccessful;
136     }
137
138     private AAAEncryptionService createAAAEncryptionService(AaaEncryptServiceConfig encrySrvConfig)
139             throws ConfigurationException {
140         final byte[] encryptionKeySalt = Base64.getDecoder().decode(encrySrvConfig.getEncryptSalt());
141         try {
142             final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encrySrvConfig.getEncryptMethod());
143             final KeySpec keySpec = new PBEKeySpec(encrySrvConfig.getEncryptKey().toCharArray(), encryptionKeySalt,
144                     encrySrvConfig.getEncryptIterationCount(), encrySrvConfig.getEncryptKeyLength());
145             SecretKey key = new SecretKeySpec(keyFactory.generateSecret(keySpec).getEncoded(),
146                     encrySrvConfig.getEncryptType());
147             IvParameterSpec ivParameterSpec = new IvParameterSpec(encryptionKeySalt);
148
149             Cipher encryptCipher = Cipher.getInstance(encrySrvConfig.getCipherTransforms());
150             encryptCipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
151
152             Cipher decryptCipher = Cipher.getInstance(encrySrvConfig.getCipherTransforms());
153             decryptCipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
154
155             return new AAAEncryptionServiceLightyImpl(encryptCipher, decryptCipher);
156
157         } catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
158                 | InvalidAlgorithmParameterException | InvalidKeyException e) {
159             throw new ConfigurationException(e);
160         }
161     }
162
163     private AaaEncryptServiceConfig getDefaultAaaEncryptServiceConfig() {
164         return new AaaEncryptServiceConfigBuilder().setEncryptKey("V1S1ED4OMeEh")
165                 .setPasswordLength(12).setEncryptSalt("TdtWeHbch/7xP52/rp3Usw==")
166                 .setEncryptMethod("PBKDF2WithHmacSHA1").setEncryptType("AES")
167                 .setEncryptIterationCount(32768).setEncryptKeyLength(128)
168                 .setCipherTransforms("AES/CBC/PKCS5Padding").build();
169     }
170 }