00d7e95145b624fa397a6e7f75b94cedf4be7afc
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt mountpoint-registrar
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18
19 package org.onap.ccsdk.features.sdnr.wt.mountpointregistrar.impl;
20
21 import java.io.IOException;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 public class DMaaPPNFRegVESMsgConsumer extends DMaaPVESMsgConsumerImpl {
30
31         private static final Logger LOG = LoggerFactory.getLogger(DMaaPPNFRegVESMsgConsumer.class);
32         private static final String DEFAULT_PROTOCOL = "SSH";
33         private static final String DEFAULT_PORT = "17830";
34         private static final String DEFAULT_USERNAME = "netconf";
35         private static final String DEFAULT_PASSWORD = "netconf";
36         private static final String DEFAULT_SDNRUSER = "admin";
37         private static final String DEFAULT_SDNRPASSWD = "admin";
38         
39         @Override
40         public void processMsg(String msg) {
41                 LOG.debug("Message from DMaaP topic is - {} ",msg);
42                 String pnfId;
43                 String pnfIPv4Address;
44                 String pnfCommProtocol;
45                 String pnfCommPort;
46                 String pnfKeyId = null;
47                 String pnfUsername;
48                 String pnfPasswd = null;
49                 ObjectMapper oMapper = new ObjectMapper();
50                 JsonNode dmaapMessageRootNode;
51                 try {
52                         dmaapMessageRootNode = oMapper.readTree(msg);
53                         pnfId = dmaapMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
54                         pnfIPv4Address =  dmaapMessageRootNode.at("/event/pnfRegistrationFields/oamV4IpAddress").textValue();  
55                         pnfCommProtocol = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
56                         pnfCommPort = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
57                         if (pnfCommProtocol != null) {
58                                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
59                                         // Read username and keyId
60                                         pnfKeyId = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
61                                         pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username").textValue();
62                                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
63                                         // Read username and password
64                                         pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username").textValue();
65                                         pnfPasswd = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password").textValue();
66                                 } else {
67                                         // log warning - Unknown protocol
68                                         LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",pnfCommProtocol,". Defaulting to SSH");
69                                         pnfCommProtocol = DEFAULT_PROTOCOL;
70                                         pnfCommPort = DEFAULT_PORT;
71                                         pnfUsername = DEFAULT_USERNAME;
72                                         pnfPasswd = DEFAULT_PASSWORD;
73                                 }
74                         } else {
75                                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
76                                 pnfCommProtocol = DEFAULT_PROTOCOL;
77                                 pnfCommPort = DEFAULT_PORT;
78                                 pnfUsername = DEFAULT_USERNAME;
79                                 pnfPasswd = DEFAULT_PASSWORD;
80                         }
81                         
82                         LOG.debug("PNF Fields - {} : {} : {} : {} : {} : {} : {}",pnfId,pnfIPv4Address,pnfCommProtocol,pnfKeyId,pnfUsername,pnfPasswd,pnfCommPort);
83
84                         String baseUrl = getBaseUrl();
85                         String sdnrUser = getSDNRUser();
86                         String sdnrPasswd = getSDNRPasswd();
87
88                         PNFMountPointClient mountpointClient = getPNFMountPointClient(baseUrl);
89                         LOG.debug("Setting RESTConf Authorization values - {} : {}",sdnrUser,sdnrPasswd);
90                         mountpointClient.setAuthorization(sdnrUser, sdnrPasswd);
91                         
92                         mountpointClient.pnfMountPointCreate(pnfId, 
93                                         pnfIPv4Address, 
94                                         pnfCommProtocol, pnfKeyId, 
95                                         pnfUsername, pnfPasswd, 
96                                         pnfCommPort);           
97                 } catch (IOException e) {
98                         LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",e.getMessage());
99                 }
100         }
101         
102         public String getBaseUrl() {
103                 return GeneralConfig.getBaseUrl();
104         }
105         
106         public String getSDNRUser() {
107                 return GeneralConfig.getSDNRUser()!= null?GeneralConfig.getSDNRUser():DEFAULT_SDNRUSER;
108         }
109         
110         public String getSDNRPasswd() {
111                 return GeneralConfig.getSDNRPasswd()!= null?GeneralConfig.getSDNRPasswd():DEFAULT_SDNRPASSWD;
112         }
113         
114         public PNFMountPointClient getPNFMountPointClient(String baseUrl) {
115                 return new PNFMountPointClient(baseUrl);
116         }
117 }