03ed89111476d1e03bd3a8c7afee5a472e7ef115
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2023 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.oran.yangspecs;
23
24 import com.google.common.collect.Sets;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Optional;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDeviceManagerQNames;
31 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
32 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.common.QNameModule;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.common.XMLNamespace;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45
46
47 public class OnapSystem extends YangModule {
48
49     private static final Logger LOG = LoggerFactory.getLogger(OnapSystem.class);
50     public static final String NAMESPACE = "urn:onap:system";
51     public static final QNameModule ONAPSYSTEM_2020_10_26 =
52             QNameModule.create(XMLNamespace.of(NAMESPACE), Revision.of("2020-10-26"));
53     public static final QNameModule ONAPSYSTEM_2022_11_04 =
54             QNameModule.create(XMLNamespace.of(NAMESPACE), Revision.of("2022-11-04"));
55     private static final List<QNameModule> MODULES = Arrays.asList(ONAPSYSTEM_2020_10_26, ONAPSYSTEM_2022_11_04);
56
57     private final QName NAME;
58     private final QName WEB_UI;
59     private final QName GEOGRAPHICAL_LOCATION;
60
61     OnapSystem(NetconfDomAccessor netconfDomAccessor, QNameModule module) {
62         super(netconfDomAccessor, module);
63
64         NAME = QName.create(module, "name");
65         WEB_UI = QName.create(module, "web-ui");
66         GEOGRAPHICAL_LOCATION = QName.create(module, "geographical-location");
67     }
68
69     public QName getName() {
70         return NAME;
71     }
72
73     public QName getWebUi() {
74         return WEB_UI;
75     }
76
77     public QName getGeoLocation() {
78         return GEOGRAPHICAL_LOCATION;
79     }
80
81     // Read from device
82     /**
83      * Read system data with GUI cut through information from device if ONAP_SYSTEM YANG is supported.
84      *
85      * @return NormalizedNode data with GUI cut through information or null if not available.
86      */
87     public @Nullable NormalizedNode getOnapSystemData() {
88         LOG.debug("Get System1 for mountpoint {}", netconfDomAccessor.getNodeId().getValue());
89         @NonNull
90         InstanceIdentifierBuilder ietfSystemIID =
91                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.IETF_SYSTEM_CONTAINER);
92         @NonNull
93         AugmentationIdentifier onapSystemIID = null;
94         if (netconfDomAccessor.getCapabilites().isSupportingNamespaceAndRevision(ONAPSYSTEM_2020_10_26))
95             onapSystemIID = YangInstanceIdentifier.AugmentationIdentifier.create(Sets.newHashSet(NAME, WEB_UI));
96         else if (netconfDomAccessor.getCapabilites().isSupportingNamespaceAndRevision(ONAPSYSTEM_2022_11_04))
97             onapSystemIID = YangInstanceIdentifier.AugmentationIdentifier
98                     .create(Sets.newHashSet(NAME, WEB_UI, GEOGRAPHICAL_LOCATION));
99
100         InstanceIdentifierBuilder augmentedOnapSystem =
101                 YangInstanceIdentifier.builder(ietfSystemIID.build()).node(onapSystemIID);
102
103         Optional<NormalizedNode> res =
104                 netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, augmentedOnapSystem.build());
105         LOG.debug("Result of System1 = {}", res);
106         return res.isPresent() ? res.get() : null;
107
108     }
109
110     /**
111      * Get specific instance, depending on capabilities
112      *
113      * @param capabilities
114      * @return
115      */
116     public static Optional<OnapSystem> getModule(NetconfDomAccessor netconfDomAccessor) {
117         Capabilities capabilities = netconfDomAccessor.getCapabilites();
118         for (QNameModule module : MODULES) {
119             if (capabilities.isSupportingNamespaceAndRevision(module)) {
120                 return Optional.of(new OnapSystem(netconfDomAccessor, module));
121             }
122         }
123         return Optional.empty();
124     }
125
126
127 }