b64f6c689b200fb4c0aea956fce09ccf378c8c7b
[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 com.fasterxml.jackson.databind.JsonNode;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import java.io.IOException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class DMaaPPNFRegVESMsgConsumer extends DMaaPVESMsgConsumerImpl {
28
29     private static final Logger LOG = LoggerFactory.getLogger(DMaaPPNFRegVESMsgConsumer.class);
30     private static final String DEFAULT_PROTOCOL = "SSH";
31     private static final String DEFAULT_PORT = "17830";
32     private static final String DEFAULT_USERNAME = "netconf";
33     private static final String DEFAULT_PASSWORD = "netconf";
34     private static final String DEFAULT_SDNRUSER = "admin";
35     private static final String DEFAULT_SDNRPASSWD = "admin";
36
37     private final GeneralConfig generalConfig;
38
39     public DMaaPPNFRegVESMsgConsumer(GeneralConfig generalConfig) {
40         this.generalConfig = generalConfig;
41     }
42
43     @Override
44     public void processMsg(String msg) {
45         LOG.debug("Message from DMaaP topic is - {} ", msg);
46         String pnfId;
47         String pnfIPv4Address;
48         String pnfCommProtocol;
49         String pnfCommPort;
50         String pnfKeyId = null;
51         String pnfUsername;
52         String pnfPasswd = null;
53         ObjectMapper oMapper = new ObjectMapper();
54         JsonNode dmaapMessageRootNode;
55         try {
56             dmaapMessageRootNode = oMapper.readTree(msg);
57             pnfId = dmaapMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
58             pnfIPv4Address = dmaapMessageRootNode.at("/event/pnfRegistrationFields/oamV4IpAddress").textValue();
59             pnfCommProtocol =
60                     dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
61             pnfCommPort = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
62             if (pnfCommProtocol != null) {
63                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
64                     // Read username and keyId
65                     pnfKeyId =
66                             dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
67                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
68                             .textValue();
69                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
70                     // Read username and password
71                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
72                             .textValue();
73                     pnfPasswd = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password")
74                             .textValue();
75                 } else {
76                     // log warning - Unknown protocol
77                     LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",
78                             pnfCommProtocol, ". Defaulting to SSH");
79                     pnfCommProtocol = DEFAULT_PROTOCOL;
80                     pnfCommPort = DEFAULT_PORT;
81                     pnfUsername = DEFAULT_USERNAME;
82                     pnfPasswd = DEFAULT_PASSWORD;
83                 }
84             } else {
85                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
86                 pnfCommProtocol = DEFAULT_PROTOCOL;
87                 pnfCommPort = DEFAULT_PORT;
88                 pnfUsername = DEFAULT_USERNAME;
89                 pnfPasswd = DEFAULT_PASSWORD;
90             }
91
92             LOG.debug("PNF Fields - {} : {} : {} : {} : {} : {} : {}", pnfId, pnfIPv4Address, pnfCommProtocol, pnfKeyId,
93                     pnfUsername, pnfPasswd, pnfCommPort);
94
95             String baseUrl = getBaseUrl();
96             String sdnrUser = getSDNRUser();
97             String sdnrPasswd = getSDNRPasswd();
98
99             PNFMountPointClient mountpointClient = getPNFMountPointClient(baseUrl);
100             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
101             mountpointClient.setAuthorization(sdnrUser, sdnrPasswd);
102
103             if ((null != pnfId) && (null != pnfIPv4Address) && (null != pnfCommProtocol) && (null != pnfUsername)
104                     && (null != pnfCommPort)) {
105                 mountpointClient.pnfMountPointCreate(pnfId, pnfIPv4Address, pnfCommProtocol, pnfKeyId, pnfUsername,
106                         pnfPasswd, pnfCommPort);
107             } else {
108                 LOG.warn("One of the mandatory fields has a null value - pnfId = {} : pnfIPv4Address = {} : pnfCommProtocol = {} : pnfUsername {} : "
109                         + "pnfCommPort {}", pnfId, pnfIPv4Address, pnfCommProtocol, pnfUsername, pnfCommPort, "- not invoking mountpoint creation");
110             }
111         } catch (IOException e) {
112             LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",
113                     e.getMessage());
114         }
115     }
116
117     public String getBaseUrl() {
118         return generalConfig.getBaseUrl();
119     }
120
121     public String getSDNRUser() {
122         return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
123     }
124
125     public String getSDNRPasswd() {
126         return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
127     }
128
129     public PNFMountPointClient getPNFMountPointClient(String baseUrl) {
130         return new PNFMountPointClient(baseUrl);
131     }
132 }