fix some sdnr sonar bugs
[ccsdk/features.git] / sdnr / wt / websocketmanager / model / src / main / java / org / onap / ccsdk / features / sdnr / wt / websocketmanager / model / data / Scope.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.websocketmanager.model.data;
23
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Optional;
28 import org.opendaylight.yangtools.yang.common.QName;
29
30 public class Scope {
31
32     private String nodeId;
33     private SchemaInfo schema;
34
35     public String getNodeId() {
36         return nodeId;
37     }
38
39     public void setNodeId(String nodeId) {
40         this.nodeId = nodeId;
41     }
42
43     public SchemaInfo getSchema() {
44         return schema;
45     }
46
47     public void setSchema(SchemaInfo schema) {
48         this.schema = schema;
49     }
50
51     @JsonIgnore
52     public boolean isValid() {
53         if (this.nodeId == null && this.schema == null) {
54             return false;
55         }
56         if (this.nodeId == null && !this.schema.isValid()) {
57             return false;
58         }
59         return true;
60     }
61
62     @JsonIgnore
63     public boolean matches(String nodeId, ReducedSchemaInfo reducedSchemaInfo) {
64         if (this.nodeId == null) {
65             return this.schema.matches(reducedSchemaInfo);
66         } else if (this.schema == null) {
67             return this.nodeId.equals(nodeId);
68         }
69         return this.nodeId.equals(nodeId) && this.schema.matches(reducedSchemaInfo);
70
71     }
72
73     public boolean addQname(QName qname) {
74         if(this.schema==null) {
75             this.schema = new SchemaInfo(qname);
76             return true;
77         }
78         if(!this.schema.equalsNamespaceAndRevision(qname)) {
79             return false;
80         }
81         this.schema.addNotification(qname.getLocalName());
82         return true;
83     }
84
85     @Override
86     public String toString() {
87         return "Scope [nodeId=" + nodeId + ", schema=" + schema + "]";
88     }
89
90     public static Scope create(QName qname) {
91         return create(null, qname);
92     }
93
94     public static Scope create(String nodeId, QName qname) {
95         return create(nodeId, qname == null ? null : new SchemaInfo(qname));
96     }
97
98     public static Scope create(String nodeId, SchemaInfo schemaInfo) {
99         Scope scope = new Scope();
100         scope.setNodeId(nodeId);
101         scope.setSchema(schemaInfo);
102         return scope;
103     }
104
105     public static Scope create(String nodeId) {
106         return create(nodeId, (SchemaInfo) null);
107     }
108
109     public static List<Scope> createList(List<QName> qnames) {
110         return createList(null, qnames);
111     }
112
113     public static List<Scope> createList(String nodeId, List<QName> qnames) {
114         List<Scope> scopes = new ArrayList<>();
115         Optional<Scope> listElem;
116         for (QName qname : qnames) {
117             listElem = scopes.stream().filter(e -> e.schema != null && e.schema.equalsNamespaceAndRevision(qname))
118                     .findFirst();
119             if (listElem.isPresent()) {
120                 if (!listElem.get().addQname(qname)) {
121                     scopes.add(Scope.create(nodeId, qname));
122                 }
123             } else {
124                 scopes.add(Scope.create(nodeId, qname));
125             }
126         }
127         return scopes;
128     }
129
130
131
132 }