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.inventory.CmHandleState;
38 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
39 import org.onap.cps.utils.CpsValidator;
42 * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB.
48 @JsonInclude(Include.NON_NULL)
49 public class YangModelCmHandle {
53 @JsonProperty("dmi-service-name")
54 private String dmiServiceName;
56 @JsonProperty("dmi-data-service-name")
57 private String dmiDataServiceName;
59 @JsonProperty("state")
60 private CmHandleState cmHandleState;
62 @JsonProperty("dmi-model-service-name")
63 private String dmiModelServiceName;
65 @JsonProperty("additional-properties")
66 private List<Property> dmiProperties;
68 @JsonProperty("public-properties")
69 private List<Property> publicProperties;
72 * Create a yangModelCmHandle.
73 * @param dmiServiceName dmi service name
74 * @param dmiDataServiceName dmi data service name
75 * @param dmiModelServiceName dmi model service name
76 * @param ncmpServiceCmHandle the cm handle
77 * @return instance of yangModelCmHandle
79 public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
80 final String dmiDataServiceName,
81 final String dmiModelServiceName,
82 final NcmpServiceCmHandle ncmpServiceCmHandle) {
83 CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId());
84 final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
85 yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
86 yangModelCmHandle.setDmiServiceName(dmiServiceName);
87 yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
88 yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
89 yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
90 yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
91 ncmpServiceCmHandle.getPublicProperties()));
92 return yangModelCmHandle;
96 * Resolve a dmi service name.
97 * @param requiredService indicates what typo of service is required
98 * @return dmi service name
100 public String resolveDmiServiceName(final RequiredDmiService requiredService) {
101 if (isNullEmptyOrBlank(dmiServiceName)) {
102 if (RequiredDmiService.DATA.equals(requiredService)) {
103 return dmiDataServiceName;
105 return dmiModelServiceName;
107 return dmiServiceName;
110 private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
111 final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
112 for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
113 yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
115 return yangModelCmHandleProperties;
118 private static boolean isNullEmptyOrBlank(final String serviceName) {
119 return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
124 public static class Property {
127 private final String name;
130 private final String value;