23c81869cbe73956c441c36393df5d74e2de1c9d
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onosproject.store.cluster.messaging.impl;
17
18 import com.google.common.base.Strings;
19
20 import org.apache.felix.scr.annotations.Activate;
21 import org.apache.felix.scr.annotations.Component;
22 import org.apache.felix.scr.annotations.Deactivate;
23 import org.apache.felix.scr.annotations.Reference;
24 import org.apache.felix.scr.annotations.ReferenceCardinality;
25 import org.apache.felix.scr.annotations.Service;
26 import org.onlab.netty.NettyMessaging;
27 import org.onosproject.cluster.ClusterMetadataService;
28 import org.onosproject.cluster.ControllerNode;
29 import org.onosproject.store.cluster.messaging.Endpoint;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Netty based MessagingService.
35  */
36 @Component(immediate = true, enabled = true)
37 @Service
38 public class NettyMessagingManager extends NettyMessaging {
39
40     private final Logger log = LoggerFactory.getLogger(getClass());
41
42     private static final short MIN_KS_LENGTH = 6;
43
44     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45     protected ClusterMetadataService clusterMetadataService;
46
47     @Activate
48     public void activate() throws Exception {
49         ControllerNode localNode = clusterMetadataService.getLocalNode();
50         getTLSParameters();
51         super.start(new Endpoint(localNode.ip(), localNode.tcpPort()));
52         log.info("Started");
53     }
54
55     @Deactivate
56     public void deactivate() throws Exception {
57         super.stop();
58         log.info("Stopped");
59     }
60
61     private void getTLSParameters() {
62         String tempString = System.getProperty("enableNettyTLS");
63         enableNettyTLS = Strings.isNullOrEmpty(tempString) ? TLS_DISABLED : Boolean.parseBoolean(tempString);
64         log.info("enableNettyTLS = {}", enableNettyTLS);
65         if (enableNettyTLS) {
66             ksLocation = System.getProperty("javax.net.ssl.keyStore");
67             if (Strings.isNullOrEmpty(ksLocation)) {
68                 enableNettyTLS = TLS_DISABLED;
69                 return;
70             }
71             tsLocation = System.getProperty("javax.net.ssl.trustStore");
72             if (Strings.isNullOrEmpty(tsLocation)) {
73                 enableNettyTLS = TLS_DISABLED;
74                 return;
75             }
76             ksPwd = System.getProperty("javax.net.ssl.keyStorePassword").toCharArray();
77             if (MIN_KS_LENGTH > ksPwd.length) {
78                 enableNettyTLS = TLS_DISABLED;
79                 return;
80             }
81             tsPwd = System.getProperty("javax.net.ssl.trustStorePassword").toCharArray();
82             if (MIN_KS_LENGTH > tsPwd.length) {
83                 enableNettyTLS = TLS_DISABLED;
84                 return;
85             }
86         }
87     }
88 }