b76572ba5c93dc52a89959debe8d5443ab710b79
[ccsdk/features.git] / sdnr / wt / devicemanager-onap / onf14 / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf14 / impl / interfaces / Onf14EthernetContainerNotificationListener.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk feature sdnr wt
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.onf14.impl.interfaces;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
28 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
29 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.AttributeValueChangedNotification;
30 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.EthernetContainer20Listener;
31 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.ObjectCreationNotification;
32 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.ObjectDeletionNotification;
33 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.ProblemNotification;
34 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.SEVERITYTYPE;
35 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.SEVERITYTYPECRITICAL;
36 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.SEVERITYTYPEMAJOR;
37 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.SEVERITYTYPEMINOR;
38 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.SEVERITYTYPENONALARMED;
39 import org.opendaylight.yang.gen.v1.urn.onf.yang.ethernet.container._2._0.rev200121.SEVERITYTYPEWARNING;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class Onf14EthernetContainerNotificationListener implements EthernetContainer20Listener {
49
50     private static final Logger log = LoggerFactory.getLogger(Onf14EthernetContainerNotificationListener.class);
51
52     private final NetconfAccessor netconfAccessor;
53     private final DeviceManagerServiceProvider serviceProvider;
54
55     private static final Map<Class<? extends SEVERITYTYPE>, SeverityType> severityMap = initSeverityMap();
56
57     public Onf14EthernetContainerNotificationListener(NetconfAccessor netconfAccessor,
58             DeviceManagerServiceProvider serviceProvider) {
59         this.netconfAccessor = netconfAccessor;
60         this.serviceProvider = serviceProvider;
61     }
62
63     private static Map<Class<? extends SEVERITYTYPE>, SeverityType> initSeverityMap() {
64         Map<Class<? extends SEVERITYTYPE>, SeverityType> map = new HashMap<>();
65         map.put(SEVERITYTYPECRITICAL.class, SeverityType.Critical);
66         map.put(SEVERITYTYPEMAJOR.class, SeverityType.Major);
67         map.put(SEVERITYTYPEMINOR.class, SeverityType.Minor);
68         map.put(SEVERITYTYPEWARNING.class, SeverityType.Warning);
69         map.put(SEVERITYTYPENONALARMED.class, SeverityType.NonAlarmed);
70         return map;
71     }
72
73     @Override
74     public void onObjectDeletionNotification(ObjectDeletionNotification notification) {
75         log.debug("Got event of type :: {}", ObjectDeletionNotification.class.getSimpleName());
76
77         EventlogBuilder eventlogBuilder = new EventlogBuilder();
78         eventlogBuilder.setNodeId(netconfAccessor.getNodeId().getValue()).setAttributeName("")
79                 .setCounter(notification.getCounter().intValue()).setNewValue("deleted")
80                 .setObjectId(notification.getObjectIdRef().getValue()).setSourceType(SourceType.Netconf)
81                 .setTimestamp(notification.getTimestamp());
82         serviceProvider.getDataProvider().writeEventLog(eventlogBuilder.build());
83         serviceProvider.getNotificationService().deletionNotification(netconfAccessor.getNodeId(),
84                 notification.getCounter().intValue(), notification.getTimestamp(),
85                 notification.getObjectIdRef().getValue());
86
87         log.debug("onObjectDeletionNotification log entry written");
88     }
89
90     @Override
91     public void onProblemNotification(ProblemNotification notification) {
92         log.debug("Got event of type :: {}", ProblemNotification.class.getSimpleName());
93         FaultlogEntity faultAlarm = new FaultlogBuilder().setObjectId(notification.getObjectIdRef().getValue())
94                 .setProblem(notification.getProblem()).setTimestamp(notification.getTimestamp())
95                 .setNodeId(this.netconfAccessor.getNodeId().getValue()).setSourceType(SourceType.Netconf)
96                 .setSeverity(mapSeverity(notification.getSeverity())).setCounter(notification.getCounter().intValue())
97                 .build();
98         serviceProvider.getFaultService().faultNotification(faultAlarm);
99         serviceProvider.getWebsocketService().sendNotification(notification, netconfAccessor.getNodeId(),
100                 ProblemNotification.QNAME, notification.getTimestamp());
101
102     }
103
104     private SeverityType mapSeverity(@Nullable Class<? extends SEVERITYTYPE> severity) {
105         return severityMap.getOrDefault(severity, SeverityType.NonAlarmed);
106     }
107
108     @Override
109     public void onAttributeValueChangedNotification(AttributeValueChangedNotification notification) {
110         log.debug("Got event of type :: {}", AttributeValueChangedNotification.class.getSimpleName());
111
112         EventlogBuilder eventlogBuilder = new EventlogBuilder();
113         eventlogBuilder.setNodeId(netconfAccessor.getNodeId().getValue())
114                 .setAttributeName(notification.getAttributeName()).setCounter(notification.getCounter().intValue())
115                 .setNewValue(notification.getNewValue()).setObjectId(notification.getObjectIdRef().getValue())
116                 .setSourceType(SourceType.Netconf).setTimestamp(notification.getTimestamp());
117         serviceProvider.getDataProvider().writeEventLog(eventlogBuilder.build());
118         serviceProvider.getWebsocketService().sendNotification(notification, netconfAccessor.getNodeId(),
119                 AttributeValueChangedNotification.QNAME, notification.getTimestamp());
120
121         log.debug("onAttributeValueChangedNotification log entry written");
122     }
123
124     @Override
125     public void onObjectCreationNotification(ObjectCreationNotification notification) {
126         log.debug("Got event of type :: {}", ObjectCreationNotification.class.getSimpleName());
127
128         EventlogBuilder eventlogBuilder = new EventlogBuilder();
129         eventlogBuilder.setNodeId(netconfAccessor.getNodeId().getValue()).setAttributeName(notification.getObjectType())
130                 .setCounter(notification.getCounter().intValue()).setNewValue("created")
131                 .setObjectId(notification.getObjectIdRef().getValue()).setSourceType(SourceType.Netconf)
132                 .setTimestamp(notification.getTimestamp());
133         serviceProvider.getDataProvider().writeEventLog(eventlogBuilder.build());
134         serviceProvider.getWebsocketService().sendNotification(notification, netconfAccessor.getNodeId(),
135                 ObjectCreationNotification.QNAME, notification.getTimestamp());
136
137         log.debug("onObjectCreationNotification log entry written");
138     }
139
140
141 }