Adjust to yangtools-2.0.0 changes
[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");
37     static final QName ENTITY_ID_QNAME = QName.create(ENTITY_QNAME, "id");
38     static final QName ENTITY_OWNER_QNAME = QName.create(ENTITY_QNAME, "owner");
39     static final QName ENTITY_TYPE_QNAME = QName.create(EntityType.QNAME, "type");
40
41     static final NodeIdentifier ENTITY_OWNERS_NODE_ID = new NodeIdentifier(EntityOwners.QNAME);
42     static final NodeIdentifier ENTITY_OWNER_NODE_ID = new NodeIdentifier(ENTITY_OWNER_QNAME);
43     static final NodeIdentifier ENTITY_NODE_ID = new NodeIdentifier(ENTITY_QNAME);
44     static final NodeIdentifier ENTITY_ID_NODE_ID = new NodeIdentifier(ENTITY_ID_QNAME);
45     static final NodeIdentifier ENTITY_TYPE_NODE_ID = new NodeIdentifier(ENTITY_TYPE_QNAME);
46     static final NodeIdentifier CANDIDATE_NODE_ID = new NodeIdentifier(Candidate.QNAME);
47     static final NodeIdentifier CANDIDATE_NAME_NODE_ID = new NodeIdentifier(CANDIDATE_NAME_QNAME);
48     static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.of(EntityOwners.QNAME);
49     static final YangInstanceIdentifier ENTITY_TYPES_PATH =
50             YangInstanceIdentifier.of(EntityOwners.QNAME).node(EntityType.QNAME);
51
52     private EntityOwnersModel() {
53     }
54
55     static YangInstanceIdentifier entityPath(final String entityType, final YangInstanceIdentifier entityId) {
56         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
57                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
58                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).build();
59
60     }
61
62     static YangInstanceIdentifier candidatePath(final String entityType, final YangInstanceIdentifier entityId,
63             final String candidateName) {
64         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
65                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
66                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).node(Candidate.QNAME)
67                                 .nodeWithKey(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
68
69     }
70
71     static YangInstanceIdentifier candidatePath(final YangInstanceIdentifier entityPath, final String candidateName) {
72         return YangInstanceIdentifier.builder(entityPath).node(Candidate.QNAME).nodeWithKey(
73                 Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
74     }
75
76     static NodeIdentifierWithPredicates candidateNodeKey(final String candidateName) {
77         return new NodeIdentifierWithPredicates(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
78     }
79
80     static NormalizedNode<?, ?> entityOwnersWithCandidate(final String entityType,
81             final YangInstanceIdentifier entityId, final String candidateName) {
82         return entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entityType,
83                 entityEntryWithCandidateEntry(entityId, candidateName)));
84     }
85
86     static ContainerNode entityOwnersWithEntityTypeEntry(final MapEntryNode entityTypeNode) {
87         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
88                 ENTITY_OWNERS_NODE_ID).addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME)
89                         .addChild(entityTypeNode).build()).build();
90     }
91
92     static MapEntryNode entityTypeEntryWithEntityEntry(final String entityType, final MapEntryNode entityNode) {
93         return ImmutableNodes.mapEntryBuilder(EntityType.QNAME,
94                 ENTITY_TYPE_QNAME, entityType).addChild(ImmutableNodes.mapNodeBuilder(
95                         ENTITY_QNAME).addChild(entityNode).build()).build();
96     }
97
98     static MapEntryNode entityEntryWithCandidateEntry(final YangInstanceIdentifier entityId,
99             final String candidateName) {
100         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
101                 candidateEntry(candidateName)).build();
102     }
103
104     static MapNode candidateEntry(final String candidateName) {
105         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(Candidate.QNAME))
106                 .addChild(candidateMapEntry(candidateName)).build();
107     }
108
109     static MapEntryNode candidateMapEntry(final String candidateName) {
110         return ImmutableNodes.mapEntry(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
111     }
112
113     static MapEntryNode entityEntryWithOwner(final YangInstanceIdentifier entityId, final String owner) {
114         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId)
115                 .addChild(ImmutableNodes.leafNode(ENTITY_OWNER_QNAME, owner != null ? owner : ""))
116                 .build();
117     }
118
119     public static String entityTypeFromEntityPath(final YangInstanceIdentifier entityPath) {
120         YangInstanceIdentifier parent = entityPath;
121         while (!parent.isEmpty()) {
122             if (EntityType.QNAME.equals(parent.getLastPathArgument().getNodeType())) {
123                 YangInstanceIdentifier.NodeIdentifierWithPredicates entityTypeLastPathArgument =
124                         (YangInstanceIdentifier.NodeIdentifierWithPredicates) parent.getLastPathArgument();
125                 return (String) entityTypeLastPathArgument.getKeyValues().get(ENTITY_TYPE_QNAME);
126             }
127             parent = parent.getParent();
128         }
129         return null;
130     }
131
132     static DOMEntity createEntity(final YangInstanceIdentifier entityPath) {
133         String entityType = null;
134         YangInstanceIdentifier entityId = null;
135         for (PathArgument pathArg: entityPath.getPathArguments()) {
136             if (pathArg instanceof NodeIdentifierWithPredicates) {
137                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
138                 Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
139                 if (ENTITY_TYPE_QNAME.equals(key.getKey())) {
140                     entityType = key.getValue().toString();
141                 } else if (ENTITY_ID_QNAME.equals(key.getKey())) {
142                     entityId = (YangInstanceIdentifier) key.getValue();
143                 }
144             }
145         }
146
147         return new DOMEntity(entityType, entityId);
148     }
149 }

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