Use singleton NodeIdentifierWithPredicates
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnersModel.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore.entityownership;
9
10 import java.util.Map.Entry;
11 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.Entity;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
28
29 /**
30  * Utility methods for entity-owners yang model.
31  *
32  * @author Thomas Pantelis
33  */
34 public final class EntityOwnersModel {
35     static final QName ENTITY_QNAME = Entity.QNAME;
36     static final QName CANDIDATE_NAME_QNAME = QName.create(Candidate.QNAME, "name").intern();
37     static final QName ENTITY_ID_QNAME = QName.create(ENTITY_QNAME, "id").intern();
38     static final QName ENTITY_OWNER_QNAME = QName.create(ENTITY_QNAME, "owner").intern();
39     static final QName ENTITY_TYPE_QNAME = QName.create(EntityType.QNAME, "type").intern();
40
41     static final NodeIdentifier ENTITY_OWNERS_NODE_ID = NodeIdentifier.create(EntityOwners.QNAME);
42     static final NodeIdentifier ENTITY_OWNER_NODE_ID = NodeIdentifier.create(ENTITY_OWNER_QNAME);
43     static final NodeIdentifier ENTITY_NODE_ID = NodeIdentifier.create(ENTITY_QNAME);
44     static final NodeIdentifier ENTITY_ID_NODE_ID = NodeIdentifier.create(ENTITY_ID_QNAME);
45     static final NodeIdentifier ENTITY_TYPE_NODE_ID = NodeIdentifier.create(ENTITY_TYPE_QNAME);
46     static final NodeIdentifier CANDIDATE_NODE_ID = NodeIdentifier.create(Candidate.QNAME);
47     static final NodeIdentifier CANDIDATE_NAME_NODE_ID = NodeIdentifier.create(CANDIDATE_NAME_QNAME);
48     static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.create(ENTITY_OWNERS_NODE_ID);
49     static final YangInstanceIdentifier ENTITY_TYPES_PATH = ENTITY_OWNERS_PATH.node(EntityType.QNAME).toOptimized();
50
51     private EntityOwnersModel() {
52     }
53
54     static YangInstanceIdentifier entityPath(final String entityType, final YangInstanceIdentifier entityId) {
55         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
56                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
57                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).build();
58
59     }
60
61     static YangInstanceIdentifier candidatePath(final String entityType, final YangInstanceIdentifier entityId,
62             final String candidateName) {
63         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
64                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
65                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).node(Candidate.QNAME)
66                                 .nodeWithKey(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
67     }
68
69     static YangInstanceIdentifier candidatePath(final YangInstanceIdentifier entityPath, final String candidateName) {
70         return YangInstanceIdentifier.builder(entityPath).node(Candidate.QNAME).nodeWithKey(
71                 Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
72     }
73
74     static NodeIdentifierWithPredicates candidateNodeKey(final String candidateName) {
75         return NodeIdentifierWithPredicates.of(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
76     }
77
78     static NormalizedNode<?, ?> entityOwnersWithCandidate(final String entityType,
79             final YangInstanceIdentifier entityId, final String candidateName) {
80         return entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entityType,
81                 entityEntryWithCandidateEntry(entityId, candidateName)));
82     }
83
84     static ContainerNode entityOwnersWithEntityTypeEntry(final MapEntryNode entityTypeNode) {
85         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
86                 ENTITY_OWNERS_NODE_ID).addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME)
87                         .addChild(entityTypeNode).build()).build();
88     }
89
90     static MapEntryNode entityTypeEntryWithEntityEntry(final String entityType, final MapEntryNode entityNode) {
91         return ImmutableNodes.mapEntryBuilder(EntityType.QNAME,
92                 ENTITY_TYPE_QNAME, entityType).addChild(ImmutableNodes.mapNodeBuilder(
93                         ENTITY_QNAME).addChild(entityNode).build()).build();
94     }
95
96     static MapEntryNode entityEntryWithCandidateEntry(final YangInstanceIdentifier entityId,
97             final String candidateName) {
98         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
99                 candidateEntry(candidateName)).build();
100     }
101
102     static MapNode candidateEntry(final String candidateName) {
103         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(Candidate.QNAME))
104                 .addChild(candidateMapEntry(candidateName)).build();
105     }
106
107     static MapEntryNode candidateMapEntry(final String candidateName) {
108         return ImmutableNodes.mapEntry(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
109     }
110
111     static MapEntryNode entityEntryWithOwner(final YangInstanceIdentifier entityId, final String owner) {
112         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId)
113                 .addChild(ImmutableNodes.leafNode(ENTITY_OWNER_QNAME, owner != null ? owner : ""))
114                 .build();
115     }
116
117     public static String entityTypeFromEntityPath(final YangInstanceIdentifier entityPath) {
118         YangInstanceIdentifier parent = entityPath;
119         while (!parent.isEmpty()) {
120             if (EntityType.QNAME.equals(parent.getLastPathArgument().getNodeType())) {
121                 YangInstanceIdentifier.NodeIdentifierWithPredicates entityTypeLastPathArgument =
122                         (YangInstanceIdentifier.NodeIdentifierWithPredicates) parent.getLastPathArgument();
123                 return (String) entityTypeLastPathArgument.getValue(ENTITY_TYPE_QNAME);
124             }
125             parent = parent.getParent();
126         }
127         return null;
128     }
129
130     static DOMEntity createEntity(final YangInstanceIdentifier entityPath) {
131         String entityType = null;
132         YangInstanceIdentifier entityId = null;
133         for (PathArgument pathArg: entityPath.getPathArguments()) {
134             if (pathArg instanceof NodeIdentifierWithPredicates) {
135                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
136                 Entry<QName, Object> key = nodeKey.entrySet().iterator().next();
137                 if (ENTITY_TYPE_QNAME.equals(key.getKey())) {
138                     entityType = key.getValue().toString();
139                 } else if (ENTITY_ID_QNAME.equals(key.getKey())) {
140                     entityId = (YangInstanceIdentifier) key.getValue();
141                 }
142             }
143         }
144
145         return new DOMEntity(entityType, entityId);
146     }
147 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.