2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.yangmodels;
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.annotation.JsonInclude.Include;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.google.common.base.Strings;
28 import java.util.ArrayList;
29 import java.util.List;
31 import lombok.AllArgsConstructor;
33 import lombok.EqualsAndHashCode;
35 import lombok.NoArgsConstructor;
37 import org.apache.commons.lang3.StringUtils;
38 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
39 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
40 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
43 * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB.
49 @JsonInclude(Include.NON_NULL)
50 @EqualsAndHashCode(onlyExplicitlyIncluded = true)
51 public class YangModelCmHandle {
53 @EqualsAndHashCode.Include
56 @JsonProperty("dmi-service-name")
57 private String dmiServiceName;
59 @JsonProperty("dmi-data-service-name")
60 private String dmiDataServiceName;
62 @JsonProperty("state")
63 private CompositeState compositeState;
65 @JsonProperty("dmi-model-service-name")
66 private String dmiModelServiceName;
68 @JsonProperty("module-set-tag")
69 private String moduleSetTag;
71 @JsonProperty("alternate-id")
72 private String alternateId;
74 @JsonProperty("additional-properties")
75 private List<Property> dmiProperties;
77 @JsonProperty("public-properties")
78 private List<Property> publicProperties;
81 * Creates a deep copy of Yang Model Cm Handle.
83 * @param original Yang Model Cm Handle
84 * @return instance of yangModelCmHandle
86 public static YangModelCmHandle deepCopyOf(final YangModelCmHandle original) {
87 final YangModelCmHandle copy = new YangModelCmHandle();
88 copy.id = original.getId();
89 copy.dmiServiceName = original.getDmiServiceName();
90 copy.dmiDataServiceName = original.getDmiDataServiceName();
91 copy.dmiModelServiceName = original.getDmiModelServiceName();
93 original.getCompositeState() == null ? null : new CompositeState(original.getCompositeState());
94 copy.dmiProperties = original.getDmiProperties() == null ? null : new ArrayList<>(original.getDmiProperties());
95 copy.publicProperties =
96 original.getPublicProperties() == null ? null : new ArrayList<>(original.getPublicProperties());
97 copy.alternateId = original.getAlternateId();
102 * Create a yangModelCmHandle.
104 * @param dmiServiceName dmi service name
105 * @param dmiDataServiceName dmi data service name
106 * @param dmiModelServiceName dmi model service name
107 * @param ncmpServiceCmHandle the cm handle
108 * @return instance of yangModelCmHandle
110 public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
111 final String dmiDataServiceName,
112 final String dmiModelServiceName,
113 final NcmpServiceCmHandle ncmpServiceCmHandle,
114 final String moduleSetTag,
115 final String alternateId) {
116 final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
117 yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
118 yangModelCmHandle.setDmiServiceName(dmiServiceName);
119 yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
120 yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
121 yangModelCmHandle.setModuleSetTag(moduleSetTag == null ? StringUtils.EMPTY : moduleSetTag);
122 yangModelCmHandle.setAlternateId(alternateId == null ? StringUtils.EMPTY : alternateId);
123 yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
124 yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
125 ncmpServiceCmHandle.getPublicProperties()));
126 yangModelCmHandle.setCompositeState(ncmpServiceCmHandle.getCompositeState());
127 return yangModelCmHandle;
131 * Resolve a dmi service name.
133 * @param requiredService indicates what typo of service is required
134 * @return dmi service name
136 public String resolveDmiServiceName(final RequiredDmiService requiredService) {
137 if (isNullEmptyOrBlank(dmiServiceName)) {
138 if (RequiredDmiService.DATA.equals(requiredService)) {
139 return dmiDataServiceName;
141 return dmiModelServiceName;
143 return dmiServiceName;
146 private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
147 final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
148 for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
149 yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
151 return yangModelCmHandleProperties;
154 private static boolean isNullEmptyOrBlank(final String serviceName) {
155 return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
160 public static class Property {
163 private final String name;
166 private final String value;