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
8 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
17 package org.onap.ccsdk.distribution.lighty;
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;
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.
55 public class CcsdkLightyApplication extends AbstractLightyModule {
57 private static final Logger LOG = LoggerFactory.getLogger(CcsdkLightyApplication.class);
59 private ControllerConfiguration controllerConfiguration;
60 private RestConfConfiguration restConfConfiguration;
62 private LightyController lightyController;
63 private CommunityRestConf communityRestConf;
64 private CcsdkLightyModule ccsdkLightyModule;
66 public CcsdkLightyApplication(ControllerConfiguration controllerConfiguration,
67 RestConfConfiguration restConfConfiguration) {
68 this.controllerConfiguration = controllerConfiguration;
69 this.restConfConfiguration = restConfConfiguration;
73 protected boolean initProcedure() {
74 // Start Lighty Controller with base OLD services
75 LightyControllerBuilder lightyControllerBuilder = new LightyControllerBuilder();
77 lightyController = lightyControllerBuilder.from(controllerConfiguration).build();
78 } catch (ConfigurationException e) {
79 LOG.error("Exception thrown while starting Lighty controller!", e);
82 if (!CcsdkLightyUtils.startLightyModule(lightyController)) {
83 LOG.error("Unable to start Lighty controller!");
88 CommunityRestConfBuilder communityRestConfBuilder = new CommunityRestConfBuilder();
89 communityRestConf = communityRestConfBuilder
90 .from(RestConfConfigUtils.getRestConfConfiguration(restConfConfiguration,
91 lightyController.getServices()))
93 if (!CcsdkLightyUtils.startLightyModule(communityRestConf)) {
94 LOG.error("Unable to start RestConf!");
99 AAAEncryptionService aaaEncryptionService = null;
101 aaaEncryptionService = createAAAEncryptionService(getDefaultAaaEncryptServiceConfig());
102 } catch (ConfigurationException e) {
103 LOG.error("Exception thrown while initializing {}!", AAAEncryptionService.class, e);
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!");
120 protected boolean stopProcedure() {
121 boolean stopSuccessful = true;
123 if (!CcsdkLightyUtils.stopLightyModule(ccsdkLightyModule)) {
124 stopSuccessful = false;
127 if (!CcsdkLightyUtils.stopLightyModule(communityRestConf)) {
128 stopSuccessful = false;
131 if (!CcsdkLightyUtils.stopLightyModule(lightyController)) {
132 stopSuccessful = false;
135 return stopSuccessful;
138 private AAAEncryptionService createAAAEncryptionService(AaaEncryptServiceConfig encrySrvConfig)
139 throws ConfigurationException {
140 final byte[] encryptionKeySalt = Base64.getDecoder().decode(encrySrvConfig.getEncryptSalt());
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);
149 Cipher encryptCipher = Cipher.getInstance(encrySrvConfig.getCipherTransforms());
150 encryptCipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
152 Cipher decryptCipher = Cipher.getInstance(encrySrvConfig.getCipherTransforms());
153 decryptCipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
155 return new AAAEncryptionServiceLightyImpl(encryptCipher, decryptCipher);
157 } catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
158 | InvalidAlgorithmParameterException | InvalidKeyException e) {
159 throw new ConfigurationException(e);
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();