1d95ea5227269c32222b40e058ae77d44e05cfbf
[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.eclipse.jdt.annotation.Nullable;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class DMaaPPNFRegVESMsgConsumer extends DMaaPVESMsgConsumerImpl {
29
30     private static final Logger LOG = LoggerFactory.getLogger(DMaaPPNFRegVESMsgConsumer.class);
31     private static final String DEFAULT_PROTOCOL = "SSH";
32     private static final String DEFAULT_PORT = "17830";
33     private static final String DEFAULT_USERNAME = "netconf";
34     private static final String DEFAULT_PASSWORD = "netconf";
35     private static final String DEFAULT_SDNRUSER = "admin";
36     private static final String DEFAULT_SDNRPASSWD = "admin";
37
38     private final GeneralConfig generalConfig;
39
40     public DMaaPPNFRegVESMsgConsumer(GeneralConfig generalConfig) {
41         this.generalConfig = generalConfig;
42     }
43
44     @Override
45     public void processMsg(String msg) {
46         LOG.debug("Message from DMaaP topic is - {} ", msg);
47         String pnfId;
48         String pnfIPAddress;
49         @Nullable
50         String pnfCommProtocol;
51         @Nullable
52         String pnfCommPort;
53         @Nullable
54         String pnfKeyId = null;
55         @Nullable
56         String pnfUsername;
57         @Nullable
58         String pnfPasswd = null;
59         String reportingEntityName;
60         ObjectMapper oMapper = new ObjectMapper();
61         JsonNode dmaapMessageRootNode;
62         try {
63             dmaapMessageRootNode = oMapper.readTree(msg);
64             reportingEntityName = dmaapMessageRootNode.at("/event/commonEventHeader/reportingEntityName").textValue();
65             if (reportingEntityName.equals("ONAP SDN-R")) {
66                 LOG.info(
67                         "VES PNF Registration message generated by SDNR, hence no need to process any further; Ignoring the received message");
68                 return;
69             }
70
71             pnfId = dmaapMessageRootNode.at("/event/commonEventHeader/sourceName").textValue();
72             pnfIPAddress = getPNFIPAddress(dmaapMessageRootNode);
73             pnfCommProtocol =
74                     dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/protocol").textValue();
75             pnfCommPort = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/oamPort").textValue();
76             if (pnfCommProtocol != null) {
77                 if (pnfCommProtocol.equalsIgnoreCase("TLS")) {
78                     // Read username and keyId
79                     pnfKeyId =
80                             dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/keyId").textValue();
81                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
82                             .textValue();
83                 } else if (pnfCommProtocol.equalsIgnoreCase("SSH")) {
84                     // Read username and password
85                     pnfUsername = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/username")
86                             .textValue();
87                     pnfPasswd = dmaapMessageRootNode.at("/event/pnfRegistrationFields/additionalFields/password")
88                             .textValue();
89                 } else {
90                     // log warning - Unknown protocol
91                     LOG.warn("Only SSH and TLS protocols supported. Protocol specified in VES message is - {}",
92                             pnfCommProtocol, ". Defaulting to SSH");
93                     pnfCommProtocol = DEFAULT_PROTOCOL;
94                     pnfCommPort = DEFAULT_PORT;
95                     pnfUsername = DEFAULT_USERNAME;
96                     pnfPasswd = DEFAULT_PASSWORD;
97                 }
98             } else {
99                 LOG.warn("Protocol not specified in VES message, Defaulting to SSH");
100                 pnfCommProtocol = DEFAULT_PROTOCOL;
101                 pnfCommPort = DEFAULT_PORT;
102                 pnfUsername = DEFAULT_USERNAME;
103                 pnfPasswd = DEFAULT_PASSWORD;
104             }
105
106             LOG.debug(
107                     "PNF Fields - ID - {} : IP Address - {} : Protocol - {} : TLS Key ID - {} : User - {} : Port - {}",
108                     pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername, pnfCommPort);
109
110             String baseUrl = getBaseUrl();
111             String sdnrUser = getSDNRUser();
112             String sdnrPasswd = getSDNRPasswd();
113
114             PNFMountPointClient mountpointClient = getPNFMountPointClient(baseUrl);
115             LOG.debug("Setting RESTConf Authorization values - {} : {}", sdnrUser, sdnrPasswd);
116             mountpointClient.setAuthorization(sdnrUser, sdnrPasswd);
117
118             if ((null != pnfId) && null != pnfIPAddress && (null != pnfCommProtocol) && (null != pnfUsername)
119                     && (null != pnfCommPort)) {
120                 mountpointClient.pnfMountPointCreate(pnfId, pnfIPAddress, pnfCommProtocol, pnfKeyId, pnfUsername,
121                         pnfPasswd, pnfCommPort);
122             } else {
123                 LOG.warn(
124                         "One of the mandatory fields has a null value - pnfId = {} : pnfIPAddress = {} : pnfCommProtocol = {} : pnfUsername {} : "
125                                 + "pnfCommPort {}",
126                         pnfId, pnfIPAddress, pnfCommProtocol, pnfUsername, pnfCommPort,
127                         "- not invoking mountpoint creation");
128             }
129         } catch (IOException e) {
130             LOG.info("Cannot parse json object, ignoring the received PNF Registration VES Message. Reason: {}",
131                     e.getMessage());
132         }
133     }
134
135     private String getPNFIPAddress(JsonNode dmaapMessageRootNode) {
136         String ipAddress = dmaapMessageRootNode.at("/event/pnfRegistrationFields/oamV6IpAddress").textValue();
137         if (ipAddress != null && ipAddress != "")
138             return ipAddress;
139
140         ipAddress = dmaapMessageRootNode.at("/event/pnfRegistrationFields/oamV4IpAddress").textValue();
141         if (ipAddress != null && ipAddress != "")
142             return ipAddress;
143
144         return null;
145     }
146
147     public String getBaseUrl() {
148         return generalConfig.getBaseUrl();
149     }
150
151     public String getSDNRUser() {
152         return generalConfig.getSDNRUser() != null ? generalConfig.getSDNRUser() : DEFAULT_SDNRUSER;
153     }
154
155     public String getSDNRPasswd() {
156         return generalConfig.getSDNRPasswd() != null ? generalConfig.getSDNRPasswd() : DEFAULT_SDNRPASSWD;
157     }
158
159     private PNFMountPointClient getPNFMountPointClient(String baseUrl) {
160         return new PNFMountPointClient(baseUrl);
161     }
162 }