a32c8b5a0402e9d043ed04d593ddc7ed63428a0c
[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.util.SharedSingletonMapTemplate;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
29
30 /**
31  * Utility methods for entity-owners yang model.
32  *
33  * @author Thomas Pantelis
34  */
35 public final class EntityOwnersModel {
36     static final QName ENTITY_QNAME = Entity.QNAME;
37     static final QName CANDIDATE_NAME_QNAME = QName.create(Candidate.QNAME, "name").intern();
38     static final QName ENTITY_ID_QNAME = QName.create(ENTITY_QNAME, "id").intern();
39     static final QName ENTITY_OWNER_QNAME = QName.create(ENTITY_QNAME, "owner").intern();
40     static final QName ENTITY_TYPE_QNAME = QName.create(EntityType.QNAME, "type").intern();
41
42     static final NodeIdentifier ENTITY_OWNERS_NODE_ID = NodeIdentifier.create(EntityOwners.QNAME);
43     static final NodeIdentifier ENTITY_OWNER_NODE_ID = NodeIdentifier.create(ENTITY_OWNER_QNAME);
44     static final NodeIdentifier ENTITY_NODE_ID = NodeIdentifier.create(ENTITY_QNAME);
45     static final NodeIdentifier ENTITY_ID_NODE_ID = NodeIdentifier.create(ENTITY_ID_QNAME);
46     static final NodeIdentifier ENTITY_TYPE_NODE_ID = NodeIdentifier.create(ENTITY_TYPE_QNAME);
47     static final NodeIdentifier CANDIDATE_NODE_ID = NodeIdentifier.create(Candidate.QNAME);
48     static final NodeIdentifier CANDIDATE_NAME_NODE_ID = NodeIdentifier.create(CANDIDATE_NAME_QNAME);
49     static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.create(ENTITY_OWNERS_NODE_ID);
50     static final YangInstanceIdentifier ENTITY_TYPES_PATH = ENTITY_OWNERS_PATH.node(EntityType.QNAME).toOptimized();
51
52     private static final SharedSingletonMapTemplate<QName> NODE_KEY_TEMPLATE = SharedSingletonMapTemplate.ordered(
53         CANDIDATE_NAME_QNAME);
54
55     private EntityOwnersModel() {
56     }
57
58     static YangInstanceIdentifier entityPath(final String entityType, final YangInstanceIdentifier entityId) {
59         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
60                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
61                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).build();
62
63     }
64
65     static YangInstanceIdentifier candidatePath(final String entityType, final YangInstanceIdentifier entityId,
66             final String candidateName) {
67         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
68                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
69                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).node(Candidate.QNAME)
70                                 .nodeWithKey(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
71     }
72
73     static YangInstanceIdentifier candidatePath(final YangInstanceIdentifier entityPath, final String candidateName) {
74         return YangInstanceIdentifier.builder(entityPath).node(Candidate.QNAME).nodeWithKey(
75                 Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
76     }
77
78     static NodeIdentifierWithPredicates candidateNodeKey(final String candidateName) {
79         return NodeIdentifierWithPredicates.of(Candidate.QNAME, NODE_KEY_TEMPLATE.instantiateWithValue(candidateName));
80     }
81
82     static NormalizedNode<?, ?> entityOwnersWithCandidate(final String entityType,
83             final YangInstanceIdentifier entityId, final String candidateName) {
84         return entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entityType,
85                 entityEntryWithCandidateEntry(entityId, candidateName)));
86     }
87
88     static ContainerNode entityOwnersWithEntityTypeEntry(final MapEntryNode entityTypeNode) {
89         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
90                 ENTITY_OWNERS_NODE_ID).addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME)
91                         .addChild(entityTypeNode).build()).build();
92     }
93
94     static MapEntryNode entityTypeEntryWithEntityEntry(final String entityType, final MapEntryNode entityNode) {
95         return ImmutableNodes.mapEntryBuilder(EntityType.QNAME,
96                 ENTITY_TYPE_QNAME, entityType).addChild(ImmutableNodes.mapNodeBuilder(
97                         ENTITY_QNAME).addChild(entityNode).build()).build();
98     }
99
100     static MapEntryNode entityEntryWithCandidateEntry(final YangInstanceIdentifier entityId,
101             final String candidateName) {
102         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
103                 candidateEntry(candidateName)).build();
104     }
105
106     static MapNode candidateEntry(final String candidateName) {
107         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(Candidate.QNAME))
108                 .addChild(candidateMapEntry(candidateName)).build();
109     }
110
111     static MapEntryNode candidateMapEntry(final String candidateName) {
112         return ImmutableNodes.mapEntry(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
113     }
114
115     static MapEntryNode entityEntryWithOwner(final YangInstanceIdentifier entityId, final String owner) {
116         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId)
117                 .addChild(ImmutableNodes.leafNode(ENTITY_OWNER_QNAME, owner != null ? owner : ""))
118                 .build();
119     }
120
121     public static String entityTypeFromEntityPath(final YangInstanceIdentifier entityPath) {
122         YangInstanceIdentifier parent = entityPath;
123         while (!parent.isEmpty()) {
124             if (EntityType.QNAME.equals(parent.getLastPathArgument().getNodeType())) {
125                 YangInstanceIdentifier.NodeIdentifierWithPredicates entityTypeLastPathArgument =
126                         (YangInstanceIdentifier.NodeIdentifierWithPredicates) parent.getLastPathArgument();
127                 return (String) entityTypeLastPathArgument.getValue(ENTITY_TYPE_QNAME);
128             }
129             parent = parent.getParent();
130         }
131         return null;
132     }
133
134     static DOMEntity createEntity(final YangInstanceIdentifier entityPath) {
135         String entityType = null;
136         YangInstanceIdentifier entityId = null;
137         for (PathArgument pathArg: entityPath.getPathArguments()) {
138             if (pathArg instanceof NodeIdentifierWithPredicates) {
139                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
140                 Entry<QName, Object> key = nodeKey.entrySet().iterator().next();
141                 if (ENTITY_TYPE_QNAME.equals(key.getKey())) {
142                     entityType = key.getValue().toString();
143                 } else if (ENTITY_ID_QNAME.equals(key.getKey())) {
144                     entityId = (YangInstanceIdentifier) key.getValue();
145                 }
146             }
147         }
148
149         return new DOMEntity(entityType, entityId);
150     }
151 }

©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.