85a0984646decc657d8c7306931016e7218b7534
[oam/oam-controller.git] /
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.types.VESStndDefinedFieldsPOJO;
48 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.config.VESCollectorCfgImpl;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class VESCollectorServiceImpl implements VESCollectorService, IConfigChangedListener, AutoCloseable {
53
54     private static final Logger LOG = LoggerFactory.getLogger(VESCollectorServiceImpl.class);
55     private final VESCollectorCfgImpl vesConfig;
56     private final ConfigurationFileRepresentation cfg;
57     private BaseHTTPClient httpClient;
58     private final Map<String, String> headerMap;
59     private List<VESCollectorConfigChangeListener> registeredObjects;
60     private final ObjectMapper objMapper;
61
62
63     public VESCollectorServiceImpl(ConfigurationFileRepresentation config) {
64         registeredObjects = new ArrayList<VESCollectorConfigChangeListener>();
65         this.vesConfig = new VESCollectorCfgImpl(config);
66         this.cfg = config;
67         this.cfg.registerConfigChangedListener(this);
68         this.objMapper = new ObjectMapper();
69
70         httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
71
72         this.headerMap = new HashMap<>();
73         this.headerMap.put("Content-Type", "application/json");
74         this.headerMap.put("Accept", "application/json");
75
76         setAuthorization(getConfig().getUsername(), getConfig().getPassword());
77     }
78
79     @Override
80     public VESCollectorCfgImpl getConfig() {
81         return this.vesConfig;
82     }
83
84     public String getBaseUrl() {
85         LOG.debug("IP Address is - {}", getConfig().getIP());
86         if (!getConfig().getTLSEnabled()) {
87             return "http://" + getConfig().getIP() + ":" + getConfig().getPort();
88         } else {
89             return "https://" + getConfig().getIP() + ":" + getConfig().getPort();
90         }
91     }
92
93     private void setAuthorization(String username, String password) {
94         if (getConfig().getTLSEnabled()) {
95             String credentials = username + ":" + password;
96             this.headerMap.put("Authorization",
97                     "Basic " + new String(Base64.getEncoder().encode(credentials.getBytes())));
98         }
99
100     }
101
102     @Override
103     public boolean publishVESMessage(VESMessage message) {
104         LOG.debug("In VESClient - {} ", message.getMessage());
105         BaseHTTPResponse response;
106         try {
107             String uri = "eventListener" + "/" + getConfig().getVersion();
108             response = httpClient.sendRequest(uri, "POST", message.getMessage(), headerMap);
109             LOG.debug("finished with responsecode {}", response.code);
110             return response.code == 200;
111         } catch (IOException e) {
112             LOG.warn("problem publishing VES message {} ", e.getMessage());
113             return false;
114         }
115
116     }
117
118     @Override
119     public void close() throws Exception {
120         this.cfg.unregisterConfigChangedListener(this);
121     }
122
123     @Override
124     public void onConfigChanged() {
125         LOG.debug("In onConfigChanged - isTrustAllCerts = {} getBaseUrl = {}", getConfig().isTrustAllCerts(),
126                 getBaseUrl());
127         httpClient = new BaseHTTPClient(getBaseUrl(), this.vesConfig.isTrustAllCerts());
128         setAuthorization(getConfig().getUsername(), getConfig().getPassword());
129         Iterator<VESCollectorConfigChangeListener> it = registeredObjects.iterator();
130         while (it.hasNext()) {
131             VESCollectorConfigChangeListener o = it.next();
132             o.notify(getConfig());
133         }
134     }
135
136     @Override
137     public void registerForChanges(VESCollectorConfigChangeListener o) {
138         registeredObjects.add(o);
139     }
140
141     @Override
142     public void deregister(VESCollectorConfigChangeListener o) {
143         registeredObjects.remove(o);
144     }
145
146     @Override
147     public @NonNull NotificationProxyParser getNotificationProxyParser() {
148         return new NotificationProxyParserImpl();
149     }
150
151     /**
152      * Generates VES Event JSON containing commonEventHeader and notificationFields fields
153      *
154      * @param commonEventHeader
155      * @param notifFields
156      * @return VESMessage - representing the VESEvent JSON
157      * @throws JsonProcessingException
158      */
159     @Override
160     public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
161             VESNotificationFieldsPOJO notifFields) throws JsonProcessingException {
162         final var evt = Map.of("event",
163                 Map.of("commonEventHeader", commonEventHeader, "notificationFields", notifFields));
164         final var str = objMapper.writeValueAsString(evt);
165         LOG.debug("In generateVESEvent - {}", str);
166         return new VESMessage(str);
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)
179             throws JsonProcessingException {
180         final var evt = Map.of("event",
181                 Map.of("commonEventHeader", commonEventHeader, "faultFields", faultFields));
182         return new VESMessage(objMapper.writeValueAsString(evt));
183     }
184
185     /**
186      * Generates VES Event JSON containing commonEventHeader and pnfRegistration fields
187      *
188      * @param commonEventHeader
189      * @param pnfRegistrationFields
190      * @return VESMessage - representing the VES Event JSON
191      * @throws JsonProcessingException
192      */
193     @Override
194     public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
195             VESPNFRegistrationFieldsPOJO pnfRegistrationFields) throws JsonProcessingException {
196         final var evt = Map.of("event",
197                 Map.of("commonEventHeader", commonEventHeader, "pnfRegistrationFields", pnfRegistrationFields));
198         return new VESMessage(objMapper.writeValueAsString(evt));
199     }
200
201     /**
202      * Generates VES Event JSON containing commonEventHeader and stndDefined fields
203      *
204      * @param commonEventHeader
205      * @param stndDefinedFields
206      * @return VESMessage - representing the VES Event JSON
207      * @throws JsonProcessingException
208      */
209     @Override
210     public VESMessage generateVESEvent(VESCommonEventHeaderPOJO commonEventHeader,
211             VESStndDefinedFieldsPOJO stndDefinedFields) throws JsonProcessingException {
212         final var evt = Map.of("event",
213                 Map.of("commonEventHeader", commonEventHeader, "stndDefinedFields", stndDefinedFields));
214         return new VESMessage(objMapper.writeValueAsString(evt));
215     }
216 }