47ac9c785085e541804bfd1469ae6fece90b9b28
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / vescollectorconnector / impl / VESCollectorServiceImpl.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Base64;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33 import org.eclipse.jdt.annotation.NonNull;
34 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
35 import org.onap.ccsdk.features.sdnr.wt.common.configuration.filechange.IConfigChangedListener;
36 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPClient;
37 import org.onap.ccsdk.features.sdnr.wt.common.http.BaseHTTPResponse;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.NotificationProxyParserImpl;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationProxyParser;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorConfigChangeListener;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESFaultFieldsPOJO;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESMessage;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESNotificationFieldsPOJO;
46 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESPNFRegistrationFieldsPOJO;
47 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class VESCollectorServiceImpl implements VESCollectorService, IConfigChangedListener, AutoCloseable {
52     private static final Logger LOG = LoggerFactory.getLogger(VESCollectorServiceImpl.class);
53     private final VESCollectorCfgImpl vesConfig;
54     private final ConfigurationFileRepresentation cfg;
55     private BaseHTTPClient httpClient;
56     private final Map<String, String> headerMap;
57     private List<VESCollectorConfigChangeListener> registeredObjects;
58     private final ObjectMapper objMapper;
59
60
61     public VESCollectorServiceImpl(ConfigurationFileRepresentation config) {
62         registeredObjects = new ArrayList<VESCollectorConfigChangeListener>();
63         this.vesConfig = new VESCollectorCfgImpl(config);
64         this.cfg = config;
65         this.cfg.registerConfigChangedListener(this);
66         this.objMapper = new ObjectMapper();
67
68         httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
69
70         this.headerMap = new HashMap<>();
71         this.headerMap.put("Content-Type", "application/json");
72         this.headerMap.put("Accept", "application/json");
73
74         setAuthorization(getConfig().getUsername(), getConfig().getPassword());
75     }
76
77     @Override
78     public VESCollectorCfgImpl getConfig() {
79         return this.vesConfig;
80     }
81
82     public String getBaseUrl() {
83         LOG.debug("IP Address is - {}", getConfig().getIP());
84         if (!getConfig().getTLSEnabled()) {
85             return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
86         } else {
87             return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
88         }
89     }
90
91     private void setAuthorization(String username, String password) {
92         if (getConfig().getTLSEnabled()) {
93             String credentials = username + ":" + password;
94             this.headerMap.put("Authorization",
95                     "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
96         }
97
98     }
99
100     @Override
101     public boolean publishVESMessage(VESMessage message) {
102         LOG.info("In VESClient - {} ", message.getMessage());
103         BaseHTTPResponse response;
104         try {
105             String uri = "eventListener" + "/" + getConfig().getVersion();
106             response = httpClient.sendRequest(uri, "POST", message.getMessage(), headerMap);
107             LOG.debug("finished with responsecode {}", response.code);
108             return response.code == 200;
109         } catch (IOException e) {
110             LOG.warn("problem publishing VES message {} ", e.getMessage());
111             return false;
112         }
113
114     }
115
116     @Override
117     public void close() throws Exception {
118         this.cfg.unregisterConfigChangedListener(this);
119     }
120
121     @Override
122     public void onConfigChanged() {
123         LOG.debug("In onConfigChanged - isTrustAllCerts = {} getBaseUrl = {}", getConfig().isTrustAllCerts(), getBaseUrl());
124         httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
125         setAuthorization(getConfig().getUsername(), getConfig().getPassword());
126         Iterator<VESCollectorConfigChangeListener> it = registeredObjects.iterator();
127         while (it.hasNext()) {
128             VESCollectorConfigChangeListener o = it.next();
129             o.notify(getConfig());
130         }
131     }
132
133     @Override
134     public void registerForChanges(VESCollectorConfigChangeListener o) {
135         registeredObjects.add(o);
136     }
137
138     @Override
139     public void deregister(VESCollectorConfigChangeListener o) {
140         registeredObjects.remove(o);
141     }
142
143     @Override
144     public @NonNull NotificationProxyParser getNotificationProxyParser() {
145         return new NotificationProxyParserImpl();
146     }
147
148     /**
149      * Generates VES Event JSON containing commonEventHeader and notificationFields fields
150      *
151      * @param commonEventHeader
152      * @param notifFields
153      * @return VESMessage - representing the VESEvent JSON
154      * @throws JsonProcessingException
155      */
156     @Override
157     public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
158             VESNotificationFieldsPOJO notifFields) throws JsonProcessingException {
159         Map<String, Object> innerEvent = new HashMap<String, Object>();
160         innerEvent.put("commonEventHeader", commonEventHeader);
161         innerEvent.put("notificationFields", notifFields);
162
163         Map<String, Object> outerEvent = new HashMap<String, Object>();
164         outerEvent.put("event", innerEvent);
165         LOG.info("in generateVESEvent - {}", objMapper.writeValueAsString(outerEvent));
166         return new VESMessage(objMapper.writeValueAsString(outerEvent));
167     }
168
169     /**
170      * Generates VES Event JSON containing commonEventHeader and faultFields fields
171      *
172      * @param commonEventHeader
173      * @param faultFields
174      * @return VESMessage - representing the VES Event JSON
175      * @throws JsonProcessingException
176      */
177     @Override
178     public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader, VESFaultFieldsPOJO faultFields) throws JsonProcessingException {
179         Map<String, Object> innerEvent = new HashMap<String, Object>();
180         innerEvent.put("commonEventHeader", commonEventHeader);
181         innerEvent.put("faultFields", faultFields);
182
183         Map<String, Object> outerEvent = new HashMap<String, Object>();
184         outerEvent.put("event", innerEvent);
185         return new VESMessage(objMapper.writeValueAsString(outerEvent));
186     }
187
188     /**
189      * Generates VES Event JSON containing commonEventHeader and pnfRegistration fields
190      *
191      * @param commonEventHeader
192      * @param pnfRegistrationFields
193      * @return VESMessage - representing the VES Event JSON
194      * @throws JsonProcessingException
195      */
196     @Override
197     public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader, VESPNFRegistrationFieldsPOJO pnfRegistrationFields) throws JsonProcessingException {
198         Map<String, Object> innerEvent = new HashMap<String, Object>();
199         innerEvent.put("commonEventHeader", commonEventHeader);
200         innerEvent.put("pnfRegistration", pnfRegistrationFields);
201
202         Map<String, Object> outerEvent = new HashMap<String, Object>();
203         outerEvent.put("event", innerEvent);
204         return new VESMessage(objMapper.writeValueAsString(outerEvent));
205     }
206
207 }