ed9f583a5e5afd8ded308b68c236692f3bbfc54c
[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 =
56                     dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
57             pnfCommPort = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
58             if (pnfCommProtocol != null) {
59                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
60                     // Read username and keyId
61                     pnfKeyId =
62                             dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
63                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
64                             .textValue();
65                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
66                     // Read username and password
67                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
68                             .textValue();
69                     pnfPasswd = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password")
70                             .textValue();
71                 } else {
72                     // log warning - Unknown protocol
73                     LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",
74                             pnfCommProtocol, ". Defaulting to SSH");
75                     pnfCommProtocol = DEFAULT_PROTOCOL;
76                     pnfCommPort = DEFAULT_PORT;
77                     pnfUsername = DEFAULT_USERNAME;
78                     pnfPasswd = DEFAULT_PASSWORD;
79                 }
80             } else {
81                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
82                 pnfCommProtocol = DEFAULT_PROTOCOL;
83                 pnfCommPort = DEFAULT_PORT;
84                 pnfUsername = DEFAULT_USERNAME;
85                 pnfPasswd = DEFAULT_PASSWORD;
86             }
87
88             LOG.debug("PNF Fields - {} : {} : {} : {} : {} : {} : {}", pnfId, pnfIPv4Address, pnfCommProtocol, pnfKeyId,
89                     pnfUsername, pnfPasswd, pnfCommPort);
90
91             String baseUrl = getBaseUrl();
92             String sdnrUser = getSDNRUser();
93             String sdnrPasswd = getSDNRPasswd();
94
95             PNFMountPointClient mountpointClient = getPNFMountPointClient(baseUrl);
96             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
97             mountpointClient.setAuthorization(sdnrUser, sdnrPasswd);
98
99             mountpointClient.pnfMountPointCreate(pnfId, pnfIPv4Address, pnfCommProtocol, pnfKeyId, pnfUsername,
100                     pnfPasswd, pnfCommPort);
101         } catch (IOException e) {
102             LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",
103                     e.getMessage());
104         }
105     }
106
107     public String getBaseUrl() {
108         return GeneralConfig.getBaseUrl();
109     }
110
111     public String getSDNRUser() {
112         return GeneralConfig.getSDNRUser() != null ? GeneralConfig.getSDNRUser() : DEFAULT_SDNRUSER;
113     }
114
115     public String getSDNRPasswd() {
116         return GeneralConfig.getSDNRPasswd() != null ? GeneralConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
117     }
118
119     public PNFMountPointClient getPNFMountPointClient(String baseUrl) {
120         return new PNFMountPointClient(baseUrl);
121     }
122 }