2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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;
34 import lombok.NoArgsConstructor;
36 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
37 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
38 import org.onap.cps.utils.CpsValidator;
41 * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB.
47 @JsonInclude(Include.NON_NULL)
48 public class YangModelCmHandle {
52 @JsonProperty("dmi-service-name")
53 private String dmiServiceName;
55 @JsonProperty("dmi-data-service-name")
56 private String dmiDataServiceName;
58 @JsonProperty("dmi-model-service-name")
59 private String dmiModelServiceName;
61 @JsonProperty("additional-properties")
62 private List<Property> dmiProperties;
64 @JsonProperty("public-properties")
65 private List<Property> publicProperties;
68 * Create a yangModelCmHandle.
69 * @param dmiServiceName dmi service name
70 * @param dmiDataServiceName dmi data service name
71 * @param dmiModelServiceName dmi model service name
72 * @param ncmpServiceCmHandle the cm handle
73 * @return instance of yangModelCmHandle
75 public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
76 final String dmiDataServiceName,
77 final String dmiModelServiceName,
78 final NcmpServiceCmHandle ncmpServiceCmHandle) {
79 CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId());
80 final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
81 yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
82 yangModelCmHandle.setDmiServiceName(dmiServiceName);
83 yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
84 yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
85 yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
86 yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
87 ncmpServiceCmHandle.getPublicProperties()));
88 return yangModelCmHandle;
92 * Resolve a dmi service name.
93 * @param requiredService indicates what typo of service is required
94 * @return dmi service name
96 public String resolveDmiServiceName(final RequiredDmiService requiredService) {
97 if (isNullEmptyOrBlank(dmiServiceName)) {
98 if (RequiredDmiService.DATA.equals(requiredService)) {
99 return dmiDataServiceName;
101 return dmiModelServiceName;
103 return dmiServiceName;
106 private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
107 final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
108 for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
109 yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
111 return yangModelCmHandleProperties;
114 private static boolean isNullEmptyOrBlank(final String serviceName) {
115 return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
120 public static class Property {
123 private final String name;
126 private final String value;