f2990a07d8603184e89ed0eb463ae037fe754a60
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2022 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.dom.impl.yangspecs;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Optional;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.interfaces.Onf14Interfaces;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.interfaces.TechnologySpecificPacKeys;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util.Onf14DMDOMUtility;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.common.Revision;
39 import org.opendaylight.yangtools.yang.common.XMLNamespace;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class CoreModel14 extends YangModule {
50
51     private static final Logger LOG = LoggerFactory.getLogger(CoreModel14.class);
52
53     private static final String NAMESPACE = "urn:onf:yang:core-model-1-4";
54     private static final List<QNameModule> MODULES =
55             Arrays.asList(QNameModule.create(XMLNamespace.of(NAMESPACE), Revision.of("2019-11-27")));
56
57     private final QName CONTROL_CONSTRUCT;
58     private final QName TOP_LEVEL_EQUIPMENT;
59
60     private CoreModel14(NetconfDomAccessor netconfDomAccessor, QNameModule module) {
61         super(netconfDomAccessor, module);
62
63         CONTROL_CONSTRUCT = QName.create(module, "control-construct");
64         TOP_LEVEL_EQUIPMENT = QName.create(module, "top-level-equipment");
65     }
66
67     public String getRevision() {
68         return module.getRevision().get().toString();
69     }
70
71     public QName getControlConstructQName() {
72         return CONTROL_CONSTRUCT;
73     }
74
75     public YangInstanceIdentifier getTopLevelEquipment_IId() {
76         return YangInstanceIdentifier.builder().node(getControlConstructQName()).node(TOP_LEVEL_EQUIPMENT).build();
77     }
78
79     private YangInstanceIdentifier getLtp_IID() {
80         return YangInstanceIdentifier.builder().node(getQName("control-construct"))
81                 .node(getQName("logical-termination-point")).build();
82     }
83
84     public YangInstanceIdentifier getLayerProtocolIId(String ltpUuid, String localId) {
85         return YangInstanceIdentifier.builder().node(getQName("control-construct"))
86                 .node(getQName("logical-termination-point"))
87                 .nodeWithKey(getQName("logical-termination-point"),
88                         QName.create(getQName("logical-termination-point"), "uuid").intern(), ltpUuid)
89                 .node(getQName("layer-protocol")).nodeWithKey(getQName("layer-protocol"),
90                         QName.create(getQName("layer-protocol"), "local-id").intern(), localId)
91                 .build();
92     }
93
94     public Optional<NormalizedNode> readLtpData(NetconfDomAccessor netconfDomAccessor) {
95         LOG.info("Reading Logical Termination Point data");
96         return netconfDomAccessor.readDataNode(LogicalDatastoreType.CONFIGURATION, getLtp_IID());
97     }
98
99     /**
100      * Get the LP list, which should contain only 1 entry. the Layer Protocol list should contain only one item, since
101      * we have an 1:1 relationship between the LTP and the LP
102      *
103      * @param ltp
104      * @return
105      */
106     private Collection<MapEntryNode> getInterfaceKeyList(DataContainerNode ltp) {
107         MapNode lpList = (MapNode) ltp.childByArg(new NodeIdentifier(getQName("layer-protocol")));
108         // the Layer Protocol list should contain only one item, since we have an 1:1
109         // relationship between the LTP and the LP
110         if (lpList != null && lpList.size() != 1) {
111             LOG.debug("Layer protocol has no 1:1 relationship with the LTP.");
112             return Collections.emptyList();
113         }
114         // accessing the LP, which should be only 1
115         return lpList.body();
116     }
117
118     /**
119      * Search through the LayerProtocol list for specific layerProtocolNamesValues
120      * @param ltp
121      * @param lp
122      * @param layerProtocolNameValue
123      * @return
124      */
125     private List<TechnologySpecificPacKeys> getTechnologySpecificPackKeys(DataContainerNode ltp, Collection<MapEntryNode> lp,
126             String layerProtocolNameValue) {
127         List<TechnologySpecificPacKeys> interfaceList = new ArrayList<>();
128         for (MapEntryNode lpEntry : lp) {
129             String layerProtocolName = Onf14DMDOMUtility.getLeafValue(lpEntry, getQName("layer-protocol-name"));
130             if (layerProtocolName != null && layerProtocolName.contains(layerProtocolNameValue)) {
131                 TechnologySpecificPacKeys interfaceKey =
132                         new TechnologySpecificPacKeys(Onf14DMDOMUtility.getLeafValue(ltp, getQName("uuid")),
133                                 Onf14DMDOMUtility.getLeafValue(lpEntry, getQName("local-id")));
134                 interfaceList.add(interfaceKey);
135                 LOG.debug("Adding Ltp with uuid {} and local-id {} to the {} list", interfaceKey.getLtpUuid(),
136                         interfaceKey.getLocalId(), layerProtocolNameValue);
137             }
138         }
139         return interfaceList;
140     }
141
142     public Onf14Interfaces readKeys(Onf14Interfaces interfaces) {
143
144         Optional<NormalizedNode> ltpData = readLtpData(netconfDomAccessor);
145         LOG.debug("LTP Data is - {}", ltpData);
146         if (ltpData.isPresent()) {
147             LOG.debug("In readKeys - ltpData = {}", ltpData.get());
148
149             MapNode ccLtp = (MapNode) ltpData.get();
150             if (ccLtp != null) {
151                 LOG.debug("Iterating the LTP list for node {}", netconfDomAccessor.getNodeId().getValue());
152                 Collection<MapEntryNode> ltpList = ccLtp.body();
153
154                 // iterating all the Logical Termination Point list
155                 for (MapEntryNode ltp : ltpList) {
156                     Collection<MapEntryNode> lp = getInterfaceKeyList(ltp);
157
158                     interfaces.add(Onf14Interfaces.Key.AIRINTERFACE,
159                             getTechnologySpecificPackKeys(ltp, lp, "LAYER_PROTOCOL_NAME_TYPE_AIR_LAYER"));
160                     interfaces.add(Onf14Interfaces.Key.ETHERNETCONTAINER,
161                             getTechnologySpecificPackKeys(ltp, lp, "LAYER_PROTOCOL_NAME_TYPE_ETHERNET_CONTAINER_LAYER"));
162                     interfaces.add(Onf14Interfaces.Key.WIREINTERFACE,
163                             getTechnologySpecificPackKeys(ltp, lp, "LAYER_PROTOCOL_NAME_TYPE_WIRE_LAYER"));
164                 }
165             }
166         }
167         return interfaces;
168     }
169
170     /**
171      * Get specific module for device, depending on capabilities
172      */
173     public static Optional<CoreModel14> getModule(NetconfDomAccessor netconfDomAccessor) {
174
175         Capabilities capabilities = netconfDomAccessor.getCapabilites();
176         for (QNameModule module : MODULES) {
177
178             if (capabilities.isSupportingNamespaceAndRevision(module)) {
179                 return Optional.of(new CoreModel14(netconfDomAccessor, module));
180             }
181         }
182         return Optional.empty();
183     }
184
185 }