Merge "Refactoring data-provider:provider generateDTOs"
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / impl / xml / MwtNotificationBase.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
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 package org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml;
19
20 import com.fasterxml.jackson.annotation.JsonProperty;
21 import javax.annotation.Nonnull;
22 import javax.xml.bind.annotation.XmlElement;
23 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalDateAndTime;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.ConnectionLogStatus;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.ConnectionlogBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev190801.ConnectionlogEntity;
29
30 public class MwtNotificationBase {
31
32     private static String EMPTY = "empty";
33
34     private String nodeName;
35     private String counter;
36     private String timeStamp;
37     private @Nonnull String objectId;
38
39     public MwtNotificationBase() {
40         // For Jaxb
41         this.objectId = EMPTY;
42     }
43
44     public MwtNotificationBase(String nodeName, Integer counter, InternalDateAndTime timeStamp, String objectId) {
45         this.nodeName = nodeName;
46         this.counter = String.valueOf(counter);
47         this.timeStamp = timeStamp.getValue();
48         this.objectId = objectId;
49         if (this.objectId == null) {
50             this.objectId = EMPTY;
51         }
52     }
53
54     @XmlElement(name = "nodeName")
55     public String getNodeName() {
56         return nodeName;
57     }
58
59     @XmlElement(name = "counter")
60     public String getCounter() {
61         return counter;
62     }
63
64     @XmlElement(name = "timeStamp")
65     public String getTimeStamp() {
66         return timeStamp;
67     }
68
69     @XmlElement(name = "objectId")
70     public String getObjectId() {
71         return objectId;
72     }
73
74     /**
75      * Provide ConnectionlogEntity type
76      * @return ConnectionlogEntity
77      */
78     public ConnectionlogEntity getConnectionlogEntity() {
79         return new ConnectionlogBuilder()
80                 .setNodeId(objectId)
81                 .setStatus(getStatus())
82                 .setTimestamp(new DateAndTime(timeStamp))
83                 .build();
84     }
85
86     /**
87      * Provide connection status for mountpoint log.
88      * TODO Add status disconnected if mountpoint is required, but does not exists.
89      * @return
90      */
91     private ConnectionLogStatus getStatus() {
92
93         if (this instanceof ObjectCreationNotificationXml) {
94             return ConnectionLogStatus.Mounted;
95
96         } else if (this instanceof ObjectDeletionNotificationXml) {
97             return ConnectionLogStatus.Unmounted;
98
99         } else if (this instanceof AttributeValueChangedNotificationXml) {
100             String pnx = ((AttributeValueChangedNotificationXml)this).getNewValue();
101             if (pnx.equals(ConnectionStatus.Connected.getName())) {
102                 return ConnectionLogStatus.Connected;
103
104             } else if (pnx.equals(ConnectionStatus.Connecting.getName())) {
105                 return ConnectionLogStatus.Connecting;
106
107             } else if (pnx.equals(ConnectionStatus.UnableToConnect.getName())) {
108                 return ConnectionLogStatus.UnableToConnect;
109             }
110         }
111         return ConnectionLogStatus.Undefined;
112     }
113
114     /**
115      * Type for the Database to document the the same name that is used in the websockets.
116      * @return String with type name of child class
117      */
118     @JsonProperty("type")
119     public String getType() {
120         return this.getClass().getSimpleName();
121     }
122
123     @Override
124     public String toString() {
125         return "MwtNotificationBase [getType()="+ getType() + ", nodeName=" + nodeName + ", counter=" + counter + ", timeStamp=" + timeStamp
126                 + ", objectId=" + objectId + "]";
127     }
128
129
130
131 }